package com.cpp.web.controller.regulation;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpp.common.core.domain.R;
import com.cpp.common.core.domain.entity.SysUser;
import com.cpp.common.core.domain.model.LoginUser;
import com.cpp.common.utils.DateUtils;
import com.cpp.common.utils.SecurityUtils;
import com.cpp.system.service.ISysConfigService;
import com.cpp.system.service.ISysUserService;
import com.cpp.web.domain.accuracy.AccuracyPassRate;
import com.cpp.web.domain.cloud.ForecastPowerShortTermCloud;
import com.cpp.web.domain.enums.DataSourcesEnum;
import com.cpp.web.domain.enums.ForecastTypeEnum;
import com.cpp.web.domain.enums.RegulationStatusEnum;
import com.cpp.web.domain.enums.UploadStatusEnum;
import com.cpp.web.domain.regulation.TempShortRegulation;
import com.cpp.web.domain.regulation.TempShortRegulationDetail;
import com.cpp.web.domain.regulation.TempShortUsual;
import com.cpp.web.domain.regulation.TempShortUsualDetail;
import com.cpp.web.domain.station.ElectricField;
import com.cpp.web.domain.station.ForecastPowerShortTermRegulation;
import com.cpp.web.domain.station.ForecastPowerShortTermStation;
import com.cpp.web.domain.station.InverterStatusData;
import com.cpp.web.dto.TempShortRegulationDto;
import com.cpp.web.service.accuracy.AccuracyPassRateService;
import com.cpp.web.service.cloud.ForecastPowerShortTermCloudService;
import com.cpp.web.service.regulation.TempShortRegulationDetailService;
import com.cpp.web.service.regulation.TempShortRegulationService;
import com.cpp.web.service.regulation.TempShortUsualDetailService;
import com.cpp.web.service.regulation.TempShortUsualService;
import com.cpp.web.service.station.ElectricFieldService;
import com.cpp.web.service.station.ForecastPowerShortTermRegulationService;
import com.cpp.web.service.station.ForecastPowerShortTermStationService;
import com.cpp.web.utils.DateTimeUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * 短期调控
 *
 * @author whc
 * @date 2022-03-18 15:49:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dqRegulationController")
public class DqRegulationController {
    @Autowired
    ForecastPowerShortTermStationService forecastPowerShortTermStationService;
    @Autowired
    AccuracyPassRateService accuracyPassRateService;
    @Autowired
    TempShortRegulationService tempShortRegulationService;
    @Autowired
    TempShortRegulationDetailService tempShortRegulationDetailService;
    @Autowired
    ElectricFieldService electricFieldService;
    @Autowired
    TempShortUsualService tempShortUsualService;
    @Autowired
    TempShortUsualDetailService tempShortUsualDetailService;
    @Autowired
    ISysConfigService configService;
    @Autowired
    ISysUserService iSysUserService;
    @Autowired
    ForecastPowerShortTermRegulationService forecastPowerShortTermRegulationService;
    @Autowired
    ForecastPowerShortTermCloudService forecastPowerShortTermCloudService;



