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

import com.alibaba.fastjson.JSON;
import com.ssj.bean.weixin.zyb.domain.*;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybStudentWorkHowDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.weixin.zuoyb.service.*;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/** 
 * @author  ZhangCaibao 
 * @version 2017年11月1日 下午3:24:33 
 * 类说明 
 */
@Service
@Transactional
public class ZuoybStudentWorkHowServiceImpl extends BaseServiceImpl<ZuoybStudentWorkHow, String> implements ZuoybStudentWorkHowService {
	private static org.slf4j.Logger logger = LoggerFactory.getLogger(ZuoybKnowHowServiceImpl.class);
	@Autowired
	private  ZuoybStudentWorkHowDao  zuoybStudentWorkHowDao;
	@Autowired
	private  ZuoybStudentWorkHowDetailsService  zuoybStudentWorkHowDetailsService;
	
	@Autowired
	private  ZuoybStudentWorkImgService   zuoybStudentWorkImgService;
	@Autowired
	private  ZuoybStudentWorkService   zuoybStudentWorkService;
	@Autowired
	private  ZuoybKnowHowService   zuoybKnowHowService;
	@Autowired
	private  ZuoybKnowHowDetailsService   zuoybKnowHowDetailsService;
	
	@Override
	public PagingAndSortingRepository<ZuoybStudentWorkHow, String> getDao() {
		return zuoybStudentWorkHowDao;
	}

	@Override
	public void saveZuoybStudentWorkHow(ZuoybStudentWork oldwork,
			List<ZuoybStudentWorkHow> workHowList, String workId) {
		if(oldwork !=null){
			zuoybStudentWorkHowDao.deleteByStudentWorkId(oldwork.getId());
			zuoybStudentWorkHowDetailsService.deleteByStudentWorkId(oldwork.getId());
			 // 删除图片
			//zuoybStudentWorkImgService.deleteByObjectId(oldwork.getId());
		}
		for (ZuoybStudentWorkHow zuoybStudentWorkHow : workHowList) {
			if(zuoybStudentWorkHow==null)continue;
			if(zuoybStudentWorkHow.getHowId()==null||zuoybStudentWorkHow.getHowName()==null)continue;
			Date date = new Date();
			zuoybStudentWorkHow.setStudentWorkId(workId);
			zuoybStudentWorkHow.setCreateTime(date);
			this.save(zuoybStudentWorkHow);
			List<ZuoybStudentWorkHowDetails> workHowDetailsList = zuoybStudentWorkHow.getWorkHowDetailsList();
			for (ZuoybStudentWorkHowDetails zuoybStudentWorkHowDetails : workHowDetailsList) {
				if(zuoybStudentWorkHowDetails==null)continue;
				zuoybStudentWorkHowDetails.setStudentWorkId(workId);
				zuoybStudentWorkHowDetails.setWorkHowId(zuoybStudentWorkHow.getId());
				zuoybStudentWorkHowDetails.setCreateTime(date);
				zuoybStudentWorkHowDetailsService.save(zuoybStudentWorkHowDetails);
				final List<ZuoybStudentWorkImg>  imgList =zuoybStudentWorkHowDetails.getImgList();
				if(imgList!=null && imgList.size()>0){
					final String id = workId;
					final String detailsId = zuoybStudentWorkHowDetails.getId();
					Thread t1 = new Thread(new Runnable() {
						public void run() {
							zuoybStudentWorkImgService.saveWorkHowDetailsImg(imgList, detailsId, id);
						}
					});
					t1.start();
				}
			}
		}
		
	}
	@Override
	public void saveStudentWorkHow(ZuoybStudentWork oldwork,
			List<ZuoybStudentWorkHow> workHowList, String workId) {
		if(oldwork !=null){
			zuoybStudentWorkHowDao.deleteByStudentWorkId(oldwork.getId());
			zuoybStudentWorkHowDetailsService.deleteByStudentWorkId(oldwork.getId());
			//删除图片
		}
		logger.info("workHowList-->"+JSON.toJSONString(workHowList));
		for (ZuoybStudentWorkHow zuoybStudentWorkHow : workHowList) {
			if(zuoybStudentWorkHow==null)continue;
			if(zuoybStudentWorkHow.getHowId()==null||zuoybStudentWorkHow.getHowName()==null)continue;
			Date date = new Date();
			zuoybStudentWorkHow.setStudentWorkId(workId);
			zuoybStudentWorkHow.setCreateTime(date);
			this.save(zuoybStudentWorkHow);
			List<ZuoybStudentWorkHowDetails> workHowDetailsList = zuoybStudentWorkHow.getWorkHowDetailsList();
			for (ZuoybStudentWorkHowDetails zuoybStudentWorkHowDetails : workHowDetailsList) {
				if(zuoybStudentWorkHowDetails==null)continue;
				zuoybStudentWorkHowDetails.setStudentWorkId(workId);
				zuoybStudentWorkHowDetails.setWorkHowId(zuoybStudentWorkHow.getId());
				zuoybStudentWorkHowDetails.setCreateTime(date);
				zuoybStudentWorkHowDetailsService.save(zuoybStudentWorkHowDetails);
				 List<ZuoybStudentWorkImg>  imgList =zuoybStudentWorkHowDetails.getImgList();
				if(imgList!=null && imgList.size()>0){
					int index = 1;	
					for (ZuoybStudentWorkImg img : imgList) {
						if(img ==null){
							continue;
						}
						img.setCreateTime(new Date());
						img.setObjectId(workId);
						img.setHowDetailsId(zuoybStudentWorkHowDetails.getId());//暂定为workid
						img.setNum(index++);
						zuoybStudentWorkImgService.save(img);
						
					}	
					
				}
			}
		}
		
	}

