package com.xhkjedu.smarking.service.report.report;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.smarking.mapper.exam.MsClassMapper;
import com.xhkjedu.smarking.mapper.report.reportclass.MsrClassQobjsubMapper;
import com.xhkjedu.smarking.mapper.report.reportclass.MsrClassQuestionMapper;
import com.xhkjedu.smarking.mapper.report.reportclass.MsrClassSubjectGradeMapper;
import com.xhkjedu.smarking.mapper.report.reportother.MsrReportparamMapper;
import com.xhkjedu.smarking.mapper.report.reportsubject.MsrSubjectMapper;
import com.xhkjedu.smarking.mapper.report.reportsubject.MsrSubjectQuestionRankgroupMapper;
import com.xhkjedu.smarking.mapper.report.reportsubject.MsrSubjectQuestionSectionMapper;
import com.xhkjedu.smarking.model.exam.MsClass;
import com.xhkjedu.smarking.model.report.reportclass.MsrClassQuestion;
import com.xhkjedu.smarking.model.report.reportclass.MsrClassSubjectGrade;
import com.xhkjedu.smarking.model.report.reportother.MsrReportparam;
import com.xhkjedu.smarking.utils.MarkingUtil;
import com.xhkjedu.smarking.vo.report.reportother.ExamReportParams;
import com.xhkjedu.smarking.vo.report.reportother.SzJsonVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description：试题表
 * @Author: WN
 * @Date: 2024/12/24 9:47:39
 **/
@Service
public class MsrClassQuestionService {
    @Resource
    private MsrClassSubjectGradeMapper msrClassSubjectGradeMapper;
    @Resource
    private MsrSubjectMapper msrSubjectMapper;
    @Resource
    private MsrReportparamMapper msrReportparamMapper;
    @Resource
    private MsrClassQuestionMapper msrClassQuestionMapper;
    @Resource
    private MsClassMapper msClassMapper;
    @Resource
    private MsrSubjectQuestionSectionMapper msrSubjectQuestionSectionMapper;
    @Resource
    private MsrSubjectQuestionRankgroupMapper msrSubjectQuestionRankgroupMapper;
    @Resource
    private MsrClassQobjsubMapper msrClassQobjsubMapper;


    //综合报告-试题汇总表-全部班级-科目情况
    public Map<String,Object> getSubjectForAllSmall(MsrClassSubjectGrade subjectGrade){
        //获取科目基础信息
        Map<String,Object> subjectMap = msrSubjectMapper.getSubjectBaseInfo(subjectGrade.getExamid(),subjectGrade.getSubjectid());
        subjectGrade.setClassid(0);
        subjectGrade.setReportcode("zh");
        //获取科目四率
        List<MsrClassSubjectGrade> subjectGradeList = msrClassSubjectGradeMapper.listSubjectGrade(subjectGrade);
        subjectMap.put("subjectGradeList",subjectGradeList);

        //获取四率参数
        MsrReportparam reportparam = msrReportparamMapper.getReportparamByRpbelongSubject(subjectGrade.getExamid(),"scorerate","zh",subjectGrade.getSubjectid());

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("reportparam",reportparam);
        rtnMap.put("subject",subjectMap);

        return rtnMap;
    }
    //综合报告-试题汇总表-全部分类-按小题分析
    public Map<String,Object> listQuestionForAllSmall(ExamReportParams params){
        List<Map<String,Object>> questionList = msrClassQuestionMapper.listQuestionSummaryForSmall(params);
        List<Map<String,Object>> objlist;
        List<Map<String,Object>> sublist;
        if(params.getSelclass() == 2){//选择班级类型1全体 2全部班级对比 3指定班级对比 4单个班级
            //获取考试关联班级
            List<MsClass> classList= msClassMapper.listClassByExamidAndSubjectid(params.getExamid(),params.getSubjectid());

            objlist = new ArrayList<>();
            sublist = new ArrayList<>();

            //初始化学校全体
            List<Map<String,Object>> allSchool = questionList.stream().filter(m -> m.get("classid").equals(0)).collect(Collectors.toList());
            for(Map<String,Object> school : allSchool){
                school.put("classname","学校全体");
                if(school.get("qtype").equals("1")){
                    objlist.add(school);
                }else{
                    sublist.add(school);
                }
            }

            for(MsClass c : classList){
                List<Map<String,Object>> classQues = questionList.stream().filter(m -> m.get("classid").equals(c.getClassid())).collect(Collectors.toList());
                for(Map<String,Object> cq : classQues){
                    cq.put("classname",c.getClassname());
                    if(cq.get("qtype").equals("1")){
                        objlist.add(cq);
                    }else{
                        sublist.add(cq);
                    }
                }
            }
        }else{
            objlist = questionList.stream().filter(m -> m.get("qtype").equals("1")).collect(Collectors.toList());
            sublist = questionList.stream().filter(m -> m.get("qtype").equals("2")).collect(Collectors.toList());
        }

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("objlist",objlist);
        rtnMap.put("sublist",sublist);
        return rtnMap;
    }
    //综合报告-试题汇总表-全部分类-按大题分析
    public List<Map<String,Object>> listQuestionForAllQtype(ExamReportParams params){
        return msrClassQuestionMapper.listQuestionSummaryForQtype(params);
    }