    @GetMapping("/queryData")
    public R queryData(String stationCode, Long time) {
        int howLongAgo = DateTimeUtil.getDaysBetweenTwoDate(System.currentTimeMillis(),time);
        // 获取要调整的短期那天的最后时间
        long endTime = DateTimeUtil.getDayLastTime(time).getTime();

        // 获取短期上报数据(原始数据)
        Map<String, Object> map = new HashMap<>();
        List<ForecastPowerShortTermStation> forecastPowerShortTermStationList = forecastPowerShortTermStationService.findByForecastTimeBetweenAndForecastHowLongAgoAndStationCode(new Date(time), new Date(endTime), howLongAgo, stationCode);
        if (forecastPowerShortTermStationList.size()>0){
            Map<Long, BigDecimal> ysShortMap = new HashMap<>();
            for (ForecastPowerShortTermStation forecastPowerShortTermStation:forecastPowerShortTermStationList) {
                ysShortMap.putIfAbsent(forecastPowerShortTermStation.getTime().getTime(), forecastPowerShortTermStation.getFpValue());
            }
            // 获取前30天的准确率生成参考值
            Date accuracyEndDate = DateUtil.offsetDay(new Date(), -1);
            Date accuracyStartDate = DateUtil.offsetDay(new Date(), -30);

            List<AccuracyPassRate> accuracyPassRateList = accuracyPassRateService.findByTimeBetweenAndForecastTypeAndDataSourcesAndAgoAndForecastModelAndStationCode(DateTimeUtil.getDayStartTime(accuracyStartDate.getTime()), DateTimeUtil.getDayLastTime(accuracyEndDate.getTime()), ForecastTypeEnum.dq, DataSourcesEnum.E1, howLongAgo, null, stationCode);
            double accuracySum = 0;
            if (accuracyPassRateList.size()>0){
                for (AccuracyPassRate accuracyPassRate:accuracyPassRateList){
                    accuracySum = accuracySum + Double.parseDouble(accuracyPassRate.getAccuracy().replaceAll("%", ""));
                }
                // 计算获取偏差率
                accuracySum = 100 - accuracySum / accuracyPassRateList.size();
            }
            // 获取调控数据
            QueryWrapper<TempShortRegulationDetail> tempShortRegulationDetailQueryWrapper = new QueryWrapper<>();
            tempShortRegulationDetailQueryWrapper.eq("station_code", stationCode);
            tempShortRegulationDetailQueryWrapper.eq("tk_date", DateTimeUtil.getDayStartTime(System.currentTimeMillis()));
            tempShortRegulationDetailQueryWrapper.eq("forecast_date", new Date(time));
            tempShortRegulationDetailQueryWrapper.between("time", new Date(time), new Date(endTime));
            List<TempShortRegulationDetail> tempShortRegulationDetailList = tempShortRegulationDetailService.list(tempShortRegulationDetailQueryWrapper);
            List<TempShortRegulationDto> tempList = new ArrayList<>();
            if (tempShortRegulationDetailList.size()==96){
                for (TempShortRegulationDetail tempShortRegulationDetail:tempShortRegulationDetailList){
                    TempShortRegulationDto tempShortRegulationDto = new TempShortRegulationDto();
                    tempShortRegulationDto.setTime(DateUtils.parseDateToStr("HH:mm",tempShortRegulationDetail.getTime()));
                    tempShortRegulationDto.setForecastDate(tempShortRegulationDetail.getForecastDate());
                    tempShortRegulationDto.setTkDate(tempShortRegulationDetail.getTkDate());
                    tempShortRegulationDto.setYsValue(tempShortRegulationDetail.getYsValue());
                    tempShortRegulationDto.setTkValue(tempShortRegulationDetail.getTkValue());
                    tempShortRegulationDto.setSz(tempShortRegulationDetail.getSz());
                    tempShortRegulationDto.setXs(tempShortRegulationDetail.getXs());
                    tempShortRegulationDto.setStationCode(stationCode);
                    tempList.add(tempShortRegulationDto);
                }
            }
            Map<Long, BigDecimal> tkShortMap = new HashMap<>();
            for (TempShortRegulationDetail tempShortRegulationDetail:tempShortRegulationDetailList) {
                tkShortMap.putIfAbsent(tempShortRegulationDetail.getTime().getTime(), tempShortRegulationDetail.getTkValue());
            }
            // 调控数据
            List<List<Object>> tkDataList = new ArrayList<>();
            // 原始短期数据
            List<BigDecimal> ysList = new ArrayList<>();
            // 参考上限短期
            List<BigDecimal> refUpList = new ArrayList<>();
            // 参考下限短期
            List<BigDecimal> refDownList = new ArrayList<>();
            ElectricField electricField = electricFieldService.findByStationCode(stationCode);
            Long momentTime = 15 * 60 * 1000L;
            for (Long tempTime = time; tempTime <= endTime; tempTime = tempTime + momentTime) {
                List<Object> listAfter = new ArrayList<>();
                listAfter.add(DateUtils.parseDateToStr("HH:mm",new Date(tempTime)));
                if (ysShortMap.get(tempTime)!=null){
                    ysList.add(ysShortMap.get(tempTime));
                    if (accuracySum>0){
                        // 计算出偏差量
                        BigDecimal baseValue = ysShortMap.get(tempTime).multiply(new BigDecimal(accuracySum).multiply(new BigDecimal("0.01"))).setScale(2, BigDecimal.ROUND_HALF_UP);
                        // 在原始数据基础上，生成上下限值。
                        BigDecimal sx = ysShortMap.get(tempTime).add(baseValue).setScale(2, BigDecimal.ROUND_HALF_UP);
                        if (sx.compareTo(electricField.getCapacity())==1){
                            sx = electricField.getCapacity();
                        }
                        BigDecimal xx = ysShortMap.get(tempTime).subtract(baseValue).setScale(2, BigDecimal.ROUND_HALF_UP);
                        if (xx.compareTo(new BigDecimal("0"))==-1){
                            xx = new BigDecimal("0");
                        }
                        refUpList.add(sx);
                        refDownList.add(xx);
                    }


                    if (tempShortRegulationDetailList.isEmpty()){
                        // 使用原始值
                        TempShortRegulationDto tempShortRegulationDto = new TempShortRegulationDto();
                        tempShortRegulationDto.setTime(DateUtils.parseDateToStr("HH:mm",new Date(tempTime)));
                        tempShortRegulationDto.setForecastDate(new Date(time));
                        tempShortRegulationDto.setTkDate(new Date());
                        tempShortRegulationDto.setYsValue(ysShortMap.get(tempTime));
                        tempShortRegulationDto.setTkValue(ysShortMap.get(tempTime));
                        tempShortRegulationDto.setSz(new BigDecimal("0"));
                        tempShortRegulationDto.setXs(new BigDecimal("1"));
                        tempShortRegulationDto.setStationCode(stationCode);
                        tempList.add(tempShortRegulationDto);
                        // 调控曲线使用原始值
                        listAfter.add(ysShortMap.get(tempTime));
                    }
                    else{
                        // 封装调控值
                        listAfter.add(tkShortMap.get(tempTime));
                    }
                }
                else{
                    ysList.add(null);
                    refUpList.add(null);
                    refDownList.add(null);
                    listAfter.add(null);
                }
                tkDataList.add(listAfter);
            }
            // 原始值
            map.put("ysData",ysList);
            // 参考上限
            map.put("refUpData",refUpList);
            // 参考下限
            map.put("refDownData",refDownList);
            // 页面列表
            map.put("tempShortRegulationList",tempList);
            // 页面调控曲线数值
            map.put("tkDataList",tkDataList);


            map.put("electricField",electricField);

            return R.ok(map);
        }
        else{
            String timeStr = DateUtil.format(new Date(time),"yyyy-MM-dd");
            return R.fail("调控日期："+timeStr+"，今日无上报数据！");
        }
    }
    @PostMapping("/updateDqRegulation")
    public R updateDqRegulation(@RequestBody List<TempShortRegulationDto> tempShortRegulationDtoList) {
        // 先删除调控明细表中已有的数据
        TempShortRegulationDto tempShortRegulationDto = tempShortRegulationDtoList.get(0);
        // 调控的预测日期
        Date forecastDate = tempShortRegulationDto.getForecastDate();
        Date startTime = DateTimeUtil.getDayStartTime(forecastDate.getTime());
        Date endTime = DateTimeUtil.getDayLastTime(forecastDate.getTime());
        QueryWrapper<TempShortRegulationDetail> tempShortRegulationDetailQueryWrapper = new QueryWrapper<>();
        tempShortRegulationDetailQueryWrapper.eq("station_code", tempShortRegulationDto.getStationCode());
        tempShortRegulationDetailQueryWrapper.eq("forecast_date", tempShortRegulationDto.getForecastDate());
        tempShortRegulationDetailQueryWrapper.eq("tk_date", DateTimeUtil.getDayStartTime(System.currentTimeMillis()));
        tempShortRegulationDetailQueryWrapper.between("time", startTime, endTime);
        tempShortRegulationDetailService.remove(tempShortRegulationDetailQueryWrapper);
        // 删除调控主表
        QueryWrapper<TempShortRegulation> tempShortRegulationQueryWrapper = new QueryWrapper<>();
        tempShortRegulationQueryWrapper.eq("station_code", tempShortRegulationDto.getStationCode());
        tempShortRegulationQueryWrapper.eq("forecast_date", tempShortRegulationDto.getForecastDate());
        tempShortRegulationQueryWrapper.eq("tk_date", DateTimeUtil.getDayStartTime(System.currentTimeMillis()));
        tempShortRegulationService.remove(tempShortRegulationQueryWrapper);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 插入调控主表
        TempShortRegulation tempShortRegulation = new TempShortRegulation();
        tempShortRegulation.setStationCode(tempShortRegulationDto.getStationCode());
        tempShortRegulation.setForecastDate(tempShortRegulationDto.getForecastDate());
        tempShortRegulation.setTkDate(tempShortRegulationDto.getTkDate());
        tempShortRegulation.setCreateBy(loginUser.getUsername());
        // 设置未下发
        tempShortRegulation.setRegulationStatusEnum(RegulationStatusEnum.E2.name());
        // 设置未回传
        tempShortRegulation.setUploadStatusEnum(UploadStatusEnum.E3.name());
        tempShortRegulationService.save(tempShortRegulation);

        // 封装新的调控明细插入数据
        List<TempShortRegulationDetail> tempShortRegulationDetailList = new ArrayList<>();
        for (TempShortRegulationDto tempShortRegulationDto1 : tempShortRegulationDtoList) {
            TempShortRegulationDetail tempShortRegulationDetail = new TempShortRegulationDetail();
            tempShortRegulationDetail.setStationCode(tempShortRegulationDto1.getStationCode());
            tempShortRegulationDetail.setForecastDate(tempShortRegulationDto1.getForecastDate());
            tempShortRegulationDetail.setTkDate(tempShortRegulationDto1.getTkDate());
            Date time = DateUtil.parse(DateUtil.format(tempShortRegulationDto1.getForecastDate(),"yyyy-MM-dd")+" "+tempShortRegulationDto1.getTime(), "yyyy-MM-dd HH:mm");
            tempShortRegulationDetail.setTime(time);
            tempShortRegulationDetail.setSz(tempShortRegulationDto1.getSz());
            tempShortRegulationDetail.setXs(tempShortRegulationDto1.getXs());
            tempShortRegulationDetail.setTkValue(tempShortRegulationDto1.getTkValue());
            tempShortRegulationDetail.setYsValue(tempShortRegulationDto1.getYsValue());
            tempShortRegulationDetail.setCreateBy(loginUser.getUsername());
            tempShortRegulationDetail.setRegulationId(tempShortRegulation.getId());
            tempShortRegulationDetailList.add(tempShortRegulationDetail);
        }
        tempShortRegulationDetailService.saveBatch(tempShortRegulationDetailList);


        return R.ok();
    }

