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

import com.xhkjedu.smarking.mapper.exam.MsClassMapper;
import com.xhkjedu.smarking.mapper.report.reportclass.MsrClassQuestionMapper;
import com.xhkjedu.smarking.mapper.report.reportsubject.MsrSubjectPointMapper;
import com.xhkjedu.smarking.mapper.report.reportsubject.MsrSubjectPointRankgroupMapper;
import com.xhkjedu.smarking.mapper.report.reportsubject.MsrSubjectPointSectionMapper;
import com.xhkjedu.smarking.model.exam.MsClass;
import com.xhkjedu.smarking.model.report.reportsubject.MsrSubjectPoint;
import com.xhkjedu.smarking.vo.report.reportother.ExamReportParams;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @Description：科目知识点
 * @Author: WN
 * @Date: 2024/12/31 16:11:43
 **/
@Service
public class MsrSubjectPointService {
    @Resource
    private MsrSubjectPointMapper msrSubjectPointMapper;
    @Resource
    private MsrSubjectPointRankgroupMapper msrSubjectPointRankgroupMapper;
    @Resource
    private MsrSubjectPointSectionMapper msrSubjectPointSectionMapper;
    @Resource
    private MsClassMapper msClassMapper;
    @Resource
    private MsrClassQuestionMapper msrClassQuestionMapper;

    //学科素养报告-标定知识点-试题知识点
    public List<Map<String,Object>> listXksyQuestionPoints(ExamReportParams params){
        //获取已标定试题知识点
        return msrSubjectPointMapper.listQuestionPointsForXhsy(params);
    }

    //学科素养报告-知识点整体分析
    public Map<String,Object> listXksyPointZt(ExamReportParams params){
        //层级下所有知识点
        List<MsrSubjectPoint> points = msrSubjectPointMapper.listXksyPointsZt(params);
        if(points.isEmpty()) return null;

        //获取全体学员的知识点
        List<MsrSubjectPoint> schoolPoints = points.stream().filter(p -> p.getClassid()==0).collect(Collectors.toList());
        //根据全体的情况，做出分析诊断
        //把知识点按照，满分排序
        List<MsrSubjectPoint> fullScorePoints = schoolPoints.stream().sorted((p1,p2) -> p2.getFullscore().compareTo(p1.getFullscore())).collect(Collectors.toList());
        //把知识点按照平均得分率排序
        List<MsrSubjectPoint> avgRatePoints = schoolPoints.stream().sorted((p1,p2) -> p2.getAvgrate().compareTo(p1.getAvgrate())).collect(Collectors.toList());
        String[] scoreFirst;
        String[] scorelast;
        String[] rateFrist;
        String[] rateLast;
        if(schoolPoints.size() >=4){
            //前后各取两个
            List<MsrSubjectPoint> firstScore = fullScorePoints.stream().limit(2).collect(Collectors.toList());
            scoreFirst = new String[]{firstScore.get(0).getPointname(), firstScore.get(1).getPointname()};
            List<MsrSubjectPoint> lastScore = fullScorePoints.stream().skip(fullScorePoints.size() - 2).collect(Collectors.toList());
            scorelast = new String[]{lastScore.get(0).getPointname(), lastScore.get(1).getPointname()};

            List<MsrSubjectPoint> firstRate = avgRatePoints.stream().limit(2).collect(Collectors.toList());
            rateFrist = new String[]{firstRate.get(0).getPointname(), firstRate.get(1).getPointname()};
            List<MsrSubjectPoint> lastRate = avgRatePoints.stream().skip(avgRatePoints.size() - 2).collect(Collectors.toList());
            rateLast = new String[]{lastRate.get(0).getPointname(), lastRate.get(1).getPointname()};

        }else if(fullScorePoints.size() <4 && avgRatePoints.size() >=2){
            //前后各取1个
            List<MsrSubjectPoint> firstScore = fullScorePoints.stream().limit(1).collect(Collectors.toList());
            scoreFirst = new String[]{firstScore.get(0).getPointname()};
            List<MsrSubjectPoint> lastScore = fullScorePoints.stream().skip(fullScorePoints.size() - 1).collect(Collectors.toList());
            scorelast = new String[]{lastScore.get(0).getPointname()};

            List<MsrSubjectPoint> firstRate = avgRatePoints.stream().limit(1).collect(Collectors.toList());
            rateFrist = new String[]{firstRate.get(0).getPointname()};
            List<MsrSubjectPoint> lastRate = avgRatePoints.stream().skip(avgRatePoints.size() - 1).collect(Collectors.toList());
            rateLast = new String[]{lastRate.get(0).getPointname()};

        }else{
            //仅前获取一个
            scoreFirst = new String[]{fullScorePoints.get(0).getPointname()};
            scorelast = null;
            rateFrist = new String[]{avgRatePoints.get(0).getPointname()};
            rateLast = null;
        }
        Map<String,Object> zdjgMap = new HashMap<>();
        zdjgMap.put("maxScore",scoreFirst);
        zdjgMap.put("minScore",scorelast);
        zdjgMap.put("maxRate",rateFrist);
        zdjgMap.put("minRate",rateLast);

        //知识点结构平均得分率表格表头
        List<Map<String,Object>> pointTitles = schoolPoints.stream().map(p -> { Map<String,Object> map = new HashMap<>();
            map.put("pointname", p.getPointname());
            map.put("pointid", p.getPointid());
            map.put("fullscore", p.getFullscore());
            return map;
        }).collect(Collectors.toList());

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

        List<Map<String,Object>> pointClassList = new ArrayList<>();
        //保存全体
        Map<String,Object> allMap = new HashMap<>();
        allMap.put("classid",0);
        allMap.put("classname","全体");
        allMap.put("points",schoolPoints);
        pointClassList.add(allMap);

        for(MsClass clazz : classList){
            List<MsrSubjectPoint> classPoints = points.stream().filter(p -> p.getClassid().equals(clazz.getClassid())).collect(Collectors.toList());

            Map<String,Object> classMap = new HashMap<>();
            classMap.put("classid",clazz.getClassid());
            classMap.put("classname",clazz.getClassname());
            classMap.put("points",classPoints);
            pointClassList.add(classMap);
        }

        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("schoolPoints",schoolPoints);//全体知识点数据
        rtnMap.put("zdjgMap",zdjgMap);//诊断结果
        rtnMap.put("pointTitles",pointTitles);//平均得分率表格表头
        rtnMap.put("pointClassList",pointClassList);//平均得分率数据
        return rtnMap;
    }