    //综合报告-试题汇总表-全部分类-按主(客)题分析
    public List<Map<String,Object>> listQuestionForAllObjsub(ExamReportParams params){
        return msrClassQuestionMapper.listQuestionSummaryForObjsub(params);
    }

    //学科报告-各班学科试题分析-得分率
    public Map<String,Object> listQuestionForXhXhstDfl(ExamReportParams params){
        params.setSelclass(3);
        params.setClassids("0," + params.getClassids());//拼上全体
        //获取选定班级的试题分析
        List<MsrClassQuestion> questions = msrClassQuestionMapper.listQuestionForXh(params);
        //获取全体试题情况
        List<MsrClassQuestion> schoolQuestions = questions.stream().filter(m -> m.getClassid() == 0).collect(Collectors.toList());
        List<MsrClassQuestion> classQuestions = questions.stream().filter(m -> m.getClassid() != 0).collect(Collectors.toList());
        //获取所有题号
        List<String> qns = new ArrayList<>();
        //获取全体得分率数据
        List<Map<String,Object>> schoolScoreRate = new ArrayList<>();
        Map<Integer,Object> mptqidScore = new LinkedHashMap<>();//全体各题得分率
        for(MsrClassQuestion q : schoolQuestions){
            Map<String,Object> qMap = new HashMap<>();
            qMap.put("qn",q.getQn());
            qMap.put("mptqid",q.getMptqid());
            qMap.put("qorder",q.getQorder());
            qMap.put("scorerate",q.getScorerate());
            schoolScoreRate.add(qMap);
            mptqidScore.put(q.getMptqid(),q.getScorerate());
            qns.add(q.getQn());
        }

        //把试题按班级分组
        Map<Integer,List<MsrClassQuestion>> classMap = classQuestions.stream().collect(Collectors.groupingBy(MsrClassQuestion::getClassid));
        List<Map<String,Object>> classList = new ArrayList<>();//班级试题得分率差
        for(Map.Entry<Integer,List<MsrClassQuestion>> entry : classMap.entrySet()){
            List<MsrClassQuestion> classQueList = entry.getValue();
            List<Map<String,Object>> queList = new ArrayList<>();
            for(MsrClassQuestion q : classQueList) {
                Map<String, Object> qMap = new HashMap<>();
                qMap.put("qn", q.getQn());
                qMap.put("mptqid", q.getMptqid());
                qMap.put("qorder", q.getQorder());
                qMap.put("scorerate", q.getScorerate());
                qMap.put("scoreratec", q.getScoreratec());
                qMap.put("schoolscorerate", mptqidScore.get(q.getMptqid()));
                queList.add(qMap);
            }

            Map<String,Object> clMap = new HashMap<>();
            clMap.put("classid",entry.getKey());
            clMap.put("classname",classQueList.get(0).getClassname());
            clMap.put("questions",queList.stream().sorted(Comparator.comparing(m -> (Integer) m.get("qorder"))).collect(Collectors.toList()));
            classList.add(clMap);
        }

        //分析诊断-得分率高于全体得分率的班级和试题
        List<MsrClassQuestion> highQuestions = new ArrayList<>();//高于全体得分率的试题情况
        for(MsrClassQuestion q : schoolQuestions){
            List<MsrClassQuestion> hightClass = questions.stream().filter(m -> m.getMptqid().equals(q.getMptqid()) && m.getClassid() != 0 && m.getScorerate() > q.getScorerate()).collect(Collectors.toList());
            if(!hightClass.isEmpty()){
                highQuestions.addAll(hightClass);
            }
        }
        //高分按班级分组，哪个班级试题数量多，则属于高班和试题(统计高于年级平均值的试题数量，看哪个班级最多)
        Map<Integer,List<MsrClassQuestion>> highClassMap = highQuestions.stream().collect(Collectors.groupingBy(MsrClassQuestion::getClassid));
        int maxNum = 0;
        List<MsrClassQuestion> maxClassQuestions = new ArrayList<>();
        for(Map.Entry<Integer,List<MsrClassQuestion>> entry : highClassMap.entrySet()){
            if(entry.getValue().size() > maxNum){
                maxNum = entry.getValue().size();
                maxClassQuestions.addAll(entry.getValue());
            }
        }
        Map<String,Object> highMap = new HashMap<>();//高得分率分析
        if(!maxClassQuestions.isEmpty()){
            highMap.put("classname",maxClassQuestions.get(0).getClassname());
            highMap.put("qnstr",maxClassQuestions.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.joining(",")));
        }
        //列出各个难度的试题中，得分率低于60%的试题,用全体试题的得分率进行难度分组
        Map<String,List<MsrClassQuestion>> ndMap = schoolQuestions.stream().collect(Collectors.groupingBy(MsrClassQuestion::getNdms));
        //获取所有试题难度分组参数
        MsrReportparam ndParam = msrReportparamMapper.getReportparamByRpbelongSubjectType(params.getExamid(),"quesummary","all",params.getSubjectid(),1);
        List<SzJsonVo> jsonList = JSON.parseArray(ndParam.getSzjson(), SzJsonVo.class);
        List<Map<String,Object>> ndList = new ArrayList<>();
        for(SzJsonVo szvo : jsonList){
            Map<String,Object> zsndMap = new HashMap<>();
            zsndMap.put("djkey",szvo.getDjkey());
            String qnStr = "";
            if(ndMap.containsKey(szvo.getDjkey())){
                List<MsrClassQuestion> ndQues = ndMap.get(szvo.getDjkey());
                //获取得分率低于60%的试题
                List<MsrClassQuestion> lowQuestions = ndQues.stream().filter(m -> m.getScorerate() < 0.6).collect(Collectors.toList());
                if(!lowQuestions.isEmpty()){
                    qnStr = lowQuestions.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.joining(","));
                }
            }
            if(N_Utils.isEmpty(qnStr)){
                qnStr = "无";
            }
            zsndMap.put("qnstr",qnStr);

