package com.ssj.service.sys.jxy.service.impl;

import java.io.File;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.jxy.domain.Jxy;
import com.ssj.bean.weixin.download.domain.DownloadFile;
import com.ssj.dao.sys.jxy.dao.JxyDao;
import com.ssj.dao.sys.jxy.dao.JxyQueryDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.weixin.util.BarcodeFactory;
import com.ssj.service.sys.jxy.service.JxyService;
import com.ssj.service.weixin.download.service.DownloadFileService;

@Service(version = "1.0")
@Transactional
public class JxyServiceImpl extends BaseServiceImpl<Jxy, String> implements JxyService {

	@Autowired
	private JxyDao jxyDao;
    @Autowired
    private JxyQueryDao jxyQueryDao;
    
    @Autowired
	private DownloadFileService downloadFileService;
	@Override
	public Page<Map<String, Object>> findTeacherIdForPage(
			Map<String, Object> searchParams, Pageable pageable) {
		return jxyQueryDao.findTeacherIdForPage(searchParams, pageable);
	}

	@Override
	public PagingAndSortingRepository<Jxy, String> getDao() {
		return jxyDao;
	}

	@Override
	public void generateQrcode(Jxy jxy) throws Exception {

		  if(jxy .getQrCode()==null || "".equals(jxy.getQrCode())){
				String fPath = DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd");
				String file_path = (String) SystemResourceLocator.getValue("file_save_path");
				String sys_url = (String) SystemResourceLocator.getValue("sys_url");
				if (!fPath.endsWith("/")) {
					fPath += "/";
				}
				File  f = new File(file_path+fPath);
				if (!f.exists()) {
					f.mkdirs();
				}
				fPath = fPath + jxy.getId() + ".jpg";
				//分享到用户主页         
				String url =sys_url+"/weixin/twoBook/libBookInfoSave_"+jxy.getIsbn13()+".html";
				BarcodeFactory.encode(url, null, file_path+fPath,300,300,true);
				jxy.setQrCode(fPath);
				//this.save(jxy);
			}
			this.save(jxy);
}

	@Override
	public Page<Map<String, Object>> getJxylib(
			Map<String, Object> searchParams, Pageable pageable) {
		return jxyQueryDao.getJxylib(searchParams, pageable);
	}

	@Override
	public Page<Jxy> findByPage(Map<String, Object> params, Pageable sp)
			throws Exception {
		 Map<String, SearchFilter> filters = SearchFilter.parse(params);
	     Specification<Jxy> specification = DynamicSpecifications.bySearchFilter(filters.values(), Jxy.class);
	     return jxyDao.findAll(specification, sp);
	}

	@Override
	public Jxy findByIsbn13(String isbn13) {
		
		return jxyDao.findByIsbn13(isbn13);
	}

	@Override
	public Jxy saveLibLeader( Jxy jxy, String[] images_localId, String[] images_serverId,
			String[] recording_localId, String[] recording_serverId) {
		
		
		
		if(recording_localId!=null&&recording_localId.length>0 && recording_serverId !=null && recording_serverId.length>0){
			DownloadFile file = new  DownloadFile();
			file.setCreateTime(new Date());
			file.setMediaId(recording_serverId[0]);
			file.setStat(0);
			file.setType(2);//语音
			file.setLocalId(recording_localId[0]);
			downloadFileService.save(file);
			jxy.setMp3Name(file.getId());
		}
		jxy.setCreateTime(new Date());
		this.save(jxy);
		return jxy;
	}
}