package com.xhkjedu.sexam.service.paper;

import com.xhkjedu.exception.MissingParametersException;
import com.xhkjedu.sexam.mapper.exam.EBaseMapper;
import com.xhkjedu.sexam.mapper.exam.ESubjectMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperAnalyzeMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperBackMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperFileMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperMapper;
import com.xhkjedu.sexam.model.paper.EPaperBack;
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 WN
 * Date 2022/7/18 10:42
 **/
@Service
public class EPaperService {

    @Resource
    private EPaperMapper ePaperMapper;
    @Resource
    private EPaperBackMapper ePaperBackMapper;
    @Resource
    private ESubjectMapper eSubjectMapper;
    @Resource
    private EPaperAnalyzeMapper ePaperAnalyzeMapper;
    @Resource
    private EBaseMapper eBaseMapper;
    @Resource
    private EPaperFileMapper ePaperFileMapper;



    //考试各科目试卷设置
    public Map listExamPapers(Integer examid) {
        Map exam = eBaseMapper.findById(examid);
        List<Map> examsub = ePaperMapper.listExamPapers(examid);
        if(examsub!=null){
            exam.put("subjects",examsub);
        }else{
            exam.put("subjects",new ArrayList<>());
        }
        return exam;
    }

    public Map getPaperAnalzyeByEpid(Integer epid){
        return ePaperAnalyzeMapper.getPaperAnalzyeByEpid(epid);
    }

    /**
     * @Description 试卷退回
     * @Param [ePaperBack]
     * @Return void
     * @Author wn
     * @Date 2022/7/19 8:34
     **/
    @Transactional(rollbackFor = Exception.class)
    public void savePaperBack(EPaperBack ePaperBack) {
        Integer examstate = ePaperMapper.getExamStateByEpId(ePaperBack.getEpid());
        if (2 <= examstate) throw new MissingParametersException("考试已结束禁止操作");
        int timestamp = N_Utils.getSecondTimestamp();
        ePaperBack.setCreatetime(timestamp);
        ePaperBackMapper.insertUseGeneratedKeys(ePaperBack);
        //更改试卷科目状态
        eSubjectMapper.updateEsubjectStateByEpid(2, ePaperBack.getEpid());
        //试卷回退需要发送通知和短息，后续添加
    }

    /**
     * @Description 试卷退回原因
     * @Param [epid]
     * @Return java.util.List<java.util.Map>
     * @Author wn
     * @Date 2022/7/19 8:34
     **/
    public List<Map> listPaperBacksByEpid(Integer epid){
        return ePaperBackMapper.listPaperBacksByEpid(epid);
    }

    //教师出卷列表
    public List<Map> listExamPapersByTeacherid(Integer teacherid,Integer esstate){
        int timestamp = N_Utils.getSecondTimestamp();
        String strtime = N_Utils.getStrtimeTimestamp(timestamp, "yyyy-MM-dd HH:mm:ss");
        return ePaperMapper.listExamPapersByTeacherid(teacherid,esstate,strtime);
    }

    //根据试卷学生id获取附件文件
    public List<Map> listExamPaperFiles(Integer epsid){
        return ePaperFileMapper.listPaperFilesByEpsid(epsid);
    }

    //获取题干原文件
    public List<String> listPaperSourceFilesByEpsid(Integer epsid){
        return ePaperFileMapper.listPaperSourceFilesByEpsid(epsid);
    }



}
