package com.cpp.web.controller.largeScreen;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpp.common.core.domain.R;
import com.cpp.system.service.ISysConfigService;
import com.cpp.web.domain.station.*;
import com.cpp.web.domain.station.enums.ElectricFieldTypeEnum;
import com.cpp.web.service.station.*;
import com.cpp.web.utils.DateTimeUtil;
import com.cpp.web.utils.LatestDataUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * 大屏模块
 *
 * @author whc
 * @date 2022-03-18 15:49:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/largeScreenController")
public class LargeScreenController {
    @Autowired
    PowerStationStatusDataService powerStationStatusDataService;
    @Autowired
    ISysConfigService configService;
    @Autowired
    ForecastPowerUltraShortTermStationService forecastPowerUltraShortTermStationService;
    @Autowired
    ForecastPowerUltraShortTermRegulationService forecastPowerUltraShortTermRegulationService;
    @Autowired
    ForecastPowerShortTermRegulationService forecastPowerShortTermRegulationService;
    @Autowired
    ElectricFieldService electricFieldService;
    @Autowired
    WindTowerStatusDataService windTowerStatusDataService;
    @Autowired
    WeatherStationStatusDataService weatherStationStatusDataService;

    @GetMapping("/getBizData")
    public R getBizData() {
        // 获取所有场站
        List<ElectricField> electricFieldList = electricFieldService.list();
        // 定义首页实际功率、可用、理论总和
        BigDecimal realSum = BigDecimal.ZERO;
        BigDecimal ableSum = BigDecimal.ZERO;
        BigDecimal theorySum = BigDecimal.ZERO;
        BigDecimal cdqSum = BigDecimal.ZERO;
        BigDecimal dqSum = BigDecimal.ZERO;
        BigDecimal fzdSum = BigDecimal.ZERO;
        BigDecimal wsSum = BigDecimal.ZERO;
        int fzdSumCount = 0;
        int wsSumCount = 0;
        // 限电场站数定义
        int xdCount = 0;
        // 获取每个场站的实时功率、可用、理论、超短期、短期
        for (ElectricField electricField:electricFieldList){
            // 实时功率、可用、理论、限电
            PowerStationStatusData powerStationStatusData = LatestDataUtil.getData(electricField.getStationCode(), PowerStationStatusData.class);
            if (powerStationStatusData!=null){
                realSum = realSum.add(powerStationStatusData.getRealValue());
                ableSum = ableSum.add(powerStationStatusData.getAbleValue());
                theorySum = theorySum.add(powerStationStatusData.getTheoryValue());
                if (powerStationStatusData.getPowerRationing().intValue()==1){
                    xdCount++;
                }
            }
            // 超短期
            ForecastPowerUltraShortTermRegulation forecastPowerUltraShortTermRegulation = LatestDataUtil.getData(electricField.getStationCode(), ForecastPowerUltraShortTermRegulation.class);
            if (forecastPowerUltraShortTermRegulation!=null){
                cdqSum = cdqSum.add(forecastPowerUltraShortTermRegulation.getFpValue());
            }
            // 短期
            ForecastPowerShortTermRegulation forecastPowerShortTermRegulation = LatestDataUtil.getData(electricField.getStationCode(), ForecastPowerShortTermRegulation.class);
            if (forecastPowerShortTermRegulation!=null){
                dqSum = dqSum.add(forecastPowerShortTermRegulation.getFpValue());
            }
            // 获取气象站或者测风塔
            if (ElectricFieldTypeEnum.E1.name().equals(electricField.getElectricFieldTypeEnum())){
                WeatherStationStatusData weatherStationStatusData = LatestDataUtil.getData(electricField.getStationCode(), WeatherStationStatusData.class);
                if (weatherStationStatusData!=null){
                    fzdSum = fzdSum.add(weatherStationStatusData.getGlobalR());
                    fzdSumCount++;
                }
            }
            else{
                WindTowerStatusData windTowerStatusData = LatestDataUtil.getData(electricField.getStationCode(), WindTowerStatusData.class);
                if (windTowerStatusData!=null){
                    wsSum = wsSum.add(windTowerStatusData.getWsHubHeight());
                    wsSumCount++;
                }
            }
        }
        Map bizDataMap = new HashMap();
        // 定义总和map
        Map sumMap = new HashMap();
        sumMap.put("realSum",realSum);
        sumMap.put("ableSum",ableSum);
        sumMap.put("theorySum",theorySum);
        bizDataMap.put("sumMap",sumMap);
        Map envDataMap = new HashMap();
        BigDecimal wsAvg = BigDecimal.ZERO;
        if (wsSum.compareTo(BigDecimal.ZERO)==1){
            wsAvg = wsAvg.divide(new BigDecimal(wsSumCount),2, BigDecimal.ROUND_HALF_UP);
        }
        envDataMap.put("wsAvg",wsAvg);
        BigDecimal fzdAvg = BigDecimal.ZERO;
        if (fzdSum.compareTo(BigDecimal.ZERO)==1){
            fzdAvg = fzdAvg.divide(new BigDecimal(fzdSumCount),2, BigDecimal.ROUND_HALF_UP);
        }
        envDataMap.put("fzdAvg",fzdAvg);
        bizDataMap.put("envDataMap",envDataMap);


        Date dayStartTime = DateTimeUtil.getDayStartTime(System.currentTimeMillis());
        Date dayEndTime = DateTimeUtil.getDayLastTime(System.currentTimeMillis());
        // 首页实际、可用、理论曲线图数组
        List<Object> realList = new ArrayList<>();
        List<Object> ableList = new ArrayList<>();
        List<Object> theoryList = new ArrayList<>();
        List<Object> cdqList = new ArrayList<>();
        List<Object> dqList = new ArrayList<>();

        // 查询实际功率、可用、理论
        Date currentTime5 = DateTimeUtil.getMomentTimeFor5Minute(System.currentTimeMillis());
        List<PowerStationStatusData> powerStationStatusDataList = powerStationStatusDataService.findByTimeBetween(dayStartTime,dayEndTime);
        // 按场时间分组
        Map<Long, List<PowerStationStatusData>> powerStationStatusDataGroup = powerStationStatusDataList.stream().collect(Collectors.groupingBy(s->s.getTime().getTime()));

        // 获取超短期
        String cdqHowLongAgo = configService.selectConfigByKey("cdqHowLongAgo");
        QueryWrapper cdqysWrapper = new QueryWrapper<>();
        cdqysWrapper.eq("forecast_how_long_ago", Integer.parseInt(cdqHowLongAgo));
        cdqysWrapper.between("time", dayStartTime, dayEndTime);
        List<ForecastPowerUltraShortTermRegulation> forecastPowerUltraShortTermStationList = forecastPowerUltraShortTermRegulationService.list(cdqysWrapper);
        // 按场时间分组
        Map<Long, List<ForecastPowerUltraShortTermRegulation>> utraShortTermStationDataGroup = forecastPowerUltraShortTermStationList.stream().collect(Collectors.groupingBy(s->s.getTime().getTime()));

        // 获取短期上报
        String dqHowLongAgo = configService.selectConfigByKey("dqHowLongAgo");
        QueryWrapper dqsbWrapper = new QueryWrapper<>();
        dqsbWrapper.eq("forecast_how_long_ago", Integer.parseInt(dqHowLongAgo));
        dqsbWrapper.between("time", dayStartTime, dayEndTime);
        List<ForecastPowerShortTermRegulation> forecastPowerShortTermRegulationList = forecastPowerShortTermRegulationService.list(dqsbWrapper);
        // 按场时间分组
        Map<Long, List<ForecastPowerShortTermRegulation>> shortTermDataGroup = forecastPowerShortTermRegulationList.stream().collect(Collectors.groupingBy(s->s.getTime().getTime()));

        Long momentTime = 15 * 60 * 1000L;
        for (Long tempTime = dayStartTime.getTime(); tempTime <= dayEndTime.getTime(); tempTime = tempTime + momentTime) {
            // 统计实际功率、可用、理论曲线
            if (powerStationStatusDataGroup.get(tempTime)!=null){
                // 对每个时间点的所有场站数据进行求和
                List<PowerStationStatusData> everyDataList = powerStationStatusDataGroup.get(tempTime);
                BigDecimal tempRealSum = BigDecimal.ZERO;
                BigDecimal tempAbleSum = BigDecimal.ZERO;
                BigDecimal tempTheorySum = BigDecimal.ZERO;
                for (PowerStationStatusData powerStationStatusData:everyDataList){
                    BigDecimal realValue = powerStationStatusData.getRealValue();
                    tempRealSum = tempRealSum.add(realValue);
                    BigDecimal ableValue = powerStationStatusData.getAbleValue();
                    tempAbleSum = tempAbleSum.add(ableValue);
                    BigDecimal theoryValue = powerStationStatusData.getTheoryValue();
                    tempTheorySum = tempTheorySum.add(theoryValue);
                }
                realList.add(tempRealSum);
                ableList.add(tempAbleSum);
                theoryList.add(tempTheorySum);
            }
            else{
                realList.add(null);
                ableList.add(null);
                theoryList.add(null);
            }
            // 统计超短期曲线
            if (utraShortTermStationDataGroup.get(tempTime)!=null){
                // 对每个时间点的所有场站数据进行求和
                List<ForecastPowerUltraShortTermRegulation> everyDataList = utraShortTermStationDataGroup.get(tempTime);
                BigDecimal tempCdqSum = BigDecimal.ZERO;
                for (ForecastPowerUltraShortTermRegulation forecastPowerUltraShortTermStation:everyDataList){
                    BigDecimal cdqValue = forecastPowerUltraShortTermStation.getFpValue();
                    tempCdqSum = tempCdqSum.add(cdqValue);
                }
                cdqList.add(tempCdqSum);
            }
            else{
                cdqList.add(null);
            }
            // 统计短期上报曲线
            if (shortTermDataGroup.get(tempTime)!=null){
                // 对每个时间点的所有场站数据进行求和
                List<ForecastPowerShortTermRegulation> everyDataList = shortTermDataGroup.get(tempTime);
                BigDecimal tempDqSum = BigDecimal.ZERO;
                for (ForecastPowerShortTermRegulation forecastPowerShortTermRegulation:everyDataList){
                    BigDecimal dqValue = forecastPowerShortTermRegulation.getFpValue();
                    tempDqSum = tempDqSum.add(dqValue);
                }
                dqList.add(tempDqSum);
            }
            else{
                dqList.add(null);
            }
        }
        // 定义曲线map
        Map curveMap = new HashMap();
        curveMap.put("realList",realList);
        curveMap.put("ableList",ableList);
        curveMap.put("theoryList",theoryList);
        sumMap.put("cdqSum",cdqSum);
        curveMap.put("cdqList",cdqList);
        sumMap.put("dqSum",dqSum);
        curveMap.put("dqList",dqList);
        bizDataMap.put("curveMap",curveMap);

        // 定义限电图map
        Map xdMap = new HashMap();
        // 总场站数
        xdMap.put("zczs",electricFieldList.size());
        // 限电场站数
        xdMap.put("xdczs",xdCount);
        // 限电比
        if (electricFieldList.size()==0){
            xdMap.put("xdbl","0");
        }
        else{
            xdMap.put("xdbl",new BigDecimal(xdCount).divide(new BigDecimal(electricFieldList.size()).multiply(new BigDecimal("100")),2, BigDecimal.ROUND_HALF_UP));
        }
        // 总场站容量
        BigDecimal totalCapacity = BigDecimal.ZERO;
        for (ElectricField electricField : electricFieldList) {
            totalCapacity = totalCapacity.add(electricField.getCapacity());
        }
        xdMap.put("totalCapacity",totalCapacity);
        bizDataMap.put("xdMap",xdMap);


        return R.ok(bizDataMap);
    }
}
