package com.ruoyi.adapter.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.adapter.domain.TjDataFormat;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.DateTimeUtil;
import com.ruoyi.predict.api.ShareController;
import com.ruoyi.predict.domain.*;
import com.ruoyi.predict.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author hlf
 * @date 2022/11/7 10:03
 * 文件说明：图迹电量数据
 * 超短期电量预测(场站(风电)、期次(风电))
 * 短期电量预测(场站(风电)、期次(风电))
 * 中长期电量预测(场站(风电)、期次(风电))
 * 可用电量(场站(风电)、期次(风电))
 */
@Api(value = "图迹电量数据Controller", tags = {"图迹电量数据操作接口"})
@Controller
@RequestMapping("/adapter/api/tjedc")
public class TuJiEnergyDataController extends BaseController {

    @Autowired
    private IWfFileConfigService wfFileConfigService;

    @Autowired
    private IWfUspTsEfPeriodDataService wfUspTsEfPeriodDataService;

    @Autowired
    private IProjectEuspTsPeriodDataService projectEuspTsPeriodDataService;

    @Autowired
    private INoscaleWfDaEfPeriodDataService noscaleWfDaEfPeriodDataService;

    @Autowired
    private INoscalePrDaEfPeriodDataService noscalePrDaEfPeriodDataService;

    @Autowired
    private ILpWfMonthDataService lpWfMonthDataService;

    @Autowired
    private ILpWfYearDataService lpWfYearDataService;

    @Autowired
    private ILpProjectMonthDataService lpProjectMonthDataService;

    @Autowired
    private ILpProjectYearDataService lpProjectYearDataService;

    @Autowired
    private IWfTheoryenergyDataService wfTheoryenergyDataService;

    @Autowired
    private IPrTheoryenergyDataService prTheoryenergyDataService;

