package org.jeecg.modules.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.web.dto.ArtRecommendMajorBaseDTO;
import org.jeecg.modules.web.dto.RecommendMajorDTO;
import org.jeecg.modules.web.vo.QueryRecommendMajorVO;
import org.jeecg.modules.yx.constant.YxConstant;
import org.jeecg.modules.yx.entity.*;
import org.jeecg.modules.yx.mapper.YxCalculationMajorMapper;
import org.jeecg.modules.yx.mapper.YxSchoolMajorMapper;
import org.jeecg.modules.yx.mapper.YxSchoolMapper;
import org.jeecg.modules.yx.service.*;
import org.jeecg.modules.yx.util.ScoreUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 用户可报专业折合关联表
 * @Author: jeecg-boot
 * @Date: 2024-03-15
 * @Version: V1.0
 */
@Service
public class YxCalculationMajorServiceImpl extends ServiceImpl<YxCalculationMajorMapper, YxCalculationMajor> implements IYxCalculationMajorService {
    @Resource
    private IYxHistoryScoreControlLineService yxHistoryScoreControlLineService;
    @Resource
    private IYxUserScoreService yxUserScoreService;
    @Resource
    private MusicService musicService;
    @Resource
    private BiaoyanService biaoyanService;
    @Resource
    private WuDaoService wuDaoService;
    @Resource
    private BoYinService boYinService;
    @Resource
    private IYxHistoryMajorEnrollService yxHistoryMajorEnrollService;
    @Resource
    private YxSchoolMajorMapper yxSchoolMajorMapper;
    @Resource
    private YxSchoolMapper yxSchoolMapper;

    @Override
    public List<RecommendMajorDTO> calculationMajor(YxUserScore userScore) {
        // 最终返回的对象
        List<RecommendMajorDTO> recommendMajorDTOList = new ArrayList<>();
        if (userScore != null) {
            // 查询专业列表参数
            QueryRecommendMajorVO queryRecommendMajorVO = new QueryRecommendMajorVO();
            queryRecommendMajorVO.setProfessionalCategory(userScore.getProfessionalCategory()); // 专业类别
            queryRecommendMajorVO.setCognitioPolyclinic(userScore.getCognitioPolyclinic()); // 文理分科
            if (("音乐类".equals(userScore.getProfessionalCategory()) ||
                    "表演类".equals(userScore.getProfessionalCategory())) && StringUtils.isNotBlank(userScore.getProfessionalCategoryChildren())) {
                queryRecommendMajorVO.setPCategoryChildrenList(Arrays.asList(userScore.getProfessionalCategoryChildren().split(",")));
            }
            long startTime = new Date().getTime();
            recommendMajorDTOList = yxSchoolMajorMapper.recommendMajorList(queryRecommendMajorVO);  // 获取到当前专业，文/理科的数据
            //将历史录取信息合并到专业信息里
            if (CollectionUtils.isNotEmpty(recommendMajorDTOList)) {
                //
                yxHistoryMajorEnrollService.recommendMajorDTOListSetHistoryInfo(recommendMajorDTOList, ScoreUtil.getCalOldYears());
                /*if (!"美术与设计类".equals(userScore.getProfessionalCategory())) {}else{
                    yxHistoryMajorEnrollService.recommendMajorDTOListSetHistoryInfo(recommendMajorDTOList, ScoreUtil.getCalOldYears());
                }*/
                this.checkEnrollProbability(recommendMajorDTOList, userScore);
            }
            long endTime = new Date().getTime();
            log.warn("测算专业录取率-耗费时长:" + (endTime - startTime));
            //保存结果到用户的专业结果表
            YxCalculationMajor yxCalculationMajor = null;
            List<YxCalculationMajor> calculationMajorList = new ArrayList<>();
            StringBuilder otherScoreLimitation = null;
            for (RecommendMajorDTO recommendMajorDTO : recommendMajorDTOList) {
                otherScoreLimitation = new StringBuilder();
                yxCalculationMajor = new YxCalculationMajor();
                BeanUtils.copyProperties(recommendMajorDTO, yxCalculationMajor);
                yxCalculationMajor.setTuition(recommendMajorDTO.getTuition());
                yxCalculationMajor.setDetail(recommendMajorDTO.getMajorDetail());
                yxCalculationMajor.setScoreId(userScore.getId());
                yxCalculationMajor.setStudentOldConvertedScore(recommendMajorDTO.getStudentScore());
                yxCalculationMajor.setStudentConvertedScore(recommendMajorDTO.getStudentConvertedScore());
                yxCalculationMajor.setPrivateStudentConvertedScore(recommendMajorDTO.getPrivateStudentScore());
                //其他分数要求
                if(true){
                    //语文成绩
                    if (recommendMajorDTO.getChineseScoreLimitation()!=null && recommendMajorDTO.getChineseScoreLimitation().compareTo(YxConstant.bigDecimal0)>0) {
                        otherScoreLimitation.append("语文成绩不低于").append(recommendMajorDTO.getChineseScoreLimitation().doubleValue()).append("分,");
                    }
                    //外语成绩
                    if (recommendMajorDTO.getEnglishScoreLimitation()!=null && recommendMajorDTO.getEnglishScoreLimitation().compareTo(YxConstant.bigDecimal0)>0) {
                        otherScoreLimitation.append("外语成绩不低于").append(recommendMajorDTO.getEnglishScoreLimitation().doubleValue()).append("分,");
                    }
                    //文化成绩
                    if (recommendMajorDTO.getCulturalScoreLimitation()!=null && recommendMajorDTO.getCulturalScoreLimitation().compareTo(YxConstant.bigDecimal0)>0) {
                        otherScoreLimitation.append("文化成绩不低于").append(recommendMajorDTO.getCulturalScoreLimitation().doubleValue()).append("分,");
                    }
                    //专业成绩
                    if (recommendMajorDTO.getProfessionalScoreLimitation()!=null && recommendMajorDTO.getProfessionalScoreLimitation().compareTo(YxConstant.bigDecimal0)>0) {
                        otherScoreLimitation.append("专业成绩不低于").append(recommendMajorDTO.getProfessionalScoreLimitation().doubleValue()).append("分,");
                    }
                    if (otherScoreLimitation.length()>0) {
                        //recommendMajorDTO.setOtherScoreLimitation(otherScoreLimitation.substring(0,otherScoreLimitation.length()-2));
                        yxCalculationMajor.setOtherScoreLimitation(otherScoreLimitation.substring(0,otherScoreLimitation.length()-2));
                    }
                }
                calculationMajorList.add(yxCalculationMajor);
            }
            long saveStartTime = new Date().getTime();
            baseMapper.insertBatchNew(userScore.getCalculationTableName(),userScore.getId(),calculationMajorList);
            //super.saveBatch(calculationMajorList,500);
            long saveEndTime = new Date().getTime();
            log.warn("插入数据耗费时长:" + (saveEndTime - saveStartTime));
        }
        return recommendMajorDTOList;
    }

