package com.xhkjedu.smarking.service.papercheck;

import com.xhkjedu.exception.ServiceException;
import com.xhkjedu.smarking.mapper.paper.MsPaperMapper;
import com.xhkjedu.smarking.mapper.papercheck.MsPaperCheckTeacherProblemMapper;
import com.xhkjedu.smarking.mapper.stupaper.*;
import com.xhkjedu.smarking.model.paper.MsPaper;
import com.xhkjedu.smarking.model.paper.MsPaperBlockQuestion;
import com.xhkjedu.smarking.model.papercheck.*;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentBlock;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion;
import com.xhkjedu.smarking.utils.MarkingUtil;
import com.xhkjedu.smarking.vo.papercheck.MsCheckParam;
import com.xhkjedu.smarking.vo.papercheck.MsPaperCheckVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description 疑难卷教师任务表 服务实现类
 * @Author auto
 * @Date 2024-11-21
 */
@Service
public class MsPaperCheckTeacherProblemService {
    @Resource
    private MsPaperCheckTeacherProblemMapper msPaperCheckTeacherProblemMapper;
    @Resource
    private MsPaperMapper msPaperMapper;
    @Resource
    private MsPaperStudentBlockMapper msPaperStudentBlockMapper;
    @Resource
    private MsPaperStudentQuestionMapper msPaperStudentQuestionMapper;
    @Resource
    private MsPaperStudentMapper msPaperStudentMapper;

