package com.ssj.service.task;

import com.ssj.bean.sys.homework.domain.HomeworkCorrectCount;
import com.ssj.bean.sys.homework.domain.HomeworkPicture;
import com.ssj.bean.sys.homework.domain.HomeworkPictureCorrect;
import com.ssj.bean.sys.template.CommentTemplate;
import com.ssj.framework.core.util.RedisUtil;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.kmt.correctsys.service.HomeworkCorrectCountService;
import com.ssj.service.kmt.dto.CompositionCommentDto;
import com.ssj.service.kmt.service.KmtCorrectHomeworkService;
import com.ssj.service.kmt.service.KmtHomeworkWrongQuestionService;
import com.ssj.service.sys.homework.request.CorrectionRequest;
import com.ssj.service.sys.homework.service.HomeworkHowDetailsService;
import com.ssj.service.sys.homework.service.HomeworkPictureCorrectService;
import com.ssj.service.sys.homework.service.HomeworkPictureService;
import com.ssj.service.sys.homework.service.HomeworkService;
import com.ssj.service.sys.template.CommentTemplateMapperService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.sys.util.xunfei.XunFeiTTSUtils;
import com.ssj.taskthread.Task;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class SyncPushTodayFinishHomework  extends Task {


	@Autowired
	private HomeworkPictureCorrectService homeworkPictureCorrectService;

	@Autowired
	private HomeworkCorrectCountService homeworkCorrectCountService;
	
	 private CorrectionRequest request;
	
	 private HomeworkPicture homeworkPicture;

	 public SyncPushTodayFinishHomework(CorrectionRequest request, HomeworkPicture homeworkPicture) {
	        super();
	        this.request=request;
	        this.homeworkPicture = homeworkPicture;
	 } 
	 
	 @Override
	 public void run() {
		 //推送报告
		 try {
			 Thread.sleep(3000);

			 logger.error("作业批改结果推送报告处理任务开始---------------");
			 HomeworkPictureService homeworkPictureService = SystemResourceLocator.getBean(HomeworkPictureService.class);
             HomeworkService homeworkService = SystemResourceLocator.getBean(HomeworkService.class);
             IBookManagerService bookManagerService = SystemResourceLocator.getBean(IBookManagerService.class);
			 CommentTemplateMapperService commentTemplateMapperService = SystemResourceLocator.getBean(CommentTemplateMapperService.class);

			 //待批改数累计减一
			 bookManagerService.updateCorrectHomeworkNum(homeworkPicture.getVipId(), homeworkPicture.getTeacherId(), -1);
             //统计报错数量
			String correctDate=null;
			 int errorCorrectCount=0;
			 int errorMatchCount=0;
			 int homeworkPicCount=0;
			 int knowHomeworkPicount=0;
			 int picTotalCount=0;
			 int matchTotalCount=0;
			 String correctAccuracyRate=null;
			 String knowAccuracyRate=null;
			 Date afterNowTime = DateUtil.parse_yyyyMMddHHmmss(LocalDate.now()+" "+"14:00:00");
			 if (true == DateUtil.compareDate(new Date(), afterNowTime)) {
				 //当前时间小于下午2点
				 correctDate = LocalDate.now().plusDays(-1).toString();
			 } else {
			 	//当前时间大于下午2点
				correctDate=LocalDate.now().toString();
			 }
             //批改老师有效批改总张数
             List<Integer> markList=new ArrayList<>();
             markList.add(0);
             markList.add(2);
			 //统计批改老师报错
		    HomeworkPictureCorrect correct=homeworkPictureCorrectService.findByHomeworkPictureIdAndType(homeworkPicture.getId(),8);
			 if(null!=correct){
                 if(1==correct.getIsCheckError()){
                     errorCorrectCount++;
                 }
                 if(0!=correct.getCorrectNums()){
                     errorCorrectCount +=correct.getCorrectNums();
                 }
                 if(0==correct.getIsCheckError()&&1==homeworkPicture.getServiceType()&&1==homeworkPicture.getIsFeedback()&&true==markList.contains(homeworkPicture.getIsMark())){
                     homeworkPicCount++;
                 }
                 HomeworkCorrectCount homeworkCorrectCount=new HomeworkCorrectCount();
                 homeworkCorrectCount.setUserId(homeworkPicture.getTeacherId());
                 homeworkCorrectCount.setCreateTime(correctDate);
                 homeworkCorrectCount.setType(8);
                 homeworkCorrectCount.setWrongCount(errorCorrectCount);
                 homeworkCorrectCount.setHomeworkPicCount(homeworkPicCount);
                 homeworkCorrectCountService.save(homeworkCorrectCount);
             }
			 //统计匹配老师报错
			 HomeworkPictureCorrect Matchcorrect=homeworkPictureCorrectService.findByHomeworkPictureIdAndType(homeworkPicture.getId(),13);
			 if(null!=Matchcorrect){
                 if(1==Matchcorrect.getIsCheckError()){
                     errorMatchCount++;
                 }
                 if(0!=Matchcorrect.getCorrectNums()){
                     errorMatchCount +=Matchcorrect.getCorrectNums();
                 }
                 //匹配老师有效批改总张数
                 if(0==Matchcorrect.getIsCheckError()&&1==homeworkPicture.getServiceType()&&1==homeworkPicture.getIsKnowFeedback()&&true==markList.contains(homeworkPicture.getIsMark())){
                     knowHomeworkPicount++;
                 }
                 HomeworkCorrectCount homeworkKnowCorrectCount=new HomeworkCorrectCount();
                 homeworkKnowCorrectCount.setUserId(homeworkPicture.getKnowTeacherId());
                 homeworkKnowCorrectCount.setCreateTime(correctDate);
                 homeworkKnowCorrectCount.setType(13);
                 homeworkKnowCorrectCount.setWrongCount(errorMatchCount);
                 homeworkKnowCorrectCount.setHomeworkPicCount(knowHomeworkPicount);
                 homeworkCorrectCountService.save(homeworkKnowCorrectCount);
             }
			 if (CollectionUtils.isNotEmpty(request.getCompositionCommentList())) {
				 List<String> selectedCompositionCommentIdList = request.getCompositionCommentList().stream()
							 .map(CompositionCommentDto::getCompositionCommentId)
						 .distinct()
						 .collect(Collectors.toList());
				 List<CommentTemplate> commentList = commentTemplateMapperService.findCompositionByIds(selectedCompositionCommentIdList);
				 StringBuilder compositionComments = new StringBuilder();
				 if (commentList != null) {
					 for (int i = 0; i < commentList.size(); i++) {
						 compositionComments.append(i + 1).append(" ").append(commentList.get(i).getComment());
					 }
					 if (!compositionComments.toString().equals(homeworkPicture.getCompositionComment())) {
						 homeworkPicture.setCompositionComment(compositionComments.toString());
						 try {
							 String videoPath = XunFeiTTSUtils.textToVoice(compositionComments.toString(), homeworkPicture.getVipId());
							 homeworkPicture.setCompositionCommentPath(videoPath);
						 } catch (Exception e) {
							 logger.error("讯飞转语音异常", e);
						 }
						 homeworkPictureService.save(homeworkPicture);
					 }
				 }
			 }
             homeworkService.findWrongQuestionAndSetRedis(homeworkPicture.getHomeworkId());
             homeworkService.findWrongHowDetailAndSetRedis(homeworkPicture.getHomeworkId());
             //批改后删除单科批改详情的缓存
             homeworkService.removeRedisCorrectDetailDto4(homeworkPicture.getVipId(), homeworkPicture.getSubject(), homeworkPicture.getCreateTime());
			 homeworkPictureService.pushTodayFinishHomework(homeworkPicture, 2);
			 logger.error("作业批改结果推送报告处理任务结束---------------");
		 }catch (Exception e){
		 	logger.error("异常", e);
		 }
	 }
	

}
