package cn.cecep.talroad.data.analyse.task.service;

import cn.cecep.talroad.data.analyse.task.real.outlet.OutletRealService;
import cn.cecep.talroad.data.analyse.task.real.outlet.impl.GasHourOutletService;
import cn.cecep.talroad.data.analyse.task.real.outlet.impl.WaterHourOutletService;
import cn.cecep.talroad.domain.*;
import cn.cecep.talroad.mapper.*;
import cn.cecep.talroad.util.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/***
 *
 * @Title:
 * @Author: xuxin
 * @CreateTime: 2023-06-28 13:28
 * @Description
 *
 **/
@Service
public class ContrastiveAnalysisGasTaskService implements TaskService {

    @Autowired
    private GasHourOutletService gasHourOutletService;

    @Autowired
    private WaterHourOutletService waterHourOutletService;

    @Autowired
    private SRaRealTimeAnalysicComsitStMapper sRaRealTimeAnalysicComsitStMapper;

    public static final Integer pageSize = 10;


    @Override
    public Object mainProcess(Object o) {

        // 计算废气
        executeData(gasHourOutletService);

        // 计算废水
        executeData(waterHourOutletService);


        return null;
    }

    public void executeData(OutletRealService outletRealService){
        List<String> outletIds = outletRealService.queryOutletIds();

        List<List<String>> idsPage = Lists.partition(outletIds, ContrastiveAnalysisGasTaskService.pageSize);
        List<SEnvGasMonHour> sEnvGasMonHours = new ArrayList<>();

        for (List<String> list : idsPage) {
            List<SEnvGasMonHour> sEnvGasMonHourList = outletRealService.queryReal(list, getStartTime(), getEndTime());
            sEnvGasMonHours.addAll(sEnvGasMonHourList);
        }


        Map<String, List> outletRealMap = outletRealService.groupOutletRealList(sEnvGasMonHours, outletIds);
        calculateSt(outletRealMap,outletRealService);
    }



    /**
     * 存储信息
     */
    public void calculateSt(Map<String, List> outletRealMap,OutletRealService outletRealService){

        for (Map.Entry<String, List> stringListEntry : outletRealMap.entrySet()) {

            // 所有字段，所有内容
            Map<String, List<BigDecimal>> pollData = outletRealService.calculatePollData(stringListEntry.getValue());

            // 每个字段一条数据
            for (Map.Entry<String, List<BigDecimal>> listEntry : pollData.entrySet()) {

                List<BigDecimal> dataAll = listEntry.getValue();
                if (CollectionUtils.isNotEmpty(dataAll)){

                    // 月均值
                    BigDecimal avg = dataAll.stream().reduce(BigDecimal.ZERO, BigDecimal::add).divide(BigDecimal.valueOf(dataAll.size()), 2, BigDecimal.ROUND_HALF_UP);

                    // 四分之一
                    int one = dataAll.size() / 4 * 1;
                    BigDecimal oneData = dataAll.get(one);

                    //四分之三
                    int three = dataAll.size()>4?dataAll.size() / 4 * 3:dataAll.size()-1;
                    BigDecimal threeData = dataAll.get(three);

                    //求最大值
                    BigDecimal max = dataAll.stream().max(BigDecimal::compareTo).get();
                    //求最小值
                    BigDecimal min = dataAll.stream().min(BigDecimal::compareTo).get();

                    // 拼接数据
                    SRaRealTimeAnalysicComsitSt sRaRealTimeAnalysicComsitSt = new SRaRealTimeAnalysicComsitSt();
                    sRaRealTimeAnalysicComsitSt.setParamName(listEntry.getKey());
                    sRaRealTimeAnalysicComsitSt.setCreateTime(new Date());
                    PcOutlet pcOutlet = JSONObject.parseObject(stringListEntry.getKey(), PcOutlet.class);
                    sRaRealTimeAnalysicComsitSt.setStationId(pcOutlet.getStationId());
                    sRaRealTimeAnalysicComsitSt.setFactroyId(pcOutlet.getFactoryId());
                    sRaRealTimeAnalysicComsitSt.setAnalysisTime(new Date());
                    sRaRealTimeAnalysicComsitSt.setMinValue(min.doubleValue());
                    sRaRealTimeAnalysicComsitSt.setMaxValue(max.doubleValue());
                    sRaRealTimeAnalysicComsitSt.setQ1Value(oneData.doubleValue());
                    sRaRealTimeAnalysicComsitSt.setQ2Value(threeData.doubleValue());
                    sRaRealTimeAnalysicComsitSt.setAcgValue(avg.doubleValue());
                    sRaRealTimeAnalysicComsitStMapper.insert(sRaRealTimeAnalysicComsitSt);

                }

            }

        }



    }

    public Date getStartTime(){
        return DateUtil.getFirstDayOfMonth(-1);
    }

    public Date getEndTime(){
        return DateUtil.getLastDayOfMonth(-1);
    }












}
