package com.ssj.service.kmt.kmt.service.impl;


import com.ssj.bean.kmt.kmt.domain.KmtArrangeDetail;
import com.ssj.bean.kmt.kmt.domain.KmtArrangeTemplate;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.bean.weixin.push.domain.SendTemplateShort;
import com.ssj.dao.kmt.kmt.dao.KmtArrangeQueryDao;
import com.ssj.dao.kmt.kmt.dao.KmtArrangeTemplateDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.Constant;
import com.ssj.service.kmt.arrange.dto.*;
import com.ssj.service.kmt.arrange.service.KmtArrangeAllotService;
import com.ssj.service.kmt.arrange.service.KmtArrangeDetailService;
import com.ssj.service.kmt.arrange.service.KmtArrangeTemplateService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.weixin.push.service.PushTemplateService;
import org.slf4j.Logger;
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.*;


	
@Transactional
@Service
public class KmtArrangeTemplateServiceImpl extends BaseServiceImpl<KmtArrangeTemplate, String> implements KmtArrangeTemplateService {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private KmtArrangeTemplateDao dao;
	
	@Autowired
	private KmtArrangeQueryDao queryDao;
	
	@Autowired
	private KmtArrangeDetailService kmtArrangeDetailService;
	
	@Autowired
	private KmtArrangeAllotService kmtArrangeAllotService;
	
	@Autowired
	private PushTemplateService pushTemplateService;
	
	@Autowired
	private IBookManagerService bookManagerService;
	
	
	
	@Override
	public PagingAndSortingRepository<KmtArrangeTemplate, String> getDao() {
		return dao;
	}


	@Override
	public void generateKmtArrangeDetailByNextMonth() {
		List<String[]> calendarList=DateHelper.nextCalendarList();
		String[] subjects={"语文","数学","英语"};
		Integer[] types={Constant.KMT_122_TEACHER_TYPE,Constant.KNOW_MATCH_TYPE};
		
		List<KmtArrangeTemplate> arrangeTemplateList=dao.findKmtArrangeTemplateList();
		List<KmtArrangeDetail> kmtArrangeDetails=new ArrayList<KmtArrangeDetail>();
		KmtArrangeDetail kmtArrangeDetail=null;
		for (String subject : subjects) {
			for (Integer type : types) {
				for (String[] dates : calendarList) {
					for (KmtArrangeTemplate kmtArrangeTemplate : arrangeTemplateList) {
						kmtArrangeDetail=new KmtArrangeDetail();
						kmtArrangeDetail.setArrangeDate(DateHelper.parseDate(dates[0],"yyyy-MM-dd"));
						kmtArrangeDetail.setWeek(Integer.valueOf(dates[1]));
						kmtArrangeDetail.setArrangeTemplateId(kmtArrangeTemplate.getId());
						kmtArrangeDetail.setStartInterval(kmtArrangeTemplate.getStartInterval());
						kmtArrangeDetail.setEndInterval(kmtArrangeTemplate.getEndInterval());
						kmtArrangeDetail.setIsOpen(1);
						kmtArrangeDetail.setMaxNum(0);
						kmtArrangeDetail.setMiniNum(0);
						kmtArrangeDetail.setHaveNum(0);
						kmtArrangeDetail.setStatus(1);
						kmtArrangeDetail.setSubject(subject);
						kmtArrangeDetail.setType(type);
						kmtArrangeDetail.setCreateTime(new Date());
						kmtArrangeDetails.add(kmtArrangeDetail);
					}
				}
			}
		}
		kmtArrangeDetailService.save(kmtArrangeDetails);
	}

