package com.xhkjedu.smarking.service.paper;

import com.xhkjedu.smarking.mapper.exam.MsSubjectMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperBlockMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperBlockQuestionMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperQtypeQuestionMapper;
import com.xhkjedu.smarking.mapper.stupaper.MsPaperStudentMapper;
import com.xhkjedu.smarking.model.paper.MsPaperBlock;
import com.xhkjedu.smarking.model.paper.MsPaperBlockQuestion;
import com.xhkjedu.smarking.vo.paper.MsPaperParams;
import com.xhkjedu.smarking.vo.paper.PExamSubjectVo;
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;

/**
 * @Description 阅卷试卷题块 服务实现类
 * @Author auto
 * @Date 2024-10-15
 */
@Service
public class MsPaperBlockService {
    @Resource
    private MsPaperBlockMapper msPaperBlockMapper;
    @Resource
    private MsPaperMapper msPaperMapper;
    @Resource
    private MsPaperBlockQuestionMapper msPaperBlockQuestionMapper;
    @Resource
    private MsSubjectMapper msSubjectMapper;
    @Resource
    private MsPaperStudentMapper msPaperStudentMapper;
    @Resource
    private MsPaperQtypeQuestionMapper msPaperQtypeQuestionMapper;

    /*
     * @Description 获取试卷所有题块
     * @Date 2024/10/21 14:36:00
     * @Author WN
     * @Param [mpid]
     * @Return java.util.List<com.xhkjedu.smarking.model.paper.MsPaperBlock>
     **/
    public List<MsPaperBlock> listPaperBlockQuestions(Integer mpid){
        Integer blocktype = 2;
        return msPaperBlockMapper.listPaperBlockQuestions(mpid,blocktype);
    }


    /**
     * @Description 新增
     * @Param [msPaperBlock]
     * @Author auto
     * @Date 2024-10-15
     * @return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public Integer save(MsPaperParams params){
        PExamSubjectVo paper = msPaperMapper.getPaperAndMxammodeByMpid(params.getMpid());
        Integer mpid = paper.getMpid();

        Integer createid = params.getCreateid();
        Integer timestamp = N_Utils.getSecondTimestamp();
        List<MsPaperBlockQuestion> questions = new ArrayList<>();//保存的题块下试题
        int papernum = 0;//试卷中试题总数量

        //先删除已存在主观题题块
        msPaperBlockMapper.deleteObjByMpidAndBlocktype(mpid,2);

        List<MsPaperBlock> blocks = params.getBlocks();
        for(MsPaperBlock block : blocks){
            block.setBlocktype(2);
            block.setMpid(mpid);
            block.setCreateid(createid);
            block.setCreatetime(timestamp);
            papernum += block.getBqnum();
        }
        msPaperBlockMapper.insertList(blocks);
        //保存题块下试题
        for(MsPaperBlock block : blocks){
            List<MsPaperBlockQuestion> bqs = block.getQuestions();
            for(MsPaperBlockQuestion bq : bqs){
                bq.setMblockid(block.getId());
                bq.setMpid(mpid);
                bq.setCreateid(createid);
                bq.setCreatetime(timestamp);

                questions.add(bq);
            }
        }

        msPaperBlockQuestionMapper.insertList(questions);
        int pbsate = 1;
        if(paper.getExammode() == 3){
            pbsate = 2;//线上考试，设置完题块，科目题块状态变为已完成；线下考试需要为题块框选
        }
        //更改科目题块框选状态
        msSubjectMapper.updatePbstateByMpid(mpid,pbsate);//题块状态变成设置中
        //修改试卷中合并后试题数量
        msPaperMapper.updaePaperMeregpnumByMpid(mpid,papernum);
        //保存完题块后，需要清除已经生成的学生答案、批阅信息之类的数据，
        msPaperStudentMapper.deletePaperStuByMpid(mpid);

        return mpid;
    }
}
