package com.xhkjedu.smarking.service.paper;

import com.xhkjedu.exception.MissingParametersException;
import com.xhkjedu.smarking.mapper.exam.MsSubjectMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperBackMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperFileMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperMapper;
import com.xhkjedu.smarking.mapper.paper.MsPaperQtypeQuestionMapper;
import com.xhkjedu.smarking.mapper.system.SchoolMapper;
import com.xhkjedu.smarking.model.paper.MsPaper;
import com.xhkjedu.smarking.model.paper.MsPaperBack;
import com.xhkjedu.smarking.model.paper.MsPaperFile;
import com.xhkjedu.smarking.utils.MarkingUtil;
import com.xhkjedu.smarking.vo.paper.MsPaperParams;
import com.xhkjedu.smarking.vo.paper.PaperFileVo;
import com.xhkjedu.smarking.vo.system.SchoolCloudVo;
import com.xhkjedu.utils.N_Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 阅卷试卷表 服务实现类
 * @Author auto
 * @Date 2024-10-15
 */
@Service
@Slf4j
public class MsPaperService {
    @Resource
    private MsPaperMapper msPaperMapper;
    @Resource
    private MsPaperBackMapper msPaperBackMapper;
    @Resource
    private MsSubjectMapper msSubjectMapper;
    @Resource
    private MsPaperQtypeQuestionMapper msPaperQtypeQuestionMapper;
    @Resource
    private MsPaperFileMapper msPaperFileMapper;
    @Resource
    private SchoolMapper schoolMapper;

    /*
     * @Description 列表--教师出卷
     * @Date 2024/10/18 10:52:31
     * @Author WN
     * @Param [teacherid, esstate]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listExamPapersByTeacherid(Integer teacherid, Integer pstate) {
        int timestamp = N_Utils.getSecondTimestamp();
        String strtime = N_Utils.getStrtimeTimestamp(timestamp, "yyyy-MM-dd HH:mm:ss");
        return msPaperMapper.listExamPapersByTeacherid(teacherid, pstate, strtime);
    }

    /*
     * @Description 试卷--退回
     * @Date 2024/10/18 11:00:57
     * @Author WN
     * @Param [paperBack]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void savePaperBack(MsPaperBack paperBack) {
        Integer examstate = msPaperMapper.getExamStateByMpid(paperBack.getMpid());
        if (examstate == 1) throw new MissingParametersException("考试已开始禁止操作");
        if (examstate >= 2) throw new MissingParametersException("考试已结束禁止操作");
        int timestamp = N_Utils.getSecondTimestamp();
        paperBack.setCreatetime(timestamp);
        msPaperBackMapper.insertUseGeneratedKeys(paperBack);
        // 更改试卷科目状态
        msSubjectMapper.updatePstateByMpid(paperBack.getMpid(), 3);
    }

    /*
     * @Description 试卷--退回原因列表
     * @Date 2024/10/18 11:01:11
     * @Author WN
     * @Param [mpid]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map<String, Object>> listPaperBacksByEpid(Integer mpid) {
        return msPaperBackMapper.listPaperBacksByMpid(mpid);
    }

    // 获取考试ID
    public Integer getExamidByMpid(Integer mpid) {
        return msPaperMapper.getExamidByMpid(mpid);
    }

    /*
     * @Description 试卷试题-获取试卷中所有试题题号及坐标
     * @Date 2024/12/4 14:39:15
     * @Author WN
     * @Param [mpid]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listPaperQuestionCoors(Integer mpid) {
        return msPaperQtypeQuestionMapper.listQuestionsForShow(mpid);
    }

    /*
     * @Description 试卷试题-保存切割
     * @Date 2024/12/4 15:20:31
     * @Author WN
     * @Param [params]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void savePaperQuestionCoors(MsPaperParams params) {
        // 保存试题切割信息
        msPaperQtypeQuestionMapper.updateBatchQuestionCoor(params.getQuestions());
        // 保存试卷切割状态
        msPaperMapper.updatePaperCutState(params.getMpid(), 1);
    }

    /*
     * @Description 试卷原卷-保存
     * @Date 2024/10/17 16:00:37
     * @Author WN
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @Transactional(rollbackFor = Exception.class)
    public void savePaperFiles(MsPaper paper) {
        Integer mpid = paper.getMpid();
        List<MsPaperFile> pfiles = paper.getPfiles();
        // 删除已保存主观题答案图片
        msPaperFileMapper.deleteByMpidAndType(mpid, 1);
        Integer timestamp = N_Utils.getSecondTimestamp();
        PaperFileVo paperFileVo = null;
        if (pfiles.get(0).getFilepath().contains("base64,")) {
            List<String> base64list = pfiles.stream().map(MsPaperFile::getFilepath).collect(Collectors.toList());

            SchoolCloudVo schoolCloudVo = schoolMapper.getSchoolCloudcode(mpid);
            String saveFloder = schoolCloudVo.getCloudcode() + "/" + schoolCloudVo.getSchoolid() + "/marking/paper";
            paperFileVo = MarkingUtil.uploadBatchBase64ImgPdf(base64list, saveFloder);
        }

        for (int i = 0; i < pfiles.size(); i++) {
            MsPaperFile pf = pfiles.get(i);
            pf.setMpid(mpid);
            pf.setCreateid(paper.getCreateid());
            pf.setCreatetime(timestamp);
            pf.setFiletype(1);
            pf.setFileorder(i + 1);
            if (paperFileVo != null) {
                pf.setSourcepath(paperFileVo.getPdfpath());
                pf.setFilepath(paperFileVo.getImgs().get(i));
            }
        }
        paper.setYjstate(1);
        msPaperMapper.updateByPrimaryKeySelective(paper);
        msPaperFileMapper.insertList(pfiles);
    }


    /*
     * @Description 试卷原卷-删除
     * @Date 2024/12/4 15:34:23
     * @Author WN
     * @Param [mpid]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void deletePaperFiles(Integer mpid) {
        // 删除试卷文件
        msPaperFileMapper.deleteByMpidAndType(mpid, 1);
        // 获取试卷切割状态
        Integer cutstate = msPaperMapper.getPaperCutStateByMpid(mpid);
        if (N_Utils.isTrueInteger(cutstate)) {
            // 说明试卷已切割，清除切割信息
            msPaperMapper.updatePaperCutState(mpid, 0);// 保存试卷切割状态
            msPaperQtypeQuestionMapper.updateQuestionCoorByMpid(mpid);// 清除试题切割信息
        }
    }

}