    //学科素养报告-单个知识点分析-知识点及知识点下试题
    public Map<Object,List<Map<String,Object>>> listXksyPointOneForPoints(ExamReportParams params){
        //获取所有知识点，把知识点按层级分组
        List<Map<String,Object>> points = msrSubjectPointMapper.listXksyPointsAll(params);
        return points.stream().collect(Collectors.groupingBy(t -> t.get("pointlevel")));
    }

    //学科素养报告-单个知识点分析-知识点详情
    public Map<String,Object> listXksyPointOneForPointDetail(ExamReportParams params){
        //获取知识点点对应的试题数据
        MsrSubjectPoint point = msrSubjectPointMapper.getXksyPointByPointid(params);
        params.setMptqids(point.getMptqids());
        //获取试题，试题详情图片暂未处理
        List<Map<String,Object>> questions = msrClassQuestionMapper.listQuestionsForXksyOnePoint(params);
        //结果图，把知识点情况取出
        Map<String,Object> pointMap = new HashMap<>();
        pointMap.put("spid",point.getSpid());
        pointMap.put("fullscore",point.getFullscore());
        pointMap.put("schoolavgscore",point.getAvgscore());
        pointMap.put("schoolavgrate",point.getAvgrate());
        pointMap.put("mfnum",point.getMfnum());
        pointMap.put("lfnum",point.getLfnum());
        questions.add(0,pointMap);

        //知识点与总分的相关性曲线
        List<Map<String,Object>> pointSections = msrSubjectPointSectionMapper.listXhsySections(params);
        //知识点考生分组分析
        List<Map<String,Object>> rankgroups = msrSubjectPointRankgroupMapper.listXksyPointOneRankgroup(params);
        //表格头部信息
        List<Object> groupRankTitles = rankgroups.stream().filter(r ->N_Utils.isEmpty(r.get("rsection"))).map(r -> r.get("rgroup")).distinct().collect(Collectors.toList());
        List<Object> rsections = rankgroups.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 = rankgroups.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 = rankgroups.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);
            }
        }
        //各班知识点对比
        List<Map<String,Object>> classPointList = msrSubjectPointMapper.listXksyPointsForAllClass(params);

        //返回试题基础信息
        Map<String,Object> rtnMap = new HashMap<>();
        rtnMap.put("questions",questions);//知识点下试题
        rtnMap.put("pointSections",pointSections);//知识点与总分的相关性
        rtnMap.put("groupRankTitles",groupRankTitles);//分组分数分布-表头
        rtnMap.put("groupRankList",groupRankList);//分组分数分布-数据
        rtnMap.put("classPointList",classPointList);//各班知识点对比
        return rtnMap;
    }

}
