package com.xhkjedu.sexam.service.paperstudent;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sexam.mapper.exam.EBaseMapper;
import com.xhkjedu.sexam.mapper.exam.EClassMapper;
import com.xhkjedu.sexam.mapper.paper.EPaperMapper;
import com.xhkjedu.sexam.mapper.paperstudent.EScanbatchMapper;
import com.xhkjedu.sexam.model.paperstudent.EScanbatch;
import com.xhkjedu.sexam.utils.ExamUtil;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @Description: TODO
 * @Author: wn
 * @CreateTime: 2022-08-11  09:31
 */
@Service
public class EScanbatchService {
    @Resource
    private EScanbatchMapper eScanbatchMapper;
    @Resource
    private EPaperMapper ePaperMapper;
    @Resource
    private EBaseMapper eBaseMapper;

    @Resource
    private EClassMapper eClassMapper;

    /**
     * @description: 保持扫描试卷批次信息
     * @param: [eScanbatch]
     * @return: java.util.Map
     * @author: wn
     * @date: 2022/8/11/011 9:42
     **/
    public Map saveScanBatch(EScanbatch eScanbatch){
        String batchname = eScanbatch.getDevice() + "-" + eScanbatch.getEpid() + "-";
        //获取数据库中该批次名的扫描批次数量
        Integer bnum = eScanbatchMapper.getBatchNameLikeNum(batchname);
        if(bnum == null || bnum == 0){
            bnum = 1;
        }else{
            bnum++;
        }
        batchname = batchname + bnum;
        //对应考试id
        Integer examid = ePaperMapper.getExamIdByEpId(eScanbatch.getEpid());
        eScanbatch.setBatchname(batchname);
        eScanbatch.setExamid(examid);
        eScanbatch.setScannum(0);
        eScanbatch.setErrornum(0);
        eScanbatch.setQknum(0);
        eScanbatch.setWgnum(0);
        eScanbatch.setCreatetime(N_Utils.getSecondTimestamp());
        eScanbatchMapper.insertUseGeneratedKeys(eScanbatch);
        Integer batchid = eScanbatch.getId();
        Map rtnmap = new HashMap<>();
        rtnmap.put("batchid",batchid);
        rtnmap.put("batchname",batchname);
        return rtnmap;
    }

    //扫描答题卡列表
    public Map listScan(Integer examid) {
        Map map = eBaseMapper.findById(examid);
        List<Map> subjects = eScanbatchMapper.listScanSubject(examid);//扫描答题卡列表
        map.put("subjects", subjects);
        return map;
    }

    /**
     * @description: 扫码答卷详情
     * @param: [epid]
     * @return: java.util.Map
     * @author: wn
     * @date:
     **/
    public Map scanPaperDetail(Integer epid) {
        Map paper = ePaperMapper.getExamPaperSubjectForScan(epid);
        String alljson = paper.get("alljson").toString();
        Map pamap = JSON.parseObject(alljson,Map.class);
        List<Map> ranglist = (List<Map>) pamap.get("ranglist");
        for(Map m : ranglist){
            String ptname = m.get("qtname").toString();
            Integer num = Integer.parseInt( m.get("num").toString());
            if("客观题".equals(ptname)){
                paper.put("kgt",num);
            }else{
                paper.put("zgt",num);
            }
        }
        paper.remove("alljson");

        List<Map> batchlist = eScanbatchMapper.listScanBatchPapers(epid);

        Map rtnmap = new HashMap();
        rtnmap.put("paper",paper);
        rtnmap.put("scans",batchlist);
        return rtnmap;
    }

    //扫描答题卡详情
    public Map getScanDetail(Integer epid, Integer examid) {
        Map map = new HashMap();
        List<Map> classes = eClassMapper.listByExamId(examid);
        int stunum = classes.stream().mapToInt(m -> (int) m.get("classnum")).sum();
        EScanbatch scanbatch = eScanbatchMapper.getScanResult(epid);//获取答题卡扫描结果
        Integer batchnum = scanbatch.getBatchnum();
        Integer scannum = scanbatch.getScannum();
        Integer errornum = scanbatch.getErrornum();
        Integer qknum = scanbatch.getQknum();
        Integer wsnum = ExamUtil.subtractInt(stunum, batchnum);
        map.put("jhnum", stunum);//计划张数
        map.put("wsnum", wsnum);//未扫张数
        map.put("ysnum", batchnum);//已扫张数
        map.put("qknum", qknum);//缺考人数
        map.put("smrate", N_Utils.getIntegerDivideAndMulitiply(wsnum, stunum));//扫描进度
        map.put("sbnum", scannum);//识别张数
        map.put("sbrate", N_Utils.getIntegerDivideAndMulitiply(scannum - errornum, scannum));//识别比例
        map.put("classes", classes);//班级列表
        return map;
    }
}