    /**
     * @Description 学科组长-疑难卷列表
     * @Date 2024/11/21 10:01
     * @Author YWX
     * @Param [mpid]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listPendingProblem(Integer mpid) {
        MsCheckParam param = new MsCheckParam();
        param.setMpid(mpid);
        Integer correctType = msPaperMapper.getCorrectTypeByMpId(param.getMpid());
        if (correctType.equals(3)) {
            return msPaperCheckTeacherProblemMapper.listBlockProblem(param);//学科组长-待仲裁题块列表
        } else {
            return msPaperCheckTeacherProblemMapper.listQuestionProblem(param);//学科组长-待仲裁试题列表
        }
    }

    /**
     * @Description 获取疑难卷任务
     * @Date 2024/11/21 10:19
     * @Author YWX
     * @Param [param]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @Transactional(rollbackFor = Exception.class)
    public ResultVo getCorrectProblem(MsCheckParam param) {
        Integer mpid = param.getMpid();
        MsPaper paper = msPaperMapper.selectByPrimaryKey(mpid);
        Integer correcttype = paper.getCorrecttype();
        if (!N_Utils.isTrueInteger(correcttype)) {
            throw new ServiceException("该试卷未设置批阅方式");
        }
        param.setCorrecttype(correcttype);
        param.setSubjectid(paper.getSubjectid());
        Map sq = msPaperCheckTeacherProblemMapper.getCorrectProblem(param);

        Integer totalNum = 0;
        Integer checkedNum = 0;
        if (sq != null) {
            sq.put("correcttype", correcttype);
            Integer mblockid = N_Utils.obj2Int(sq.get("mblockid"));
            param.setMblockid(mblockid);
            MsPaperCheckTeacher teacher = msPaperCheckTeacherProblemMapper.getPCheckNum(param);
            totalNum = teacher.getTasknum();
            checkedNum = teacher.getCheckednum();
            sq.put("mpid", mpid);
            msPaperCheckTeacherProblemMapper.updateHasProblem(param);

            //序号
            Integer taskorder = checkedNum + 1;
            sq.put("taskorder", taskorder);
            MsPaperCheckTeacherProblem task = new MsPaperCheckTeacherProblem();
            task.setMpid(mpid);
            task.setMblockid(mblockid);
            task.setTeacherid(param.getHandleid());
            task.setTaskorder(taskorder);
            task.setMpsbid(N_Utils.obj2Int(sq.get("mpsbid")));
            task.setCheckstate(2);
            task.setCreatetime(N_Utils.getSecondTimestamp());
            msPaperCheckTeacherProblemMapper.save(task);
        }
        Map map = new HashMap();
        map.put("sq", sq);
        map.put("totalNum", totalNum);
        map.put("checkedNum", checkedNum);
        String msg = "获取成功";
        return new ResultVo(0, msg, map);
    }

    /**
     * @Description 处理疑难卷
     * @Date 2024/11/21 11:17
     * @Author YWX
     * @Param [param]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void correct(MsPaperCheckVo param) {
        Integer correcttype = msPaperMapper.getCorrectTypeByMpId(param.getMpid());
        Integer mpsbid = param.getMpsbid();
        Integer handleid = param.getHandleid();
        Double qscore = param.getQscore();
        Double arbitratescore = param.getArbitratescore();
        Integer hasgood = param.getHasgood();
        Integer hasbad = param.getHasbad();
        String stumerge = param.getStumerge();
        if (correcttype.equals(3)) {//按题块批阅
            List<MsPaperBlockQuestion> questions = param.getQuestions();
            List<MsPaperStudentQuestion> studentQuestions = new ArrayList<>();
            for (MsPaperBlockQuestion question : questions) {
                Double stuscore = question.getStuscore();
                if (N_Utils.isTrueInteger(question.getMptqid())) {
                    MsPaperStudentQuestion studentQuestion = new MsPaperStudentQuestion();
                    studentQuestion.setMptqid(question.getMptqid());
                    studentQuestion.setStuscore(stuscore);
                    studentQuestions.add(studentQuestion);
                } else {
                    String[] mptqids = question.getMergeqid().split(",");
                    String[] mergestuscore = question.getMergestuscore().split(",");
                    double[] scores = new double[mergestuscore.length];
                    int last = mergestuscore.length - 1;
                    double score = 0;
                    for (int i = 0; i < mergestuscore.length; i++) {
                        scores[i] = MarkingUtil.mul(MarkingUtil.div(mergestuscore[i], qscore), stuscore);
                        score = MarkingUtil.add(score, scores[i]);
                        if (i == last) {
                            scores[i] = MarkingUtil.sub(stuscore, score);
                        }
                    }
                    for (int i = 0; i < mptqids.length; i++) {
                        MsPaperStudentQuestion studentQuestion = new MsPaperStudentQuestion();
                        studentQuestion.setMptqid(N_Utils.obj2Int(mptqids[i]));
                        studentQuestion.setStuscore(scores[i]);
                        studentQuestion.setArbitratescore(scores[i]);
                        studentQuestions.add(studentQuestion);
                    }
                }
            }

            //更处理结果
            MsPaperStudentBlock psq = new MsPaperStudentBlock();
            psq.setMpsbid(mpsbid);
            psq.setChecked(3);
            psq.setHasgood(hasgood);
            psq.setHasbad(hasbad);
            psq.setHasproblem(3);
            psq.setProblehid(handleid);
            psq.setStumerge(stumerge);
            msPaperStudentBlockMapper.updateByPrimaryKeySelective(psq);
            msPaperStudentQuestionMapper.checkByMptqidAndStudentId(studentQuestions, psq);
        } else {
            //更新处理结果
            MsPaperStudentQuestion psq = new MsPaperStudentQuestion();
            psq.setMpsqid(mpsbid);
            psq.setChecked(3);
            psq.setStuscore(arbitratescore);
            psq.setHasgood(hasgood);
            psq.setHasbad(hasbad);
            psq.setHasproblem(3);
            psq.setProblehid(handleid);
            psq.setStumerge(stumerge);
            msPaperStudentQuestionMapper.updateByPrimaryKeySelective(psq);
        }
        msPaperCheckTeacherProblemMapper.correct(param);

        Integer mpsid = msPaperCheckTeacherProblemMapper.getMpsIdByMpsbid(mpsbid, correcttype);
        msPaperStudentMapper.updateStudentPaperScoreByMpsId(mpsid);//更新学生总分
    }

    /**
     * @Description 获取上一份疑难卷
     * @Date 2024/11/21 14:35
     * @Author YWX
     * @Param [param]
     * @Return java.util.Map
     **/
    public Map getPreProblem(MsCheckParam param) {
        return msPaperCheckTeacherProblemMapper.getPreProblem(param);
    }

    /**
     * @Description 历史记录列表
     * @Date 2024/11/21 11:00
     * @Author YWX
     * @Param [param]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listHistory(MsCheckParam param) {
        return msPaperCheckTeacherProblemMapper.listHistory(param);
    }

    /**
     * @Description 历史记录详情
     * @Date 2024/11/21 14:04
     * @Author YWX
     * @Param [param]
     * @Return java.util.Map
     **/
    public Map detailHistory(MsCheckParam param) {
        return msPaperCheckTeacherProblemMapper.detailHistory(param);
    }

    /**
     * @Description 打回重阅
     * @Date 2024/11/21 15:19
     * @Author YWX
     * @Param [param]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void backCorrect(MsPaperCheckVo param) {
        msPaperCheckTeacherProblemMapper.backCorrect(param);//打回重阅
        msPaperCheckTeacherProblemMapper.deleteByMpsbidAndMpid(param.getMpsbid(), param.getMpid());//删除单个任务
    }
}