	@Override
	public void deteletByWorkId(String workId) {
		zuoybStudentWorkHowDao.deleteByStudentWorkId(workId);
		zuoybStudentWorkHowDetailsService.deleteByStudentWorkId(workId);
	}

	@Override
	public List<ZuoybStudentWorkHow> getStartAndEndWorkHow(String vipId,
			String start, String end) {
		return zuoybStudentWorkHowDao.getStartAndEndWorkHow(vipId, start, end);
	}
	@Override
	public List<ZuoybStudentWorkHow> getStartAndEndWorkHowAndCourse(String vipId,String start,String end,String course){
		return zuoybStudentWorkHowDao.getStartAndEndWorkHowAndCourse(vipId, start, end,course);
	 }

	@Override
	public List<ZuoybStudentWorkHow> getWorkHow4SaveWorkHow(List<String> howIds, String zybWorkId, int type) {
		return zuoybStudentWorkHowDao.findByStudentWorkIdAndHowIdIn(zybWorkId, howIds, type);
	}

	@Override
	public void oldDataWorkHow() {
		 List<ZuoybStudentWork> list =zuoybStudentWorkService.oldDataWorkHow();
		 for (ZuoybStudentWork zuoybStudentWork : list) {
			 List<ZuoybKnowHow> zuoybKnowHowList= zuoybKnowHowService.getChindIdParentName(zuoybStudentWork.getHowIds());
			 for (ZuoybKnowHow zuoybKnowHow : zuoybKnowHowList) {
				 ZuoybStudentWorkHow  zuoybStudentWorkHow= new  ZuoybStudentWorkHow();
				 zuoybStudentWorkHow.setCreateTime(zuoybStudentWork.getScoringTime());
				 zuoybStudentWorkHow.setHowId(zuoybKnowHow.getId());
				 zuoybStudentWorkHow.setHowName(zuoybKnowHow.getName());
				 zuoybStudentWorkHow.setNum(zuoybKnowHow.getNum());
				 zuoybStudentWorkHow.setStudentWorkId(zuoybStudentWork.getId());
				 this.save(zuoybStudentWorkHow);
				 List<ZuoybKnowHowDetails>  zuoybKnowHowDetailslist=zuoybKnowHowDetailsService.getNameByHowId(zuoybStudentWork.getHowIds());
				 for (ZuoybKnowHowDetails zuoybKnowHowDetails : zuoybKnowHowDetailslist) {
					 ZuoybStudentWorkHowDetails zuoybStudentWorkHowDetails =new  ZuoybStudentWorkHowDetails();
					 zuoybStudentWorkHowDetails.setCreateTime(zuoybStudentWork.getScoringTime());
					// zuoybStudentWorkHowDetails.setNum(zuoybKnowHowDetails.getNum());
					 zuoybStudentWorkHowDetails.setName(zuoybKnowHowDetails.getName());
					 zuoybStudentWorkHowDetails.setHowDetailsId(zuoybKnowHowDetails.getId());
					 zuoybStudentWorkHowDetails.setStudentWorkId(zuoybStudentWork.getId());
					 zuoybStudentWorkHowDetails.setWorkHowId(zuoybStudentWorkHow.getId());
					 zuoybStudentWorkHowDetailsService.save(zuoybStudentWorkHowDetails);
				}
			}
		}
		
	}

}
