package com.cpp.web.service.station.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cpp.common.core.domain.entity.SysDictData;
import com.cpp.common.utils.DictUtils;
import com.cpp.system.service.ISysConfigService;
import com.cpp.web.domain.BaseCppEntity;
import com.cpp.web.domain.cloud.ForecastPowerShortTermCloud;
import com.cpp.web.domain.station.*;
import com.cpp.web.mapper.station.PowerStationStatusDataMapper;
import com.cpp.web.service.cloud.ForecastPowerShortTermCloudService;
import com.cpp.web.service.cloud.ForecastPowerUltraShortTermCloudService;
import com.cpp.web.service.cloud.NwpCloudService;
import com.cpp.web.service.station.*;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * cpp_power_station_status_data
 *
 * @author tl
 * @date 2024-09-23 15:28:33
 */
@Service
@RequiredArgsConstructor
public class PowerStationStatusDataServiceImpl extends ServiceImpl<PowerStationStatusDataMapper, PowerStationStatusData> implements PowerStationStatusDataService {
    private final ForecastPowerUltraShortTermStationService forecastPowerUltraShortTermStationService;
    private final ForecastPowerUltraShortTermCloudService forecastPowerUltraShortTermCloudService;
    private final ForecastPowerShortTermCloudService forecastPowerShortTermCloudService;
    private final ElectricFieldService electricFieldService;
    private final ForecastPowerShortTermStationService forecastPowerShortTermStationService;
    private final ForecastPowerShortTermRegulationService forecastPowerShortTermRegulationService;
    private final ForecastPowerShortTermSendService forecastPowerShortTermSendService;

    private final WindTowerStatusDataService windTowerStatusDataService;

//    private final SysParameterService sysParameterService;

    private final WeatherStationStatusDataService weatherStationStatusDataService;

    private final NwpCloudService nwpCloudService;

    @Autowired
    private ISysConfigService configService;


    @Override
    public List<PowerStationStatusData> findByStationCodeAndStartTimeAndEndTime(String stationCode, Date startTime, Date endTime) {
        QueryWrapper<PowerStationStatusData> wrapper = new QueryWrapper<>();
        if (stationCode != null && !stationCode.equals("")) {
            wrapper.eq("station_code", stationCode);
        }
        if (startTime != null && !startTime.equals("") && endTime != null && !endTime.equals("")) {
            wrapper.between("time", startTime, endTime);
        }
        return baseMapper.selectList(wrapper);
    }

    public List<ArrayList<String>> timeStamp(Date startTime, Date endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startDate = sdf.format(startTime);
        String endDate = sdf.format(endTime);
        List<ArrayList<String>> dateList = new ArrayList<>();
        while (startDate.compareTo(endDate) <= 0) {
            List<String> subList = new ArrayList<>();
            subList.add(startDate);
            dateList.add(new ArrayList<>(subList));
            startDate = nextSecond(startDate);
        }
        return dateList;
    }


    @Override
    public Map<String, List> getCenterData(Date startTime, Date endTime) {
        return null;
    }

    public static String nextSecond(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(str);
        long time = date.getTime() + 1000L * 60 * 15;
        date = new Date(time);
        return sdf.format(date);
    }

