package com.ruoyi.mall.service.impl;

import com.ruoyi.mall.domain.Bo.StaticReportItemBo;
import com.ruoyi.mall.domain.Bo.staticDataBo;
import com.ruoyi.mall.domain.Vo.StaticDataVo;
import com.ruoyi.mall.mapper.StaticDataMapper;
import com.ruoyi.mall.service.StaticDataService;
import org.springframework.stereotype.Service;

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

/**
 * 后台统计
 *
 */
@Service
public class StaticDataServiceImpl implements StaticDataService {
    @Resource
    private StaticDataMapper staticDataMapper;
    @Resource
    private MallCountMethodServiceImpl mallCountMethodService;

    /**来源渠道占比
     *
     * @param staticData
     * @return
     */
    @Override
    public Map<String, Long> getIntroFree(staticDataBo staticData) {
        return staticDataMapper.getIntroFree(staticData);
    }

    /**会员统计占比
     *
     * @param staticData
     * @return
     */
    @Override
    public Map<String, Long> getYLife(staticDataBo staticData) {
        return staticDataMapper.getYLife(staticData);
    }

    /**学校统计
     *
     * @param staticData
     * @return
     */
    //通过批次的选择缩小学校范围 再通过成绩区间和时间区间 筛选各学校填报数据
    //限制了批次 + 时间区间 两个都必传
    //再计算成绩
    @Override
    public StaticDataVo getSchool(staticDataBo staticData) {
        //基础数据
        List<StaticReportItemBo> basic = staticDataMapper.getSchool(staticData);
        List<String> xData = staticDataMapper.getSchoolX(staticData);
        Long[] yData = new Long[xData.size()];
        int i = 0;
        for (String x : xData) {
            Long num = 0L;
            List<StaticReportItemBo> collect = basic.stream().filter(s -> s.getSchoolName().equals(x)).collect(Collectors.toList());
            if(null !=collect && collect.size()>0){
                //计算每个学生的综合成绩是否在区间内
                if(null != staticData.getMaxScore() || null != staticData.getMinScore() ){
                    for (StaticReportItemBo staticReportItemBo : collect) {
                        Boolean flag = false;
                        //计算综合分 MallCountMethodServiceImpl getTotalScore()
                        BigDecimal totalScore = mallCountMethodService.getTotalScore(staticReportItemBo.getCultureScore(), staticReportItemBo.getMajorScore(), staticReportItemBo.getAcceptRuleId());
                        //比较综合分所在区间
                        if(null != staticData.getMaxScore() &&  null != staticData.getMinScore()){//有最大值也有最小值
                            //a.compareTo(b) < 1 a小于等于b  &&  a.compareTo(b) > -1 a大于等于b
                            if(totalScore.compareTo(staticData.getMaxScore()) < 1 && totalScore.compareTo(staticData.getMinScore()) > -1){
                                 flag = true;
                            }
                        }
                        else if(null != staticData.getMaxScore()){//有最大值
                            //a.compareTo(b) < 1 a小于等于b
                            if(totalScore.compareTo(staticData.getMaxScore()) < 1 ){
                                flag = true;
                            }
                        }
                        else if(null != staticData.getMinScore()){//有最小值
                            //a.compareTo(b) > -1 a大于等于b
                            if(totalScore.compareTo(staticData.getMinScore()) > -1){
                                flag = true;
                            }
                        }

                        //flag为true num+1
                        if(flag){
                            num+=1;
                        }
                    }
                }else{//没有成绩区间限制 全部count
                    num = Long.valueOf(collect.size());
                }
            }
            yData[i++] = num;
        }
        StaticDataVo result = new StaticDataVo();
        result.setxData(xData);
        result.setyData(yData);
        return result;
    }


    /** 专业统计
     *
     * @param staticData
     * @return
     */
    //-- 根据批次+学校筛选 缩小样本范围 再通过成绩区间和时间区间筛选数据
    //-- 限制了批次 + 学校 + 时间区间
    //-- 再计算成绩
    @Override
    public StaticDataVo getMajor(staticDataBo staticData) {
        //基础数据
        List<StaticReportItemBo> basic = staticDataMapper.getMajor(staticData);
        List<String> xData = staticDataMapper.getMajorX(staticData);
        Long[] yData = new Long[xData.size()];
        int i = 0;
        for (String x : xData) {
            Long num = 0L;
            List<StaticReportItemBo> collect = basic.stream().filter(s -> s.getMajorName().equals(x)).collect(Collectors.toList());
            if(null !=collect && collect.size()>0){
                //计算每个学生的综合成绩是否在区间内
                if(null != staticData.getMaxScore() || null != staticData.getMinScore() ){
                    for (StaticReportItemBo staticReportItemBo : collect) {
                        Boolean flag = false;
                        //计算综合分 MallCountMethodServiceImpl getTotalScore()
                        BigDecimal totalScore = mallCountMethodService.getTotalScore(staticReportItemBo.getCultureScore(), staticReportItemBo.getMajorScore(), staticReportItemBo.getAcceptRuleId());
                        //比较综合分所在区间
                        if(null != staticData.getMaxScore() &&  null != staticData.getMinScore()){//有最大值也有最小值
                            //a.compareTo(b) < 1 a小于等于b  &&  a.compareTo(b) > -1 a大于等于b
                            if(totalScore.compareTo(staticData.getMaxScore()) < 1 && totalScore.compareTo(staticData.getMinScore()) > -1){
                                flag = true;
                            }
                        }
                        else if(null != staticData.getMaxScore()){//有最大值
                            //a.compareTo(b) < 1 a小于等于b
                            if(totalScore.compareTo(staticData.getMaxScore()) < 1 ){
                                flag = true;
                            }
                        }
                        else if(null != staticData.getMinScore()){//有最小值
                            //a.compareTo(b) > -1 a大于等于b
                            if(totalScore.compareTo(staticData.getMinScore()) > -1){
                                flag = true;
                            }
                        }

                        //flag为true num+1
                        if(flag){
                            num+=1;
                        }
                    }
                }else{//没有成绩区间限制 全部count
                    num = Long.valueOf(collect.size());
                }
            }
            yData[i++] = num;
        }
        StaticDataVo result = new StaticDataVo();
        result.setxData(xData);
        result.setyData(yData);
        return result;
    }
}
