package com.xhkjedu.sstudy.service.paperreport;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sstudy.mapper.paperreport.PaperStudentReportMapper;
import com.xhkjedu.sstudy.utils.StudyUtil;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @author ywx
 * @classname PaperStudentReportService
 * @description 学生作业报告
 * @date 2023/6/8 14:54
 **/
@Service
public class PaperStudentReportService {
    @Resource
    private PaperStudentReportMapper paperStudentReportMapper;

    /**
     * @Description 作业概况
     * @Param [paperid, classid, studentid]
     * @Author ywx
     * @Date 2023/6/8 16:13
     * @return java.util.Map
     **/
    public Map getPaperOverview(Integer paperid, Integer classid, Integer studentid) {
        Map map = paperStudentReportMapper.getPaperStu(paperid, classid, studentid);// 作业概况-学生概况
        List<Map> questions = paperStudentReportMapper.listPaperClassQuestionRate(paperid, classid, studentid);
        map.put("questions", questions);// 作业概况-试题错误率
        List<Map> points = paperStudentReportMapper.listPaperClassPointRate(paperid, classid, studentid);
        map.put("points", points);// 作业概况-知识点错误率
        return map;
    }

    /**
     * @return java.util.Map
     * @Description 作业详情
     * @Param [paperid, classid]
     * @Author ywx
     * @Date 2023/6/8 16:26
     **/
    public Map getPaperDetail(Integer paperid, Integer classid, Integer studentid) {
        Map paper = paperStudentReportMapper.getPaperAnalyze(paperid, classid, studentid);
        Integer papertype = (Integer) paper.get("papertype");
        Integer showanswer = N_Utils.str2Int(paper.get("showanswer").toString());
        Integer hasreport = N_Utils.str2Int(paper.get("hasreport").toString());
        List<Map> list;
        if (papertype == 1 || papertype == 3) {// 题库、靶向作业
            list = paperStudentReportMapper.listTkSt(paperid, classid, studentid, showanswer, hasreport);
            List<Map> questions = new ArrayList<>();// 题库作业子题
            List<String> questionids = new ArrayList<>();
            list.forEach(t -> {
                List<Map> questionList = (List<Map>) t.get("questions");
                questionids.addAll(questionList.stream().filter(q -> q.get("qlevel").equals(2))
                                               .map(q -> q.get("questionid").toString()).collect(Collectors.toList()));
            });

            if (N_Utils.isListNotEmpty(questionids)) questions = paperStudentReportMapper.listZtQuestion(questionids, (Integer) paper.get("psid"), showanswer, hasreport);
            Object pointjson = paper.get("pointjson");
            List<Map> points = JSON.parseArray(pointjson.toString(), Map.class);
            for (Map t : list) {
                for (Map q : (List<Map>) t.get("questions")) {
                    String questionid = q.get("questionid").toString();
                    if (q.get("qlevel").equals(2)) {
                        List<Map> sonques = questions.stream().filter(tq -> questionid.equals(tq.get("questionpid"))).collect(Collectors.toList());
                        q.put("sonques", sonques);
                        if (showanswer == 1 || hasreport == 1) {
                            Integer good = 0;
                            long count = sonques.stream().filter(s -> s.get("good").equals(1)).count();
                            if (count != 0) {
                                good = 1;
                            }
                            q.put("good", good);
                        }
                    }
                    if (showanswer == 1 || hasreport == 1) {
                        if(N_Utils.isEmpty(q.get("classavgscore"))){
                            q.put("classavgscore",0);
                        }
                        if(N_Utils.isEmpty(q.get("classscorerate"))){
                            q.put("classscorerate",0);
                        }
                        q.put("ratetype", StudyUtil.getScoreRateType(StudyUtil.objToDouble(q.get("scorerate"))));
                    }
                    if (showanswer == 1) {
                        q.put("pointnames", getQuestionPoint(points, questionid));// 试题知识点
                    }
                }
            }
        } else {// 附件作业
            String papername = paper.get("papername").toString();
            list = paperStudentReportMapper.listFjSt(paperid, classid, studentid, showanswer, hasreport);
            for (Map t : list) {
                for (Map q : (List<Map>) t.get("questions")) {
                    q.put("qstem", papername + "-第" + q.get("qn") + "题");
                    if (showanswer == 1 || hasreport == 1) {
                        if (N_Utils.isEmpty(q.get("classavgscore"))) {
                            q.put("classavgscore", 0);
                        }
                        if (N_Utils.isEmpty(q.get("classscorerate"))) {
                            q.put("classscorerate", 0);
                        }
                        q.put("ratetype", StudyUtil.getScoreRateType(StudyUtil.objToDouble(q.get("scorerate"))));
                    }
                }
            }
        }
        paper.remove("papername");
        paper.remove("pointjson");
        paper.put("types", list);
        return paper;
    }

    // 试题知识点
    private List<String> getQuestionPoint(List<Map> points, String questionid) {
        List<String> pointnames = new ArrayList<>();
        for (Map p : points) {
            String ques = p.get("ques").toString();
            String pointname = p.get("pointname").toString();
            if (ques.contains("\"" + questionid + "\"")) pointnames.add(pointname);
        }
        return pointnames;
    }

    /**
     * @Description 优秀作答
     * @Param [paperid, classid, papertype]
     * @Author ywx
     * @Date 2023/6/9 16:25
     * @return java.util.List<java.util.Map>
     **/
    public List<Map> listGoodAnswer(Integer paperid, Integer classid, Integer papertype) {
        return paperStudentReportMapper.listGoodAnswer(paperid, classid, papertype);
    }

    /**
     * @Description 作业基本信息
     * @Param [paperid, classid, studentid]
     * @Author ywx
     * @Date 2023/6/9 16:25
     * @return java.util.Map
     **/
    public Map getPaper(Integer paperid, Integer classid, Integer studentid) {
        return paperStudentReportMapper.getPaper(paperid, classid, studentid);
    }
}
