package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.mall.domain.MallAcceptRateSetting;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MemberReportIntelligence;
import com.ruoyi.mall.domain.ReportIntelligence;
import com.ruoyi.mall.domain.Vo.MajorItemVo;
import com.ruoyi.mall.domain.Vo.ReportIntelligenceVo;
import com.ruoyi.mall.mapper.MallAcceptRateSettingMapper;
import com.ruoyi.mall.mapper.MemberReportIntelligenceMapper;
import com.ruoyi.mall.mapper.ReportIntelligenceMapper;
import com.ruoyi.mall.service.IMallCountMethodService;
import com.ruoyi.mall.service.IMallSchoolAcceptService;
import com.ruoyi.mall.service.SlipAlertService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

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

/**滑档预警 实现类*/
@Service
public class SlipAlertServiceImpl implements SlipAlertService {
    @Resource
    ReportIntelligenceMapper reportIntelligenceMapper;
    @Resource
    IMallCountMethodService mallCountMethodService;
    @Resource
    MemberReportIntelligenceMapper memberReportIntelligenceMapper;
    @Resource
    MallAcceptRateSettingMapper mallAcceptRateSettingMapper;
    @Resource
    IMallSchoolAcceptService mallSchoolAcceptService;



    /**滑档预警 报告列表
     *
     * @param memberReportIntelligence
     * @return
     */
    @Override
    public List<MemberReportIntelligence> selectMemberReportIntelligenceList(MemberReportIntelligence memberReportIntelligence, MallMemberUser user) {
        List<MemberReportIntelligence> result = new ArrayList<>();
        List<Long> ids = new ArrayList<>();
        //获取当前用户 已正式填报的志愿列表
        ReportIntelligence param = new ReportIntelligence();
        param.setUserId(memberReportIntelligence.getUserId());
//        param.setBatchId(memberReportIntelligence.getBatchId());
        List<ReportIntelligenceVo> userItemList = reportIntelligenceMapper.getUserItemList(param);
        //准备数据

        //遍历 当前用户 相同志愿的
        if(userItemList.size()>0){
            for (ReportIntelligenceVo vo : userItemList) {
                computNum(vo,user,ids);
            }
            //去重id
            ids = ids.stream().distinct().collect(Collectors.toList());
            //根据ids 获取报告列表 返回
            if(null != ids && ids.size()>0){
                result = memberReportIntelligenceMapper.getListByIds(ids);
            }

        }

        return result;
    }

    @Async
    public void computNum(ReportIntelligenceVo vo,MallMemberUser user,List<Long> ids){

        Map<String, Object> rate = new HashMap<>();
        if (vo.getReference() != null){
             rate = mallCountMethodService.getAcceptRateForReport(vo.getReference(), vo.getAcceptRuleId(), user.getCultureScore(),user.getMajorScore());

        }else{
            rate.put("acceptRate", 0);
            rate.put("rateType", -2);
        }
         if (!rate.isEmpty() && new BigDecimal(rate.get("acceptRate").toString()).compareTo(BigDecimal.ZERO) > 0 ){
            vo.setAcceptRate(new BigDecimal(rate.get("acceptRate").toString()));
            vo.setShowAcceptRate(vo.getAcceptRate().multiply(new BigDecimal("100")).setScale(0, RoundingMode.HALF_UP) + "%");
            vo.setRateType((Integer)rate.get("rateType"));
        }

        //和当前用户志愿相同的用户id 批次 学校 专业 非当前用户id
        ReportIntelligence en = BeanUtil.toBean(vo,ReportIntelligence.class);
        en.setUserId(vo.getUserId());
        en.setBatchId(vo.getBatchId());
        en.setSchoolId(vo.getSchoolId());
        en.setMajorId(vo.getMajorId());
        en.setSubjectType(user.getSubjectType());
        List<MajorItemVo> reportNum = reportIntelligenceMapper.reportNum(en);
        //计算录取率 再 count 录取率比用户大的 人数
        int num = 0;
        for (MajorItemVo majorItemVo : reportNum) {
            countNum(num,majorItemVo,vo,user,ids);
        }
    }
    @Async
    public void countNum(int num,MajorItemVo majorItemVo,ReportIntelligenceVo vo,MallMemberUser user,List<Long> ids){
        //专业文化分都大于当前用户 直接num+1
        if(majorItemVo.getCultureScore()>user.getCultureScore() && majorItemVo.getMajorScore()>user.getMajorScore()){
            //if 当前报名用户的录取率 小于等于 历史报名用户
            if(vo.getAcceptRate().compareTo(majorItemVo.getAcceptRate())<=0){
                num+=1;
            }
            if(num >= vo.getAcceptNum()){
                ids.add(vo.getReportId());
            }
        }else{
            // 专业录取率
            Map<String, Object> acceptRate = new HashMap<>();
            if (vo.getReference() != null){
                 acceptRate = mallCountMethodService.getAcceptRateForReport(vo.getReference(),vo.getAcceptRuleId(), majorItemVo.getCultureScore(),majorItemVo.getMajorScore());

            }else{
                acceptRate.put("acceptRate", 0);
                acceptRate.put("rateType", -2);

            }
             if (!acceptRate.isEmpty() && new BigDecimal(acceptRate.get("acceptRate").toString()).compareTo(BigDecimal.ZERO) > 0 ){
                majorItemVo.setAcceptRate(new BigDecimal(acceptRate.get("acceptRate").toString()));
                //if 当前报名用户的录取率 小于等于 历史报名用户
                if(vo.getAcceptRate().compareTo(majorItemVo.getAcceptRate())<=0){
                    num+=1;
                }
                if(num >= vo.getAcceptNum()){
                    ids.add(vo.getReportId());
                }
            }
        }

    }
}
