package com.xhkjedu.sexam.service.paperstudent;

import com.xhkjedu.sexam.mapper.paperstudent.EPaperStudentMapper;
import com.xhkjedu.sexam.mapper.paperstudent.EPaperStudentQuestionHistoryMapper;
import com.xhkjedu.sexam.mapper.paperstudent.EPaperStudentQuestionMapper;
import com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestion;
import com.xhkjedu.sexam.model.paperstudent.EPaperStudentQuestionHistory;
import com.xhkjedu.sexam.vo.paperstudent.PaperStudentParamsVo;
import com.xhkjedu.sexam.vo.paperstudent.QuestionScoreVo;
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;

@Service
public class EPaperStudentQuestionHistoryService {

    @Resource
    private EPaperStudentMapper ePaperStudentMapper;
    @Resource
    private EPaperStudentQuestionMapper ePaperStudentQuestionMapper;
    @Resource
    private EPaperStudentQuestionHistoryMapper ePaperStudentQuestionHistoryMapper;


    /**
     * @Description 更改学生试题得分
     * @Param [sqh]
     * @Return void
     * @Author wn
     * @Date 2022/8/10 16:17
     **/
    @Transactional(rollbackFor = Exception.class)
    public void updateQuestionStuscore(PaperStudentParamsVo psparams) {
        //获取学生试卷作答情况
        List<EPaperStudentQuestion> stuques = ePaperStudentQuestionMapper.listStuQuestionsByEpsid(psparams.getEpsid());
        List<QuestionScoreVo> queslist = psparams.getQuestions();
        List<EPaperStudentQuestion> upqueslist = new ArrayList<>();//修改的试题分值
        List<EPaperStudentQuestionHistory> sqhlist = new ArrayList<>();//修改历史记录
        for (QuestionScoreVo q : queslist) {
            EPaperStudentQuestion sq = new EPaperStudentQuestion();
            sq.setEpsqid(q.getEpsqid());
            sq.setStuscore(q.getStuscore());
            upqueslist.add(sq);

            //获取该试题
            EPaperStudentQuestion psques = stuques.stream().filter(s-> s.getEpsqid().equals(q.getEpsqid())).findFirst().orElse(null);
            if(psques !=null){
                EPaperStudentQuestionHistory sqh = new EPaperStudentQuestionHistory();
                sqh.setExamid(psques.getExamid());
                sqh.setSubjectid(psques.getSubjectid());
                sqh.setEpid(psques.getEpid());
                sqh.setClassid(psques.getClassid());
                sqh.setEpsqid(q.getEpsqid());
                sqh.setStudentid(psques.getStudentid());
                sqh.setCreatetime(N_Utils.getSecondTimestamp());
                sqh.setStuscore(psques.getStuscore());
                sqh.setUpscore(q.getStuscore());
                sqh.setQn(psques.getQn());
                sqh.setTeacherid(psparams.getTeacherid());
                sqhlist.add(sqh);
            }
        }

        //修改学生单个试题得分
        ePaperStudentQuestionMapper.updateBatchStuQuestionScore(upqueslist);
        //修改学生试卷分值
        ePaperStudentMapper.updateStuPaperscoreEpsid(psparams.getEpsid());
        //保存更改学生分值记录
        ePaperStudentQuestionHistoryMapper.insertList(sqhlist);
    }

    /**
     * @Description 更改学生试题得分
     * @Param [sqh]
     * @Return void
     * @Author wn
     * @Date 2022/8/10 15:07
     **/
    public List<Map> listExamStuQuestionHistory(Integer examid, String subjectid){
        return ePaperStudentQuestionHistoryMapper.listExamStuQuestionHistory(examid,subjectid);
    }
}
