package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.domain.Bo.CountScoreBO;
import com.ruoyi.mall.domain.Vo.CountRuleVO;
import com.ruoyi.mall.domain.Vo.SchoolVO;
import com.ruoyi.mall.mapper.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallCountMethodMapper;
import com.ruoyi.mall.service.IMallCountMethodService;

import javax.annotation.Resource;

/**
 * 计算公式数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-25
 */
@Service
public class MallCountMethodServiceImpl implements IMallCountMethodService
{
    @Resource
    private MallCountMethodMapper mallCountMethodMapper;

    @Resource
    private MallSchoolAcceptRecordMapper mallSchoolAcceptRecordMapper;

    @Resource
    private MallAcceptRateSettingMapper mallAcceptRateSettingMapper;

    @Resource
    private MallAcceptRuleMapper mallAcceptRuleMapper;

    @Resource
    private MallLinkMajorSchoolMapper mallLinkMajorSchoolMapper;

    @Resource
    private ContrastRecordMapper contrastRecordMapper;

    @Resource
    private MallSchoolTagMapper mallSchoolTagMapper;

    @Resource
    private MallMemberUserMapper mallMemberUserMapper;

    @Resource
    private MallSchoolAcceptMapper mallSchoolAcceptMapper;

    /**
     * 查询计算公式数据
     *
     * @param countMethodId 计算公式数据主键
     * @return 计算公式数据
     */
    @Override
    public MallCountMethod selectMallCountMethodByCountMethodId(Long countMethodId)
    {
        return mallCountMethodMapper.selectMallCountMethodByCountMethodId(countMethodId);
    }

    /**
     * 查询计算公式数据列表
     *
     * @param mallCountMethod 计算公式数据
     * @return 计算公式数据
     */
    @Override
    public List<MallCountMethod> selectMallCountMethodList(MallCountMethod mallCountMethod)
    {
        return mallCountMethodMapper.selectMallCountMethodList(mallCountMethod);
    }

    /**
     * 新增计算公式数据
     *
     * @param mallCountMethod 计算公式数据
     * @return 结果
     */
    @Override
    public int insertMallCountMethod(MallCountMethod mallCountMethod)
    {
        return mallCountMethodMapper.insertMallCountMethod(mallCountMethod);
    }

    /**
     * 修改计算公式数据
     *
     * @param mallCountMethod 计算公式数据
     * @return 结果
     */
    @Override
    public int updateMallCountMethod(MallCountMethod mallCountMethod)
    {
        return mallCountMethodMapper.updateMallCountMethod(mallCountMethod);
    }

    /**
     * 批量删除计算公式数据
     *
     * @param countMethodIds 需要删除的计算公式数据主键
     * @return 结果
     */
    @Override
    public int deleteMallCountMethodByCountMethodIds(Long[] countMethodIds)
    {
        return mallCountMethodMapper.deleteMallCountMethodByCountMethodIds(countMethodIds);
    }

    /**
     * 删除计算公式数据信息
     *
     * @param countMethodId 计算公式数据主键
     * @return 结果
     */
    @Override
    public int deleteMallCountMethodByCountMethodId(Long countMethodId)
    {
        return mallCountMethodMapper.deleteMallCountMethodByCountMethodId(countMethodId);
    }

    /**
     * 计算所需文化分
     * @param majorScore 用户联考分(专业分)
     * @param acceptScore 录取成绩
     * @param acceptRuleId  计算公式id
     * @return 结果
     */
    @Override
    public BigDecimal getNeedCultureScore(Integer majorScore,BigDecimal acceptScore, Long acceptRuleId) {
        BigDecimal userMajorScore = new BigDecimal(majorScore.toString());
        BigDecimal cultureScore = BigDecimal.ZERO;
        // 获取计算字段
        List<MallCountMethod> list = mallCountMethodMapper.selectMethodListByRuleId(acceptRuleId);
        if (list.size() > 0){
            // 专业分部分计算
            List<MallCountMethod> majorCollect = list.stream().filter(item -> item.getCountMode() == 1).collect(Collectors.toList());
            // 乘法
            List<MallCountMethod> collect1 = majorCollect.stream().filter(item -> item.getCountType() == 0).collect(Collectors.toList());
            if (!collect1.isEmpty()){
                for (MallCountMethod mallCountMethod : collect1) {
                    userMajorScore = userMajorScore.multiply(mallCountMethod.getCountValue());
                }
            }
            // 除法
            List<MallCountMethod> collect2 = majorCollect.stream().filter(item -> item.getCountType() == 1).collect(Collectors.toList());
            if (!collect2.isEmpty()){
                BigDecimal temp = BigDecimal.ONE;
                for (MallCountMethod mallCountMethod : collect2) {
                    temp = temp.multiply(mallCountMethod.getCountValue());
                }
                userMajorScore = userMajorScore.divide(temp, 1, RoundingMode.HALF_UP);
            }

            // 计算文化分
            BigDecimal subtract = acceptScore.subtract(userMajorScore);

            List<MallCountMethod> cultureCollect = list.stream().filter((item -> item.getCountMode() == 0)).collect(Collectors.toList());
            // 除法
            List<MallCountMethod> collect3 = cultureCollect.stream().filter(item -> item.getCountType() == 1).collect(Collectors.toList());
            if (!collect3.isEmpty()){
                for (MallCountMethod mallCountMethod : collect3) {
                    subtract = subtract.multiply(mallCountMethod.getCountValue());
                }
            }
            // 乘法
            List<MallCountMethod> collect4 = cultureCollect.stream().filter(item -> item.getCountType() == 0).collect(Collectors.toList());
            if (!collect4.isEmpty()){
                BigDecimal temp = BigDecimal.ONE;
                for (MallCountMethod mallCountMethod : collect4) {
                    temp = temp.multiply(mallCountMethod.getCountValue());
                }
                subtract = subtract.divide(temp, 1, RoundingMode.HALF_UP);
            }

            cultureScore = subtract.setScale(2, RoundingMode.HALF_UP);
        }
        return cultureScore;
    }