    public Map<String, Object> getCompositeData(String stationCode, Long startTime, Long endTime) {
        ElectricField electricField = electricFieldService.findByStationCode(stationCode);
        String stationType = electricField.getElectricFieldTypeEnum();

        // 封装列表头信息
        List headList = new ArrayList();
        Map<String, String> czmcHeadMap = new HashMap<>();
        czmcHeadMap.put("field", "czmc");
        czmcHeadMap.put("label", "场站名称");
        headList.add(czmcHeadMap);
        Map<String, String> timeHeadMap = new HashMap<>();
        timeHeadMap.put("field", "time");
        timeHeadMap.put("label", "时间");
        headList.add(timeHeadMap);
        Map<String, String> hjHeadMap = new HashMap<>();
        hjHeadMap.put("field", "hj");

        Map<Long, WeatherStationStatusData> qxzMap = new HashMap<>();
        Map<Long, WindTowerStatusData> cftMap = new HashMap<>();
        if ("E1".equals(stationType)) {
            // 光伏
            hjHeadMap.put("label", "气象站总辐射(W/㎡)");
            // 查询气象站数据
            QueryWrapper<WeatherStationStatusData> wrapper = new QueryWrapper<>();
            wrapper.eq("station_code", stationCode);
            wrapper.between("time", new Date(startTime), new Date(endTime));
            List<WeatherStationStatusData> list = weatherStationStatusDataService.list(wrapper);
            for (WeatherStationStatusData weatherStationStatusData : list) {
                qxzMap.put(weatherStationStatusData.getTime().getTime(), weatherStationStatusData);
            }
        } else {
            hjHeadMap.put("label", "测风塔轮毂风速(m/s)");
            // 查询测风塔数据
            QueryWrapper<WindTowerStatusData> wrapper = new QueryWrapper<>();
            wrapper.eq("station_code", stationCode);
            wrapper.between("time", new Date(startTime), new Date(endTime));
            List<WindTowerStatusData> list = windTowerStatusDataService.list(wrapper);
            for (WindTowerStatusData windTowerStatusData : list) {
                cftMap.put(windTowerStatusData.getTime().getTime(), windTowerStatusData);
            }
        }
        headList.add(hjHeadMap);
        Map<String, String> sjglHeadMap = new HashMap<>();
        sjglHeadMap.put("field", "sjgl");
        sjglHeadMap.put("label", "实际功率(MW)");
        headList.add(sjglHeadMap);
        Map<String, String> kyglHeadMap = new HashMap<>();
        kyglHeadMap.put("field", "kygl");
        kyglHeadMap.put("label", "可用功率(MW)");
        headList.add(kyglHeadMap);
        Map<String, String> llglHeadMap = new HashMap<>();
        llglHeadMap.put("field", "llgl");
        llglHeadMap.put("label", "理论功率(MW)");
        headList.add(llglHeadMap);
        Map<String, String> cdqysHeadMap = new HashMap<>();
        cdqysHeadMap.put("field", "cdqys");
        cdqysHeadMap.put("label", "超短期(MW)");
        headList.add(cdqysHeadMap);
        Map<String, String> dqysHeadMap = new HashMap<>();
        dqysHeadMap.put("field", "dqys");
        dqysHeadMap.put("label", "短期原始(MW)");
        headList.add(dqysHeadMap);
        Map<String, String> dqzxHeadMap = new HashMap<>();
        dqzxHeadMap.put("field", "dqzx");
        dqzxHeadMap.put("label", "短期调控(MW)");
        headList.add(dqzxHeadMap);
        Map<String, String> dqsbHeadMap = new HashMap<>();
        dqsbHeadMap.put("field", "dqsb");
        dqsbHeadMap.put("label", "短期上报(MW)");
        headList.add(dqsbHeadMap);
        Map<String, String> czHeadMap = new HashMap<>();
        czHeadMap.put("field", "cz");
        czHeadMap.put("label", "下发与上报差值");
        headList.add(czHeadMap);

        List<String> timeList = new ArrayList<>();
        List<String> hjList = new ArrayList();
        List<String> sjList = new ArrayList();
        List<String> kyList = new ArrayList();
        List<String> llList = new ArrayList();
        List<String> cdqysList = new ArrayList();
        List<String> dqysList = new ArrayList();
        List<String> dqzxList = new ArrayList();
        List<String> dqsbList = new ArrayList();

        // 获取实际功率
        Map<Long, PowerStationStatusData> sjMap = new HashMap<>();
        List<PowerStationStatusData> powerStationStatusDataList = findByStationCodeAndStartTimeAndEndTime(stationCode, new Date(startTime), new Date(endTime));
        for (PowerStationStatusData powerStationStatusData : powerStationStatusDataList) {
            sjMap.put(powerStationStatusData.getTime().getTime(), powerStationStatusData);
        }
        // 获取短期原始曲线
        Map<Long, ForecastPowerShortTermStation> dqysMap = new HashMap<>();
        String dqHowLongAgo = configService.selectConfigByKey("dqHowLongAgo");
        QueryWrapper dqysWrapper = new QueryWrapper<>();
        dqysWrapper.eq("forecast_how_long_ago", Integer.parseInt(dqHowLongAgo));
        dqysWrapper.eq("station_code", stationCode);
        dqysWrapper.between("time", new Date(startTime), new Date(endTime));
        List<ForecastPowerShortTermStation> forecastPowerShortTermStationList = forecastPowerShortTermStationService.list(dqysWrapper);
        for (ForecastPowerShortTermStation forecastPowerShortTermStation : forecastPowerShortTermStationList) {
            dqysMap.put(forecastPowerShortTermStation.getTime().getTime(), forecastPowerShortTermStation);
        }
        // 获取超短期原始曲线
        Map<Long, ForecastPowerUltraShortTermStation> cdqysMap = new HashMap<>();
        String cdqHowLongAgo = configService.selectConfigByKey("cdqHowLongAgo");
        QueryWrapper cdqysWrapper = new QueryWrapper<>();
        cdqysWrapper.eq("forecast_how_long_ago", Integer.parseInt(dqHowLongAgo));
        cdqysWrapper.eq("station_code", stationCode);
        cdqysWrapper.between("time", new Date(startTime), new Date(endTime));
        List<ForecastPowerUltraShortTermStation> forecastPowerUltraShortTermStationList = forecastPowerUltraShortTermStationService.list(dqysWrapper);
        for (ForecastPowerUltraShortTermStation forecastPowerUltraShortTermStation : forecastPowerUltraShortTermStationList) {
            cdqysMap.put(forecastPowerUltraShortTermStation.getTime().getTime(), forecastPowerUltraShortTermStation);
        }
        // 获取短期上报曲线
        Map<Long, ForecastPowerShortTermRegulation> dqsbMap = new HashMap<>();
        QueryWrapper dqsbWrapper = new QueryWrapper<>();
        dqsbWrapper.eq("forecast_how_long_ago", Integer.parseInt(dqHowLongAgo));
        dqsbWrapper.eq("station_code", stationCode);
        dqsbWrapper.between("time", new Date(startTime), new Date(endTime));
        List<ForecastPowerShortTermRegulation> forecastPowerShortTermRegulationList = forecastPowerShortTermRegulationService.list(dqysWrapper);
        for (ForecastPowerShortTermRegulation forecastPowerShortTermRegulation : forecastPowerShortTermRegulationList) {
            dqsbMap.put(forecastPowerShortTermRegulation.getTime().getTime(), forecastPowerShortTermRegulation);
        }
        // 获取短期调控下发的中心曲线
        Map<Long, ForecastPowerShortTermSend> dqzxMap = new HashMap<>();
        QueryWrapper dqzxWrapper = new QueryWrapper<>();
        dqzxWrapper.eq("forecast_how_long_ago", Integer.parseInt(dqHowLongAgo));
        dqzxWrapper.eq("station_code", stationCode);
        dqzxWrapper.between("time", new Date(startTime), new Date(endTime));
        List<ForecastPowerShortTermSend> forecastPowerShortTermSendList = forecastPowerShortTermSendService.list(dqysWrapper);
        for (ForecastPowerShortTermSend forecastPowerShortTermSend : forecastPowerShortTermSendList) {
            dqzxMap.put(forecastPowerShortTermSend.getTime().getTime(), forecastPowerShortTermSend);
        }


        List<Map<String, Object>> tableList = new ArrayList();
        int i = 0;
        for (Long tempDateTime = startTime; tempDateTime < endTime; tempDateTime = tempDateTime + 15 * 60 * 1000) {
            // 封装列表的数据
            Map<String, Object> map1 = new HashMap<>();
            map1.put("czmc", electricField.getName());
            map1.put("time", DateUtil.format(new Date(tempDateTime), "yyyy-MM-dd HH:mm"));
            // 气象站或者测风塔数据
            if ("E1".equals(stationType)) {
                if (qxzMap.get(tempDateTime) != null) {
                    WeatherStationStatusData weatherStationStatusData = qxzMap.get(tempDateTime);
                    if (weatherStationStatusData.getGlobalR().compareTo(new BigDecimal("-99")) == 0) {
                        map1.put("hj", null);
                        hjList.add(null);
                    } else {
                        map1.put("hj", weatherStationStatusData.getGlobalR().toString());
                        hjList.add(weatherStationStatusData.getGlobalR().toString());
                    }
                } else {
                    map1.put("hj", null);
                    hjList.add(null);
                }
            } else {
                if (cftMap.get(tempDateTime) != null) {
                    WindTowerStatusData windTowerStatusData = cftMap.get(tempDateTime);
                    if (windTowerStatusData.getWsHubHeight().compareTo(new BigDecimal("-99")) == 0) {
                        map1.put("hj", null);
                        hjList.add(null);
                    } else {
                        map1.put("hj", windTowerStatusData.getWsHubHeight().toString());
                        hjList.add(windTowerStatusData.getWsHubHeight().toString());
                    }
                } else {
                    map1.put("hj", null);
                    hjList.add(null);
                }
            }

            // 封装实际功率list
            if (sjMap.get(tempDateTime) != null) {
                // 实际功率
                PowerStationStatusData powerStationStatusData = sjMap.get(tempDateTime);
                if (powerStationStatusData.getRealValue().compareTo(new BigDecimal("-99")) == 0) {
                    sjList.add(null);
                    map1.put("sjgl", null);
                } else {
                    sjList.add(powerStationStatusData.getRealValue().toString());
                    map1.put("sjgl", powerStationStatusData.getRealValue().toString());
                }
                if (powerStationStatusData.getAbleValue().compareTo(new BigDecimal("-99")) == 0) {
                    kyList.add(null);
                    map1.put("kygl", null);
                } else {
                    kyList.add(powerStationStatusData.getAbleValue().toString());
                    map1.put("kygl", powerStationStatusData.getAbleValue().toString());
                }
                if (powerStationStatusData.getTheoryValue().compareTo(new BigDecimal("-99")) == 0) {
                    llList.add(null);
                    map1.put("llgl", null);
                } else {
                    llList.add(powerStationStatusData.getTheoryValue().toString());
                    map1.put("llgl", powerStationStatusData.getTheoryValue().toString());
                }
            } else {
                map1.put("sjgl", null);
                map1.put("kygl", null);
                map1.put("llgl", null);
                sjList.add(null);
                kyList.add(null);
                llList.add(null);
            }
            // 封装超短期原始曲线list
            if (cdqysMap.get(tempDateTime) != null) {
                ForecastPowerUltraShortTermStation forecastPowerUltraShortTermStation = cdqysMap.get(tempDateTime);
                if (forecastPowerUltraShortTermStation.getFpValue().compareTo(new BigDecimal("-99")) == 0) {
                    cdqysList.add(null);
                    map1.put("cdqys", null);
                } else {
                    cdqysList.add(forecastPowerUltraShortTermStation.getFpValue().toString());
                    map1.put("cdqys", forecastPowerUltraShortTermStation.getFpValue().toString());
                }
            } else {
                cdqysList.add(null);
            }

            // 封装短期原始曲线list
            if (dqysMap.get(tempDateTime) != null) {
                ForecastPowerShortTermStation forecastPowerShortTermStation = dqysMap.get(tempDateTime);
                if (forecastPowerShortTermStation.getFpValue().compareTo(new BigDecimal("-99")) == 0) {
                    dqysList.add(null);
                    map1.put("dqys", null);
                } else {
                    dqysList.add(forecastPowerShortTermStation.getFpValue().toString());
                    map1.put("dqys", forecastPowerShortTermStation.getFpValue().toString());
                }
            } else {
                dqysList.add(null);
            }
            // 封装短期上报曲线list
            if (dqsbMap.get(tempDateTime) != null) {
                ForecastPowerShortTermRegulation forecastPowerShortTermRegulation = dqsbMap.get(tempDateTime);
                if (forecastPowerShortTermRegulation.getFpValue().compareTo(new BigDecimal("-99")) == 0) {
                    dqsbList.add(null);
                    map1.put("dqsb", null);
                    map1.put("cz", null);
                } else {
                    dqsbList.add(forecastPowerShortTermRegulation.getFpValue().toString());
                    map1.put("dqsb", forecastPowerShortTermRegulation.getFpValue().toString());
                    // 站端上报了短期，再判断短期下发的值是否为空
                    if (dqzxMap.get(tempDateTime) != null) {
                        // 需要计算差值，下发减上报差值
                        ForecastPowerShortTermSend forecastPowerShortTermSend = dqzxMap.get(tempDateTime);
                        map1.put("cz", forecastPowerShortTermSend.getFpValue().subtract(forecastPowerShortTermRegulation.getFpValue()).toString());
                    } else {
                        // 中心没有调控下发，则差值字段正常就是空
                        map1.put("cz", null);
                    }
                }
            } else {
                dqsbList.add(null);
                // 站端没有上报短期，差值字段为空
                map1.put("cz", "—");
            }
            // 封装短期中心调控下发曲线list
            if (dqzxMap.get(tempDateTime) != null) {
                ForecastPowerShortTermSend forecastPowerShortTermSend = dqzxMap.get(tempDateTime);
                if (forecastPowerShortTermSend.getFpValue().compareTo(new BigDecimal("-99")) == 0) {
                    dqzxList.add(null);
                    map1.put("dqzx", null);
                } else {
                    dqzxList.add(forecastPowerShortTermSend.getFpValue().toString());
                    map1.put("dqzx", forecastPowerShortTermSend.getFpValue().toString());
                }
            } else {
                dqzxList.add(null);
            }


            // 封装时间轴list
            String time = DateUtil.format(new Date(tempDateTime), "yyyy-MM-dd HH:mm");
            timeList.add(time);

            i++;
            tableList.add(map1);
        }


        Map<String, Object> map = new HashMap<>();
        map.put("fromHead", headList);
        map.put("tableList", tableList);
        map.put("timeList", timeList);


        LinkedHashMap<String, List> zhMap = new LinkedHashMap<>();
        zhMap.put("实际功率", sjList);
        zhMap.put("可用功率", kyList);
        zhMap.put("理论功率", llList);
        zhMap.put("超短期", cdqysList);
        zhMap.put("短期原始", dqysList);
        zhMap.put("短期调控", dqzxList);
        zhMap.put("短期上报", dqsbList);
        if ("E1".equals(stationType)) {
            zhMap.put("气象站总辐射", hjList);
        } else {
            zhMap.put("测风塔轮毂风速", hjList);
        }
        map.put("stationType", stationType);
        map.put("zhMap", zhMap);

        return map;
    }

