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

import com.alibaba.fastjson.JSON;
import com.xhkjedu.smarking.mapper.report.reportclass.MsrClassActionMapper;
import com.xhkjedu.smarking.mapper.report.reportstu.MsrStudentActionMapper;
import com.xhkjedu.smarking.model.exam.MsClassStudent;
import com.xhkjedu.smarking.model.paper.MsPaper;
import com.xhkjedu.smarking.model.report.reportclass.MsrClassAction;
import com.xhkjedu.smarking.model.report.reportstu.MsrStudentAction;
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 ywx
 *Date 2025/1/3 11:39
 **/
@Service
public class ReportGenerateActionService {
    @Resource
    private MsrClassActionMapper msrClassActionMapper;
    @Resource
    private MsrStudentActionMapper msrStudentActionMapper;

    //违纪优秀分析
    public void saveAction(Integer examid, List<MsPaper> subjects, List<MsClassStudent> students, Integer raid) {
        String msids = subjects.stream().map(p -> p.getMsid().toString()).collect(Collectors.joining(","));
        String subjectids = subjects.stream().map(MsPaper::getSubjectid).collect(Collectors.joining("','"));
        Map<Integer, String> subjectIdMap = new HashMap<>();
        Map<String, String> subjectNameMap = new HashMap<>();
        for (MsPaper subject : subjects) {
            String subjectid = subject.getSubjectid();
            String subjectname = subject.getSubjectname();
            subjectIdMap.put(subject.getMsid(), subjectid);
            subjectNameMap.put(subjectid, subjectname);
        }
        Map<String, List<MsClassStudent>> collect = students.stream().collect(Collectors.groupingBy(s -> s.getSubjectid() + "_" + s.getStudentid()));
        Map<String, List<Integer>> sscMap = new HashMap<>();//学生科目班级map
        for (Map.Entry<String, List<MsClassStudent>> entry : collect.entrySet()) {
            sscMap.put(entry.getKey(), entry.getValue().stream().map(MsClassStudent::getClassid).distinct().collect(Collectors.toList()));
        }

        List<MsrClassAction> classActions = new ArrayList<>();
        List<MsrStudentAction> studentActions = new ArrayList<>();
        //违纪行为
        List<Map> bads = msrClassActionMapper.listBadActionByExamId(msids);
        for (Map bad : bads) {
            String subjectid = subjectIdMap.get((Integer) bad.get("msid"));
            bad.put("subjectid", subjectid);
        }
        int actiontype = 1;
        setActions(examid, classActions, studentActions, bads, actiontype, subjectNameMap, sscMap, raid);
        //优秀试卷
        List<Map> goods = msrClassActionMapper.listGoodActionByExamId(examid, subjectids);
        actiontype = 2;
        setActions(examid, classActions, studentActions, goods, actiontype, subjectNameMap, sscMap, raid);

        //违规试卷
        List<Map> badques = msrClassActionMapper.listBadQuesActionByExamId(examid, subjectids);
        actiontype = 3;
        setActions(examid, classActions, studentActions, badques, actiontype, subjectNameMap, sscMap, raid);

        if (N_Utils.isListNotEmpty(classActions)) msrClassActionMapper.insertList(classActions);
        if (N_Utils.isListNotEmpty(studentActions)) msrStudentActionMapper.insertList(studentActions);
    }

    //处理行为数据
    private void setActions(Integer examid, List<MsrClassAction> classActions, List<MsrStudentAction> studentActions
            , List<Map> actionList, Integer actiontype, Map<String, String> subjectNameMap
            , Map<String, List<Integer>> sscMap, Integer raid) {
        if (N_Utils.isListNotEmpty(actionList)) {
            List<Map> actions = new ArrayList<>();
            for (Map map : actionList) {
                String key = map.get("subjectid") + "_" + map.get("studentid");
                List<Integer> classids = sscMap.get(key);
                for (Integer classid : classids) {
                    Map ca = new HashMap(map);
                    ca.put("classid", classid);
                    actions.add(ca);
                }
            }
            //班级行为分析
            Map<Integer, List<Map>> ccollect = actions.stream().collect(Collectors.groupingBy(s -> (Integer) s.get("classid")
                    , LinkedHashMap::new, Collectors.toList()));
            for (Map.Entry<Integer, List<Map>> entry : ccollect.entrySet()) {
                List<Map> subjects = new ArrayList<>();
                Map<String, List<Map>> scollect = entry.getValue().stream().collect(Collectors.groupingBy(s -> s.get("subjectid").toString()
                        , LinkedHashMap::new, Collectors.toList()));
                for (Map.Entry<String, List<Map>> sentry : scollect.entrySet()) {
                    String subjectid = sentry.getKey();
                    int znum = (int) actions.stream().filter(s -> s.get("subjectid").equals(subjectid)).count();//总人数
                    List<Map> list = sentry.getValue();
                    Map subject = new HashMap();
                    subject.put("subjectid", subjectid);
                    subject.put("subjectname", subjectNameMap.get(subjectid));
                    int num = list.size();
                    subject.put("num", num);
                    subject.put("nrate", N_Utils.getIntegerDivideAndMulitiply(num, znum));
                    List<Map> students = new ArrayList<>();
                    for (Map s : list) {
                        Map stu = new HashMap();
                        Integer studentid = (Integer) s.get("studentid");
                        stu.put("studentid", studentid);
                        stu.put("num", s.get("num"));
                        stu.put("studentname", s.get("studentname"));
                        students.add(stu);
                    }
                    subject.put("students", students);
                    subjects.add(subject);
                }
                MsrClassAction ca = new MsrClassAction();
                ca.setClassid(entry.getKey());
                ca.setExamid(examid);
                ca.setRaid(raid);
                ca.setActiontype(actiontype);
                String subjectjson = JSON.toJSONString(subjects);
                ca.setSubjectjson(subjectjson);
                classActions.add(ca);
            }

            //学生行为分析
            Map<Integer, List<Map>> collect = actions.stream().collect(Collectors.groupingBy(s -> (Integer) s.get("studentid")
                    , LinkedHashMap::new, Collectors.toList()));
            for (Map.Entry<Integer, List<Map>> entry : collect.entrySet()) {
                List<Map> list = entry.getValue();
                Map sq = list.get(0);
                MsrStudentAction sa = new MsrStudentAction();
                sa.setClassid((Integer) sq.get("classid"));
                sa.setStudentid(entry.getKey());
                sa.setExamid(examid);
                sa.setRaid(raid);
                sa.setActiontype(actiontype);
                for (Map s : list) {
                    s.remove("classid");
                    s.remove("studentid");
                    s.remove("studentname");
                    s.remove("msid");
                    s.put("subjectname", subjectNameMap.get(s.get("subjectid").toString()));
                }
                String subjectjson = JSON.toJSONString(list);
                sa.setSubjectjson(subjectjson);
                studentActions.add(sa);
            }
        }
    }

}
