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.MsPaperStudentScoreMapper;
import com.xhkjedu.smarking.model.exam.MsClass;
import com.xhkjedu.smarking.model.exam.MsClassStudent;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudent;
import com.xhkjedu.smarking.model.stupaper.MsPaperStudentScore;
import com.xhkjedu.smarking.utils.ImportUtil;
import com.xhkjedu.smarking.vo.exam.ESubjectVo;
import com.xhkjedu.smarking.vo.stupaper.PaperStuScoreVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @Description：补录成绩
 * @Author: WN
 * @Date: 2024/11/11 17:24:33
 **/
@Service
public class MsPaperStudentScoreService {
    @Resource
    private MsPaperStudentScoreMapper msPaperStudentScoreMapper;
    @Resource
    private MsPaperStudentMapper msPaperStudentMapper;
    @Resource
    private MsExamMapper msExamMapper;

    /*
     * @Description 补录成绩-列表
     * @Date 2024/11/12 11:44:54
     * @Author WN
     * @Param [examid]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listPaperStudentScores(Integer examid){
        return msPaperStudentScoreMapper.listStuScores(examid);
    }

    /*
     * @Description 补录成绩-补录单科
     * @Date 2024/11/12 9:53:21
     * @Author WN
     * @Param [msPaperStudentScore]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void saveStudentScore(MsPaperStudentScore msPaperStudentScore) throws Exception{
        MsPaperStudent paperStudent = msPaperStudentMapper.selectByPrimaryKey(msPaperStudentScore.getMpsid());
        //获取考试状态
        Integer examStatus = msExamMapper.getExamstate(paperStudent.getExamid());
        if(examStatus==3){
            throw new Exception("考试已统分，无法补录成绩");
        }

        msPaperStudentScore.setMpid(paperStudent.getMpid());
        msPaperStudentScore.setExamid(paperStudent.getExamid());
        msPaperStudentScore.setSubjectid(paperStudent.getSubjectid());
        msPaperStudentScore.setStudentid(paperStudent.getStudentid());
        msPaperStudentScore.setFillpre(paperStudent.getStuscore());
        msPaperStudentScore.setCreatetime(N_Utils.getSecondTimestamp());
        msPaperStudentScoreMapper.insert(msPaperStudentScore);//保存补录历史
        msPaperStudentMapper.updateStuScoreForFillScore(msPaperStudentScore);//修改学生分数
    }

    /*
     * @Description 补录成绩-补录多科
     * @Date 2024/11/12 10:20:37
     * @Author WN
     * @Param [list]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchStudentScores(PaperStuScoreVo stuscore) throws Exception{
        //获取考试状态
        Integer examStatus = msExamMapper.getExamstate(stuscore.getExamid());
        if(examStatus==3){
            throw new Exception("考试已统分，无法补录成绩");
        }

        //获取考试对应学生所有科目信息
        List<MsPaperStudent> paperStudents = msPaperStudentMapper.listPaperStudentForFillScore(stuscore.getExamid(), stuscore.getStudentid());

        List<MsPaperStudentScore> list = stuscore.getStuscores();
        List<MsPaperStudentScore> saveScore = new ArrayList<>();
        Integer timestamp = N_Utils.getSecondTimestamp();
        for(MsPaperStudentScore pscore : list){
            //补录分数大于0时保存
            if(pscore.getFillscore()!=null && pscore.getFillscore()>0){
                pscore.setCreateid(stuscore.getCreateid());
                pscore.setCreatetime(timestamp);
                pscore.setExamid(stuscore.getExamid());
                pscore.setStudentid(stuscore.getStudentid());

                //根据学生试卷ID获取其他信息
                MsPaperStudent paperStudent = paperStudents.stream().filter(ps -> ps.getMpsid().equals(pscore.getMpsid())).findFirst().get();
                pscore.setMpid(paperStudent.getMpid());
                pscore.setSubjectid(paperStudent.getSubjectid());
                pscore.setFillpre(paperStudent.getStuscore());
                saveScore.add(pscore);
            }
        }

        if(!saveScore.isEmpty()){
            msPaperStudentScoreMapper.insertList(saveScore);//保存补录历史
            msPaperStudentMapper.updateBatchStuScoreForFillScore(saveScore);//修改学生分数
        }
    }

    /*
     * @Description 补录成绩-删除
     * @Date 2024/11/12 15:39:22
     * @Author WN
     * @Param [examid, studentid]
     * @Return void
     **/
    public void deleteStuScore(Integer examid,Integer studentid){
        //删除学生所有科目补录成绩
        msPaperStudentScoreMapper.deleteStuScoreByExamid(examid,studentid);
        //恢复学生补录成绩
        msPaperStudentMapper.updateStuScoreForRestoreFillScore(examid,studentid);
    }

    /*
     * @Description 补录成绩-补录记录
     * @Date 2024/11/12 11:31:07
     * @Author WN
     * @Param [classStudent]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listStuScoresHistory(MsClassStudent classStudent){
        return msPaperStudentScoreMapper.listStuScoresHistory(classStudent);
    }

    /*
     * @Description 补录成绩-新增补录-班级学生
     * @Date 2024/11/12 16:27:16
     * @Author WN
     * @Param [examid, classid]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listStudentsForAdd(Integer examid, Integer classid){
        return msPaperStudentScoreMapper.listStudentsForAdd(examid, classid);
    }

    /*
     * @Description 补录成绩-新增补录-学生科目分数
     * @Date 2024/11/12 16:34:53
     * @Author WN
     * @Param [examid, studentid]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listStudentSubjectScores(Integer examid, Integer studentid){
        return msPaperStudentScoreMapper.listStudentSubjectScores(examid, studentid);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultVo saveBatch(MsPaperStudentScore mpstuScore, MultipartFile file) throws Exception{

        //获取考试状态
        Integer examStatus = msExamMapper.getExamstate(mpstuScore.getExamid());
        if(examStatus==3){
            return new ResultVo(1, "考试已统分，无法补录成绩");
        }
        //获取考试的所有科目
        List<ESubjectVo> subjects = msPaperStudentScoreMapper.listSubjectsByExamid(mpstuScore.getExamid());
        Map<String,ESubjectVo> subjectMap = subjects.stream().collect(HashMap::new, (m,v)->m.put(v.getSubjectid(),v), HashMap::putAll);
        //考试班级
        List<MsClass> classes = msPaperStudentScoreMapper.listClassesByExamid(mpstuScore.getExamid());
        List<String> classNames = classes.stream().map(MsClass::getClassname).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        //获取考试的所有学生
        List<PaperStuScoreVo> allStudents = msPaperStudentScoreMapper.listStudentScoreByExamid(mpstuScore.getExamid());
        Map<String,PaperStuScoreVo> stuMap = allStudents.stream().collect(HashMap::new, (m,v)->m.put(v.getExamno(),v), HashMap::putAll);
        //表格中学生在考生管理找不到时：统一成：学生不存在或没有参加本次考试
        List<MsPaperStudentScore> saveScore = ImportUtil.readExcelStudentScore(file, subjectMap, stuMap,classNames, mpstuScore.getCreateid());
        if(!saveScore.isEmpty()){
            msPaperStudentScoreMapper.insertList(saveScore);//保存补录历史
            msPaperStudentMapper.updateBatchStuScoreForFillScore(saveScore);//修改学生得分
        }
        return new ResultVo(0, "保存成功");
    }
}