    @Override
    public List<PowerStationStatusData> findByTimeBetweenAndStationCode(Date pointStarTime, Date pointEndTime, String stationCode) {
        QueryWrapper<PowerStationStatusData> wrapper = new QueryWrapper<>();

        if (stationCode != null && !stationCode.equals("")) {
            wrapper.eq("station_code", stationCode);
        }
        if (pointStarTime != null && pointEndTime != null) {
            wrapper.between("time", pointStarTime, pointEndTime);
        }
        return baseMapper.selectList(wrapper);
    }


    //    /**
//     * 中心侧预测数据统计，查询短期、超短期实时表厂家是SYJY的，每个点位值累加一起展示
//     *
//     * @param startTime
//     * @param endTime
//     * @return
//     */
//    public Map<String, List> getCenterData(Date startTime, Date endTime) {
//        Long startDateTime = startTime.getTime();
//        Long endDateTime = endTime.getTime();
//
//        // 封装列表头信息
//        List headList = new ArrayList();
//        Map<String,String> timeHeadMap = new HashMap<>();
//        timeHeadMap.put("field", "time");
//        timeHeadMap.put("label", "时间");
//        headList.add(timeHeadMap);
//        Map<String,String> dqHeadMap = new HashMap<>();
//        dqHeadMap.put("field", "dq");
//        dqHeadMap.put("label", "短期(MW)");
//        headList.add(dqHeadMap);
//        Map<String,String> cdqHeadMap = new HashMap<>();
//        cdqHeadMap.put("field", "cdq");
//        cdqHeadMap.put("label", "超短期(MW)");
//        headList.add(cdqHeadMap);
//
//        // 获取短期数据SYJY
//        QueryWrapper<ForecastPowerShortTermCloud> wrapper = new QueryWrapper<>();
//        wrapper.eq("forecast_manufactor","SYJY");
//        wrapper.between("forecast_time", startTime, endTime);
//        List<ForecastPowerShortTermCloud> forecastPowerShortTermCloudList = forecastPowerShortTermService.list(wrapper);
////        Map<Long, List<ForecastPowerShortTermCloud>> shortGroupMap = forecastPowerShortTermCloudList.stream().collect(Collectors.groupingBy(s->s.getForecastTime().getTime()));
//
//        // 获取超短期数据SYJY
//        QueryWrapper<ForecastPowerUltraShortTermCloud> ultrawrapper = new QueryWrapper<>();
//        ultrawrapper.eq("forecast_manufactor","SYJY");
//        ultrawrapper.between("forecast_time", startTime, endTime);
//        List<ForecastPowerUltraShortTermCloud> forecastPowerUltraShortTermCloudList = forecastPowerUltraShortTermService.list(ultrawrapper);
////        Map<Long, List<ForecastPowerUltraShortTermCloud>> ultraGroupMap = forecastPowerUltraShortTermCloudList.stream().collect(Collectors.groupingBy(s->s.getForecastTime().getTime()));
//
//        List<String> timeList = new ArrayList<>();
//        List<String> dqList = new ArrayList();
//        List<String> cdqList = new ArrayList();
//
//        List<Map<String,Object>> tableList = new ArrayList();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        int i=0;
//        for (Long tempDateTime = startDateTime; tempDateTime < endDateTime; tempDateTime = tempDateTime + 15 * 60 * 1000) {
//            // 封装时间轴list
//            String time = format.format(tempDateTime);
//            timeList.add(time);
//            // 封装列表的数据
//            Map<String,Object> map1 = new HashMap<>();
//            map1.put("time", DateUtil.format(new Date(tempDateTime), "yyyy-MM-dd HH:mm"));
//            // 短期数据求和
//            BigDecimal dqBigDecimal = new BigDecimal(0);
//            if (shortGroupMap.get(tempDateTime)!=null){
//                List<ForecastPowerShortTermCloud> forecastPowerShortTermCloudList1 = shortGroupMap.get(tempDateTime);
//                for (ForecastPowerShortTermCloud forecastPowerShortTermCloud : forecastPowerShortTermCloudList1){
//                    dqBigDecimal = dqBigDecimal.add(forecastPowerShortTermCloud.getFpValue());
//                }
//                map1.put("dq",dqBigDecimal.toString());
//            }
//            else{
//                map1.put("dq",0);
//            }
//            dqList.add(dqBigDecimal.toString());
//
//            // 超短期数据求和
//            BigDecimal cdqBigDecimal = new BigDecimal(0);
//            if (ultraGroupMap.get(tempDateTime)!=null){
//                List<ForecastPowerUltraShortTermCloud> forecastPowerUltraShortTermCloudList1 = ultraGroupMap.get(tempDateTime);
//                for (ForecastPowerUltraShortTermCloud forecastPowerUltraShortTermCloud : forecastPowerUltraShortTermCloudList1){
//                    cdqBigDecimal = cdqBigDecimal.add(forecastPowerUltraShortTermCloud.getFpValue());
//                }
//                map1.put("cdq",cdqBigDecimal.toString());
//            }
//            else{
//                map1.put("cdq",0);
//            }
//            cdqList.add(cdqBigDecimal.toString());
//
//            tableList.add(map1);
//        }
//
//        Map<String, List> map = new HashMap<>();
//        map.put("dq", dqList);
//        map.put("cdq", cdqList);
//        map.put("time", timeList);
//        map.put("fromHead", headList);
//        map.put("tableList", tableList);
//
//        return map;
//    }
    @Override
    public List<PowerStationStatusData> findByTimeBetween(Date starTime, Date endTime) {
        QueryWrapper<PowerStationStatusData> wrapper = new QueryWrapper<>();

        if (starTime != null && endTime != null) {
            wrapper.between("time", starTime, endTime);
        }
        return baseMapper.selectList(wrapper);
    }