    /**
     * 试算数据
     * @param stationCode
     * @param time
     * @param usualName 常规策略名称
     * @return
     */

    @GetMapping("/trialData")
    public R trialData(String stationCode, Long time,String usualName,String selectRadio) {
        ElectricField electricField = electricFieldService.findByStationCode(stationCode);
        int howLongAgo = DateTimeUtil.getDaysBetweenTwoDate(System.currentTimeMillis(),time);
        // 获取要调整的短期那天的最后时间
        long endTime = DateTimeUtil.getDayLastTime(time).getTime();
        List<TempShortRegulationDto> trialList = new ArrayList<>();
        Map<Long, BigDecimal> ysShortMap = new HashMap<>();

        // 获取短期上报数据(原始数据)
        List<ForecastPowerShortTermStation> forecastPowerShortTermStationList = forecastPowerShortTermStationService.findByForecastTimeBetweenAndForecastHowLongAgoAndStationCode(new Date(time), new Date(endTime), howLongAgo, stationCode);
        for (ForecastPowerShortTermStation forecastPowerShortTermStation:forecastPowerShortTermStationList) {
            ysShortMap.putIfAbsent(forecastPowerShortTermStation.getTime().getTime(), forecastPowerShortTermStation.getFpValue());
        }

        // 判断前端选择的策略是常用还是中心模型
        if ("3".equals(selectRadio)){
            Map<Long, BigDecimal> forecastPowerShortCloudMap = new HashMap();
            // 获取中心模型数据
            List<ForecastPowerShortTermCloud> forecastPowerShortTermCloudList = forecastPowerShortTermCloudService.findByForecastTimeBetweenAndHowLongAgoAndStationCode(new Date(time), new Date(endTime), howLongAgo, stationCode);
            if (forecastPowerShortTermCloudList.size()>0){
                for (ForecastPowerShortTermCloud forecastPowerShortTermCloud:forecastPowerShortTermCloudList) {
                    forecastPowerShortCloudMap.putIfAbsent(forecastPowerShortTermCloud.getTime().getTime(), forecastPowerShortTermCloud.getFpValue());
                }
                Long momentTime = 15 * 60 * 1000L;
                for (Long tempTime = time; tempTime <= endTime; tempTime = tempTime + momentTime) {
                    String timeHHmm = DateUtils.parseDateToStr("HH:mm",new Date(tempTime));
                    TempShortRegulationDto tempShortRegulationDto = new TempShortRegulationDto();
                    tempShortRegulationDto.setTime(timeHHmm);
                    tempShortRegulationDto.setForecastDate(new Date(time));
                    tempShortRegulationDto.setTkDate(new Date());
                    tempShortRegulationDto.setYsValue(ysShortMap.get(tempTime));
                    tempShortRegulationDto.setSz(new BigDecimal("0"));
                    tempShortRegulationDto.setXs(new BigDecimal("1"));
                    BigDecimal tkValue = forecastPowerShortCloudMap.get(tempTime);
                    // 判断调控值小于0，则调控值为0，超出装机容量按装机处理
                    if (tkValue.compareTo(new BigDecimal("0")) < 0) {
                        tkValue = new BigDecimal("0");
                    } else if (tkValue.compareTo(electricField.getCapacity()) > 0) {
                        tkValue = electricField.getCapacity();
                    }
                    tempShortRegulationDto.setTkValue(tkValue);
                    tempShortRegulationDto.setStationCode(stationCode);
                    trialList.add(tempShortRegulationDto);
                }
            }
        }
        else{
            // 先获取常规策略
            QueryWrapper<TempShortUsual> tempShortUsualQueryWrapper = new QueryWrapper<>();
            tempShortUsualQueryWrapper.eq("usual_name", usualName);
            TempShortUsual tempShortUsual = tempShortUsualService.getOne(tempShortUsualQueryWrapper);
            QueryWrapper<TempShortUsualDetail> tempShortUsualDetailQueryWrapper = new QueryWrapper<>();
            tempShortUsualDetailQueryWrapper.eq("usual_id", tempShortUsual.getId());
            List<TempShortUsualDetail> tempShortUsualDetailList = tempShortUsualDetailService.list(tempShortUsualDetailQueryWrapper);
            Map<String,TempShortUsualDetail> usualDetailMap = new HashMap<>();
            for (TempShortUsualDetail tempShortUsualDetail:tempShortUsualDetailList){
                usualDetailMap.put(tempShortUsualDetail.getTimePoint(),tempShortUsualDetail);
            }

            Long momentTime = 15 * 60 * 1000L;
            for (Long tempTime = time; tempTime <= endTime; tempTime = tempTime + momentTime) {
                String timeHHmm = DateUtils.parseDateToStr("HH:mm",new Date(tempTime));
                TempShortRegulationDto tempShortRegulationDto = new TempShortRegulationDto();
                tempShortRegulationDto.setTime(timeHHmm);
                tempShortRegulationDto.setForecastDate(new Date(time));
                tempShortRegulationDto.setTkDate(new Date());
                tempShortRegulationDto.setYsValue(ysShortMap.get(tempTime));
                TempShortUsualDetail tempShortUsualDetail = usualDetailMap.get(timeHHmm);
                tempShortRegulationDto.setSz(tempShortUsualDetail.getSz());
                tempShortRegulationDto.setXs(tempShortUsualDetail.getXs());
                // 计算调控值，调控值=原始值*系数+数值
                BigDecimal tkValue = ysShortMap.get(tempTime).multiply(tempShortUsualDetail.getXs()).add(tempShortUsualDetail.getSz()).setScale(2, BigDecimal.ROUND_HALF_UP);
                // 判断调控值小于0，则调控值为0，超出装机容量按装机处理
                if (tkValue.compareTo(new BigDecimal("0")) < 0) {
                    tkValue = new BigDecimal("0");
                } else if (tkValue.compareTo(electricField.getCapacity()) > 0) {
                    tkValue = electricField.getCapacity();
                }

                tempShortRegulationDto.setTkValue(tkValue);
                tempShortRegulationDto.setStationCode(stationCode);
                trialList.add(tempShortRegulationDto);
            }
        }

        return R.ok(trialList);
    }
    /**
     * 获取短期上报截止时间
     * @return
     */

