package com.xhkjedu.smarking.service.stupaper;

import com.xhkjedu.smarking.mapper.exam.MsExamMapper;
import com.xhkjedu.smarking.mapper.stupaper.MsPaperStudentMapper;
import com.xhkjedu.smarking.mapper.stupaper.MsPaperStudentQuestionMapper;
import com.xhkjedu.smarking.mapper.stupaper.MsPaperStudentQuestionScoreMapper;
import com.xhkjedu.smarking.model.exam.MsClassStudent;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestion;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentQuestionScore;
import com.xhkjedu.smarking.vo.stupaper.PaperStuScoreVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 阅卷学生分数修改历史表 服务实现类
 * @Author auto
 * @Date 2024-10-15
 */
@Service
public class MsPaperStudentQuestionScoreService {
    @Resource
    private MsPaperStudentQuestionScoreMapper msPaperStudentQuestionScoreMapper;
    @Resource
    private MsPaperStudentQuestionMapper msPaperStudentQuestionMapper;
    @Resource
    private MsPaperStudentMapper msPaperStudentMapper;
    @Resource
    private MsExamMapper msExamMapper;

    /**
     * @Description 新增
     * @Param [msPaperStudentQuestionScore]
     * @Author auto
     * @Date 2024-10-15
     * @return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void add(PaperStuScoreVo paperStuScoreVo) throws Exception {

        //获取考试状态
        Integer examStatus = msExamMapper.getExamstate(paperStuScoreVo.getExamid());
        if(examStatus==3){
            throw new Exception("考试已统分，无法修改分数");
        }

        //获取试卷中所有试题
        List<MsPaperStudentQuestion> stuQuestions = msPaperStudentQuestionScoreMapper.listStudentQuestions(paperStuScoreVo.getMpsid());
        List<MsPaperStudentQuestionScore> stuscores = paperStuScoreVo.getQuescores();
        List<MsPaperStudentQuestionScore> saveStuscores = new ArrayList<>();
        Integer timestamp = N_Utils.getSecondTimestamp();
        Integer createid = paperStuScoreVo.getCreateid();

        List<Integer> mpsids = new ArrayList<>();
        Integer mpid = null;

        for(MsPaperStudentQuestionScore stuscore : stuscores){
            if(stuscore.getStuscore()!=null){

                stuscore.setCreateid(createid);
                stuscore.setCreatetime(timestamp);
                MsPaperStudentQuestion ques = stuQuestions.stream().filter(q -> q.getMpsqid().equals(stuscore.getMpsqid())).findFirst().orElse(null);
                stuscore.setMpid(ques.getMpid());
                stuscore.setExamid(ques.getExamid());
                stuscore.setSubjectid(ques.getSubjectid());
                stuscore.setStudentid(ques.getStudentid());
                stuscore.setQn(ques.getQn());
                stuscore.setPrescore(ques.getStuscore());
                saveStuscores.add(stuscore);
                mpsids.add(ques.getMpsid());
                mpid = ques.getMpid();
            }
        }

        if(N_Utils.isListNotEmpty(saveStuscores)){
            msPaperStudentQuestionScoreMapper.insertList(saveStuscores);
            //修改学生试题分数
            msPaperStudentQuestionMapper.updateBatchStuscore(saveStuscores);
            //批量修改学生试卷总分
            msPaperStudentMapper.updateBathStudentPaperScoreByMpsid(mpid, mpsids);
        }
    }

    /*
     * @Description 修改分数-班级学生
     * @Date 2024/11/13 9:58:55
     * @Author WN
     * @Param [student]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listClassStudentScores(MsClassStudent student){
        return msPaperStudentQuestionScoreMapper.listClassStudentScores(student);
    }


    /*
     * @Description 修改分数-修改记录
     * @Date 2024/11/11 17:54:16
     * @Author WN
     * @Param [student]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listStuQuestionScore(MsClassStudent student){
        return msPaperStudentQuestionScoreMapper.listStuQuestionScore(student);
    }
}
