package com.ssj.service.weixin.zuoyb.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import com.ssj.bean.weixin.zyb.domain.ZuoybScoringContent;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybScoringContentDao;
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.service.weixin.zuoyb.service.ZuoybScoringContentService;
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 java.util.Date;
import java.util.List;
import java.util.Map;


/** 
 * @author  ZhangCaibao 
 * @version 2017年7月4日 上午11:30:05 
 * 类说明 
 */
@Service(version="1.0")
@Transactional
public class ZuoybScoringContentServiceImpl extends BaseServiceImpl<ZuoybScoringContent, String> implements ZuoybScoringContentService{
    @Autowired
	private  ZuoybScoringContentDao  zuoybScoringContentDao;
	@Override
	public PagingAndSortingRepository<ZuoybScoringContent, String> getDao() {
		return zuoybScoringContentDao;
	}
	@Override
	public Page<ZuoybScoringContent> findByPage(Map<String, Object> params,
			Pageable sp) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<ZuoybScoringContent> specification = DynamicSpecifications.bySearchFilter(filters.values(), ZuoybScoringContent.class);    
		return  zuoybScoringContentDao.findAll(specification, sp);
	}
	@Override
	public void insertZuoybScoringContent(String libId) {
		Integer count=zuoybScoringContentDao.getLibIdCount(libId, libId);
		if(count==0){
			List<ZuoybScoringContent>  list  =zuoybScoringContentDao.findByLibId("SSJ"); 
			for (ZuoybScoringContent zuoybScoringContent : list) {
				ZuoybScoringContent  content = new ZuoybScoringContent();
				content.setContent(zuoybScoringContent.getContent());
				content.setCreateTime(new Date());
				content.setLibId(libId);
				content.setNum(zuoybScoringContent.getNum());
				content.setZybCourseId(libId);
				content.setType(zuoybScoringContent.getType());
				content.setStat(zuoybScoringContent.getStat());
				content.setMatching(zuoybScoringContent.getMatching());
				content.setChildStat(zuoybScoringContent.getChildStat());
				this.save(content);
				//zuoybScoringContentDao.insertZuoybScoringContentDetail(libId, content.getId(), zuoybScoringContent.getId());
			}
		}
	}
	@Override
	public void bathInsertZuoybScoringContent(List<TbLibJoin> joinlist){
		List<ZuoybScoringContent>  list  =zuoybScoringContentDao.findByLibId("SSJ"); 
		for (TbLibJoin tbLibJoin : joinlist) {
			Integer count=zuoybScoringContentDao.getLibIdCount(tbLibJoin.getId(), tbLibJoin.getId());
			if(count==0){
				for (ZuoybScoringContent zuoybScoringContent : list) {
					ZuoybScoringContent  content = new ZuoybScoringContent();
					content.setContent(zuoybScoringContent.getContent());
					content.setCreateTime(new Date());
					content.setLibId(tbLibJoin.getId());
					content.setNum(zuoybScoringContent.getNum());
					content.setZybCourseId(tbLibJoin.getId());
					content.setType(zuoybScoringContent.getType());
					content.setStat(zuoybScoringContent.getStat());
					content.setMatching(zuoybScoringContent.getMatching());
					content.setChildStat(zuoybScoringContent.getChildStat());
					this.save(content);
					//zuoybScoringContentDao.insertZuoybScoringContentDetail(tbLibJoin.getId(), content.getId(), zuoybScoringContent.getId());
				}
			}
		}
	}
	@Override
	public void insertLibIdScoringContent(String libId,String zybCourseId,String sortCode) {
		List<ZuoybScoringContent>  list  ;
		if("zyb_course_zt".equals(sortCode)){
			list  =zuoybScoringContentDao.findByLibIdListZt(libId,libId); 
		}else{
			list  =zuoybScoringContentDao.findByLibIdListKm(libId,libId); 
		}

		}
	@Override
	public void selectContentSave(String zybCourseId, String libId, String[] ids) {

		
	}
	@Override
	public List<ZuoybScoringContent> findByLibIdAndZybCourseId(String libId,
			String zybCourseId) {
		return zuoybScoringContentDao.findByLibIdAndZybCourseId(libId, zybCourseId);
	}

}