    /**
     * 计算综合分
     * @param cultureScore 文化分
     * @param majorScore 专业分
     * @param acceptRuleId 计算公式id
     * @return 结果
     */
    @Override
    public BigDecimal getTotalScore(Integer cultureScore, Integer majorScore, Long acceptRuleId) {
        BigDecimal totalScore = BigDecimal.ZERO;
        BigDecimal culture = new BigDecimal(cultureScore.toString());
        BigDecimal major = new BigDecimal(majorScore.toString());
        // 查询计算字段
        List<MallCountMethod> list = mallCountMethodMapper.selectMethodListByRuleId(acceptRuleId);

        if (list.size() > 0){
            // 计算文化分
            List<MallCountMethod> cultureCollect = list.stream().filter(item -> item.getCountMode() == 0).collect(Collectors.toList());
            // 乘法
            List<MallCountMethod> collect1 = cultureCollect.stream().filter(item -> item.getCountType() == 0).collect(Collectors.toList());
            if (collect1.size() > 0){
                for (MallCountMethod mallCountMethod : collect1) {
                    culture = culture.multiply(mallCountMethod.getCountValue());
                }
            }

            // 除法
            List<MallCountMethod> collect2 = cultureCollect.stream().filter(item -> item.getCountType() == 1).collect(Collectors.toList());
            if (collect2.size() > 0){
                BigDecimal temp = BigDecimal.ONE;
                for (MallCountMethod mallCountMethod : collect2) {
                    temp = temp.multiply(mallCountMethod.getCountValue());
                }
                culture = culture.divide(temp, 2, RoundingMode.HALF_UP);

            }


            // 计算专业分
            List<MallCountMethod> majorCollect = list.stream().filter((item -> item.getCountMode() == 1)).collect(Collectors.toList());
            // 乘法
            List<MallCountMethod> collect3 = majorCollect.stream().filter(item -> item.getCountType() == 0).collect(Collectors.toList());
            if (collect3.size() > 0){
                for (MallCountMethod mallCountMethod : collect3) {
                    major = major.multiply(mallCountMethod.getCountValue());
                }
            }

            List<MallCountMethod> collect4 = majorCollect.stream().filter(item -> item.getCountType() == 1).collect(Collectors.toList());
            if (collect4.size() > 0){
                BigDecimal temp1 = BigDecimal.ONE;
                for (MallCountMethod mallCountMethod : collect4) {
                    temp1 = temp1.multiply(mallCountMethod.getCountValue());
                }
                major = major.divide(temp1, 2, RoundingMode.HALF_UP);

            }
            if (acceptRuleId == 338L){
                totalScore = culture.add(major).setScale(3, RoundingMode.HALF_UP);
            }else{
                totalScore = culture.add(major).setScale(2, RoundingMode.HALF_UP);
            }

        }
       return totalScore;
    }

    /**
     * 获取计算方法字符串
     * @param acceptRuleId 计算方法id
     * @return 结果
     */
    @Override
    public String getCountMethodStr(Long acceptRuleId) {
        List<MallCountMethod> list = mallCountMethodMapper.selectMethodListByRuleId(acceptRuleId);
        StringBuilder sb = new StringBuilder();
        // 文化分部分
        List<MallCountMethod> cultureCollect = list.stream().filter(item -> item.getCountMode() == 0).collect(Collectors.toList());

        sb.append("文化");
        for (MallCountMethod mallCountMethod : cultureCollect) {
            BigDecimal countValue = mallCountMethod.getCountValue();
            String s = this.deleteZero(countValue.toString());
            if (mallCountMethod.getCountType() == 0){
                sb.append("*").append(s);
            }else{
                sb.append("/").append(s);
            }
        }
        sb.append("+");

        // 专业分部分
        List<MallCountMethod> majorCollect = list.stream().filter(item -> item.getCountMode() == 1).collect(Collectors.toList());
        sb.append("专业");
        for (MallCountMethod mallCountMethod : majorCollect) {
            BigDecimal countValue = mallCountMethod.getCountValue();
            String s = this.deleteZero(countValue.toString());
            if (mallCountMethod.getCountType() == 0){
                sb.append("*").append(s);
            }else{
                sb.append("/").append(s);
            }
        }
        return sb.toString();
    }