    @GetMapping("/getDqEndTime")
    public R getDqEndTime() {
        String dqEndTime = configService.selectConfigByKey("dqEndTime");
        String[] times = dqEndTime.trim().split(":");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(times[1]));
        calendar.set(Calendar.SECOND, 0);
        return R.ok(DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 短期调控记录列表分页
     *
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/getDqRegulationRecordList")
    public R getDqRegulationRecordList(String stationCode, Long startTime,Long endTime) {
        QueryWrapper<TempShortRegulation> tempShortRegulationQueryWrapper = new QueryWrapper<>();
        String[] stationCodeSplit = stationCode.split(",");
        tempShortRegulationQueryWrapper.in("station_code", stationCodeSplit);
        tempShortRegulationQueryWrapper.between("tk_date", new Date(startTime), new Date(endTime));
        tempShortRegulationQueryWrapper.orderByAsc("tk_date").orderByAsc("station_code").orderByAsc("forecast_date");
        List<TempShortRegulation> tempShortRegulationList = tempShortRegulationService.list(tempShortRegulationQueryWrapper);
        if (tempShortRegulationList.size()>0){
            // 排序调控日期、调控场站、上报预测日期
            tempShortRegulationList.sort(Comparator.comparing(TempShortRegulation::getTkDate).thenComparing(TempShortRegulation::getStationCode).thenComparing(TempShortRegulation::getForecastDate));
        }
        List<Map<String, Object>> tableList = new ArrayList();
        List<SysUser> sysUserList = iSysUserService.findAll();
        // 封装列表数据
        for (TempShortRegulation tempShortRegulation : tempShortRegulationList) {
            Map<String, Object> map = new HashMap<>();
            map.put("tkDate", DateUtil.format(tempShortRegulation.getTkDate(), "yyyy-MM-dd"));
            map.put("stationCode", tempShortRegulation.getStationCode());
            map.put("forecastDate", DateUtil.format(tempShortRegulation.getForecastDate(), "yyyy-MM-dd"));
            map.put("regulationStatusEnum", RegulationStatusEnum.valueOf(tempShortRegulation.getRegulationStatusEnum()).getMessage());
            map.put("uploadStatusEnum", UploadStatusEnum.valueOf(tempShortRegulation.getUploadStatusEnum()).getMessage());
            // 根据账号获取昵称
            Optional<SysUser> sysUserOptional = sysUserList.stream().filter(sysUser -> sysUser.getUserName().equals(tempShortRegulation.getCreateBy())).findFirst();
            if (sysUserOptional.isPresent()) {
                map.put("createBy", sysUserOptional.get().getNickName());
            }
            else{
                map.put("createBy", tempShortRegulation.getCreateBy());
            }
            tableList.add(map);
        }
        return R.ok(tableList);
    }

    /**
     * 短期调控记录单行记录的明细参数及曲线图
     *
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/getDqRegulationDetail")
    public R getDqRegulationDetail(String stationCode, String tkDate,String forecastDate) {
        // 获取明细列表
        QueryWrapper<TempShortRegulationDetail> tempShortRegulationDetailQueryWrapper = new QueryWrapper<>();
        tempShortRegulationDetailQueryWrapper.eq("station_code", stationCode);
        tempShortRegulationDetailQueryWrapper.eq("tk_date", DateUtil.parse(tkDate, "yyyy-MM-dd"));
        tempShortRegulationDetailQueryWrapper.eq("forecast_date", DateUtil.parse(forecastDate, "yyyy-MM-dd"));
        List<TempShortRegulationDetail> tempShortRegulationDetailList = tempShortRegulationDetailService.list(tempShortRegulationDetailQueryWrapper);
        // 定义调控参数列表
        List<TempShortRegulationDto> detailList = new ArrayList<>();
        // 定义调控曲线数据
        List<BigDecimal> tkLineList = new ArrayList<>();
        // 定义原始曲线数据
        List<BigDecimal> ysLineList = new ArrayList<>();
        // 定义上报曲线数据
        List<BigDecimal> sbLineList = new ArrayList<>();
        if (tempShortRegulationDetailList.size()>0){
            // 先按预测时间排序
            tempShortRegulationDetailList.sort(Comparator.comparing(TempShortRegulationDetail::getTime));
            for (TempShortRegulationDetail tempShortRegulationDetail:tempShortRegulationDetailList){
                TempShortRegulationDto tempShortRegulationDto = new TempShortRegulationDto();
                tempShortRegulationDto.setTime(DateUtils.parseDateToStr("HH:mm",tempShortRegulationDetail.getTime()));
                tempShortRegulationDto.setForecastDate(tempShortRegulationDetail.getForecastDate());
                tempShortRegulationDto.setTkDate(tempShortRegulationDetail.getTkDate());
                tempShortRegulationDto.setYsValue(tempShortRegulationDetail.getYsValue());
                tempShortRegulationDto.setTkValue(tempShortRegulationDetail.getTkValue());
                tempShortRegulationDto.setSz(tempShortRegulationDetail.getSz());
                tempShortRegulationDto.setXs(tempShortRegulationDetail.getXs());
                tempShortRegulationDto.setStationCode(stationCode);
                detailList.add(tempShortRegulationDto);
                // 调控曲线数据封装
                tkLineList.add(tempShortRegulationDetail.getTkValue());
                // 原始曲线数据封装
                ysLineList.add(tempShortRegulationDetail.getYsValue());
            }
        }
        Map<String, Object> map = new HashMap<>();
        // 弹出框明细列表
        map.put("detailList",detailList);
        map.put("tkLineList",tkLineList);
        map.put("ysLineList",ysLineList);
        // 获取上报曲线数据
        long time = DateUtil.parse(forecastDate, "yyyy-MM-dd").getTime();
        Date startForecastTime = DateTimeUtil.getDayStartTime(time);
        Date endForecastTime = DateTimeUtil.getDayLastTime(time);
        int howLongAgo = DateTimeUtil.getDaysBetweenTwoDate(DateUtil.parse(tkDate, "yyyy-MM-dd").getTime(),startForecastTime.getTime());

        QueryWrapper<ForecastPowerShortTermRegulation> forecastPowerShortTermRegulationQueryWrapper = new QueryWrapper<>();
        forecastPowerShortTermRegulationQueryWrapper.eq("station_code", stationCode);
        forecastPowerShortTermRegulationQueryWrapper.between("time", startForecastTime,endForecastTime);
        forecastPowerShortTermRegulationQueryWrapper.eq("forecast_how_long_ago",howLongAgo);
        List<ForecastPowerShortTermRegulation> forecastPowerShortTermRegulationList = forecastPowerShortTermRegulationService.list(forecastPowerShortTermRegulationQueryWrapper);
        if (forecastPowerShortTermRegulationList.size()>0){
            forecastPowerShortTermRegulationList.sort(Comparator.comparing(ForecastPowerShortTermRegulation::getTime));
            for (ForecastPowerShortTermRegulation forecastPowerShortTermRegulation:forecastPowerShortTermRegulationList){
                sbLineList.add(forecastPowerShortTermRegulation.getFpValue());
            }
        }
        map.put("sbLineList",sbLineList);
        ElectricField electricField = electricFieldService.findByStationCode(stationCode);
        map.put("electricField",electricField);
        return R.ok(map);
    }
}
