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/9 14:26
 * 文件说明：图迹发电时间数据
 * 短期预测发电时长(场站(风电)、期次(风电))
 * 中长期预测发电时长(场站(风电)、期次(风电))
 * 实际利用时长(场站(风电)、期次(风电))
 */
@Api(value = "图迹发电时间数据Controller", tags = {"图迹发电时间数据操作接口"})
@Controller
@RequestMapping("/adapter/api/tjgtdc")
public class TuJiGenerationTimeDataController extends BaseController {

    @Autowired
    private IWfFileConfigService wfFileConfigService;

    @Autowired
    private IWfPrPfutilizationhoursService wfPrPfutilizationhoursService;

    @Autowired
    private ILpProjectMonthDataService lpProjectMonthDataService;

    @Autowired
    private ILpProjectYearDataService lpProjectYearDataService;

    @Autowired
    private IWfPrUtilizationhoursService wfPrUtilizationhoursService;

    @ApiOperation("短期预测发电时长-场站")
    @GetMapping("/getTerminalDqycfdscData")
    @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 getTerminalDqycfdscData(@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.getDayBetween(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)) {
                WfPrPfutilizationhours wfPrPfutilizationhours = new WfPrPfutilizationhours();
                wfPrPfutilizationhours.setWindplantNo(Convert.toLong(stationNumber));
                wfPrPfutilizationhours.setProjectNo((long) 0);
                wfPrPfutilizationhours.setModelNo(wfFileConfig.getWpfmodelNo());
                wfPrPfutilizationhours.setParams(params);
                List<WfPrPfutilizationhours> wfPrPfutilizationhoursList = wfPrPfutilizationhoursService.selectWfPrPfutilizationhoursList(wfPrPfutilizationhours);
                if (wfPrPfutilizationhoursList.size() > 0) {
                    for (WfPrPfutilizationhours w : wfPrPfutilizationhoursList) {
                        ShareController.addMapValue(map, w.getPfHourCount(), DateUtil.formatDate(w.getDataTime()), 0, 1);
                    }
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.formatDate(date);
            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("/getPeriodDqycfdscData")
    @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 getPeriodDqycfdscData(@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.getDayBetween(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) {//风电场
            WfPrPfutilizationhours wfPrPfutilizationhours = new WfPrPfutilizationhours();
            wfPrPfutilizationhours.setWindplantNo(Convert.toLong(stationNumber));
            wfPrPfutilizationhours.setProjectNo(Convert.toLong(projectNo));
            wfPrPfutilizationhours.setParams(params);
            List<WfPrPfutilizationhours> wfPrPfutilizationhoursList = wfPrPfutilizationhoursService.selectWfPrPfutilizationhoursList(wfPrPfutilizationhours);
            if (wfPrPfutilizationhoursList.size() > 0) {
                for (WfPrPfutilizationhours w : wfPrPfutilizationhoursList) {
                    ShareController.addMapValue(map, w.getPfHourCount(), DateUtil.formatDate(w.getDataTime()), 0, 1);
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.formatDate(date);
            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("/getTerminalZcqycfdsjData")
    @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 getTerminalZcqycfdsjData(@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) {//风电场
                LpProjectMonthData lpProjectMonthData = new LpProjectMonthData();
                lpProjectMonthData.setWfPrNo(Convert.toLong(stationNumber));
                lpProjectMonthData.setParams(params);
                List<LpProjectMonthData> lpProjectMonthDataList = lpProjectMonthDataService.selectLpProjectMonthDataList(lpProjectMonthData);
                if (lpProjectMonthDataList.size() > 0) {
                    for (LpProjectMonthData l : lpProjectMonthDataList) {
                        ShareController.addMapValue(map, l.getPfHourCount(), 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(stationNumber));
                lpProjectYearData.setParams(params);
                List<LpProjectYearData> lpWfYearDataList = lpProjectYearDataService.selectLpProjectYearDataList(lpProjectYearData);
                if (lpWfYearDataList.size() > 0) {
                    for (LpProjectYearData l : lpWfYearDataList) {
                        ShareController.addMapValue(map, l.getPfHourCount(), 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("/getPeriodZcqycfdsjData")
    @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 getPeriodZcqycfdsjData(@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.getPfHourCount(), 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> lpWfYearDataList = lpProjectYearDataService.selectLpProjectYearDataList(lpProjectYearData);
                if (lpWfYearDataList.size() > 0) {
                    for (LpProjectYearData l : lpWfYearDataList) {
                        ShareController.addMapValue(map, l.getPfHourCount(), 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("/getTerminalSjlyscData")
    @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 getTerminalSjlyscData(@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.getDayBetween(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) {//风电场
            WfPrUtilizationhours wfPrUtilizationhours = new WfPrUtilizationhours();
            wfPrUtilizationhours.setWindplantNo(Convert.toLong(stationNumber));
            wfPrUtilizationhours.setProjectNo((long) 0);
            wfPrUtilizationhours.setParams(params);
            List<WfPrUtilizationhours> wfPrPfutilizationhoursList = wfPrUtilizationhoursService.selectWfPrUtilizationhoursList(wfPrUtilizationhours);
            if (wfPrPfutilizationhoursList.size() > 0) {
                for (WfPrUtilizationhours w : wfPrPfutilizationhoursList) {
                    ShareController.addMapValue(map, w.getHourCount(), DateUtil.formatDate(w.getDataTime()), 0, 1);
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.formatDate(date);
            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("/getPeriodSjlyscData")
    @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 getPeriodSjlyscData(@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.getDayBetween(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) {//风电场
            WfPrUtilizationhours wfPrUtilizationhours = new WfPrUtilizationhours();
            wfPrUtilizationhours.setWindplantNo(Convert.toLong(stationNumber));
            wfPrUtilizationhours.setProjectNo(Convert.toLong(projectNo));
            wfPrUtilizationhours.setParams(params);
            List<WfPrUtilizationhours> wfPrPfutilizationhoursList = wfPrUtilizationhoursService.selectWfPrUtilizationhoursList(wfPrUtilizationhours);
            if (wfPrPfutilizationhoursList.size() > 0) {
                for (WfPrUtilizationhours w : wfPrPfutilizationhoursList) {
                    ShareController.addMapValue(map, w.getHourCount(), DateUtil.formatDate(w.getDataTime()), 0, 1);
                }
            }
        } else if (itemVal == 4) {//光伏电站
            //暂无 后补
        }
        List<TjDataFormat> tjDataFormatList = new ArrayList<>();
        for (Date date : dateList) {
            String stringDateTime = DateUtil.formatDate(date);
            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);
    }
}
