package com.qmth.jkserver.templete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.model.ExamPaperTemp;
import com.qmth.jkserver.model.ExamStudentAnswer;
import com.qmth.jkserver.model.ExamTask;
import com.qmth.jkserver.service.ExamPaperTempService;
import com.qmth.jkserver.service.ExamStudentAnswerService;
import com.qmth.jkserver.service.JointFlowSimulationService;
import com.qmth.jkserver.templete.service.TaskLogicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 任务处理逻辑impl
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/3/29
 */
@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class TaskLogicServiceImpl implements TaskLogicService {
    private final static Logger log = LoggerFactory.getLogger(TaskLogicServiceImpl.class);

    @Resource
    JointFlowSimulationService jointFlowSimulationService;

    @Resource
    ExamPaperTempService examPaperTempService;

    @Resource
    ExamStudentAnswerService examStudentAnswerService;

//    @Resource
//    MongoTemplate mongoTemplate;

    /**
     * 执行云阅卷同步数据逻辑
     *
     * @param map 任务包信息
     * @return 返回值
     * @throws IOException 异常
     */
    @Override
    public Map<String, Object> executeCloudMarkLogic(Map<String, Object> map) throws Exception {
        ExamTask examTask = (ExamTask) map.get(SystemConstant.TASK);
        String jkExamCode = examTask.getExamCode();

        // 试卷总数
        List<ExamPaperTemp> examPaperTempList = examPaperTempService.findExamCourseAllByTask(jkExamCode);
        // 考生总数
        List<ExamStudentAnswer> examStudentAnswerDatasource = examStudentAnswerService.findByExamCode(jkExamCode, null);

        BigDecimal totalProgress = new BigDecimal(examPaperTempList.size())
                .multiply(new BigDecimal(2))
                .add(new BigDecimal(examStudentAnswerDatasource.size()))
                .add(new BigDecimal(examStudentAnswerDatasource.stream().filter(e -> !e.getIsAbsent()).count()))
                .subtract(new BigDecimal(examStudentAnswerDatasource.stream().filter(e -> !e.getIsAbsent() && e.getIsCheat() && Objects.isNull(e.getPaperId())).count()).multiply(new BigDecimal(2)));

        examTask.setTotalProgress(totalProgress);
        // 初始化
        AtomicInteger completeCount = new AtomicInteger(examTask.getCompleteProgress());
        if (completeCount == null) {
            completeCount = new AtomicInteger(0);
            examTask.setCompleteProgress(completeCount.get());
        }

        // 1.创建考试
        map = jointFlowSimulationService.saveYunExam(map);

//        Query jkExamStudentAnswerQuery = new Query();
//        jkExamStudentAnswerQuery.addCriteria(Criteria.where("examCode").is(jkExamCode));
//        jkExamStudentAnswerQuery.addCriteria(Criteria.where("isAbsent").is(false));
//        jkExamStudentAnswerQuery.addCriteria(Criteria.where("decodeFile").exists(true));
//        List<ExamStudentAnswer> examStudentAnswerList = this.mongoTemplate.find(jkExamStudentAnswerQuery, ExamStudentAnswer.class);
        QueryWrapper<ExamStudentAnswer> examStudentAnswerQueryWrapper = new QueryWrapper<>();
        examStudentAnswerQueryWrapper.lambda().eq(ExamStudentAnswer::getExamCode, jkExamCode)
                .eq(ExamStudentAnswer::getIsAbsent, false)
                .isNotNull(ExamStudentAnswer::getDecodeFileId);
        List<ExamStudentAnswer> examStudentAnswerList = examStudentAnswerService.list(examStudentAnswerQueryWrapper);
        if (Objects.isNull(examStudentAnswerList) || examStudentAnswerList.size() == 0) {
            examTask.setProgress(new BigDecimal(100));
            return map;
        } else {
            map.put("examStudentAnswerList", examStudentAnswerList);
            // 2.科目和考生创建混合逻辑
            return jointFlowSimulationService.saveYunSubject(map);
        }
//        examPaperTempService.getPaperToJson(jkExamCode);
//        examTask.setTotalProgress(100D);
//        return map;
    }
}