    /**
     * 中心侧数据查询
     *
     * @param startTime
     * @param endTime
     * @param forecastHowLongAgo
     * @param stationCode
     * @return
     */
    public Map<String, Object> cloudDataQuery(Date startTime, Date endTime, Integer forecastHowLongAgo, String stationCode) {
        Map<String, Object> map = new HashMap<>();
        //表数据
        map.put("tableData", new ArrayList<Map<String, Object>>());

        Map<String, Map<Date, List<ForecastPowerShortTermCloud>>> forecastPowerShortTermCloudModelTimeMap = forecastPowerShortTermCloudService.findByForecastTimeBetweenAndHowLongAgoAndStationCode(startTime, endTime, forecastHowLongAgo, stationCode).stream().collect(Collectors.groupingBy(ForecastPowerShortTermCloud::getForecastModel, Collectors.groupingBy(ForecastPowerShortTermCloud::getTime, Collectors.toList())));
        Map<Date, List<ForecastPowerShortTermStation>> forecastPowerShortTermStationTimeMap = forecastPowerShortTermStationService.findByForecastTimeBetweenAndForecastHowLongAgoAndStationCode(startTime, endTime, forecastHowLongAgo, stationCode).stream().collect(Collectors.groupingBy(ForecastPowerShortTermStation::getTime, Collectors.toList()));

        Map<Date, List<PowerStationStatusData>> powerStationStatusDataTimeMap = findByTimeBetweenAndStationCode(startTime, endTime, stationCode).stream().collect(Collectors.groupingBy(PowerStationStatusData::getTime, Collectors.toList()));

        ElectricField electricField = electricFieldService.findByStationCode(stationCode);
        String electricFieldType = electricField.getElectricFieldTypeEnum();
        List<TableFiled> tableFields = new ArrayList<>();

        tableFields.addAll(Arrays.asList(new TableFiled("时间", "time"), new TableFiled("实际功率", "realPower"), new TableFiled("可用功率", "ablePower"), new TableFiled("理论功率", "theoryPower"),new TableFiled("站端", "fpValue")));

        Map<String, String> forecastModelMap = new HashMap<>();

        for (SysDictData sysDictData : DictUtils.getDictCache("forecast_model")) {
            forecastModelMap.put(sysDictData.getDictLabel(), sysDictData.getDictValue());
            tableFields.add(new TableFiled(sysDictData.getDictLabel(), sysDictData.getDictValue()));
        }

        Map<Date, List<BaseCppEntity>> enStatusDataMap;
        if (electricFieldType.equals("E1")) {
            tableFields.add(new TableFiled("总辐射", "globalR"));
            enStatusDataMap = weatherStationStatusDataService.findByBetweenTimeAndStationCode(startTime, endTime, stationCode).stream().collect(Collectors.groupingBy(WeatherStationStatusData::getTime, Collectors.toList()));
        } else {
            tableFields.add(new TableFiled("轮毂风速", "wsHubHeight"));
            enStatusDataMap = windTowerStatusDataService.findByBetweenTimeAndStationCode(startTime, endTime, stationCode).stream().collect(Collectors.groupingBy(WindTowerStatusData::getTime, Collectors.toList()));
        }


        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("tableData");

        for (long l = startTime.getTime(); l < endTime.getTime(); l += 900000L) {
            Map<String, Object> dataMap = new HashMap<>();
            Date time = new Date(l);
            dataMap.put("time", DateFormatUtils.format(time,"yyyy-MM-dd HH:mm"));
            PowerStationStatusData powerStationStatusData = Optional.ofNullable(powerStationStatusDataTimeMap.containsKey(time) ? powerStationStatusDataTimeMap.get(time).get(0) : null).orElse(new PowerStationStatusData());
            dataMap.put("realPower", powerStationStatusData.getRealValue());
            dataMap.put("ablePower", powerStationStatusData.getAbleValue());
            dataMap.put("theoryPower", powerStationStatusData.getTheoryValue());

            if (electricFieldType.equals("E1")) {
                WeatherStationStatusData weatherStationStatusData = Optional.ofNullable(enStatusDataMap.containsKey(time) ? (WeatherStationStatusData) enStatusDataMap.get(time).get(0) : null).orElse(new WeatherStationStatusData());
                dataMap.put("globalR", weatherStationStatusData.getGlobalR());

            } else {
                WindTowerStatusData windTowerStatusData = Optional.ofNullable(enStatusDataMap.containsKey(time) ? (WindTowerStatusData) enStatusDataMap.get(time).get(0) : null).orElse(new WindTowerStatusData());
                dataMap.put("wsHubHeight", windTowerStatusData.getWsHubHeight());
            }

            ForecastPowerShortTermStation forecastPowerShortTermStation = Optional.ofNullable(forecastPowerShortTermStationTimeMap.containsKey(time) ?  forecastPowerShortTermStationTimeMap.get(time).get(0) : null).orElse(new ForecastPowerShortTermStation());
            dataMap.put("fpValue", forecastPowerShortTermStation.getFpValue());


            for (Map.Entry<String, String> stringStringEntry : forecastModelMap.entrySet()) {
                ForecastPowerShortTermCloud forecastPowerShortTermCloud = new ForecastPowerShortTermCloud();
                if (forecastPowerShortTermCloudModelTimeMap.containsKey(stringStringEntry.getValue())) {
                    Map<Date, List<ForecastPowerShortTermCloud>> dateListMap = forecastPowerShortTermCloudModelTimeMap.get(stringStringEntry.getValue());
                    forecastPowerShortTermCloud = Optional.ofNullable(dateListMap.containsKey(time) ? dateListMap.get(time).get(0) : null).orElse(new ForecastPowerShortTermCloud());
                }
                dataMap.put(stringStringEntry.getValue(), forecastPowerShortTermCloud.getFpValue());
            }


            list.add(dataMap);
        }

        //表头
        map.put("tableField", tableFields);
        return map;
    }


    @Data
    public class TableFiled {
        private String title;
        private String field;

        public TableFiled(String title, String field) {
            this.title = title;
            this.field = field;
        }
    }

}