    /**
     * 字符串去掉后面的0
     * @param str 字符串
     * @return 结果
     */
    public String deleteZero(String str){
        String result = "";
        for (int i = str.length() - 1; i >= 0 ; i--) {
            if (str.charAt(i) != '0'){
                result =  str.substring(0, i + 1);
                break;
            }
        }
        int length = result.length();
        char lastChar = result.charAt(length - 1);
        if (lastChar == '.') {
            result = result.replaceAll("[^a-zA-Z0-9\\s]", "");
        }
        return result;
    }

    // /**
    //  * 计算录取率
    //  * @param schoolId 学校id
    //  * @param cultureScore 文化分
    //  * @param majorScore 专业分
    //  * @return 结果
    //  */
    // @Override
    // public Map<String, Object> getAcceptRate(String subjectType, Long schoolId, Long batchId, Integer cultureScore, Integer majorScore) {
    //     BigDecimal  acceptRate = BigDecimal.ZERO;
    //     Integer rateType = 0;
    //     Map<String, Object> result = new HashMap<>();
    //
    //     if (cultureScore >= 370 || majorScore >= 230) {
    //         // 查询学校最新录取信息
    //
    //         // 查询学校最新三年录取记录数据
    //         List<MallSchoolAcceptRecord> threeYearRecordList = mallSchoolAcceptRecordMapper.selectThreeYearRecordList(schoolId, batchId, subjectType);
    //
    //         if (!threeYearRecordList.isEmpty()){
    //
    //             // 录取规则id
    //             Long acceptRuleId = threeYearRecordList.get(0).getAcceptRuleId();
    //             MallAcceptRule mallAcceptRule = mallAcceptRuleMapper.selectMallAcceptRuleByAcceptRuleId(acceptRuleId);
    //             // 录取原则满分
    //             BigDecimal fullScore = mallAcceptRule.getFullScore();
    //
    //             if (fullScore == null) {
    //                 fullScore = new BigDecimal(1050);
    //             }
    //             if (fullScore.compareTo(new BigDecimal(0)) == 0) {
    //                 fullScore = new BigDecimal(10);
    //             }
    //
    //             // 学校三年综合分
    //             BigDecimal threeScore = this.countThreeYearScore( threeYearRecordList, fullScore );
    //             if (threeScore == null) {
    //                 threeScore = new BigDecimal(0);
    //             }
    //
    //             // 个人综合分
    //             BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);
    //
    //             if (totalScore.compareTo(threeScore) >= 0){
    //                 // 计算1
    //                 BigDecimal count1 = fullScore.multiply(new BigDecimal("0.9")).subtract(threeScore).multiply(BigDecimal.valueOf(4));
    //
    //                 if (count1.compareTo(new BigDecimal(0)) == 0) {
    //                     count1 = new BigDecimal(10);
    //                 }
    //
    //                 // 计算2
    //                 BigDecimal count2 = BigDecimal.valueOf(1).multiply(BigDecimal.valueOf(100)).divide(count1, 4, RoundingMode.HALF_UP);
    //
    //                 // 录取率
    //                 acceptRate = count2.multiply((totalScore.subtract(threeScore))).abs().add(BigDecimal.valueOf(75)).setScale(2, RoundingMode.HALF_UP);
    //
    //             }else{
    //                 // 计算1
    //                 BigDecimal count1 = threeScore.subtract(totalScore).multiply((BigDecimal.valueOf(10)).multiply(BigDecimal.valueOf(100)).divide(fullScore, 4, RoundingMode.HALF_UP));
    //                 // 计算2
    //                 acceptRate = BigDecimal.valueOf(75).subtract(count1).setScale(2, RoundingMode.HALF_UP);
    //             }
    //
    //             if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
    //                 acceptRate = BigDecimal.valueOf(99);
    //             }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
    //                 acceptRate = BigDecimal.ONE;
    //             }
    //             if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(60)) < 0){
    //                 rateType = 0;
    //             }else if (acceptRate.compareTo(BigDecimal.valueOf(60)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(75)) <= 0){
    //                 rateType = 1;
    //             }else if (acceptRate.compareTo(BigDecimal.valueOf(75)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(80)) <= 0){
    //                 rateType = 2;
    //             }else if (acceptRate.compareTo(BigDecimal.valueOf(80)) >=0 ){
    //                 rateType = 3;
    //             }else{
    //                 rateType = -2;
    //             }
    //             result.put("acceptRate", acceptRate);
    //             result.put("rateType", rateType);
    //         }
    //     } else {
    //         result.put("acceptRate", new BigDecimal(0));
    //         result.put("rateType", -2);
    //     }
    //
    //
    //
    //     return result;
    // }

    /**
     * 计算录取率

     * @param cultureScore 文化分
     * @param majorScore 专业分
     * @return 结果
     */
    @Override
    public Map<String, Object> getAcceptRate(BigDecimal reference, Long acceptRuleId, Integer cultureScore, Integer majorScore) {
        BigDecimal  acceptRate = BigDecimal.ZERO;
        Integer rateType = 0;
        Map<String, Object> result = new HashMap<>();

            // 录取原则满分
            BigDecimal fullScore = new BigDecimal("750");
                // 个人综合分
                BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);

