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

import com.xhkjedu.smarking.mapper.exam.MsClassMapper;
import com.xhkjedu.smarking.mapper.exam.MsClassStudentMapper;
import com.xhkjedu.smarking.mapper.exam.MsExamMapper;
import com.xhkjedu.smarking.mapper.exam.MsSubjectMapper;
import com.xhkjedu.smarking.mapper.report.reportother.MsrAnalyzeMapper;
import com.xhkjedu.smarking.mapper.report.reportother.MsrExamMapper;
import com.xhkjedu.smarking.mapper.stupaper.MsPaperStudentMapper;
import com.xhkjedu.smarking.model.exam.MsClassStudent;
import com.xhkjedu.smarking.model.exam.MsExam;
import com.xhkjedu.smarking.model.paper.MsPaper;
import com.xhkjedu.smarking.model.report.reportother.MsrAnalyze;
import com.xhkjedu.smarking.model.report.reportother.MsrExam;
import com.xhkjedu.smarking.model.report.reportstu.MsrStudent;
import com.xhkjedu.smarking.model.report.reportsubject.MsrSubject;
import com.xhkjedu.smarking.service.report.generate.*;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description 阅卷报告-总体分析表 服务实现类
 * @Author auto
 * @Date 2024-12-09
 */
@Service
public class MsrExamService {
    @Resource
    private MsrExamMapper msrExamMapper;
    @Resource
    private MsExamMapper examMapper;
    @Resource
    private MsSubjectMapper subjectMapper;
    @Resource
    private MsClassMapper classMapper;
    @Resource
    private MsClassStudentMapper classStudentMapper;
    @Resource
    private MsPaperStudentMapper paperStudentMapper;
    @Resource
    private MsrAnalyzeMapper msrAnalyzeMapper;

    @Resource
    private ReportGenerateSubjectService reportGenerateSubjectService;
    @Resource
    private ReportGenerateStudentService reportGenerateStudentService;
    @Resource
    private ReportGenerateClassService reportGenerateClassService;
    @Resource
    private ReportGenerateActionService reportGenerateActionService;
    @Resource
    private ReportGenerateQuestionService reportGenerateQuestionService;


    /**
     * @Description 生成报告
     * @Date 2024/12/9 15:43
     * @Author YWX
     * @Param [examid]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void generateExamReport(Integer examid,Integer createid) {
        MsExam exam = examMapper.selectByPrimaryKey(examid);
        Integer exammode = exam.getExammode();
        List<MsPaper> subjects = subjectMapper.listReportSubject(examid, exammode);//考试报告科目列表
        if (N_Utils.isListEmpty(subjects)) return;
        List<MsClassStudent> students = classStudentMapper.listStudentByExamId(examid,null);//获取考试关联学生
        List<Integer> mpids = new ArrayList<>();
        for (MsPaper subject : subjects) {
            Integer mpid = subject.getMpid();
            mpids.add(mpid);
        }
        List<MsrStudent> paperStudents = paperStudentMapper.listPaperStudentByExamId(examid, mpids);//试学生成绩列表
        for (MsrStudent student : paperStudents) {
            student.setScorerate(N_Utils.getDoubleDivideAndMulitiply(student.getStuscore(), student.getFullscore()));
        }

        //科目分析
        Map<String, MsrSubject> subjectMap = reportGenerateSubjectService.saveSubject(examid, paperStudents, subjects, mpids);

        //学生成绩分析
        List<MsrStudent> msrStudents = reportGenerateStudentService.saveStudent(examid, subjects, paperStudents, students, subjectMap);
        //删除已有默认分析
        msrAnalyzeMapper.deleteReportByExamid(examid);
        //保存默认分析
        MsrAnalyze msrAnalyze = new MsrAnalyze();
        msrAnalyze.setExamid(examid);
        msrAnalyze.setAname("默认分析");
        msrAnalyze.setAtype(0);
        msrAnalyze.setAstate(0);
        msrAnalyze.setCreateid(createid);
        msrAnalyze.setCreatetime(N_Utils.getSecondTimestamp());
        msrAnalyzeMapper.insertUseGeneratedKeys(msrAnalyze);
        int raid = msrAnalyze.getRaid();

        //总体分析
        saveExam(examid, subjectMap.get("zf"), subjects.size(), raid);

        reportGenerateSubjectService.saveSubjectClass(examid, msrStudents, raid);//科目班级分数分布区间分析

        reportGenerateClassService.saveClassTop(examid, msrStudents, raid);//班级学科前60分析
        reportGenerateClassService.saveClassSubjectGrade(examid, "zh", msrStudents, raid);//综合报告-班级学科四率等级分析
        reportGenerateClassService.saveClassSubjectGrade(examid, "xj", msrStudents, raid);//校级报告-班级学科四率等级分析
        reportGenerateClassService.saveClassSubject(examid, msrStudents, raid);//班级学科分析
        reportGenerateClassService.saveClassRankGroup(examid, msrStudents, raid);//学科报告-班级学科名次分组分析
        reportGenerateClassService.saveClassRankRate(examid, msrStudents, raid);//综合报表-名次和比例分析
        reportGenerateClassService.saveClassSection(examid, 1, "zh", msrStudents, raid);//综合报表-分段统计
        reportGenerateClassService.saveClassSection(examid, 2, "zh", msrStudents, raid);//综合报表-累计统计
        reportGenerateClassService.saveClassSection(examid, 1, "xj", msrStudents, raid);//校级报告-分段统计
        reportGenerateClassService.saveClassSection(examid, 1, "xk", msrStudents, raid);//学科报告-分段统计
        reportGenerateClassService.saveClassSection(examid, 1, "bj", msrStudents, raid);//班级报告-分段统计
        reportGenerateClassService.saveClassSection(examid, 1, "jxzl", msrStudents, raid);//教学质量-分段统计
        reportGenerateClassService.saveClassNearLine(examid, msrStudents,raid);//分档上线统计
        reportGenerateClassService.saveClassGoodBad(examid, "all", msrStudents, raid);//重点学生-优劣名次统计

        reportGenerateActionService.saveAction(examid, subjects, students, raid);//行为分析

        reportGenerateQuestionService.generateQuestion(examid,raid,subjects,students);

        examMapper.updateExamState(examid, 3);
        msrAnalyzeMapper.updateState(examid, 1);
    }

    //总体分析
    public void saveExam(Integer examid, MsrSubject msrSubject, Integer subjectnum,Integer raid) {
        MsrExam msrExam = new MsrExam();
        msrExam.setRaid(raid);
        msrExam.setExamid(examid);
        msrExam.setClassnum(classMapper.getClassNumByExamId(examid));
        msrExam.setTotalnum(msrSubject.getTotalnum());
        msrExam.setStunum(msrSubject.getStunum());
        msrExam.setMissnum(msrSubject.getMissnum());
        msrExam.setSubjectnum(subjectnum);
        msrExam.setFullscore(msrSubject.getFullscore());
        msrExam.setMaxscore(msrSubject.getMaxscore());
        msrExam.setMinscore(msrSubject.getMinscore());
        msrExam.setAvgscore(msrSubject.getAvgscore());
        msrExam.setBzc(msrSubject.getBzc());
        msrExam.setCreatetime(N_Utils.getSecondTimestamp());
        msrExamMapper.insert(msrExam);
    }

}