            ndList.add(zsndMap);
        }

        Map<String,Object> rtnMap = new LinkedHashMap<>();
        rtnMap.put("qns",qns);
        rtnMap.put("schoolQues",schoolScoreRate);
        rtnMap.put("classQues",classList);
        rtnMap.put("highMap",highMap);
        rtnMap.put("ndList",ndList);
        return rtnMap;
    }
    //学科报告-各班学科试题分析-试题难度和区分度
    public Map<String,Object> listQuestionForXhXhstQfd(ExamReportParams params){
        params.setSelclass(4);
        //获取选定班级的试题分析
        List<MsrClassQuestion> questions = msrClassQuestionMapper.listQuestionForXh(params);
        //表现较好的题目和表现不足的题目，根据得分率差进行排序
        List<MsrClassQuestion> goodAndBadQues = questions.stream().sorted(Comparator.comparing(MsrClassQuestion::getScoreratec).reversed()).collect(Collectors.toList());
        List<MsrClassQuestion> firstN;
        List<MsrClassQuestion> lastN;
        int n = 5;
        if(goodAndBadQues.size() < n){
            n = goodAndBadQues.size()/2;
        }
        // 获取前N个元素
        firstN = goodAndBadQues.stream().limit(n).collect(Collectors.toList());
        // 获取后N个元素
        lastN = goodAndBadQues.stream().skip(goodAndBadQues.size() - n).collect(Collectors.toList());

        //统计图按区分度进行排序
        List<MsrClassQuestion> qfdQuestions = questions.stream().sorted(Comparator.comparing(MsrClassQuestion::getQfd).reversed()).collect(Collectors.toList());
        List<Map<String,Object>> qfdList = new ArrayList<>();
        for(MsrClassQuestion qfd : qfdQuestions){
            Map<String,Object> qfdMap = new HashMap<>();
            qfdMap.put("qn",qfd.getQn());
            qfdMap.put("mptqid",qfd.getMptqid());    // 题目ID
            qfdMap.put("qfd",qfd.getQfd());
            qfdMap.put("scorerate",qfd.getScorerate());
            qfdMap.put("scoreratec",qfd.getScoreratec());
            qfdList.add(qfdMap);
        }

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("firstN",firstN);
        rtnMap.put("lastN",lastN);
        rtnMap.put("qfdList",qfdList);
        return rtnMap;
    }
    //学科报告-各班学科试题分析-试题难度分组表现差异
    public Map<String,Object> listQuestionForXhXhstNdGroup(ExamReportParams params){
        //所有所有试题
        List<MsrClassQuestion> questions = msrClassQuestionMapper.listQuestionForXh(params);

        //获取考试关联班级
        List<MsClass> classList= msClassMapper.listClassByExamidAndSubjectid(params.getExamid(),params.getSubjectid());

        //获取所有试题难度分组参数
        MsrReportparam ndParam = msrReportparamMapper.getReportparamByRpbelongSubjectType(params.getExamid(),"quesummary","all",params.getSubjectid(),1);
        List<SzJsonVo> jsonList = JSON.parseArray(ndParam.getSzjson(), SzJsonVo.class);
        List<Map<String,Object>> ndList = new ArrayList<>();
        List<String> ndfwList = new ArrayList<>();
        for(SzJsonVo szvo : jsonList){
            Map<String,Object> zsndMap = new HashMap<>();
            zsndMap.put("djkey",szvo.getDjkey());
            ndfwList.add(szvo.getDjkey());
            zsndMap.put("classid",0);
            zsndMap.put("classname","全体");
            //获取该难度下所有试题
            List<MsrClassQuestion> ndQues = questions.stream().filter(m -> m.getNdms().equals(szvo.getDjkey())).collect(Collectors.toList());
            //处理全体班级情况
            List<MsrClassQuestion> schoolQues = ndQues.stream().filter(m -> m.getClassid() == 0).collect(Collectors.toList());
            if(schoolQues.isEmpty()){
                //获取
                zsndMap.put("qnstr", "");
                zsndMap.put("scorerate", 0);
            }else{
                //获取
                List<String> qnList = schoolQues.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.toList());
                double sumScoreRate = schoolQues.stream().mapToDouble(MsrClassQuestion::getScorerate).sum();
                zsndMap.put("qnstr", String.join(",", qnList));
                zsndMap.put("scorerate", MarkingUtil.div(sumScoreRate,qnList.size()));
            }
            ndList.add(zsndMap);
            //处理各班情况
            for(MsClass cl : classList){
                List<MsrClassQuestion> classQues = ndQues.stream().filter(m -> Objects.equals(m.getClassid(), cl.getClassid())).collect(Collectors.toList());

                Map<String,Object> classNdMap = new HashMap<>();
                classNdMap.put("djkey",szvo.getDjkey());
                classNdMap.put("classid",cl.getClassid());
                classNdMap.put("classname",cl.getClassname());
                if(classQues.isEmpty()){
                    classNdMap.put("qnstr", "");
                    classNdMap.put("scorerate", 0);
                }else{
                    List<String> classqnList = classQues.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.toList());
                    double classScoreRate = classQues.stream().mapToDouble(MsrClassQuestion::getScorerate).sum();
                    classNdMap.put("qnstr", String.join(",", classqnList));
                    classNdMap.put("scorerate",MarkingUtil.div(classScoreRate, classqnList.size()));
                }
                ndList.add(classNdMap);
            }
        }
        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("ndfwList",ndfwList);
        rtnMap.put("ndList",ndList);
        return rtnMap;
    }

    //班级报告-全部班级-学科试题得分率对比
    public Map<String,Object> listQuestionForBjQuestionScoreRate(ExamReportParams params){
        //获取考试关联班级
        List<MsClass> classList= msClassMapper.listClassByExamidAndSubjectid(params.getExamid(),params.getSubjectid());
        //所有所有试题
        List<MsrClassQuestion> questions = msrClassQuestionMapper.listQuestionForXh(params);
        //返回所有的班级信息
        List<Map<String,Object>> classListMap = new ArrayList<>();
        Map<String,Object> schoolMap = new HashMap<>();
        schoolMap.put("classid",0);
        schoolMap.put("classname","全体");
        classListMap.add(schoolMap);

        for(MsClass cl : classList) {
            Map<String,Object> classMap = new HashMap<>();
            classMap.put("classid",cl.getClassid());
            classMap.put("classname",cl.getClassname());
            classListMap.add(classMap);
        }
        //把试题按试题id分组
        Map<Integer,List<MsrClassQuestion>> qnMap = questions.stream().collect(Collectors.groupingBy(MsrClassQuestion::getMptqid));
        List<Map<String,Object>> quesList = new ArrayList<>();
        for(Map.Entry<Integer,List<MsrClassQuestion>> entry : qnMap.entrySet()) {
            List<MsrClassQuestion> entryList = entry.getValue();
            Map<String,Object> queMap = new HashMap<>();
            queMap.put("qn",entryList.get(0).getQn());
            List<Map<String,Object>> classQueList = new ArrayList<>();
            for(Map<String,Object> cmap : classListMap){
                int classid = (int)cmap.get("classid");
                Map<String,Object> classMap = new HashMap<>();
                classMap.put("classid",classid);
                MsrClassQuestion classQues = entryList.stream().filter(m -> m.getClassid() == classid).findFirst().orElse(null);
                if(classQues!= null){
                    classMap.put("scorerate",classQues.getScorerate());
                }else{
                    classMap.put("scorerate",0);
                }
                classQueList.add(classMap);
            }
            queMap.put("questions",classQueList);
            quesList.add(queMap);
        }

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("questions",quesList);
        rtnMap.put("classList",classListMap);
        return rtnMap;
    }
    //班级报告-单个班级-学科试题考试内在表现
    public Map<String,Object> listQuestionForBjStks(ExamReportParams params){

        //类似学科报告，获取数据
        Map<String,Object> rtnMap = listQuestionForXhXhstQfd(params);

        //所有选择的班级和年级的试题情况
        params.setSelclass(3);
        Integer classid = params.getClassid();
        params.setClassids("0," + params.getClassid());
        List<MsrClassQuestion> questions = msrClassQuestionMapper.listQuestionForXh(params);

        //获取所有试题难度分组参数
        MsrReportparam ndParam = msrReportparamMapper.getReportparamByRpbelongSubjectType(params.getExamid(),"quesummary","all",params.getSubjectid(),1);
        List<SzJsonVo> jsonList = JSON.parseArray(ndParam.getSzjson(), SzJsonVo.class);
        List<Map<String,Object>> ndList = new ArrayList<>();
        for(SzJsonVo szvo : jsonList){
            Map<String,Object> zsndMap = new HashMap<>();
            zsndMap.put("djkey",szvo.getDjkey());
            //获取该难度下所有试题
            List<MsrClassQuestion> ndQues = questions.stream().filter(m -> m.getNdms().equals(szvo.getDjkey())).collect(Collectors.toList());
            //处理全体班级情况
            List<MsrClassQuestion> schoolQues = ndQues.stream().filter(m -> m.getClassid() == 0).collect(Collectors.toList());
            if(schoolQues.isEmpty()){
                zsndMap.put("schoolscorerate", 0);
            }else{
                //获取年级的
                List<String> qnList = schoolQues.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.toList());
                double sumScoreRate = schoolQues.stream().mapToDouble(MsrClassQuestion::getScorerate).sum();
                zsndMap.put("schoolscorerate", MarkingUtil.div(sumScoreRate,qnList.size()));
            }

            //获取班级的情况
            List<MsrClassQuestion> classQues = ndQues.stream().filter(m -> m.getClassid().equals(classid)).collect(Collectors.toList());
            if(classQues.isEmpty()){
                zsndMap.put("classscorerate", 0);
            }else{
                List<String> qnList = schoolQues.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.toList());
                double classScoreRate = schoolQues.stream().mapToDouble(MsrClassQuestion::getScorerate).sum();
                zsndMap.put("classscorerate",MarkingUtil.div(classScoreRate,qnList.size()));
            }

            ndList.add(zsndMap);
        }
        rtnMap.put("ndList",ndList);
        return rtnMap;
    }

    //命题质量报告-命题分析-试题分析指标明细表
    public List<Map<String,Object>> listMtzlStfxQuestions(ExamReportParams params){
        return msrClassQuestionMapper.listMtzlMtfxQuestions(params);
    }

    //命题质量报告-命题分析-试题难度、区分度分布结构表 (缺少区分度诊断)
    public Map<String,Object> listMtzlStndAndQfd(ExamReportParams params){
        //获取本学科整体难度
        params.setSelclass(4);
        params.setClassid(0);
        List<MsrClassQuestion> questions = msrClassQuestionMapper.listQuestionForXh(params);

        //获取所有试题难度分组参数
        MsrReportparam ndParam = msrReportparamMapper.getReportparamByRpbelongSubjectType(params.getExamid(),"quesummary","all",params.getSubjectid(),1);
        List<SzJsonVo> jsonList = JSON.parseArray(ndParam.getSzjson(), SzJsonVo.class);
        List<Map<String,Object>> objList = new ArrayList<>();
        List<Map<String,Object>> subList = new ArrayList<>();
        List<String> ndfwList = new ArrayList<>();
        int ndGroupNum = jsonList.size();
        String ndQuesNum = "";//试卷中试题比例数
        for(SzJsonVo szvo : jsonList){
            //获取难度下试题
            List<MsrClassQuestion> allQues = questions.stream().filter(m -> m.getNdms().equals(szvo.getDjkey())).collect(Collectors.toList());
            ndQuesNum = ndQuesNum + allQues.size() + ":";
            List<MsrClassQuestion> objQues = allQues.stream().filter(m -> m.getQtype() == 1).collect(Collectors.toList());
            List<MsrClassQuestion> subQues = allQues.stream().filter(m -> m.getQtype() == 2).collect(Collectors.toList());

            Map<String,Object> objMap = new HashMap<>();
            objMap.put("ndms",szvo.getDjkey());
            String ndfw;
            if(objQues.isEmpty()){
                ndfw = "[" + szvo.getMinvalue() + "-" + szvo.getMaxvalue();
                if(szvo.getMaxvalue() == 1){
                    ndfw = ndfw + "]";
                }else{
                    ndfw = ndfw + ")";
                }
                objMap.put("num",0);
                objMap.put("score",0);
                objMap.put("qns","");
            }else{
                ndfw = objQues.get(0).getNdfw();

                objMap.put("num",objQues.size());
                objMap.put("score",objQues.stream().mapToDouble(MsrClassQuestion::getFullscore).sum());
                objMap.put("qns",objQues.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.joining(",")));
            }
            objMap.put("ndfw",ndfw);
            objList.add(objMap);

            Map<String,Object> subMap = new HashMap<>();
            subMap.put("ndms",szvo.getDjkey());
            if(subQues.isEmpty()){

                subMap.put("num",0);
                subMap.put("score",0);
                subMap.put("qns","");
            }else{
                subMap.put("num",subQues.size());
                subMap.put("score",subQues.stream().mapToDouble(MsrClassQuestion::getFullscore).sum());
                subMap.put("qns",subQues.stream().map(MsrClassQuestion::getQn).distinct().collect(Collectors.joining(",")));
            }
            subMap.put("ndfw",ndfw);
            subList.add(subMap);

            ndfwList.add(szvo.getDjkey() + ndfw);
        }

        //试题区分度
        List<MsrClassQuestion> qfdQuestions = questions.stream().sorted(Comparator.comparing(MsrClassQuestion::getQfd).reversed()).collect(Collectors.toList());
        List<Map<String,Object>> qfdList = new ArrayList<>();
        for(MsrClassQuestion qfd : qfdQuestions){
            Map<String,Object> qfdMap = new HashMap<>();
            qfdMap.put("qn",qfd.getQn());
            qfdMap.put("qfd",qfd.getQfd());
            qfdMap.put("nd",qfd.getNd());
            qfdList.add(qfdMap);
        }

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("objList",objList);//客观题
        rtnMap.put("subList",subList);//主观题
        rtnMap.put("ndfwList",ndfwList);//表格表头
        rtnMap.put("ndgroupnum",ndGroupNum);//难度分布诊断-分组数量
        rtnMap.put("ndquenum",ndQuesNum.substring(0,ndQuesNum.length()-1));//难度分布诊断-试题量比例
        rtnMap.put("qfdList",qfdList);//试题区分度
        return rtnMap;
    }

    //命题质量报告-试题分析-客观题/主观题分组
    public Map<String,Object> listMtzlStfxAllSt(ExamReportParams params){
        //获取试卷试题
        List<Map<String,Object>> questions = msrClassQuestionMapper.listQuestionsFroMtzlSt(params);
        //把试题按客观题、主观题分组
        Map<Integer,List<Map<String,Object>>> queMap = questions.stream().collect(Collectors.groupingBy(m -> (int)m.get("qtype")));

        MsrReportparam ndParam = msrReportparamMapper.getReportparamByRpbelongSubjectType(params.getExamid(),"quesummary","all",params.getSubjectid(),1);
        List<SzJsonVo> jsonList = JSON.parseArray(ndParam.getSzjson(), SzJsonVo.class);
        List<String> ndfwList = new ArrayList<>();
        for(SzJsonVo szvo : jsonList){
            String ndfw = "[" + szvo.getMinvalue() + "-" + szvo.getMaxvalue();
            if(szvo.getMaxvalue() == 1) {
                ndfw = ndfw + "]";
            }else{
                ndfw = ndfw + ")";
            }
            ndfwList.add(szvo.getDjkey() + ndfw);
        }

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("objList",queMap.get(1));
        rtnMap.put("subList",queMap.get(2));
        rtnMap.put("ndfwList",ndfwList);
        return rtnMap;
    }
    //命题质量报告-试题分析-客观题/主观题分组-单个试题分析
    public Map<String,Object> listMtzlStfxOneQue(ExamReportParams params){
        //试题常用分析指标
        Map<String,Object> question = msrClassQuestionMapper.listQuestionByMptqidForMtzlSt(params);
        //平均得分率曲线图
        params.setQntype(1);
        List<Map<String,Object>> avgRateList = msrSubjectQuestionSectionMapper.listQuestionSectionByQntype(params);
        //试题分组分数
        List<Map<String,Object>> groupList = msrSubjectQuestionRankgroupMapper.listMtzlRankgroup(params);

        Map<String,Object> rtnMap = handleMtzlGroupRank(groupList);

        //客观题选项数据
        List<Map<String,Object>> objList = groupList.stream().filter(m -> m.get("rgtype").equals(1)).collect(Collectors.toList());
        List<Map<String,Object>> objGroupList = new ArrayList<>();
        List<String> optionTitles = new ArrayList<>();
        List<Map<String,Object>> objOptionList = new ArrayList<>();
        if(!objList.isEmpty()){
            List<String> objGroupNames = objList.stream().map(m -> m.get("rgroupname").toString()).distinct().collect(Collectors.toList());
            for(String objGroupName : objGroupNames){
                Map<String,Object> objMap = new HashMap<>();
                objMap.put("groupname",objGroupName);
                objMap.put("options",objList.stream().filter(m -> m.get("rgroupname").equals(objGroupName)).collect(Collectors.toList()));
                objGroupList.add(objMap);
            }

            optionTitles = objList.stream().map(m -> m.get("rgroup").toString()).distinct().collect(Collectors.toList());
            List<Object> options = objList.stream().map(m -> m.get("rsection")).distinct().collect(Collectors.toList());
            for(Object option : options){
                Map<String,Object> map = new HashMap<>();
                map.put("option",option);
                map.put("groupranks",objList.stream().filter(m -> m.get("rsection").equals(option)).collect(Collectors.toList()));
                objOptionList.add(map);
            }

            List<Map<String,Object>> groupScoreList2 = groupList.stream().filter(m -> N_Utils.isEmpty(m.get("rsection")) && m.get("rgtype").equals(2)).collect(Collectors.toList());
            Map<String,Object> hjmap = new HashMap<>();
            hjmap.put("option","合计");
            hjmap.put("groupranks",groupScoreList2);
            objOptionList.add(hjmap);
            Map<String,Object> dflmap = new HashMap<>();
            dflmap.put("option","得分率");
            dflmap.put("groupranks",groupScoreList2);
            objOptionList.add(dflmap);
        }

        rtnMap.put("question",question);//试题常用指标
        rtnMap.put("avgRateList",avgRateList);//平均得分率曲线
        rtnMap.put("objGroupList",objGroupList);//客观题-选项分组分布
        rtnMap.put("optionTitles",optionTitles);//客观题-选项分组分布表-表头
        rtnMap.put("objOptionList",objOptionList);//客观题-选项分组分布表-数据
        return rtnMap;
    }

    //命题质量报告-试题结构与题块分析
    public Map<String,Object> listMtzlStjgAndZk(ExamReportParams params){
        //常用分析指标
        Map<String,Object> cjzb = msrClassQobjsubMapper.listQobjOrSubsBySubjectid(params);
        if(params.getQtype() == 1){
            params.setQntype(2);
        }else{
            params.setQntype(3);
        }
        //分均得分率曲线图
        List<Map<String,Object>> avgRateList = msrSubjectQuestionSectionMapper.listQuestionSectionByQntype(params);
        params.setRgtype(2);
        //试题分组分数
        List<Map<String,Object>> groupList = msrSubjectQuestionRankgroupMapper.listMtzlRankgroup(params);
        Map<String,Object> rtnMap = handleMtzlGroupRank(groupList);
        rtnMap.put("cjzb",cjzb);//常用分析指标
        rtnMap.put("avgRateList",avgRateList);//平均得分率曲线
        return rtnMap;
    }

    //命题质量-试题分组分布
    private Map<String,Object> handleMtzlGroupRank(List<Map<String,Object>> groupList){

        //分组表现水平
        List<Map<String,Object>> groupScoreRateList = groupList.stream().filter(m -> N_Utils.isEmpty(m.get("rsection"))).map(m -> {
            Map<String,Object> map = new HashMap<>();
            map.put("groupname",m.get("rgroupname"));    //分组名
            map.put("avgrate",m.get("avgrate"));
            return map;}).collect(Collectors.toList());

        //分组分数分表
        List<String> groupRankTitles = groupList.stream().map(m -> m.get("rgroup").toString()).distinct().collect(Collectors.toList());
        List<Object> rsections = groupList.stream().map(m -> m.get("rsection")).distinct().collect(Collectors.toList());
        //分组分数分布表
        List<Map<String,Object>> groupRankList = new ArrayList<>();
        for(Object rsection : rsections){
            if(N_Utils.isNotEmpty(rsection)){
                List<Map<String,Object>> rsectionList = groupList.stream().filter(m -> m.get("rsection").equals(rsection)).collect(Collectors.toList());
                Map<String,Object> rsMap = new HashMap<>();
                rsMap.put("rsection",rsection);
                rsMap.put("groupranks",rsectionList);
                groupRankList.add(rsMap);
            }else{
                List<Map<String,Object>> rsectionList = groupList.stream().filter(m -> N_Utils.isEmpty(m.get("rsection"))).collect(Collectors.toList());
                Map<String,Object> rsMap = new HashMap<>();
                rsMap.put("rsection","合计");
                rsMap.put("groupranks",rsectionList);
                groupRankList.add(rsMap);

                Map<String,Object> dflMap = new HashMap<>();
                dflMap.put("rsection","得分率");
                dflMap.put("groupranks",rsectionList);
                groupRankList.add(dflMap);
            }
        }

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("groupScoreRateList",groupScoreRateList);//分组表现水平折线图
        rtnMap.put("groupRankTitles",groupRankTitles);//分组分数分布-表头
        rtnMap.put("groupRankList",groupRankList);//分组分数分布-数据
        return rtnMap;
    }

}
