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

import com.xhkjedu.smarking.mapper.report.reportother.MsrReportparamMapper;
import com.xhkjedu.smarking.mapper.wholeparam.MspReportparamMapper;
import com.xhkjedu.smarking.model.report.reportother.MsrReportparam;
import com.xhkjedu.smarking.model.wholeparam.MspReportparam;
import com.xhkjedu.smarking.vo.report.reportother.ReportParams;
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;
import java.util.stream.Collectors;

/**
 * @Description：考试参数
 * @Author: WN
 * @Date: 2024/12/9 14:13:17
 **/
@Service
public class MsrReportparamService {
    @Resource
    private MsrReportparamMapper msrReportparamMapper;
    @Resource
    private MspReportparamMapper mspReportparamMapper;


    /*
     * @Description 考试参数--保存
     * @Date 2024/12/9 14:55:30
     * @Author WN
     * @Param [reportParams]
     * @Return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void saveReportparam(ReportParams reportParams) {
        List<MsrReportparam> originalParams = msrReportparamMapper.listReportparamByRpbelong(reportParams.getExamid(),reportParams.getRpbelong(),reportParams.getReportcode());

        Integer timestamp = N_Utils.getSecondTimestamp();
        List<MsrReportparam> params = reportParams.getParams();
        List<Integer> delids = new ArrayList<>();
        for(MsrReportparam param : params) {
            param.setRpbelong(reportParams.getRpbelong());
            param.setExamid(reportParams.getExamid());
            param.setHandleid(reportParams.getHandleid());
            param.setHandletime(timestamp);
            if(param.getRptype() == null) param.setRptype(0);
            if(param.getGradenum() == null) param.setGradenum(0);

            //如果同类型同科目的数据已存在，删除原数据
            if(originalParams!= null && originalParams.size() > 0) {
                MsrReportparam oldParam;
                if(N_Utils.isNotEmpty(param.getSubjectid())){
                    oldParam = originalParams.stream().filter(p -> param.getRptype().equals(p.getRptype()) && param.getSubjectid().equals(p.getSubjectid())).findFirst().orElse(null);
                }else{
                    oldParam = originalParams.stream().filter(p -> param.getRptype().equals(p.getRptype())).findFirst().orElse(null);
                }
                if(oldParam!= null) {
                    delids.add(oldParam.getRpid());
                }
            }
        }
        if(delids.size() > 0) {
            msrReportparamMapper.deleteBatchReportparam(delids);
        }

        msrReportparamMapper.insertList(params);
    }

    /*
     * @Description 考试参数--获取
     * @Date 2024/12/9 14:56:14
     * @Author WN
     * @Param [rpbelong, examid]
     * @Return java.util.List<com.xhkjedu.smarking.model.report.reportother.MsrReportparam>
     **/
    public List<MsrReportparam> listReportparamByRpbelong(ReportParams reportParams) {
        return msrReportparamMapper.listReportparamByRpbelong(reportParams.getExamid(),reportParams.getRpbelong(),reportParams.getReportcode());
    }

    /*
     * @Description 初始化参数
     * @Date 2024/12/9 15:06:42
     * @Author WN
     * @Param [schoolid, gradeid, examid, handleid]
     * @Return void
     **/
    public List<MsrReportparam> initReportparam(Integer schoolid,Integer gradeid,Integer examid,Integer handleid) {
        //初始化时判断参数是否已经存在
        List<MsrReportparam> examParams = msrReportparamMapper.listReportparamByRpbelong(examid,null,null);
        if(examParams!= null && examParams.size() > 0){
            //说明考试已经有参数存在
            return examParams;
        }

        List<MspReportparam> mspParams = mspReportparamMapper.listAllByGrade(schoolid,gradeid);
        List<MsrReportparam> saveParams = new ArrayList<>();
        Integer timestamp = N_Utils.getSecondTimestamp();
        //按类型进行分组
        Map<String,List<MspReportparam>> mspParamMap = mspParams.stream().collect(Collectors.groupingBy(MspReportparam::getRpbelong));
        for (Map.Entry<String, List<MspReportparam>> entry : mspParamMap.entrySet()) {
            List<MspReportparam> params = entry.getValue();
            //获取对应年级的
            List<MspReportparam> gradeParams = params.stream().filter(p -> p.getGradeid().equals(gradeid)).collect(Collectors.toList());
            if(gradeParams == null || gradeParams.size() == 0){
                gradeParams = params;
            }

            //说明对应年级下有设置
            for(MspReportparam param : gradeParams){
                MsrReportparam rp = new MsrReportparam();
                rp.setExamid(examid);
                rp.setRpbelong(entry.getKey());
                rp.setRptype(param.getRptype());
                rp.setGradenum(param.getGradenum());
                rp.setSubjectid(param.getSubjectid());
                rp.setSzjson(param.getSzjson());
                rp.setHandletime(timestamp);
                rp.setHandleid(handleid);
                saveParams.add(rp);
            }
        }
        msrReportparamMapper.insertList(saveParams);
        return saveParams;
    }

    //获取报告参数
    public List<MsrReportparam> getMsrReportParams(Integer examid, String rpbelong, String reportcode) {
        List<MsrReportparam> list = msrReportparamMapper.listReportparamByRpbelong(examid, rpbelong, reportcode);
        if (N_Utils.isListEmpty(list) && !"all".equals(reportcode)) {
            list = msrReportparamMapper.listReportparamByRpbelong(examid, rpbelong, "all");
        }
        return list;
    }

    //获取单科报告参数
    public MsrReportparam getOneReportParams(Integer examid, String rpbelong, String reportcode, String subjectid) {
        MsrReportparam reportparam = msrReportparamMapper.getReportparamByRpbelongSubject(examid, rpbelong, reportcode, subjectid);
        if (reportparam == null && !"all".equals(reportcode)) {
            reportparam = msrReportparamMapper.getReportparamByRpbelongSubject(examid, rpbelong, "all", subjectid);
        }
        return reportparam;
    }
}