	@Override
	public void updateKmtArrangeDetailByNextMonth() {   
		//月活泼数量
		Integer activeNum=kmtArrangeAllotService.countArrangeActiveNum();
		  
		List<KmtArrangeTemplate> arrangeTemplateList=dao.findKmtArrangeTemplateList();
		List<KmtArrangeDetail> kmtArrangeDetails=kmtArrangeDetailService.findKmtArrangeDetailByMonth(DateHelper.format(DateHelper.nextMonthFirstDate(), "yyyy-MM-dd"));
		if(kmtArrangeDetails==null || kmtArrangeDetails.size()<=0){
			//没有的话，再生成下一个月的数据
			generateKmtArrangeDetailByNextMonth();
			kmtArrangeDetails=kmtArrangeDetailService.findKmtArrangeDetailByMonth(DateHelper.format(DateHelper.nextMonthFirstDate(), "yyyy-MM-dd"));
		}
		for (KmtArrangeTemplate kmtArrangeTemplate : arrangeTemplateList) {
			
			//如果系统中配置的activeNum数为null或小于0,就使用系统中生成的月活泼数量，要不就使用手动配置的月活泼数量
			if(kmtArrangeTemplate.getActiveNum()==null || kmtArrangeTemplate.getActiveNum()<0){
				kmtArrangeTemplate.setActiveNum(activeNum);
				kmtArrangeTemplate.setUpdateTime(new Date());
				this.save(kmtArrangeTemplate);
			}
			
			for (KmtArrangeDetail kmtArrangeDetail : kmtArrangeDetails) {
				if(kmtArrangeTemplate.getId().equals(kmtArrangeDetail.getArrangeTemplateId())){
					if(kmtArrangeDetail.getWeek()<=5){//周中
						if(kmtArrangeDetail.getType()==Constant.KMT_122_TEACHER_TYPE){//批改人数 
							kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum() * 1.5));
							kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum()));
						}else if(kmtArrangeDetail.getType()==Constant.KNOW_MATCH_TYPE){//知识点匹配人数
							if(kmtArrangeDetail.getSubject().equals("语文")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 5 ));
							}else if(kmtArrangeDetail.getSubject().equals("数学")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 10));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 10));
							}else if(kmtArrangeDetail.getSubject().equals("英语")){
								Integer eActiveNum=(int)Math.ceil(kmtArrangeTemplate.getActiveNum() * 0.8) ;
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() / 5));
							}
						}
					}else{//周末
						if(kmtArrangeDetail.getType()==Constant.KMT_122_TEACHER_TYPE){//批改人数 
							kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum() * 1.5));
							kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum()));
						}else if(kmtArrangeDetail.getType()==Constant.KNOW_MATCH_TYPE){//知识点匹配人数
							if(kmtArrangeDetail.getSubject().equals("语文")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 5));
							}else if(kmtArrangeDetail.getSubject().equals("数学")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 10));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 10));
							}else if(kmtArrangeDetail.getSubject().equals("英语")){
								Integer eActiveNum=(int)Math.ceil(kmtArrangeTemplate.getActiveNum() * 0.8) ;
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() / 5));
							}
						}
					}
				}
				kmtArrangeDetail.setUpdateTime(new Date());
			}
		}
		kmtArrangeDetailService.save(kmtArrangeDetails);
		//this.updateKmtArrangeTemplateByActiveNum(activeNum);
	}


	@Override
	public void updateKmtArrangeTemplateByActiveNum(int activeNum) {
		dao.updateKmtArrangeTemplateByActiveNum(activeNum);
	}


	@Override
	public List<CurCalendarDto> findArrangeTemplateIntervalList() {
		List<CurCalendarDto> list=new ArrayList<CurCalendarDto>();
		ArrangeCalendarDto arrangeCalendarDto=kmtArrangeDetailService.findCalendarList(null, "8", "语文");
		List<ArrangeDetailDto> arrangeDetailDtos=arrangeCalendarDto.getList();
		for (ArrangeDetailDto arrangeDetailDto : arrangeDetailDtos) {
			CurCalendarDto curCalendarDto=new CurCalendarDto();
			curCalendarDto.setArrangeDate(arrangeDetailDto.getArrangeDate());
			curCalendarDto.setIsOpen(arrangeDetailDto.getIsOpen());
			curCalendarDto.setWeek(arrangeDetailDto.getWeek());
			
			List<ArrangeIntervalDto> ArrangeIntervalDtos=arrangeDetailDto.getIntervalList();
			for (ArrangeIntervalDto arrangeIntervalDto : ArrangeIntervalDtos) {
				CurCalendarIntervalDto curCalendarIntervalDto=new CurCalendarIntervalDto();
				curCalendarIntervalDto.setId(arrangeIntervalDto.getId());
				curCalendarIntervalDto.setArrangeDate(arrangeIntervalDto.getArrangeDate());
				curCalendarIntervalDto.setStartInterval(arrangeIntervalDto.getStartInterval());
				curCalendarIntervalDto.setEndInterval(arrangeIntervalDto.getEndInterval());
				Date aDate=DateHelper.parseDate(arrangeIntervalDto.getArrangeDate()+" "+arrangeIntervalDto.getEndInterval(), "yyyy-MM-dd HH:mm");
				if(aDate.getTime()<=new Date().getTime()){
					curCalendarIntervalDto.setIsEnd(1);
				}else{
					curCalendarIntervalDto.setIsEnd(0);
				}
				curCalendarDto.getIntervalList().add(curCalendarIntervalDto);
			}
			list.add(curCalendarDto);
		}
		return list;
	}
	
	
	@Override
	public KmtArrangeTemplate getKmtArrangeDetailByCurTime() {
		return dao.getKmtArrangeDetailByCurTime();
	}
	


	@Override
	public void pushArrangeTemplateByUserList() {
		
		KmtArrangeTemplate kmtArrangeTemplate=this.getKmtArrangeDetailByCurTime();
		if(kmtArrangeTemplate!=null){
			List<Map<String, Object>> teacherUsers=queryDao.findKmtArrangeAllotByArrangeTemplateIdList(kmtArrangeTemplate.getId());
		
			String template_id_short="OPENTM413234390";
			Map<String, String> data = new HashMap<String, String>();
			data.put("first", "根据您的作业批改排班时间，距离开始批改作业还有10分钟，请及时上线。");
            
			for (Map<String, Object> map : teacherUsers) {
				try {
					data.put("keyword2", map.get("arrange_date")+" "+map.get("start_interval"));
					TbLibManager tbLibManager=null;
					List<TbLibManager> tbLibManagers= bookManagerService.findByUserIdAndTypeIn(map.get("teacher_id").toString(), new int[]{Constant.KMT_122_TEACHER_TYPE,Constant.KNOW_MATCH_TYPE});
					if(tbLibManagers!=null && tbLibManagers.size()>0){
						tbLibManagers.get(0);
						
						for (TbLibManager tm : tbLibManagers) {
							if(tm.getType()==Integer.parseInt(map.get("type").toString())){
								tbLibManager=tm;
								break;
							}
						}	
					}
					
					if(map.containsKey("wx_account")){
						data.put("keyword1", tbLibManager!=null ? tbLibManager.getName() : "");
						SendTemplateShort sendTemplateShort = new SendTemplateShort(map.get("wx_account").toString(), template_id_short, null, data);
						pushTemplateService.saveAndSendPushWxTemplate(sendTemplateShort);
					}
					
				} catch (Exception e) {
					logger.error("距离开始批改作业还有10分钟推送异常{}...",e.getMessage());
				}
			}
		}
		
	}


	@Override
	public void updateKmtArrangeDetailByNextMonth(Integer activeNum) {
		if(activeNum==null || activeNum<=0){
			logger.error("活泼数量不能为空");
			return;
		}
		List<KmtArrangeTemplate> arrangeTemplateList=dao.findKmtArrangeTemplateList();
		List<KmtArrangeDetail> kmtArrangeDetails=kmtArrangeDetailService.findKmtArrangeDetailByMonth(DateHelper.format(DateHelper.nextMonthFirstDate(), "yyyy-MM-dd"));
		if(kmtArrangeDetails==null || kmtArrangeDetails.size()<=0){
			//没有的话，再生成下一个月份的数据
			generateKmtArrangeDetailByNextMonth();
			kmtArrangeDetails=kmtArrangeDetailService.findKmtArrangeDetailByMonth(DateHelper.format(DateHelper.nextMonthFirstDate(), "yyyy-MM-dd"));
		}
		for (KmtArrangeTemplate kmtArrangeTemplate : arrangeTemplateList) {
			kmtArrangeTemplate.setActiveNum(activeNum);
			for (KmtArrangeDetail kmtArrangeDetail : kmtArrangeDetails) {
				if(kmtArrangeTemplate.getId().equals(kmtArrangeDetail.getArrangeTemplateId())){
					if(kmtArrangeDetail.getWeek()<=5){//周中
						if(kmtArrangeDetail.getType()==Constant.KMT_122_TEACHER_TYPE){//批改人数 
							kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum() * 1.5));
							kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum()));
						}else if(kmtArrangeDetail.getType()==Constant.KNOW_MATCH_TYPE){//知识点匹配人数
							if(kmtArrangeDetail.getSubject().equals("语文")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 5 ));
							}else if(kmtArrangeDetail.getSubject().equals("数学")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 10));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 10));
							}else if(kmtArrangeDetail.getSubject().equals("英语")){
								Integer eActiveNum=(int)Math.ceil(kmtArrangeTemplate.getActiveNum() * 0.8) ;
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWorkRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() / 5));
							}
						}
					}else{//周末
						if(kmtArrangeDetail.getType()==Constant.KMT_122_TEACHER_TYPE){//批改人数 
							kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum() * 1.5));
							kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getAverageNum()));
						}else if(kmtArrangeDetail.getType()==Constant.KNOW_MATCH_TYPE){//知识点匹配人数
							if(kmtArrangeDetail.getSubject().equals("语文")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 5));
							}else if(kmtArrangeDetail.getSubject().equals("数学")){
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 10));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * kmtArrangeTemplate.getActiveNum() / kmtArrangeTemplate.getKnowAverageNum() / 10));
							}else if(kmtArrangeDetail.getSubject().equals("英语")){
								Integer eActiveNum=(int)Math.ceil(kmtArrangeTemplate.getActiveNum() * 0.8) ;
								kmtArrangeDetail.setMaxNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() * 1.5 / 5));
								kmtArrangeDetail.setMiniNum((int)Math.ceil(kmtArrangeTemplate.getKnowWeekendRatio() * eActiveNum / kmtArrangeTemplate.getKnowAverageNum() / 5));
							}
						}
					}
				}
				kmtArrangeDetail.setUpdateTime(new Date());
			}
		}
		kmtArrangeDetailService.save(kmtArrangeDetails);
		this.updateKmtArrangeTemplateByActiveNum(activeNum);
	}


	

}
	

