package com.ssj.service.task;

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.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.framework.basic.utils.StringUtils;
import com.ssj.framework.core.util.Constant;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.conch.teacher.service.TeacherService;
import com.ssj.service.kmt.dto.CompositionCommentDto;
import com.ssj.service.sys.homework.request.CorrectionRequest;
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.homework.service.SconTeacherCorrectRecordService;
import com.ssj.service.sys.template.CommentTemplateMapperService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.weixin.library.service.ILibVipService;
import com.ssj.sys.util.xunfei.XunFeiTTSUtils;
import com.ssj.taskthread.Task;
import org.apache.commons.collections.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chchen
 * @Description
 * @Date Create in 16:19 2019/11/6
 * @Modified By
 */
public class SyncCheckerSubmitHomeworkCorrect  extends Task {

    private CorrectionRequest request;

    private HomeworkPicture homeworkPicture;

    public SyncCheckerSubmitHomeworkCorrect(CorrectionRequest request, HomeworkPicture homeworkPicture) {
        super();
        this.request=request;
        this.homeworkPicture = homeworkPicture;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
            HomeworkPictureService homeworkPictureService = SystemResourceLocator.getBean(HomeworkPictureService.class);
            CommentTemplateMapperService commentTemplateMapperService = SystemResourceLocator.getBean(CommentTemplateMapperService.class);
            TeacherService teacherService = SystemResourceLocator.getBean(TeacherService.class);
            ILibVipService vipService = SystemResourceLocator.getBean(ILibVipService.class);
            IBookManagerService bookManagerService = SystemResourceLocator.getBean(IBookManagerService.class);
            HomeworkPictureCorrectService homeworkPictureCorrectService = SystemResourceLocator.getBean(HomeworkPictureCorrectService.class);
            HomeworkService homeworkService = SystemResourceLocator.getBean(HomeworkService.class);

            Date nowDateTime = new Date();
            //作文评语转语音
            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);
                    }
                }
            }

            //不批改的需要把状态改成正常的状态
            if (homeworkPicture.getIsMark() == 10){
                homeworkPictureService.push2conch4vagueImg(homeworkPicture, 12);
                if (CollectionUtils.isEmpty(request.getAnswerList())){
                    //分配知识点老师
                    try {
                        teacherService.allotKnowTeacherByHomeworkPicture(homeworkPicture, vipService.getById(homeworkPicture.getVipId()));
                    } catch (Exception e){
                        logger.error("分配匹配老师异常{}", e);
                    }
                }
            }

            //二次批改状态记录，只有是可批改时间才设置为未匹配
            int hour = DateUtil.getHour(nowDateTime);
            if (homeworkPicture.getIsFeedback() == 1 && DateUtil.compareDate(hour < 14 ? DateUtil.getTodatDate() : DateUtil.addDays(nowDateTime, -1),
                    homeworkPicture.getCreateTime())){
                TbLibManager knowTeacher = bookManagerService.findByUserId(homeworkPicture.getKnowTeacherId(), Constant.KNOW_MATCH_TYPE);
                if (knowTeacher != null) {
                    teacherService.pushWxToKnowTeacher4SecondCorrect(knowTeacher, homeworkPicture.getVipId(), homeworkPicture.getSubject());
                }
            }

            homeworkService.findWrongQuestionAndSetRedis(homeworkPicture.getHomeworkId());
            homeworkService.findWrongHowDetailAndSetRedis(homeworkPicture.getHomeworkId());
            //质检员批改的时候单科作业肯定是已完成
            if (StringUtils.isNotBlank(homeworkPicture.getDerver()) && !"1".equals(homeworkPicture.getDerver())){
                homeworkPictureService.singleSujectPushConche(homeworkPicture, true, nowDateTime);
            }
            homeworkPictureService.singleSujectPushPad(homeworkPicture, nowDateTime);
        } catch (Exception e){
            logger.error("异常", e);
        }
    }
}