                acceptRate = totalScore.subtract(reference).add(new BigDecimal("80"));

                if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
                    acceptRate = BigDecimal.valueOf(99);
                }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
                    acceptRate = BigDecimal.ONE;
                }
                if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(45)) < 0){
                    rateType = 0;
                }else if (acceptRate.compareTo(BigDecimal.valueOf(45)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(65)) < 0){
                    rateType = 1;
                }else if (acceptRate.compareTo(BigDecimal.valueOf(65)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(85)) < 0){
                    rateType = 2;
                }else if (acceptRate.compareTo(BigDecimal.valueOf(85)) >=0 ){
                    rateType = 3;
                }else{
                    rateType = -2;
                    acceptRate = BigDecimal.ONE;
                }
                result.put("acceptRate", acceptRate);
                result.put("rateType", rateType);

        return result;
    }

    /**
     * 计算录取率

     * @param cultureScore 文化分
     * @param majorScore 专业分
     * @return 结果
     */
    @Override
    public Map<String, Object> zhuankegetAcceptRate(BigDecimal reference, Long acceptRuleId, Integer cultureScore, Integer majorScore) {
        BigDecimal  acceptRate = BigDecimal.ZERO;
        Integer rateType = 0;
        Map<String, Object> result = new HashMap<>();

        // 录取原则满分
        BigDecimal fullScore = new BigDecimal("750");
        // 个人综合分
        BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);


            if (totalScore.compareTo(reference) > 0){
                // 计算1

                BigDecimal count1 = fullScore.multiply(new BigDecimal("0.9")).subtract(reference).divide(new BigDecimal("0.15"), 4, RoundingMode.HALF_UP);
                if (count1.compareTo(new BigDecimal(0)) == 0) {
                    count1 = new BigDecimal(10);
                }

                // 计算2
                BigDecimal count2 = BigDecimal.valueOf(1).multiply(BigDecimal.valueOf(100)).divide(count1, 4, RoundingMode.HALF_UP);

                // 录取率
                acceptRate = count2.multiply((totalScore.subtract(reference))).abs().add(BigDecimal.valueOf(85)).setScale(2, RoundingMode.HALF_UP);

            } else{
                // 计算1
                BigDecimal count1 = reference.subtract(totalScore).multiply((BigDecimal.valueOf(10)).multiply(BigDecimal.valueOf(100)).divide(fullScore, 4, RoundingMode.HALF_UP));
                // 计算2
                acceptRate = BigDecimal.valueOf(80).subtract(count1).setScale(2, RoundingMode.HALF_UP);
            }


        if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
            acceptRate = BigDecimal.valueOf(99);
        }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
            acceptRate = BigDecimal.ONE;
        }
        if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(60)) < 0){
            rateType = 0;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(60)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(75)) < 0){
            rateType = 1;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(75)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(80)) < 0){
            rateType = 2;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(80)) >=0 ){
            rateType = 3;
        }else{
            rateType = -2;
            acceptRate = BigDecimal.ONE;
        }
        result.put("acceptRate", acceptRate);
        result.put("rateType", rateType);

        return result;
    }

    ///**
    // * 计算批次学校专业录取率
    // * 用于志愿报告填报
    // * @param cultureScore 文化分
    // * @param majorScore 专业分
    // * @return 结果
    // */
    //@Override
    //public Map<String, Object> getAcceptRateForReport(Long schoolId, Long batchId, Long majorId,  String subjectType, Integer cultureScore, Integer majorScore) {
    //    BigDecimal  acceptRate = BigDecimal.ZERO;
    //    Integer rateType = 0;
    //    Map<String, Object> result = new HashMap<>();
    //
    //    if (cultureScore >= 370 || majorScore >= 230) {
    //        // 查询学校最新三年录取记录数据
    //        List<MallSchoolAccept> list = mallSchoolAcceptMapper.selectThreeAcceptList(schoolId, batchId, majorId, subjectType);
    //
    //        if (!list.isEmpty()){
    //
    //            // 录取规则id
    //            Long acceptRuleId = list.get(0).getAcceptRuleId();
    //            MallAcceptRule mallAcceptRule = mallAcceptRuleMapper.selectMallAcceptRuleByAcceptRuleId(acceptRuleId);
    //            if (ObjectUtils.isNotEmpty(mallAcceptRule)){
    //                // 录取原则满分
    //                BigDecimal fullScore = mallAcceptRule.getFullScore();
    //                if (fullScore == null) {
    //                    fullScore = new BigDecimal(750);
    //                }
    //                if (fullScore.compareTo(new BigDecimal(0)) == 0) {
    //                    fullScore = new BigDecimal(10);
    //                }
    //
    //                // 学校三年综合分
    //                BigDecimal threeScore = this.countThreeYearMajorScore( list, fullScore );
    //                if (threeScore == null) {
    //                    threeScore = new BigDecimal(0);
    //                }
    //
    //                // 个人综合分
    //                BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);
    //                if (totalScore.compareTo(threeScore) >= 0){
    //                    // 计算1
    //                    BigDecimal count1 = fullScore.multiply(new BigDecimal("0.9")).subtract(threeScore).multiply(BigDecimal.valueOf(4));
    //
    //                    if (count1.compareTo(new BigDecimal(0)) == 0) {
    //                        count1 = new BigDecimal(10);
    //                    }
    //
    //                    // 计算2
    //                    BigDecimal count2 = BigDecimal.valueOf(1).divide(count1, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
    //
    //                    // 录取率
    //                    acceptRate = count2.multiply((totalScore.subtract(threeScore))).abs().add(BigDecimal.valueOf(75)).setScale(2, RoundingMode.HALF_UP);
    //
    //                }else{
    //                    // 计算1
    //                    BigDecimal count1 = threeScore.subtract(totalScore).multiply(BigDecimal.valueOf(10)).multiply(BigDecimal.valueOf(100)).divide(fullScore, 4, RoundingMode.HALF_UP);
    //                    // 计算2
    //                    acceptRate = BigDecimal.valueOf(75).subtract(count1).abs().setScale(2, RoundingMode.HALF_UP);
    //
    //                }
    //
    //                if (acceptRate.compareTo(BigDecimal.valueOf(75)) > 0 &&acceptRate.compareTo(BigDecimal.valueOf(100)) < 0){
    //                    rateType = 3;
    //                }
    //
    //                if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
    //                    acceptRate = BigDecimal.valueOf(99);
    //                }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
    //                    acceptRate = BigDecimal.ONE;
    //                }
    //                if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(60)) < 0){
    //                    rateType = 0;
    //                }else if (acceptRate.compareTo(BigDecimal.valueOf(60)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(75)) <= 0){
    //                    rateType = 1;
    //                }else if (acceptRate.compareTo(BigDecimal.valueOf(75)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(80)) <= 0){
    //                    rateType = 2;
    //                }else if (acceptRate.compareTo(BigDecimal.valueOf(80)) >=0 ){
    //                    rateType = 3;
    //                }else{
    //                    rateType = -2;
    //                }
    //                result.put("acceptRate", acceptRate);
    //                result.put("rateType", rateType);
    //            }else{
    //                result.put("acceptRate", new BigDecimal(0));
    //                result.put("rateType", -2);
    //            }
    //        }
    //    } else {
    //        result.put("acceptRate", new BigDecimal(0));
    //        result.put("rateType", -2);
    //    }
    //
    //    return result;
    //
    //}

    /**
     * 计算批次学校专业录取率
     * 用于志愿报告填报
     * @param cultureScore 文化分
     * @param majorScore 专业分
     * @return 结果
     */
    @Override
    public Map<String, Object> getAcceptRateForReport(BigDecimal reference, Long acceptRuleId, Integer cultureScore, Integer majorScore) {
        BigDecimal  acceptRate = BigDecimal.ZERO;
        Integer rateType = 0;
        Map<String, Object> result = new HashMap<>();

                // 录取原则满分
                //BigDecimal fullScore = new BigDecimal("750");

                // 个人综合分
                BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);

                acceptRate = totalScore.subtract(reference).add(new BigDecimal("80"));

                if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
                    acceptRate = BigDecimal.valueOf(99);

                }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
                    acceptRate = BigDecimal.ZERO;
                }
                if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(45)) < 0){
                    rateType = 0;
                }else if (acceptRate.compareTo(BigDecimal.valueOf(45)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(65)) < 0){
                    rateType = 1;
                }else if (acceptRate.compareTo(BigDecimal.valueOf(65)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(85)) < 0){
                    rateType = 2;
                }else if (acceptRate.compareTo(BigDecimal.valueOf(85)) >=0 ){
                    rateType = 3;
                }else{
                    acceptRate = BigDecimal.ZERO;
                    rateType = -2;
                }
                result.put("acceptRate", acceptRate.toString());
                result.put("rateType", rateType);

        return result;

    }

    /**
     * 计算批次学校专业录取率
     * 用于志愿报告填报
     * @param cultureScore 文化分
     * @param majorScore 专业分
     * @return 结果
     */
    @Override
    public Map<String, Object> getAcceptRateForReport2(BigDecimal reference, BigDecimal totalScore, Integer cultureScore, Integer majorScore) {
        BigDecimal  acceptRate = BigDecimal.ZERO;
        Integer rateType = 0;
        Map<String, Object> result = new HashMap<>();

        // 录取原则满分
        //BigDecimal fullScore = new BigDecimal("750");

        // 个人综合分
        //BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);

        acceptRate = totalScore.subtract(reference).add(new BigDecimal("80"));

        if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
            acceptRate = BigDecimal.valueOf(99);

        }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
            acceptRate = BigDecimal.ZERO;
        }
        if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(45)) < 0){
            rateType = 0;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(45)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(65)) < 0){
            rateType = 1;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(65)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(85)) < 0){
            rateType = 2;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(85)) >=0 ){
            rateType = 3;
        }else{
            acceptRate = BigDecimal.ZERO;
            rateType = -2;
        }
        result.put("acceptRate", acceptRate.toString());
        result.put("rateType", rateType);

        return result;

    }

    /**
     * 计算批次学校专业录取率
     * 用于志愿报告填报
     * @param cultureScore 文化分
     * @param majorScore 专业分
     * @return 结果
     */
    @Override
    public Map<String, Object> zhuankegetAcceptRateForReport(BigDecimal acceptLowest, Long acceptRuleId, Integer cultureScore, Integer majorScore) {
        BigDecimal  acceptRate = BigDecimal.ZERO;
        Integer rateType = 0;
        Map<String, Object> result = new HashMap<>();

        // 录取原则满分
        BigDecimal fullScore = new BigDecimal("750");

        // 个人综合分
        BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);
        if (totalScore.compareTo(acceptLowest) >= 0){
            // 计算1
            BigDecimal count1 = fullScore.multiply(new BigDecimal("0.9")).subtract(acceptLowest).multiply(BigDecimal.valueOf(4));

            if (count1.compareTo(new BigDecimal(0)) == 0) {
                count1 = new BigDecimal(10);
            }

            // 计算2
            BigDecimal count2 = BigDecimal.valueOf(100).divide(count1, 4, RoundingMode.HALF_UP);

            // 录取率
            acceptRate = count2.multiply((totalScore.subtract(acceptLowest))).abs().add(BigDecimal.valueOf(85)).setScale(2, RoundingMode.HALF_UP);

        }else{
            // 计算1
            BigDecimal count1 = acceptLowest.subtract(totalScore).multiply(BigDecimal.valueOf(10)).multiply(BigDecimal.valueOf(100)).divide(fullScore, 4, RoundingMode.HALF_UP);
            // 计算2
            acceptRate = BigDecimal.valueOf(80).subtract(count1).setScale(2, RoundingMode.HALF_UP);

        }

        //if (acceptRate.compareTo(BigDecimal.valueOf(75)) > 0 &&acceptRate.compareTo(BigDecimal.valueOf(100)) < 0){
        //    rateType = 3;
        //}

        if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
            acceptRate = BigDecimal.valueOf(99);

        }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
            acceptRate = BigDecimal.ZERO;
        }
        if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(60)) < 0){
            rateType = 0;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(60)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(75)) < 0){
            rateType = 1;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(75)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(80)) < 0){
            rateType = 2;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(80)) >=0 ){
            rateType = 3;
        }else{
            acceptRate = BigDecimal.ZERO;
            rateType = -2;
        }
        result.put("acceptRate", acceptRate);
        result.put("rateType", rateType);


        return result;

    }

    @Override
    public Map<String, Object> zhuankegetAcceptRateForReport2(BigDecimal acceptLowest, BigDecimal totalScore, Integer cultureScore, Integer majorScore) {
        BigDecimal  acceptRate = BigDecimal.ZERO;
        Integer rateType = 0;
        Map<String, Object> result = new HashMap<>();

        // 录取原则满分
        BigDecimal fullScore = new BigDecimal("750");

        // 个人综合分
        //BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);
        if (totalScore.compareTo(acceptLowest) >= 0){
            // 计算1
            BigDecimal count1 = fullScore.multiply(new BigDecimal("0.9")).subtract(acceptLowest).multiply(BigDecimal.valueOf(4));

            if (count1.compareTo(new BigDecimal(0)) == 0) {
                count1 = new BigDecimal(10);
            }

            // 计算2
            BigDecimal count2 = BigDecimal.valueOf(100).divide(count1, 4, RoundingMode.HALF_UP);

            // 录取率
            acceptRate = count2.multiply((totalScore.subtract(acceptLowest))).abs().add(BigDecimal.valueOf(85)).setScale(2, RoundingMode.HALF_UP);

        }else{
            // 计算1
            BigDecimal count1 = acceptLowest.subtract(totalScore).multiply(BigDecimal.valueOf(10)).multiply(BigDecimal.valueOf(100)).divide(fullScore, 4, RoundingMode.HALF_UP);
            // 计算2
            acceptRate = BigDecimal.valueOf(80).subtract(count1).setScale(2, RoundingMode.HALF_UP);

        }

        //if (acceptRate.compareTo(BigDecimal.valueOf(75)) > 0 &&acceptRate.compareTo(BigDecimal.valueOf(100)) < 0){
        //    rateType = 3;
        //}

        if (acceptRate.compareTo(BigDecimal.valueOf(99)) >= 0){
            acceptRate = BigDecimal.valueOf(99);

        }else if (acceptRate.compareTo(BigDecimal.ZERO) <= 0){
            acceptRate = BigDecimal.ZERO;
        }
        if (acceptRate.compareTo(BigDecimal.valueOf(25)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(60)) < 0){
            rateType = 0;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(60)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(75)) < 0){
            rateType = 1;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(75)) >= 0 && acceptRate.compareTo(BigDecimal.valueOf(80)) < 0){
            rateType = 2;
        }else if (acceptRate.compareTo(BigDecimal.valueOf(80)) >=0 ){
            rateType = 3;
        }else{
            acceptRate = BigDecimal.ZERO;
            rateType = -2;
        }
        result.put("acceptRate", acceptRate);
        result.put("rateType", rateType);


        return result;

    }

    /**
     * 计算方法-计算综合分方法
     * @param countScoreBO 用户分数信息
     * @return 结果
     */
    @Override
    public AjaxResult countScore(CountScoreBO countScoreBO) {
        Integer cultureScore = countScoreBO.getCultureScore();
        Integer majorScore = countScoreBO.getMajorScore();

        // 查找不同计算分公式
        List<MallAcceptRule> ruleList = mallAcceptRuleMapper.selectRuleList();

        // 查找院校对应的计算公式和投档公式
        //List<CountRuleVO> countRuleVOS = mallAcceptRuleMapper.selectSendRuleListForCount();

        List<CountRuleVO> allList = new ArrayList<>();
        if (!ruleList.isEmpty()){
            for (MallAcceptRule mallAcceptRule : ruleList) {

                Long acceptRuleId = mallAcceptRule.getAcceptRuleId();
                BigDecimal fullScore = mallAcceptRule.getFullScore();

                if (fullScore != null && fullScore.compareTo(new BigDecimal(0)) > 0) {
                    // 计算综合分
                    BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);

                    // 满分达成率
                    String fullScoreRate = this.fullScoreRate(totalScore, fullScore);
                    System.out.println("+++++++" + acceptRuleId);
                    // 联考转换系数
                    BigDecimal changeRate = mallAcceptRule.getChangeRate();

                    // 录取公式字符串
                    String acceptStr = mallAcceptRule.getRuleStr();

                        // 查询拥有系数的学校和专业的投档原则
                    CountRuleVO countRuleVO = new CountRuleVO();
                    countRuleVO.setAcceptRuleId(mallAcceptRule.getAcceptRuleId());
                    countRuleVO.setTotalScore(totalScore);

                    countRuleVO.setChangeRate(changeRate);


                    countRuleVO.setFullScoreRate(fullScoreRate);

                    countRuleVO.setAcceptRuleStr(acceptStr);
                        // 投档公式字符串
                        //String sendStr = mallAcceptRule.getRuleCountStr();
                    countRuleVO.setSendRuleStr(acceptStr);
                    allList.add(countRuleVO);

                }
            }

        }

        //List<CountRuleVO> list = new ArrayList<>();
        //if (!allList.isEmpty()){
        //    Integer pageNum = countScoreBO.getPageNum();
        //    Integer pageSize = countScoreBO.getPageSize();
        //    // 排序并处理分页
        //    allList.sort(Comparator.comparing(CountRuleVO::getFullScoreRate).reversed());
        //    // 分页
        //    int size = allList.size();
        //    if(pageNum * pageSize <= size){
        //        list = allList.subList((pageNum - 1) * pageSize, (pageNum - 1) * pageSize + pageSize);
        //    }else if (pageNum * pageSize > size){
        //        int i = size / pageSize;
        //        list = allList.subList(i, size - (i * pageSize));
        //    }
        //}


        return AjaxResult.success(new PageInfo<>(allList));
    }

    /**
     * 专科批计算方法
     * @return 结果
     */
    @Override
    public AjaxResult getTrainingSchoolMethod(Integer cultureScore, Integer majorScore) {
        CountRuleVO countRuleVO = mallAcceptRuleMapper.selectTrainingMethod();
        Long acceptRuleId = countRuleVO.getAcceptRuleId();
        // 综合分
        BigDecimal totalScore = this.getTotalScore(cultureScore, majorScore, acceptRuleId);

        String countMethodStr = this.getCountMethodStr(acceptRuleId);

        countRuleVO.setTotalScore(totalScore);
        countRuleVO.setAcceptRuleStr(countMethodStr);
        countRuleVO.setSendRuleStr(countMethodStr);

        return AjaxResult.success(countRuleVO);
    }

    /**
     * 满分达成率
     * @param userScore 用户综合分
     * @param fullScore 对应原则满分
     * @return 结果
     */
    @Override
    public String fullScoreRate(BigDecimal userScore, BigDecimal fullScore) {
        String result ="";
        if (ObjectUtils.isNotEmpty(userScore) && ObjectUtils.isNotEmpty(fullScore)){
            BigDecimal rate = userScore.divide(fullScore, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
            result = rate + "%";
        }
        return result;
    }

    /**
     * 计算方法匹配院校
     * @param id 用户id
     * @param acceptRuleId 计算方法id
     * @param batchId 批次id
     * @param positionId 定位id
     * @param suggestId 推荐度id
     * @return 结果
     */
    @Override
    public PageInfo<SchoolVO> ruleMatchSchoolList(Long id, Long acceptRuleId, Long batchId, Long positionId, Long suggestId, Integer pageNum, Integer pageSize) {

        // 查询用户信息
        MallMemberUser mallMemberUser = mallMemberUserMapper.selectMallMemberUserById(id);
        String subjectType = mallMemberUser.getSubjectType();

        //// 录取规则id查询院校列表
        PageHelper.startPage(pageNum, pageSize);
        List<SchoolVO> schoolVOList = mallLinkMajorSchoolMapper.selectRuleMatchSchool(acceptRuleId, batchId, positionId, suggestId, subjectType);

        // 查询是否对比
        if (!schoolVOList.isEmpty()){
            for (SchoolVO schoolVO : schoolVOList) {
                List<Long> longs = contrastRecordMapper.selectSchoolContrastStatus(id, schoolVO.getSchoolId(), schoolVO.getBatchId());

                if (!longs.isEmpty()){
                    schoolVO.setRecordType("1");
                    schoolVO.setRecordId(longs.get(0));
                }else{
                    schoolVO.setRecordType("0");
                }
                // 查询标签列表
                if (StringUtils.isNotBlank(schoolVO.getTags())) {
                    List<String> list = mallSchoolTagMapper.selectTagByTagId(schoolVO.getTags());
                    schoolVO.setTagList(list);
                }
            }

        }
        return new PageInfo<>(schoolVOList);
    }

    // // 计算学校三年综合分
    // public BigDecimal countThreeYearScore( List<MallSchoolAcceptRecord> list, BigDecimal fullScore){
    //     BigDecimal threeScore = BigDecimal.ZERO;
    //        if (!list.isEmpty()){
    //         int size = list.size();
    //
    //         // 该规则满分综合分
    //         BigDecimal multiply = fullScore.multiply(new BigDecimal("0.1"));
    //         // 判断是否选择平均值
    //         if( size == 1){
    //             threeScore = list.get(0).getAcceptLowest();
    //         }else{
    //
    //             list = list.stream().filter(s -> s.getAcceptLowest()!=null).collect(Collectors.toList());
    //             MallSchoolAcceptRecord max = list.stream().max(Comparator.comparing(MallSchoolAcceptRecord::getAcceptLowest)).orElse(null);
    //             MallSchoolAcceptRecord min = list.stream().min(Comparator.comparing(MallSchoolAcceptRecord::getAcceptLowest)).orElse(null);
    //             if (ObjectUtils.isNotEmpty(max) && ObjectUtils.isNotEmpty(min)){
    //                 BigDecimal sub = max.getAcceptLowest().subtract(min.getAcceptLowest());
    //                 if (multiply.compareTo(sub) <= 0){
    //                     // 差值大于等于满分综合分的10%,取最大值
    //                     threeScore = max.getAcceptLowest();
    //                 }else{
    //                     // 取平均值
    //                     BigDecimal ave = BigDecimal.ZERO;
    //                     for (MallSchoolAcceptRecord mallSchoolAcceptRecord : list) {
    //                         ave = ave.add(mallSchoolAcceptRecord.getAcceptLowest());
    //                     }
    //                     threeScore = ave.divide(BigDecimal.valueOf(size), 2, RoundingMode.HALF_UP);
    //                 }
    //             }
    //         }
    //     }
    //     return threeScore;
    // }

    // 计算学校三年综合分
    public BigDecimal countThreeYearScore( List<MallSchoolAcceptRecord> list, BigDecimal fullScore){
        BigDecimal threeScore = BigDecimal.ZERO;
        if (!list.isEmpty()){

                list = list.stream().filter(s -> s.getAcceptLowest()!=null).collect(Collectors.toList());
                MallSchoolAcceptRecord max = list.stream().max(Comparator.comparing(MallSchoolAcceptRecord::getAcceptLowest)).orElse(null);
                if (ObjectUtils.isNotEmpty(max)){
                    // 取最大值
                    threeScore = max.getAcceptLowest();
                }
        }
        return threeScore;
    }

    // 计算专业三年综合分
    public BigDecimal countThreeYearMajorScore( List<MallSchoolAccept> list, BigDecimal fullScore){
        BigDecimal threeScore = BigDecimal.ZERO;
        if (!list.isEmpty()){
            int size = list.size();

            // 该规则满分综合分
            BigDecimal multiply = fullScore.multiply(new BigDecimal("0.1"));
            // 判断是否选择平均值
            if( size == 1){
                threeScore = list.get(0).getAcceptLowest();
            }else{
                list = list.stream().filter(s -> s.getAcceptLowest()!=null).collect(Collectors.toList());
                MallSchoolAccept max = list.stream().max(Comparator.comparing(MallSchoolAccept::getAcceptLowest)).orElse(null);
                MallSchoolAccept min = list.stream().min(Comparator.comparing(MallSchoolAccept::getAcceptLowest)).orElse(null);
                if (ObjectUtils.isNotEmpty(max) && ObjectUtils.isNotEmpty(min)){
                    BigDecimal sub = max.getAcceptLowest().subtract(min.getAcceptLowest());
                    if (multiply.compareTo(sub) <= 0){
                        // 差值大于等于满分综合分的10%,取最大值
                        threeScore = max.getAcceptLowest();
                    }else{
                        // 取平均值
                        BigDecimal ave = BigDecimal.ZERO;
                        for (MallSchoolAccept mallSchoolAcceptRecord : list) {
                            ave = ave.add(mallSchoolAcceptRecord.getAcceptLowest());
                        }
                        threeScore = ave.divide(BigDecimal.valueOf(size), 2, RoundingMode.HALF_UP);
                    }
                }
            }
        }
        return threeScore;
    }
}