    @Override
    public ArtRecommendMajorBaseDTO recommendMajorPage(QueryRecommendMajorVO queryRecommendMajorVO) {
        Integer pageNum = queryRecommendMajorVO.getPageNum();
        Integer pageSize = queryRecommendMajorVO.getPageSize();
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        //获取当前分数
        YxUserScore activeCurrentUserScore = yxUserScoreService.getActiveCurrentUserScore();
        queryRecommendMajorVO.setScoreId(activeCurrentUserScore.getId());
        //文理分科
        queryRecommendMajorVO.setCognitioPolyclinic(activeCurrentUserScore.getCognitioPolyclinic());
        //专业类别
        queryRecommendMajorVO.setProfessionalCategory(activeCurrentUserScore.getProfessionalCategory());

        if (StringUtils.isNotBlank(queryRecommendMajorVO.getBatch())) {
            queryRecommendMajorVO.setBatch(queryRecommendMajorVO.getBatch().replace("批",""));
        }

        //有子级专业
        if (StringUtils.isNotBlank(activeCurrentUserScore.getProfessionalCategoryChildren()) && !"高职高专".equals(queryRecommendMajorVO.getBatch())) {
            queryRecommendMajorVO.setPCategoryChildrenList(new ArrayList<>(Arrays.asList(activeCurrentUserScore.getProfessionalCategoryChildren().split(","))));
            if("表演类".equals(activeCurrentUserScore.getProfessionalCategory())){
                queryRecommendMajorVO.getPCategoryChildrenList().add("兼报");
            }
        }
        Map<String, YxHistoryScoreControlLine> historyScoreControlLineMap = null;
        if ("音乐类".equals(activeCurrentUserScore.getProfessionalCategory())||
                "表演类".equals(activeCurrentUserScore.getProfessionalCategory())) {
            historyScoreControlLineMap = new LinkedHashMap<>();
            LambdaQueryWrapper<YxHistoryScoreControlLine> yhsclWrapper = new LambdaQueryWrapper<>();
            yhsclWrapper.like(YxHistoryScoreControlLine::getProfessionalCategory, activeCurrentUserScore.getProfessionalCategory());
            List<YxHistoryScoreControlLine> yhsclList = yxHistoryScoreControlLineService.list(yhsclWrapper);
            String key = null;
            for (YxHistoryScoreControlLine yhscl : yhsclList) {
                key = yhscl.getYear()+"_" +yhscl.getCategory()+"-"+ yhscl.getBatch()+"-"+yhscl.getProfessionalCategory();
                historyScoreControlLineMap.put(key,yhscl);
            }
        }else{
            historyScoreControlLineMap = yxHistoryScoreControlLineService.mapsBatchByProfessionalCategoryOfYear(YxConstant.nowYear, activeCurrentUserScore.getProfessionalCategory(), activeCurrentUserScore.getCognitioPolyclinic());
        }


        //标签类别
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getTagsStrs())) {
            queryRecommendMajorVO.setTagsList(Arrays.asList(queryRecommendMajorVO.getTagsStrs().split(",")));
        }
        //院校类型
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getSchoolNatureStrs())) {
            queryRecommendMajorVO.setSchoolNatureList(Arrays.asList(queryRecommendMajorVO.getSchoolNatureStrs().split(",")));
        }
        //院校地区
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getProvince())) {
            queryRecommendMajorVO.setAddressList(Arrays.asList(queryRecommendMajorVO.getProvince().split(",")));
        }
        //科研教学
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getKyjxStrs())) {
            queryRecommendMajorVO.setKyjxList(Arrays.asList(queryRecommendMajorVO.getKyjxStrs().split(",")));
        }
        //录取方式
        String rulesEnrollProbability = queryRecommendMajorVO.getRulesEnrollProbability();
        if (StringUtils.isNotBlank(rulesEnrollProbability)) {
            queryRecommendMajorVO.setRulesEnrollProbabilityList(Arrays.asList(rulesEnrollProbability.split(",")));
        }

        //获取推荐志愿信息service
        List<RecommendMajorDTO> recommendMajorList = baseMapper.recommendMajorList(activeCurrentUserScore.getCalculationTableName(),queryRecommendMajorVO);
        ArtRecommendMajorBaseDTO artRecommendMajorBaseDTO = this.checkLevelNum(recommendMajorList);
        //当前选中的录取率页面
        String paneName = queryRecommendMajorVO.getPaneName();
        //判断 paneName
        if (StringUtils.isNotBlank(paneName) && !paneName.equals("全部")) {
            BigDecimal bigDecimal93 = new BigDecimal("93");
            BigDecimal bigDecimal92 = new BigDecimal("92");
            BigDecimal bigDecimal74 = new BigDecimal("74");
            BigDecimal bigDecimal73 = new BigDecimal("73");
            BigDecimal bigDecimal60 = new BigDecimal("60");
            switch (paneName) {
                case "可保底":
                    recommendMajorList = recommendMajorList.stream().filter(r -> r.getEnrollProbability().compareTo(bigDecimal93) >= 0).collect(Collectors.toList());
                    break;
                case "较稳妥":
                    recommendMajorList = recommendMajorList.stream().filter(r -> r.getEnrollProbability().compareTo(bigDecimal92) < 0 && r.getEnrollProbability().compareTo(bigDecimal74) >= 0).collect(Collectors.toList());
                    break;
                case "可冲击":
                    recommendMajorList = recommendMajorList.stream().filter(r -> r.getEnrollProbability().compareTo(bigDecimal73) < 0 && r.getEnrollProbability().compareTo(bigDecimal60) >= 0).collect(Collectors.toList());
                    break;
                case "难录取":
                    recommendMajorList = recommendMajorList.stream().filter(r -> r.getEnrollProbability().compareTo(bigDecimal60) < 0).collect(Collectors.toList());
                    break;
            }
        }
        //插入 院校标签
        this.setSchoolTagsList(recommendMajorList);
        List<RecommendMajorDTO> recommendMajorDTOList = recommendMajorList.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize)
                .collect(Collectors.toList());

        YxHistoryScoreControlLine yxHistoryScoreControlLine = null;
        BigDecimal bigDecimal = null;
        String key = null;
        for (RecommendMajorDTO recommendMajorDTO : recommendMajorDTOList) {
            key = YxConstant.nowYear + "_" + recommendMajorDTO.getCategory();

            if("高职高专".equals(recommendMajorDTO.getBatch())){
                key = key + "-" + recommendMajorDTO.getBatch();
            }else{
                key = key + "-本科";
            }
            // 判断是 音乐教育还是音乐表演 器乐/声乐
            if ("音乐类".equals(recommendMajorDTO.getMajorType())) {
                key =  key +"-音乐类-"+ recommendMajorDTO.getMajorTypeChild();
                if ("音乐表演".equals(recommendMajorDTO.getMajorTypeChild())) {
                    key += recommendMajorDTO.getMainSubjects();
                }
            }else if ("表演类".equals(recommendMajorDTO.getMajorType())) {
                key =  key +"-表演类-"+ recommendMajorDTO.getMajorTypeChild();
            }else{
                key = recommendMajorDTO.getBatch();
            }
            yxHistoryScoreControlLine = historyScoreControlLineMap.get(key);
            if (yxHistoryScoreControlLine!=null) {
                if (recommendMajorDTO.getStudentScore()!=null) {
                    bigDecimal = ScoreUtil.covertIntoControlLine(recommendMajorDTO.getRulesEnrollProbability(),
                            yxHistoryScoreControlLine.getCulturalScore(),
                            yxHistoryScoreControlLine.getSpecialScore(),
                            recommendMajorDTO.getFirstLevelDiscipline(), recommendMajorDTO.getProbabilityOperator());
                    recommendMajorDTO.setScoreLineDifference(recommendMajorDTO.getStudentScore().subtract(bigDecimal));
                }
            }
        }
        //包装历年信息
        yxHistoryMajorEnrollService.recommendMajorDTOListSetHistoryInfo(recommendMajorDTOList, ScoreUtil.getShowOldYears());
        //组装返回对象
        int total = recommendMajorList.size();
        IPage<RecommendMajorDTO> pageList = new Page<>();
        pageList.setTotal(total);//总记录数
        pageList.setCurrent(pageNum);
        pageList.setSize(pageSize);
        convertRecommendMajorListToNull(recommendMajorDTOList);
        pageList.setRecords(recommendMajorDTOList);
        artRecommendMajorBaseDTO.setPageList(pageList);
        return artRecommendMajorBaseDTO;
    }

    @Override
    public ArtRecommendMajorBaseDTO schoolOtherMajor(QueryRecommendMajorVO queryRecommendMajorVO) {
        //获取当前分数
        YxUserScore activeCurrentUserScore = null;
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getScoreId())) {
            //是根据成绩单来的
            activeCurrentUserScore = yxUserScoreService.findById(queryRecommendMajorVO.getScoreId());
        } else {
            //直接获取当前使用中的成绩单
            activeCurrentUserScore = yxUserScoreService.getActiveCurrentUserScore();
        }
        queryRecommendMajorVO.setScoreId(activeCurrentUserScore.getId());
        //文理分科
        queryRecommendMajorVO.setCognitioPolyclinic(activeCurrentUserScore.getCognitioPolyclinic());
        //专业类别
        queryRecommendMajorVO.setProfessionalCategory(activeCurrentUserScore.getProfessionalCategory());
        queryRecommendMajorVO.setScoreId(activeCurrentUserScore.getId());
        //文理分科
        queryRecommendMajorVO.setCognitioPolyclinic(activeCurrentUserScore.getCognitioPolyclinic());
        //专业类别
        queryRecommendMajorVO.setProfessionalCategory(activeCurrentUserScore.getProfessionalCategory());
        //有子级专业
        if (StringUtils.isNotBlank(activeCurrentUserScore.getProfessionalCategoryChildren()) && !"高职高专".equals(queryRecommendMajorVO.getBatch())) {
            queryRecommendMajorVO.setPCategoryChildrenList(new ArrayList<>(Arrays.asList(activeCurrentUserScore.getProfessionalCategoryChildren().split(","))));
            if("表演类".equals(activeCurrentUserScore.getProfessionalCategory())){
                queryRecommendMajorVO.getPCategoryChildrenList().add("兼报");
            }
        }
        //获取推荐志愿信息service
        List<RecommendMajorDTO> recommendMajorList = baseMapper.recommendMajorList(activeCurrentUserScore.getCalculationTableName(),queryRecommendMajorVO);
        yxHistoryMajorEnrollService.recommendMajorDTOListSetHistoryInfo(recommendMajorList, ScoreUtil.getShowOldYears());
        ArtRecommendMajorBaseDTO artRecommendMajorBaseDTO = new ArtRecommendMajorBaseDTO();
        convertRecommendMajorListToNull(recommendMajorList);
        artRecommendMajorBaseDTO.setList(recommendMajorList);
        return artRecommendMajorBaseDTO;
    }

    /**
     * 转变要隐藏的数据
     * @param recommendMajorList
     */
    private static void convertRecommendMajorListToNull(List<RecommendMajorDTO> recommendMajorList) {
//        List<String> rulesEnrollProbabilityList = Arrays.asList("文*0.8+文*0.5", "文*0.7+文*0.75","文*0.6+专*1","文*0.5+专*1.25");
//        List<String> rulesEnrollProbabilitySxList = Arrays.asList("文7专3","专过文排","文8专2","文6专4","文5专5");
//        recommendMajorList.forEach(r->{
////            r.setPlanNum(null);
//            //非体育，非专科
//            if(!r.getMajorType().equals("体育类") && !r.getBatch().equals("高职高专")){
//                if (rulesEnrollProbabilitySxList.contains(r.getRulesEnrollProbabilitySx())) {
//                }else if(!rulesEnrollProbabilityList.contains(r.getRulesEnrollProbability())){
//                    r.setRulesEnrollProbability(null);
//                }
//            }
//        });
    }

    @Override
    public ArtRecommendMajorBaseDTO checkLevelNum(List<RecommendMajorDTO> recommendMajorList) {
        int number = 0;
        int kbd=0,jwt=0,kcj=0,nan=0;
        double enrollProbabilityDouble = 0;
        for (RecommendMajorDTO recommendMajorDTO : recommendMajorList) {
            number++;
            enrollProbabilityDouble  = recommendMajorDTO.getEnrollProbability().doubleValue();
            //保底
            if (enrollProbabilityDouble >= 93) {
                kbd++;
            } else if (enrollProbabilityDouble < 92 && enrollProbabilityDouble >= 74) {
                //较稳妥
                jwt++;
            } else if (enrollProbabilityDouble < 73 && enrollProbabilityDouble >= 60) {
                //可冲击
                kcj++;
            } else {
                //难
                nan++;
            }
        }
        ArtRecommendMajorBaseDTO artRecommendMajorBaseDTO = new ArtRecommendMajorBaseDTO();
        artRecommendMajorBaseDTO.setKcj(kcj);
        artRecommendMajorBaseDTO.setJwt(jwt);
        artRecommendMajorBaseDTO.setKbd(kbd);
        artRecommendMajorBaseDTO.setNan(nan);
        artRecommendMajorBaseDTO.setAllNumber(number);
        return artRecommendMajorBaseDTO;
    }

    @Override
    public String checkCalculationTableNameOfCreate(String calculationTableName) {
        return null;
    }

    @Override
    public ArtRecommendMajorBaseDTO aiAutoRecommendMajor(QueryRecommendMajorVO queryRecommendMajorVO) {
        String batch = queryRecommendMajorVO.getBatch();
        int pageNum = 1;
        int pageSize = 64;
        if ("提前批".equals(batch)) {
            pageSize = 1;
        }
        YxUserScore activeCurrentUserScore = null;
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getScoreId())) {
            activeCurrentUserScore = yxUserScoreService.findById(queryRecommendMajorVO.getScoreId());
        } else {
            activeCurrentUserScore = yxUserScoreService.getActiveCurrentUserScore();
        }

        String cognitioPolyclinic = activeCurrentUserScore.getCognitioPolyclinic();
        String professionalCategory = activeCurrentUserScore.getProfessionalCategory();//专业类别
        queryRecommendMajorVO.setProfessionalCategory(professionalCategory);
        queryRecommendMajorVO.setCognitioPolyclinic(cognitioPolyclinic);

        //有子级专业
        if (StringUtils.isNotBlank(activeCurrentUserScore.getProfessionalCategoryChildren()) && !"高职高专".equals(queryRecommendMajorVO.getBatch())) {
            queryRecommendMajorVO.setPCategoryChildrenList(new ArrayList<>(Arrays.asList(activeCurrentUserScore.getProfessionalCategoryChildren().split(","))));
            if("表演类".equals(activeCurrentUserScore.getProfessionalCategory())){
                queryRecommendMajorVO.getPCategoryChildrenList().add("兼报");
            }
        }
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getMajorCode())) {
            queryRecommendMajorVO.setMajorCodeList(Arrays.asList(queryRecommendMajorVO.getMajorCode().split(",")));
        }
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getSchoolNatureStrs())) {
            queryRecommendMajorVO.setSchoolNatureList(Arrays.asList(queryRecommendMajorVO.getSchoolNatureStrs().split(",")));
        }
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getProvince())) {
            queryRecommendMajorVO.setAddressList(Arrays.asList(queryRecommendMajorVO.getProvince().split(",")));
        }
        queryRecommendMajorVO.setScoreId(activeCurrentUserScore.getId());
        List<RecommendMajorDTO> recommendMajorList = baseMapper.recommendMajorListBenOrZhuan(activeCurrentUserScore.getCalculationTableName(),queryRecommendMajorVO);

        yxHistoryMajorEnrollService.recommendMajorDTOListSetHistoryInfo(recommendMajorList,ScoreUtil.getShowOldYears());

        //插入 院校标签
        this.setSchoolTagsList(recommendMajorList);
        int total = recommendMajorList.size();
        List<RecommendMajorDTO> recommendMajorDTOList = recommendMajorList.stream().skip(0).limit(pageSize).collect(Collectors.toList());
        ArtRecommendMajorBaseDTO artRecommendMajorBaseDTO = new ArtRecommendMajorBaseDTO();
        IPage<RecommendMajorDTO> pageList = new Page<>();
        pageList.setTotal(total);//总记录数
        pageList.setCurrent(pageNum);
        pageList.setSize(pageSize);
        convertRecommendMajorListToNull(recommendMajorDTOList);
        pageList.setRecords(recommendMajorDTOList);
        artRecommendMajorBaseDTO.setPageList(pageList);
        return artRecommendMajorBaseDTO;
    }

    @Override
    public void copyNew(String oldScoreId, String newScoreId, String oldCalculationMajorTableName, String newCalculationMajorTableName) {
        LambdaQueryWrapper<YxCalculationMajor> yxCalculationMajorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yxCalculationMajorLambdaQueryWrapper.eq(YxCalculationMajor::getScoreId,oldScoreId);
        List<YxCalculationMajor> list = this.list(yxCalculationMajorLambdaQueryWrapper);
        for (YxCalculationMajor yxCalculationMajor : list) {
            yxCalculationMajor.setId(null);
            yxCalculationMajor.setScoreId(newScoreId);
        }
        baseMapper.insertBatch(newCalculationMajorTableName,newScoreId,list);
    }

    @Override
    public List<String> groupByBatchNumber(QueryRecommendMajorVO queryRecommendMajorVO) {
        YxUserScore activeCurrentUserScore = yxUserScoreService.getActiveCurrentUserScore();
        queryRecommendMajorVO.setScoreId(activeCurrentUserScore.getId());
        //文理分科
        queryRecommendMajorVO.setCognitioPolyclinic(activeCurrentUserScore.getCognitioPolyclinic());
        //专业类别
        queryRecommendMajorVO.setProfessionalCategory(activeCurrentUserScore.getProfessionalCategory());
        //标签类别
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getTagsStrs())) {
            queryRecommendMajorVO.setTagsList(Arrays.asList(queryRecommendMajorVO.getTagsStrs().split(",")));
        }
        //院校类型
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getSchoolNatureStrs())) {
            queryRecommendMajorVO.setSchoolNatureList(Arrays.asList(queryRecommendMajorVO.getSchoolNatureStrs().split(",")));
        }
        //院校地区
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getProvince())) {
            queryRecommendMajorVO.setAddressList(Arrays.asList(queryRecommendMajorVO.getProvince().split(",")));
        }
        //科研教学
        if (StringUtils.isNotBlank(queryRecommendMajorVO.getKyjxStrs())) {
            queryRecommendMajorVO.setKyjxList(Arrays.asList(queryRecommendMajorVO.getKyjxStrs().split(",")));
        }
        //录取方式
        String rulesEnrollProbability = queryRecommendMajorVO.getRulesEnrollProbability();
        if (StringUtils.isNotBlank(rulesEnrollProbability)) {
            queryRecommendMajorVO.setRulesEnrollProbabilityList(Arrays.asList(rulesEnrollProbability.split(",")));
        }
        //获取推荐志愿信息service
        return baseMapper.groupByBatchNumber(activeCurrentUserScore.getCalculationTableName(),queryRecommendMajorVO);
    }

    @Override
    public YxCalculationMajor getCalculationById(String calculationTableName, String calculationMajorId) {
        return baseMapper.getCalculationById(calculationTableName, calculationMajorId);
    }

    @Override
    public boolean updateScoreId(String calculationTableName, String scoreId, String bakScoreId) {
        return baseMapper.updateScoreId(calculationTableName, scoreId, bakScoreId) >= 1;
    }

    @Override
    public List<YxCalculationMajor> listByIdsOfYear(List<String> calculationMajorIdList, String newCalculationMajorName) {
        return baseMapper.selectBatchIdsOfYear(calculationMajorIdList, newCalculationMajorName);
    }

    private void setSchoolTagsList(List<RecommendMajorDTO> recommendMajorList) {
        if (CollectionUtils.isNotEmpty(recommendMajorList)) {
            Set<String> schoolIdSet = recommendMajorList.stream().map(RecommendMajorDTO::getSchoolId).collect(Collectors.toSet());
            List<YxSchool> schoolList = yxSchoolMapper.selectBatchIds(schoolIdSet);
            if (CollectionUtils.isNotEmpty(schoolList)) {
                Map<String, YxSchool> yxSchoolMap = schoolList.stream().collect(Collectors.toMap(YxSchool::getId, s -> s));
                YxSchool school = null;
                Set<String> tagsList;
                for (RecommendMajorDTO recommendMajorDTO : recommendMajorList) {
                    school = yxSchoolMap.get(recommendMajorDTO.getSchoolId());
                    if (school == null) {
                        continue;
                    }
                    tagsList = new HashSet<>();
                    if (org.apache.commons.lang3.StringUtils.isNotBlank(school.getSchoolType())) {
                        tagsList.add(school.getSchoolType());//学校层次
                    }
                    if (org.apache.commons.lang3.StringUtils.isNotBlank(school.getSchoolNature())) {
                        tagsList.add(school.getSchoolNature());//办学性质
                    }
                    if (org.apache.commons.lang3.StringUtils.isNotBlank(school.getInstitutionType())) {
                        String[] split = school.getInstitutionType().split(" ");
                        for (String s : split) {
                            tagsList.add(s + "类");//院校类型
                        }
                    }

                    //判断 是否是 985,211
                    if ("1".equals(school.getIs211())) {
                        tagsList.add("211");
                    }
                    if ("1".equals(school.getIs985())) {
                        tagsList.add("985");
                    }
                    if ("双一流".equals(school.getSfsyl())) {
                        tagsList.add("双一流");
                    }
                    if (org.apache.commons.lang3.StringUtils.isNotBlank(school.getGzsz())) {
                        tagsList.add(school.getGzsz());
                    }
                    recommendMajorDTO.setTagsList(tagsList);
                }
            }
        }
    }

    /**
     * 统一测算录取率方法
     *
     * @param recommendMajorDTOList
     * @param userScore
     */
    private void checkEnrollProbability(List<RecommendMajorDTO> recommendMajorDTOList, YxUserScore userScore) {
        String professionalCategory = userScore.getProfessionalCategory();
        if ("表演类".equals(professionalCategory)) {
            biaoyanService.biaoyanRecommendMajorListSetEnrollProbability(recommendMajorDTOList, userScore);
        } else if ("音乐类".equals(professionalCategory)) {
            //音乐类的
            musicService.musicRecommendMajorListSetEnrollProbability(recommendMajorDTOList, userScore);
//        } else if ("舞蹈类".equals(professionalCategory)) {
//            wuDaoService.wudaoRecommendMajorListSetEnrollProbability(recommendMajorDTOList, userScore);
//        } else if ("播音与主持类".equals(professionalCategory)) {
//            boYinService.boYinRecommendMajorListSetEnrollProbability(recommendMajorDTOList, userScore);
        } else {
            betaRecommendMajorListSetEnrollProbability(recommendMajorDTOList, userScore);
        }
    }

    /**
     * 美术与设计类，书法类，体育类 按这个走 获取录取率
     */
    private void betaRecommendMajorListSetEnrollProbability(List<RecommendMajorDTO> recommendMajorList, YxUserScore activeCurrentUserScore) {
        int index = 0;
        try {
            long startTime = new Date().getTime();
            //专业类别
            String professionalCategory = activeCurrentUserScore.getProfessionalCategory();
            String rulesEnrollProbability = null;
            String probabilityOperator = null;
            String showRulesEnrollProbability = null;
            String showProbabilityOperator = null;
            String nowBatch = activeCurrentUserScore.getBatch();
            BigDecimal studentScore = null;//学生折合分
            BigDecimal privateStudentScore = null;//学生折合分显示用
            BigDecimal culturalScore = activeCurrentUserScore.getCulturalScore();//学生的文化分
            BigDecimal professionalScore = activeCurrentUserScore.getProfessionalScore();//学生的专业分
            BigDecimal nowYearDiff = null;//当前年分差
            Map<String, YxHistoryScoreControlLine> historyScoreControlLineMap = yxHistoryScoreControlLineService.mapsBatchByProfessionalCategoryOfYear(YxConstant.nowYear, professionalCategory, activeCurrentUserScore.getCognitioPolyclinic());
            BigDecimal culturalControlLine = null;//文化分省控线
            BigDecimal specialControlLine = null;//专业分省控线
            BigDecimal nowYearProvincialControlLine = null;//当前年省控线
            BigDecimal historyThreeYearDiff = null;//近三年平均差值
            BigDecimal enrollProbability = null;//录取率
            Map<String, Object> differenceMap = null;
            List<YxHistoryMajorEnroll> historyMajorEnrollList = null;//历年录取信息
            if (CollectionUtils.isNotEmpty(recommendMajorList)) {
                //遍历专业列表
                for (RecommendMajorDTO recommendMajorDTO : recommendMajorList) {
                    index++;
                    //取出往年的分数信息, 算出 三年平均差值
                    historyMajorEnrollList = recommendMajorDTO.getHistoryMajorEnrollList();
                    rulesEnrollProbability = recommendMajorDTO.getPrivateRulesEnrollProbability();
                    probabilityOperator = recommendMajorDTO.getPrivateProbabilityOperator();
                    culturalControlLine = historyScoreControlLineMap.get(recommendMajorDTO.getBatch()).getCulturalScore();
                    specialControlLine = historyScoreControlLineMap.get(recommendMajorDTO.getBatch()).getSpecialScore();
                    //没有计算方式 跳过
                    if (StringUtils.isBlank(rulesEnrollProbability)) {
                        continue;
                    }

                    // && "美术学（师范）".equals(recommendMajorDTO.getMajorName())
                    if("2565".equals(recommendMajorDTO.getSchoolCode())
                    && "视觉传达设计".equals(recommendMajorDTO.getMajorName())){
                        System.out.println("qvq");
                    }

                    //不是体育类的专业，如果是文过专排，并且没有录取计算运算符时，文过专排是文*0+专*1,专过文排是文*1+专*0
                    if ("文过专排".equals(rulesEnrollProbability) && StringUtils.isBlank(probabilityOperator)) {
                        probabilityOperator = "文*0+专*1";
                    } else if ("专过文排".equals(rulesEnrollProbability) && StringUtils.isBlank(probabilityOperator)) {
                        probabilityOperator = "文*1+专*0";
                    }
                    if (StringUtils.isBlank(probabilityOperator)) {
                        recommendMajorDTO.setEnrollProbability(YxConstant.bigDecimal0);
                        continue;
                    }

                    //判断其他录取要求
                    if(!ScoreUtil.otherScoreJudge(professionalScore,activeCurrentUserScore,recommendMajorDTO)){
                        recommendMajorDTO.setEnrollProbability(YxConstant.bigDecimal0);
                        continue;
                    }

                    // 25年专业录取原则变动，概率修改为0
                    if("体育类".equals(recommendMajorDTO.getMajorType()) && Arrays.asList("6530、6085、6110、6065、6050".split("、")).contains(recommendMajorDTO.getSchoolCode())){
                        recommendMajorDTO.setEnrollProbability(YxConstant.bigDecimal0);
                        continue;
                    }

                    //判断录取方式，分数是否过省控线
                    if (!ScoreUtil.crossingControlLine(rulesEnrollProbability, culturalScore, professionalScore, culturalControlLine, specialControlLine)) {
                        recommendMajorDTO.setEnrollProbability(YxConstant.bigDecimal0);
                        continue;
                    }

                    //算分用
                    // 当前学生的分数折合
                    studentScore = ScoreUtil.convertIntoScore(rulesEnrollProbability, activeCurrentUserScore.getCulturalScore(), activeCurrentUserScore.getProfessionalScore(), recommendMajorDTO.getFirstLevelDiscipline(), probabilityOperator);
                    recommendMajorDTO.setPrivateStudentScore(studentScore);

                    if (StringUtils.isNotBlank(recommendMajorDTO.getRulesEnrollProbability())) {
                        //recommendMajorDTO.setStudentConvertedScore(studentScore);
                        //计算展示用分数
                        recommendMajorDTO.setStudentScore(studentScore);
                    }

                    //没有往年录取分数线信息
                    if (CollectionUtils.isEmpty(historyMajorEnrollList) || !(ScoreUtil.hasComputeEnrollProbabilityPermissions(nowBatch, recommendMajorDTO.getBatch()))) {
                        recommendMajorDTO.setEnrollProbability(YxConstant.bigDecimal0);
                        continue;
                    }

                    //录取方式计算
                    if (rulesEnrollProbability.equals("文过专排主科")) {
                        if (CollectionUtils.isEmpty(historyMajorEnrollList)) {
                            recommendMajorDTO.setEnrollProbability(YxConstant.bigDecimal0);
                            continue;
                        } else {
                            YxHistoryMajorEnroll yxHistoryMajorEnroll = historyMajorEnrollList.get(0);
                            BigDecimal admissionLine = yxHistoryMajorEnroll.getAdmissionLine();
                            enrollProbability = studentScore.multiply(admissionLine).multiply(YxConstant.bigDecimal075);
                            if (studentScore.compareTo(admissionLine) < 0) {
                                //如果当前主项成绩<近一年的录取分数，录取概率*0.5
                                enrollProbability = enrollProbability.multiply(YxConstant.bigDecimal05);
                            }
                        }
                    } else {
                        //当前年省控线 折合后
                        nowYearProvincialControlLine = ScoreUtil.covertIntoControlLine(rulesEnrollProbability, culturalControlLine, specialControlLine, recommendMajorDTO.getFirstLevelDiscipline(), probabilityOperator);
                        if (nowYearProvincialControlLine == null) {
                            continue;
                        }

                        //历年分差
                        differenceMap = ScoreUtil.computeHistoryMajorEnrollScoreLineDifferenceWithRulesEnrollProbability(rulesEnrollProbability, probabilityOperator, historyMajorEnrollList);
                        historyThreeYearDiff = (BigDecimal) differenceMap.get("scoreDifference");
//                        if(professionalCategory.equals("体育类")
//                                && "文过专排".equals(rulesEnrollProbability)){
//                            historyThreeYearDiff = historyThreeYearDiff.multiply(YxConstant.bigDecimal5);
//                        }

                        // 体育，文过专排 / 5
//                        if (professionalCategory.equals("体育类")
//                                && !"文过专排".equals(rulesEnrollProbability)
//                                && !"专过文排".equals(rulesEnrollProbability)) {
//                            // 体育，非专过文排 / 7.5
//                            nowYearProvincialControlLine = nowYearProvincialControlLine.divide(YxConstant.bigDecimal7p5, 4, RoundingMode.HALF_UP);
//                            //当前年线差
//                            studentScore = studentScore.divide(YxConstant.bigDecimal7p5, 4, RoundingMode.HALF_UP);
//                        }
                        //当前年线差
                        nowYearDiff = studentScore.subtract(nowYearProvincialControlLine);

                        //艺术类，高职高专 算 当前年线差率 (当前年线差/当前年省控线)
//                        if ("高职高专".equals(recommendMajorDTO.getBatch()) && !"体育类".equals(recommendMajorDTO.getMajorType())) {
//                            nowYearDiff = nowYearDiff.divide(nowYearProvincialControlLine,6,RoundingMode.HALF_UP);
//                        }
                        if(historyThreeYearDiff.compareTo(YxConstant.bigDecimal0) == 0){
                            // 历年线差无,无概率
                            recommendMajorDTO.setEnrollProbability(YxConstant.bigDecimal0);
                            continue;
                        }
                        enrollProbability = ScoreUtil.commonCheckEnrollProbability(nowYearDiff,historyThreeYearDiff);

                        //if ("体育类".equals(recommendMajorDTO.getMajorType()) && ("文过专排".equals(rulesEnrollProbability) || "专过文排".equals(rulesEnrollProbability))) {
                        //    enrollProbability = enrollProbability.divide(YxConstant.bigDecimal075, 4, RoundingMode.HALF_UP);
                        //}
                    }

                    enrollProbability = ScoreUtil.commonCheckEnrollProbabilityBeilv(enrollProbability);
                    recommendMajorDTO.setEnrollProbability(enrollProbability);
                }
            }
            long endTime = new Date().getTime();
            log.warn("用时："+(endTime-startTime)+"毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
