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

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

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ssj.bean.weixin.zyb.domain.ZuoybKnowHow;
import com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetails;
import com.ssj.bean.weixin.zyb.domain.ZuoybStudentWork;
import com.ssj.bean.weixin.zyb.domain.ZuoybStudentWorkHow;
import com.ssj.bean.weixin.zyb.domain.ZuoybStudentWorkHowDetails;
import com.ssj.bean.weixin.zyb.domain.ZuoybStudentWorkImg;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybStudentWorkHowDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.weixin.zuoyb.service.ZuoybKnowHowDetailsService;
import com.ssj.service.weixin.zuoyb.service.ZuoybKnowHowService;
import com.ssj.service.weixin.zuoyb.service.ZuoybStudentWorkHowDetailsService;
import com.ssj.service.weixin.zuoyb.service.ZuoybStudentWorkHowService;
import com.ssj.service.weixin.zuoyb.service.ZuoybStudentWorkImgService;
import com.ssj.service.weixin.zuoyb.service.ZuoybStudentWorkService;

/** 
 * @author  ZhangCaibao 
 * @version 2017年11月1日 下午3:24:33 
 * 类说明 
 */
@Service(version="1.0")
@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);
				}
			}
		}
		
	}

}
