package com.xhkjedu.smarking.service.scan;

import com.xhkjedu.smarking.mapper.exam.MsClassMapper;
import com.xhkjedu.smarking.mapper.scan.MsScanbatchMapper;
import com.xhkjedu.smarking.mapper.scan.MsScanbatchPaperMapper;
import com.xhkjedu.smarking.model.exam.MsExam;
import com.xhkjedu.smarking.model.scan.MsScanbatch;
import com.xhkjedu.smarking.model.scan.MsScanbatchPaper;
import com.xhkjedu.smarking.vo.scan.ScanParams;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;

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

/**
 * @Description 阅卷答题卡扫描批次表 服务实现类
 * @Author auto
 * @Date 2024-10-15
 */
@Service
public class MsScanbatchService {
    @Resource
    private MsScanbatchMapper msScanbatchMapper;
    @Resource
    private MsScanbatchPaperMapper msScanbatchPaperMapper;
    @Resource
    private MsClassMapper msClassMapper;

    /**
     * @Description 新增扫描批次
     * @Param [msScanbatch]
     * @Author auto
     * @Date 2024-10-15
     * @return void
     **/
    public ResultVo save(MsScanbatch msScanbatch){
        MsExam exam = msScanbatchMapper.getExamStateAndId(msScanbatch.getMpid());
        if(exam.getExamstate()==3){
            return new ResultVo(1,"考试已统分，禁止扫描");
        }

        String batchname = msScanbatch.getDevice() + "-" + msScanbatch.getMpid() + "-";
        Integer batchnum = msScanbatchMapper.getBatchNumByBatchname(batchname);
        if(!N_Utils.isTrueInteger(batchnum)){
            batchnum = 1;
        }else{
            batchnum ++;
        }
        batchname += batchnum;
        Integer examid = exam.getExamid();
        msScanbatch.setBatchname(batchname);
        msScanbatch.setExamid(examid);
        msScanbatch.setCreatetime(N_Utils.getSecondTimestamp());
        msScanbatch.setScannum(0);
        msScanbatch.setUploadnum(0);
        msScanbatch.setIdentifynum(0);
        msScanbatch.setQknum(0);
        msScanbatch.setErrornum(0);
        msScanbatch.setWgnum(0);
        msScanbatchMapper.insertUseGeneratedKeys(msScanbatch);

        Map<String,Object> map = new HashMap<>();
        map.put("batchname",batchname);
        map.put("batchid",msScanbatch.getId());
        return new ResultVo(0,"保存成功",map);
    }

    /*
     * @Description 扫描答卷-获取批次下所有试卷
     * @Date 2024/10/24 16:25:47
     * @Author WN
     * @Param [batchid]
     * @Return java.util.List<com.xhkjedu.smarking.model.scan.MsScanbatchPaper>
     **/
    public List<MsScanbatchPaper> listPaperByBatchid(Integer batchid){
        return msScanbatchPaperMapper.listPaperByBatchid(batchid);
    }

    /*
     * @Description 扫描答卷-考试扫描信息
     * @Date 2024/10/24 15:09:33
     * @Author WN
     * @Param [mpid]
     * @Return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String,Object> getExamPaperScanInfo(Integer mpid){
        Map<String,Object> map = msScanbatchMapper.getExamPaperScanInfo(mpid);
        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("paper",map);
        rtnMap.put("scans",msScanbatchMapper.listScanBatchForPaper(mpid));
        return rtnMap;
    }

    /*
     * @Description 扫描记录-扫描信息、考试班级、扫描批次
     * @Date 2024/10/25 14:47:51
     * @Author WN
     * @Param [mpid]
     * @Return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String,Object> getScanRecordAndClass(Integer mpid){
        Map<String,Object> map = msScanbatchMapper.getScanPaperRecord(mpid);

        //试卷扫描批次集合
        List<Map<String,Object>> scans = msScanbatchMapper.listScanBatchByMpidForSel(mpid);
        //参加考试的班级
        List<Map<String,Object>> classes = msClassMapper.listByExamId(Integer.parseInt(map.get("examid").toString()), map.get("subjectid").toString());
        map.put("scans",scans);
        map.put("classes",classes);

        return map;
    }

    /*
     * @Description 扫描记录-按班级
     * @Date 2024/10/25 16:50:44
     * @Author WN
     * @Param [params]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listRecordClassStudents(ScanParams params){
        return msScanbatchMapper.listRecordClassStudents(params);
    }

    /*
     * @Description 扫描记录-按批次
     * @Date 2024/10/25 16:54:57
     * @Author WN
     * @Param [params]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String,Object>> listRecordBatchStudents(ScanParams params){
        return msScanbatchMapper.listRecordBatchStudents(params);
    }

}