    @ApiOperation("超短期电量预测-场站")
    @GetMapping("/getTerminalCdqdlycData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "timeScale", value = "时间尺度", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getTerminalCdqdlycData(@RequestParam(value = "stationNumber") String stationNumber,
                                             @RequestParam(value = "itemVal") int itemVal,
                                             @RequestParam(value = "timeScale") int timeScale,
                                             @RequestParam(value = "beginDataTime") String beginDataTime,
                                             @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDateList10(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        Map<String, Object> params = new HashMap<>();
        params.put("beginDataTime", beginDataTime);
        params.put("endDataTime", endDataTime);
        if (itemVal == 0) {//风电场
            WfFileConfig wfFileConfig = wfFileConfigService.selectWfFileConfigById(Convert.toLong(stationNumber));
            if (ObjectUtil.isNotNull(wfFileConfig)) {
                WfUspTsEfPeriodData wfUspTsEfPeriodData = new WfUspTsEfPeriodData();
                wfUspTsEfPeriodData.setWindplantNo(Convert.toLong(stationNumber));
                wfUspTsEfPeriodData.setModelNo(wfFileConfig.getUwpfemodelNo());
                wfUspTsEfPeriodData.setTimeScale(Convert.toLong(timeScale));
                wfUspTsEfPeriodData.setParams(params);
                List<WfUspTsEfPeriodData> wfUspTsEfPeriodDataList = wfUspTsEfPeriodDataService.selectWfUspTsEfPeriodDataList(wfUspTsEfPeriodData);
                double modEnergyCount = 0;
                for (WfUspTsEfPeriodData w : wfUspTsEfPeriodDataList) {
                    String dataTime = DateUtil.format(w.getDataTime(), "yyyy-MM-dd HH:mm");
                    if ("00:10".equals(dataTime.substring(11, 16))) {
                        modEnergyCount = w.getModEnergy();
                    } else if (beginDataTime.equals(dataTime)) {
                        Map<String, Object> paramsF = new HashMap<>();
                        Date dTime;
                        if ("00:00".equals(dataTime.substring(11, 16))) {
                            dTime = DateUtil.date(DateUtil.offsetDay(w.getDataTime(), -1).getTime() + 600000);
                        } else {
                            dTime = DateUtil.date(DateUtil.beginOfDay(DateUtil.parse(dataTime)).getTime() + 600000);
                        }
                        paramsF.put("beginDataTime", DateUtil.formatDateTime(dTime));
                        paramsF.put("endDataTime", DateUtil.formatDateTime(DateUtil.parse(dataTime)));
                        WfUspTsEfPeriodData beforeData = new WfUspTsEfPeriodData();
                        beforeData.setWindplantNo(Convert.toLong(stationNumber));
                        beforeData.setModelNo(wfFileConfig.getUwpfemodelNo());
                        beforeData.setTimeScale(Convert.toLong(timeScale));
                        beforeData.setParams(paramsF);
                        modEnergyCount = wfUspTsEfPeriodDataService.selectWfUspTsEfPeriodDataList(beforeData).stream().mapToDouble(WfUspTsEfPeriodData::getModEnergy).sum();
                    } else {
                        modEnergyCount += w.getModEnergy();
                    }
                    ShareController.addMapValue(map, modEnergyCount, dataTime, 0, 1);
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.format(date, "yyyy-MM-dd HH:mm");
            TjDataFormat tjDataFormat = new TjDataFormat();
            tjDataFormat.setDataTime(stringDateTime);
            List<Double> entry = map.get(stringDateTime);
            if (entry != null) {
                if (entry.size() > 0) {
                    tjDataFormat.setData(entry.get(0));
                }
            }
            tjDataFormatList.add(tjDataFormat);
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    @ApiOperation("超短期电量预测-期次")
    @GetMapping("/getPeriodCdqdlycData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectNo", value = "期次编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "timeScale", value = "时间尺度", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getPeriodCdqdlycData(@RequestParam(value = "stationNumber") String stationNumber,
                                           @RequestParam(value = "projectNo") String projectNo,
                                           @RequestParam(value = "itemVal") int itemVal,
                                           @RequestParam(value = "timeScale") int timeScale,
                                           @RequestParam(value = "beginDataTime") String beginDataTime,
                                           @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDateList10(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        Map<String, Object> params = new HashMap<>();
        params.put("beginDataTime", beginDataTime);
        params.put("endDataTime", endDataTime);
        if (itemVal == 0) {//风电场
            ProjectEuspTsPeriodData projectEuspTsPeriodData = new ProjectEuspTsPeriodData();
            projectEuspTsPeriodData.setProjectNo(Convert.toLong(projectNo));
            projectEuspTsPeriodData.setModelNo((long) 7041);
            projectEuspTsPeriodData.setTimeScale(Convert.toLong(timeScale));
            projectEuspTsPeriodData.setParams(params);
            List<ProjectEuspTsPeriodData> projectEuspTsPeriodDataList = projectEuspTsPeriodDataService.selectProjectEuspTsPeriodDataList(projectEuspTsPeriodData);
            double modEnergyCount = 0;
            for (ProjectEuspTsPeriodData p : projectEuspTsPeriodDataList) {
                String dataTime = DateUtil.format(p.getDataTime(), "yyyy-MM-dd HH:mm");
                if ("00:10".equals(dataTime.substring(11, 16))) {
                    modEnergyCount = p.getModEnergy();
                } else if (beginDataTime.equals(dataTime)) {
                    Map<String, Object> paramsF = new HashMap<>();
                    Date dTime;
                    if ("00:00".equals(dataTime.substring(11, 16))) {
                        dTime = DateUtil.date(DateUtil.offsetDay(p.getDataTime(), -1).getTime() + 600000);
                    } else {
                        dTime = DateUtil.date(DateUtil.beginOfDay(DateUtil.parse(dataTime)).getTime() + 600000);
                    }
                    paramsF.put("beginDataTime", DateUtil.formatDateTime(dTime));
                    paramsF.put("endDataTime", DateUtil.formatDateTime(DateUtil.parse(dataTime)));
                    ProjectEuspTsPeriodData beforeData = new ProjectEuspTsPeriodData();
                    beforeData.setProjectNo(Convert.toLong(projectNo));
                    beforeData.setModelNo((long) 7041);
                    beforeData.setTimeScale(Convert.toLong(timeScale));
                    beforeData.setParams(paramsF);
                    modEnergyCount = projectEuspTsPeriodDataService.selectProjectEuspTsPeriodDataList(beforeData).stream().mapToDouble(ProjectEuspTsPeriodData::getModEnergy).sum();
                } else {
                    modEnergyCount += p.getModEnergy();
                }
                ShareController.addMapValue(map, modEnergyCount, dataTime, 0, 1);
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.format(date, "yyyy-MM-dd HH:mm");
            TjDataFormat tjDataFormat = new TjDataFormat();
            tjDataFormat.setDataTime(stringDateTime);
            List<Double> entry = map.get(stringDateTime);
            if (entry != null) {
                if (entry.size() > 0) {
                    tjDataFormat.setData(entry.get(0));
                }
            }
            tjDataFormatList.add(tjDataFormat);
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    @ApiOperation("短期电量预测-场站")
    @GetMapping("/getTerminalDqdlycData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getTerminalDqdlycData(@RequestParam(value = "stationNumber") String stationNumber,
                                            @RequestParam(value = "itemVal") int itemVal,
                                            @RequestParam(value = "beginDataTime") String beginDataTime,
                                            @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDateList10(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        Map<String, Object> params = new HashMap<>();
        params.put("beginDataTime", beginDataTime);
        params.put("endDataTime", endDataTime);
        if (itemVal == 0) {//风电场
            WfFileConfig wfFileConfig = wfFileConfigService.selectWfFileConfigById(Convert.toLong(stationNumber));
            if (ObjectUtil.isNotNull(wfFileConfig)) {
                NoscaleWfDaEfPeriodData noscaleWfDaEfPeriodData = new NoscaleWfDaEfPeriodData();
                noscaleWfDaEfPeriodData.setWindplantNo(Convert.toLong(stationNumber));
                noscaleWfDaEfPeriodData.setModelNo(wfFileConfig.getWpfemodelNo());
                noscaleWfDaEfPeriodData.setParams(params);
                List<NoscaleWfDaEfPeriodData> noscaleWfDaEfPeriodDataList = noscaleWfDaEfPeriodDataService.selectNoscaleWfDaEfPeriodDataList(noscaleWfDaEfPeriodData);
                double valueCount = 0;
                for (NoscaleWfDaEfPeriodData n : noscaleWfDaEfPeriodDataList) {
                    String dataTime = DateUtil.format(n.getDataTime(), "yyyy-MM-dd HH:mm");
                    if ("00:10".equals(dataTime.substring(11, 16))) {
                        valueCount = n.getValue();
                    } else if (beginDataTime.equals(dataTime)) {
                        Map<String, Object> paramsF = new HashMap<>();
                        Date dTime;
                        if ("00:00".equals(dataTime.substring(11, 16))) {
                            dTime = DateUtil.date(DateUtil.offsetDay(n.getDataTime(), -1).getTime() + 600000);
                        } else {
                            dTime = DateUtil.date(DateUtil.beginOfDay(DateUtil.parse(dataTime)).getTime() + 600000);
                        }
                        paramsF.put("beginDataTime", DateUtil.formatDateTime(dTime));
                        paramsF.put("endDataTime", DateUtil.formatDateTime(DateUtil.parse(dataTime)));
                        NoscaleWfDaEfPeriodData beforeData = new NoscaleWfDaEfPeriodData();
                        beforeData.setWindplantNo(Convert.toLong(stationNumber));
                        beforeData.setModelNo(wfFileConfig.getWpfemodelNo());
                        beforeData.setParams(paramsF);
                        valueCount = noscaleWfDaEfPeriodDataService.selectNoscaleWfDaEfPeriodDataList(beforeData).stream().mapToDouble(NoscaleWfDaEfPeriodData::getValue).sum();
                    } else {
                        valueCount += n.getValue();
                    }
                    ShareController.addMapValue(map, valueCount, dataTime, 0, 1);
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.format(date, "yyyy-MM-dd HH:mm");
            TjDataFormat tjDataFormat = new TjDataFormat();
            tjDataFormat.setDataTime(stringDateTime);
            List<Double> entry = map.get(stringDateTime);
            if (entry != null) {
                if (entry.size() > 0) {
                    tjDataFormat.setData(entry.get(0));
                }
            }
            tjDataFormatList.add(tjDataFormat);
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    @ApiOperation("短期电量预测-期次")
    @GetMapping("/getPeriodDqdlycData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectNo", value = "期次编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getPeriodDqdlycData(@RequestParam(value = "stationNumber") String stationNumber,
                                          @RequestParam(value = "projectNo") String projectNo,
                                          @RequestParam(value = "itemVal") int itemVal,
                                          @RequestParam(value = "beginDataTime") String beginDataTime,
                                          @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDateList10(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        Map<String, Object> params = new HashMap<>();
        params.put("beginDataTime", beginDataTime);
        params.put("endDataTime", endDataTime);
        if (itemVal == 0) {//风电场
            NoscalePrDaEfPeriodData noscalePrDaEfPeriodData = new NoscalePrDaEfPeriodData();
            noscalePrDaEfPeriodData.setWfPrNo(Convert.toLong(projectNo));
            noscalePrDaEfPeriodData.setModelNo((long) 7045);
            noscalePrDaEfPeriodData.setParams(params);
            List<NoscalePrDaEfPeriodData> noscalePrDaEfPeriodDataList = noscalePrDaEfPeriodDataService.selectNoscalePrDaEfPeriodDataList(noscalePrDaEfPeriodData);
            double valueCount = 0;
            for (NoscalePrDaEfPeriodData n : noscalePrDaEfPeriodDataList) {
                String dataTime = DateUtil.format(n.getDataTime(), "yyyy-MM-dd HH:mm");
                if ("00:10".equals(dataTime.substring(11, 16))) {
                    valueCount = n.getValue();
                } else if (beginDataTime.equals(dataTime)) {
                    Map<String, Object> paramsF = new HashMap<>();
                    Date dTime;
                    if ("00:00".equals(dataTime.substring(11, 16))) {
                        dTime = DateUtil.date(DateUtil.offsetDay(n.getDataTime(), -1).getTime() + 600000);
                    } else {
                        dTime = DateUtil.date(DateUtil.beginOfDay(DateUtil.parse(dataTime)).getTime() + 600000);
                    }
                    paramsF.put("beginDataTime", DateUtil.formatDateTime(dTime));
                    paramsF.put("endDataTime", DateUtil.formatDateTime(DateUtil.parse(dataTime)));
                    NoscalePrDaEfPeriodData beforeData = new NoscalePrDaEfPeriodData();
                    beforeData.setWfPrNo(Convert.toLong(projectNo));
                    beforeData.setModelNo((long) 7045);
                    beforeData.setParams(paramsF);
                    valueCount = noscalePrDaEfPeriodDataService.selectNoscalePrDaEfPeriodDataList(beforeData).stream().mapToDouble(NoscalePrDaEfPeriodData::getValue).sum();
                } else {
                    valueCount += n.getValue();
                }
                ShareController.addMapValue(map, valueCount, dataTime, 0, 1);
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.format(date, "yyyy-MM-dd HH:mm");
            TjDataFormat tjDataFormat = new TjDataFormat();
            tjDataFormat.setDataTime(stringDateTime);
            List<Double> entry = map.get(stringDateTime);
            if (entry != null) {
                if (entry.size() > 0) {
                    tjDataFormat.setData(entry.get(0));
                }
            }
            tjDataFormatList.add(tjDataFormat);
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    @ApiOperation("中长期电量预测-场站")
    @GetMapping("/getTerminalZcqdlycData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "predictType", value = "预测类型(1中期:日期传年-月 2长期：日期传年)", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getTerminalZcqdlycData(@RequestParam(value = "stationNumber") String stationNumber,
                                             @RequestParam(value = "itemVal") int itemVal,
                                             @RequestParam(value = "predictType") int predictType,
                                             @RequestParam(value = "beginDataTime") String beginDataTime,
                                             @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        List<Date> dateList = new ArrayList<>();
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        if (predictType == 1) {//中期
            //获取所有时间
            dateList.addAll(DateTimeUtil.getMonthBetween(DateUtil.parse(beginDataTime + "-01"), DateUtil.parse(endDataTime + "-01")));
            Map<String, Object> params = new HashMap<>();
            params.put("beginDataTime", beginDataTime + "-01 00:00:00");
            params.put("endDataTime", endDataTime + "-01 00:00:00");
            if (itemVal == 0) {//风电场
                LpWfMonthData lpWfMonthData = new LpWfMonthData();
                lpWfMonthData.setWfNo(Convert.toLong(stationNumber));
                lpWfMonthData.setParams(params);
                List<LpWfMonthData> lpWfMonthDataList = lpWfMonthDataService.selectLpWfMonthDataList(lpWfMonthData);
                if (lpWfMonthDataList.size() > 0) {
                    for (LpWfMonthData l : lpWfMonthDataList) {
                        ShareController.addMapValue(map, l.getPfGeneratingCapacity(), DateUtil.format(l.getDataTime(), "yyyy-MM"), 0, 1);
                    }
                }
            } else if (itemVal == 4) {//光伏电站
                //暂无 后补
            }
            for (Date date : dateList) {
                String stringDateTime = DateUtil.format(date, "yyyy-MM");
                TjDataFormat tjDataFormat = new TjDataFormat();
                tjDataFormat.setDataTime(stringDateTime);
                List<Double> entry = map.get(stringDateTime);
                if (entry != null) {
                    if (entry.size() > 0) {
                        tjDataFormat.setData(entry.get(0));
                    }
                }
                tjDataFormatList.add(tjDataFormat);
            }
        } else if (predictType == 2) {//长期
            //获取所有时间
            dateList.addAll(DateTimeUtil.getYearBetween(beginDataTime, endDataTime));
            Map<String, Object> params = new HashMap<>();
            params.put("beginDataTime", beginDataTime + "-01-01 00:00:00");
            params.put("endDataTime", endDataTime + "-01-01 00:00:00");
            if (itemVal == 0) {//风电场
                LpWfYearData lpWfYearData = new LpWfYearData();
                lpWfYearData.setWfNo(Convert.toLong(stationNumber));
                lpWfYearData.setParams(params);
                List<LpWfYearData> lpWfYearDataList = lpWfYearDataService.selectLpWfYearDataList(lpWfYearData);
                if (lpWfYearDataList.size() > 0) {
                    for (LpWfYearData l : lpWfYearDataList) {
                        ShareController.addMapValue(map, l.getPfGeneratingCapacity(), DateUtil.format(l.getDataTime(), "yyyy"), 0, 1);
                    }
                }
            } else if (itemVal == 4) {//光伏电站
                //暂无 后补
            }
            for (Date date : dateList) {
                String stringDateTime = DateUtil.format(date, "yyyy");
                TjDataFormat tjDataFormat = new TjDataFormat();
                tjDataFormat.setDataTime(stringDateTime);
                List<Double> entry = map.get(stringDateTime);
                if (entry != null) {
                    if (entry.size() > 0) {
                        tjDataFormat.setData(entry.get(0));
                    }
                }
                tjDataFormatList.add(tjDataFormat);
            }
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    @ApiOperation("中长期电量预测-期次")
    @GetMapping("/getPeriodZcqdlycData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectNo", value = "期次编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "predictType", value = "预测类型(1中期:日期传年-月 2长期：日期传年)", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getPeriodZcqdlycData(@RequestParam(value = "projectNo") String projectNo,
                                           @RequestParam(value = "itemVal") int itemVal,
                                           @RequestParam(value = "predictType") int predictType,
                                           @RequestParam(value = "beginDataTime") String beginDataTime,
                                           @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        List<Date> dateList = new ArrayList<>();
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        if (predictType == 1) {//中期
            //获取所有时间
            dateList.addAll(DateTimeUtil.getMonthBetween(DateUtil.parse(beginDataTime + "-01"), DateUtil.parse(endDataTime + "-01")));
            Map<String, Object> params = new HashMap<>();
            params.put("beginDataTime", beginDataTime + "-01 00:00:00");
            params.put("endDataTime", endDataTime + "-01 00:00:00");
            if (itemVal == 0) {//风电场
                LpProjectMonthData lpProjectMonthData = new LpProjectMonthData();
                lpProjectMonthData.setWfPrNo(Convert.toLong(projectNo));
                lpProjectMonthData.setParams(params);
                List<LpProjectMonthData> lpProjectMonthDataList = lpProjectMonthDataService.selectLpProjectMonthDataList(lpProjectMonthData);
                if (lpProjectMonthDataList.size() > 0) {
                    for (LpProjectMonthData l : lpProjectMonthDataList) {
                        ShareController.addMapValue(map, l.getPfGeneratingCapacity(), DateUtil.format(l.getDataTime(), "yyyy-MM"), 0, 1);
                    }
                }
            } else if (itemVal == 4) {//光伏电站
                //暂无 后补
            }
            for (Date date : dateList) {
                String stringDateTime = DateUtil.format(date, "yyyy-MM");
                TjDataFormat tjDataFormat = new TjDataFormat();
                tjDataFormat.setDataTime(stringDateTime);
                List<Double> entry = map.get(stringDateTime);
                if (entry != null) {
                    if (entry.size() > 0) {
                        tjDataFormat.setData(entry.get(0));
                    }
                }
                tjDataFormatList.add(tjDataFormat);
            }
        } else if (predictType == 2) {//长期
            //获取所有时间
            dateList.addAll(DateTimeUtil.getYearBetween(beginDataTime, endDataTime));
            Map<String, Object> params = new HashMap<>();
            params.put("beginDataTime", beginDataTime + "-01-01 00:00:00");
            params.put("endDataTime", endDataTime + "-01-01 00:00:00");
            if (itemVal == 0) {//风电场
                LpProjectYearData lpProjectYearData = new LpProjectYearData();
                lpProjectYearData.setWfPrNo(Convert.toLong(projectNo));
                lpProjectYearData.setParams(params);
                List<LpProjectYearData> lpProjectYearDataList = lpProjectYearDataService.selectLpProjectYearDataList(lpProjectYearData);
                if (lpProjectYearDataList.size() > 0) {
                    for (LpProjectYearData l : lpProjectYearDataList) {
                        ShareController.addMapValue(map, l.getPfGeneratingCapacity(), DateUtil.format(l.getDataTime(), "yyyy"), 0, 1);
                    }
                }
            } else if (itemVal == 4) {//光伏电站
                //暂无 后补
            }
            for (Date date : dateList) {
                String stringDateTime = DateUtil.format(date, "yyyy");
                TjDataFormat tjDataFormat = new TjDataFormat();
                tjDataFormat.setDataTime(stringDateTime);
                List<Double> entry = map.get(stringDateTime);
                if (entry != null) {
                    if (entry.size() > 0) {
                        tjDataFormat.setData(entry.get(0));
                    }
                }
                tjDataFormatList.add(tjDataFormat);
            }
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    @ApiOperation("可用电量-场站")
    @GetMapping("/getTerminalKydlData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getTerminalKydlData(@RequestParam(value = "stationNumber") String stationNumber,
                                          @RequestParam(value = "itemVal") int itemVal,
                                          @RequestParam(value = "beginDataTime") String beginDataTime,
                                          @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDateList10(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        Map<String, Object> params = new HashMap<>();
        params.put("beginDataTime", beginDataTime);
        params.put("endDataTime", endDataTime);
        if (itemVal == 0) {//风电场
            WfFileConfig wfFileConfig = wfFileConfigService.selectWfFileConfigById(Convert.toLong(stationNumber));
            if (ObjectUtil.isNotNull(wfFileConfig)) {
                WfTheoryenergyData wfTheoryenergyData = new WfTheoryenergyData();
                wfTheoryenergyData.setWindplantNo(Convert.toLong(stationNumber));
                wfTheoryenergyData.setParams(params);
                double availableenergy = 0;
                List<WfTheoryenergyData> wfTheoryenergyDataList = wfTheoryenergyDataService.selectWfTheoryenergyDataList(wfTheoryenergyData);
                if (wfTheoryenergyDataList.size() > 0) {
                    for (WfTheoryenergyData w : wfTheoryenergyDataList) {
                        String dataTime = DateUtil.format(w.getDataTime(), "yyyy-MM-dd HH:mm");
                        if ("00:10".equals(dataTime.substring(11, 16))) {
                            availableenergy = w.getAvailableenergy();
                        } else if (beginDataTime.equals(dataTime)) {
                            Map<String, Object> paramsF = new HashMap<>();
                            Date dTime;
                            if ("00:00".equals(dataTime.substring(11, 16))) {
                                dTime = DateUtil.date(DateUtil.offsetDay(w.getDataTime(), -1).getTime() + 600000);
                            } else {
                                dTime = DateUtil.date(DateUtil.beginOfDay(DateUtil.parse(dataTime)).getTime() + 600000);
                            }
                            paramsF.put("beginDataTime", DateUtil.formatDateTime(dTime));
                            paramsF.put("endDataTime", DateUtil.formatDateTime(DateUtil.parse(dataTime)));
                            WfTheoryenergyData beforeData = new WfTheoryenergyData();
                            beforeData.setWindplantNo(Convert.toLong(stationNumber));
                            beforeData.setParams(paramsF);
                            List<WfTheoryenergyData> list = wfTheoryenergyDataService.selectWfTheoryenergyDataList(beforeData);
                            availableenergy = list.stream().mapToDouble(WfTheoryenergyData::getAvailableenergy).sum();
                        } else {
                            availableenergy += w.getAvailableenergy();
                        }
                        ShareController.addMapValue(map, availableenergy, dataTime, 0, 1);
                    }
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.format(date, "yyyy-MM-dd HH:mm");
            TjDataFormat tjDataFormat = new TjDataFormat();
            tjDataFormat.setDataTime(stringDateTime);
            List<Double> entry = map.get(stringDateTime);
            if (entry != null) {
                if (entry.size() > 0) {
                    tjDataFormat.setData(entry.get(0));
                }
            }
            tjDataFormatList.add(tjDataFormat);
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }

    @ApiOperation("可用电量-期次")
    @GetMapping("/getPeriodKydlData")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectNo", value = "期次编号", required = true),
            @ApiImplicitParam(name = "itemVal", value = "场站类型(0风电场 4光伏电站)", required = true),
            @ApiImplicitParam(name = "beginDataTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endDataTime", value = "结束时间", required = true),
    })
    public AjaxResult getPeriodKydlData(@RequestParam(value = "stationNumber") String stationNumber,
                                        @RequestParam(value = "projectNo") String projectNo,
                                        @RequestParam(value = "itemVal") int itemVal,
                                        @RequestParam(value = "beginDataTime") String beginDataTime,
                                        @RequestParam(value = "endDataTime") String endDataTime) {
        JSONObject json = new JSONObject();
        //获取所有时间
        List<Date> dateList = DateTimeUtil.getDateList10(DateUtil.parse(beginDataTime), DateUtil.parse(endDataTime));
        LinkedMultiValueMap<String, Double> map = new LinkedMultiValueMap<String, Double>();
        Map<String, Object> params = new HashMap<>();
        params.put("beginDataTime", beginDataTime);
        params.put("endDataTime", endDataTime);
        if (itemVal == 0) {//风电场
            PrTheoryenergyData prTheoryenergyData = new PrTheoryenergyData();
            prTheoryenergyData.setWindplantNo(Convert.toLong(stationNumber));
            prTheoryenergyData.setProjectNo(Convert.toLong(projectNo));
            prTheoryenergyData.setParams(params);
            double availableenergy = 0;
            List<PrTheoryenergyData> prTheoryenergyDataList = prTheoryenergyDataService.selectPrTheoryenergyDataList(prTheoryenergyData);
            if (prTheoryenergyDataList.size() > 0) {
                for (PrTheoryenergyData p : prTheoryenergyDataList) {
                    String dataTime = DateUtil.format(p.getDataTime(), "yyyy-MM-dd HH:mm");
                    if ("00:10".equals(dataTime.substring(11, 16))) {
                        availableenergy = p.getAvailableenergy();
                    } else if (beginDataTime.equals(dataTime)) {
                        Map<String, Object> paramsF = new HashMap<>();
                        Date dTime;
                        if ("00:00".equals(dataTime.substring(11, 16))) {
                            dTime = DateUtil.date(DateUtil.offsetDay(p.getDataTime(), -1).getTime() + 600000);
                        } else {
                            dTime = DateUtil.date(DateUtil.beginOfDay(DateUtil.parse(dataTime)).getTime() + 600000);
                        }
                        paramsF.put("beginDataTime", DateUtil.formatDateTime(dTime));
                        paramsF.put("endDataTime", DateUtil.formatDateTime(DateUtil.parse(dataTime)));
                        PrTheoryenergyData beforeData = new PrTheoryenergyData();
                        beforeData.setWindplantNo(Convert.toLong(stationNumber));
                        beforeData.setProjectNo(Convert.toLong(projectNo));
                        beforeData.setParams(paramsF);
                        List<PrTheoryenergyData> list = prTheoryenergyDataService.selectPrTheoryenergyDataList(beforeData);
                        availableenergy = list.stream().mapToDouble(PrTheoryenergyData::getAvailableenergy).sum();
                    } else {
                        availableenergy += p.getAvailableenergy();
                    }
                    ShareController.addMapValue(map, availableenergy, dataTime, 0, 1);
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.format(date, "yyyy-MM-dd HH:mm");
            TjDataFormat tjDataFormat = new TjDataFormat();
            tjDataFormat.setDataTime(stringDateTime);
            List<Double> entry = map.get(stringDateTime);
            if (entry != null) {
                if (entry.size() > 0) {
                    tjDataFormat.setData(entry.get(0));
                }
            }
            tjDataFormatList.add(tjDataFormat);
        }
        json.put("list", tjDataFormatList);
        return AjaxResult.success(json);
    }
}
