package com.ruoyi.predict.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
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.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.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author hlf
 * @date 2021/8/2 10:03
 * 文件说明：公共方法
 */
@Api(value = "公共方法Controller", tags = {"公共方法操作接口"})
@Controller
@RequestMapping("/predict/api/share")
public class ShareController extends BaseController {

    @Autowired
    private IWindplantService windplantService;

    @Autowired
    private IPvPowerStationService pvPowerStationService;

    @Autowired
    private IWfProjectCwWtgService wfProjectCwWtgService;

    @Autowired
    private IHistorydataPointService historydataPointService;

    @Autowired
    private IPsmJobV3Service psmJobV3Service;

    @ApiOperation("获取所有场站信息")
    @GetMapping("/getStationsAll")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号"),
            @ApiImplicitParam(name = "stationName", value = "场站名称"),
    })
    public AjaxResult getStationsAll() {
        JSONObject json = new JSONObject();
        List<StationInformation> list = new ArrayList<>();
        List<Windplant> windplantList = windplantService.selectWindplantList(new Windplant());
        for (Windplant wp : windplantList) {
            StationInformation si = new StationInformation();
            si.setNo(wp.getWindplantNo());
            si.setStationName(wp.getName());
            si.setStationCode(wp.getPathname());
            si.setItemVal(wp.getItemVal());
            list.add(si);
        }
        List<PvPowerStation> pvPowerStationList = pvPowerStationService.selectPvPowerStationList(new PvPowerStation());
        for (PvPowerStation pps : pvPowerStationList) {
            StationInformation si = new StationInformation();
            si.setNo(pps.getPvStationNo());
            si.setStationName(pps.getName());
            si.setStationCode(pps.getRef());
            si.setItemVal(pps.getItemVal());
            list.add(si);
        }
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("功率-获取所有场站信息")
    @GetMapping("/getGlStationsAll")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号"),
            @ApiImplicitParam(name = "stationName", value = "场站名称"),
    })
    public AjaxResult getGlStationsAll() {
        JSONObject json = new JSONObject();
        List<StationInformation> list = new ArrayList<>();
        List<Windplant> windplantList = windplantService.selectWindplantListByGlFlag();
        for (Windplant wp : windplantList) {
            StationInformation si = new StationInformation();
            si.setNo(wp.getWindplantNo());
            si.setStationName(wp.getName());
            si.setStationCode(wp.getPathname());
            si.setItemVal(wp.getItemVal());
            list.add(si);
        }
        List<PvPowerStation> pvPowerStationList = pvPowerStationService.selectPvPowerStationList(new PvPowerStation());
        for (PvPowerStation pps : pvPowerStationList) {
            StationInformation si = new StationInformation();
            si.setNo(pps.getPvStationNo());
            si.setStationName(pps.getName());
            si.setStationCode(pps.getRef());
            si.setItemVal(pps.getItemVal());
            list.add(si);
        }
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("电量-获取所有场站信息")
    @GetMapping("/getDlStationsAll")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号"),
            @ApiImplicitParam(name = "stationName", value = "场站名称"),
    })
    public AjaxResult getDlStationsAll() {
        JSONObject json = new JSONObject();
        List<StationInformation> list = new ArrayList<>();
        List<Windplant> windplantList = windplantService.selectWindplantListByDlFlag();
        for (Windplant wp : windplantList) {
            StationInformation si = new StationInformation();
            si.setNo(wp.getWindplantNo());
            si.setStationName(wp.getName());
            si.setStationCode(wp.getPathname());
            si.setItemVal(wp.getItemVal());
            list.add(si);
        }
        List<PvPowerStation> pvPowerStationList = pvPowerStationService.selectPvPowerStationList(new PvPowerStation());
        for (PvPowerStation pps : pvPowerStationList) {
            StationInformation si = new StationInformation();
            si.setNo(pps.getPvStationNo());
            si.setStationName(pps.getName());
            si.setStationCode(pps.getRef());
            si.setItemVal(pps.getItemVal());
            list.add(si);
        }
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("获取所有机组状态信息")
    @GetMapping("/getUnitStatusInfoAll")
    @ResponseBody
    public AjaxResult getUnitStatusInfoAll() {
        JSONObject json = new JSONObject();
        int zzj = 0;
        double zzjrl = 0;
        int yx = 0;
        int dj = 0;
        int wh = 0;
        int tj = 0;
        int xd = 0;
        int lx = 0;
        int jx = 0;
        List<Windplant> windplantList = windplantService.selectWindplantListByGlFlag();
        for (Windplant windplant : windplantList) {
            zzjrl += windplant.getInstallPower();
            List<WfProjectCwWtg> wfProjectCwWtgList = wfProjectCwWtgService.selectWfProjectCwWtgByWindplantNo(windplant.getWindplantNo());
            zzj += wfProjectCwWtgList.size();
            for (WfProjectCwWtg wfProjectCwWtg : wfProjectCwWtgList) {
                if (wfProjectCwWtg.getMag() == 1) {//运行
                    yx++;
                } else if (wfProjectCwWtg.getMag() == 0) {//待机
                    dj++;
                } else if (wfProjectCwWtg.getMag() == 4) {//维护
                    wh++;
                } else if (wfProjectCwWtg.getMag() == 2) {//停机
                    tj++;
                } else if (wfProjectCwWtg.getMag() == 5) {//限电
                    xd++;
                } else if (wfProjectCwWtg.getMag() == 3) {//离线
                    lx++;
                } else if (wfProjectCwWtg.getMag() == 6) {//检修
                    jx++;
                }
            }
        }
        List<PvPowerStation> pvPowerStationList = pvPowerStationService.selectPvPowerStationList(new PvPowerStation());
        for (PvPowerStation pvPowerStation : pvPowerStationList){
            zzjrl += pvPowerStation.getRatingPower();
            zzj += pvPowerStation.getInverterCnt();
        }
        json.put("zzj", zzj);
        json.put("zzjrl", zzjrl);
        json.put("yx", yx);
        json.put("dj", dj);
        json.put("wh", wh);
        json.put("tj", tj);
        json.put("xd", xd);
        json.put("lx", lx);
        json.put("jx", jx);
        return AjaxResult.success(json);
    }

    @ApiOperation("根据场站编号获取机组状态信息")
    @GetMapping("/getUnitStatusInfoByStationNo")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNo", value = "场站编号", required = true),
    })
    public AjaxResult getUnitStatusInfoByStationNo(@RequestParam(value = "stationNo") String stationNo) {
        JSONObject json = new JSONObject();
        int zzjrl = 0;
        int zzj = 0;
        int yx = 0;
        int dj = 0;
        int wh = 0;
        int tj = 0;
        int xd = 0;
        int lx = 0;
        int jx = 0;
        Windplant windplant = windplantService.selectWindplantById(Convert.toLong(stationNo));
        if (ObjectUtil.isNotNull(windplant)){
            zzjrl += windplant.getInstallPower();
            List<WfProjectCwWtg> wfProjectCwWtgList = wfProjectCwWtgService.selectWfProjectCwWtgByWindplantNo(windplant.getWindplantNo());
            if (wfProjectCwWtgList.size() > 0){
                zzj += wfProjectCwWtgList.size();
                for (WfProjectCwWtg wfProjectCwWtg : wfProjectCwWtgList) {
                    if (wfProjectCwWtg.getMag() == 1) {//运行
                        yx++;
                    } else if (wfProjectCwWtg.getMag() == 0) {//待机
                        dj++;
                    } else if (wfProjectCwWtg.getMag() == 4) {//维护
                        wh++;
                    } else if (wfProjectCwWtg.getMag() == 2) {//停机
                        tj++;
                    } else if (wfProjectCwWtg.getMag() == 5) {//限电
                        xd++;
                    } else if (wfProjectCwWtg.getMag() == 3) {//离线
                        lx++;
                    } else if (wfProjectCwWtg.getMag() == 6) {//检修
                        jx++;
                    }
                }
            }
        }
        json.put("zzjrl", zzjrl);
        json.put("zzj", zzj);
        json.put("yx", yx);
        json.put("dj", dj);
        json.put("wh", wh);
        json.put("tj", tj);
        json.put("xd", xd);
        json.put("lx", lx);
        json.put("jx", jx);
        return AjaxResult.success(json);
    }

    @ApiOperation("获取预警状态信息")
    @GetMapping("/getEarlyWarningInfo")
    @ResponseBody
    public AjaxResult getEarlyWarningInfo() {
        JSONObject json = new JSONObject();
        //红色-1正常 绿色-2异常 黄色-3部分异常
        int txyj = 2;
        int cftyj = 0;
        int fjsj = 0;
        int zgszqx;
        int ozszqx = 0;
        String currentTime = DateUtil.now();
        long mm = Convert.toLong(currentTime.substring(14, 16));
        String newTimeGl = "";
        if (0 <= mm && mm < 15) {
            newTimeGl = currentTime.substring(0, 14) + "00:00";
        } else if (15 <= mm && mm < 30) {
            newTimeGl = currentTime.substring(0, 14) + "15:00";
        } else if (30 <= mm && mm < 45) {
            newTimeGl = currentTime.substring(0, 14) + "30:00";
        } else if (45 <= mm && mm < 60) {
            newTimeGl = currentTime.substring(0, 14) + "45:00";
        }
        String newTimeDl = "";
        if (0 <= mm && mm < 10) {
            newTimeDl = currentTime.substring(0, 14) + "00:00";
        } else if (10 <= mm && mm < 20) {
            newTimeDl = currentTime.substring(0, 14) + "10:00";
        } else if (20 <= mm && mm < 30) {
            newTimeDl = currentTime.substring(0, 14) + "20:00";
        } else if (30 <= mm && mm < 40) {
            newTimeDl = currentTime.substring(0, 14) + "30:00";
        } else if (40 <= mm && mm < 50) {
            newTimeDl = currentTime.substring(0, 14) + "40:00";
        } else if (50 <= mm && mm < 60) {
            newTimeDl = currentTime.substring(0, 14) + "50:00";
        }
        HistorydataPoint historydataPoint = historydataPointService.selectHistorydataPointById((long) 0);
        if (ObjectUtil.isNotNull(historydataPoint)) {
            if (historydataPoint.getStatTime1().getTime() == DateUtil.parse(newTimeGl).getTime()) {
                cftyj = 1;
            } else {
                cftyj = 2;
            }
            if (historydataPoint.getStatTime1().getTime() == DateUtil.parse(newTimeGl).getTime() && historydataPoint.getStatTime5().getTime() == DateUtil.parse(newTimeDl).getTime()) {
                fjsj = 1;
            } else {
                fjsj = 2;
            }
        }
        //7大陆风电 8大陆光伏 3欧洲风电
        List<PsmJobV3> psmJobV3List7 = psmJobV3Service.selectPsmJobV3ByJobKey1((long) 7);
        int jobKey7 = 0;
        for (PsmJobV3 psmJobV3 : psmJobV3List7) {
            if (psmJobV3.getNextExecTime().getTime() == DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.date()), 1).getTime()) {
                jobKey7 = 1;
            } else {
                jobKey7 = 2;
                break;
            }
        }
        int jobKey8 = 0;
        List<PsmJobV3> psmJobV3List8 = psmJobV3Service.selectPsmJobV3ByJobKey1((long) 8);
        for (PsmJobV3 psmJobV3 : psmJobV3List8) {
            if (psmJobV3.getNextExecTime().getTime() == DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.date()), 1).getTime()) {
                jobKey8 = 1;
            } else {
                jobKey8 = 2;
                break;
            }
        }
        if (jobKey7 == 1 && jobKey8 == 1) {
            zgszqx = 1;
        } else if (jobKey7 == 2 && jobKey8 == 2) {
            zgszqx = 2;
        } else {
            zgszqx = 3;
        }
        List<PsmJobV3> psmJobV3List3 = psmJobV3Service.selectPsmJobV3ByJobKey1((long) 3);
        for (PsmJobV3 psmJobV3 : psmJobV3List3) {
            if (psmJobV3.getNextExecTime().getTime() == DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.date()), 1).getTime()) {
                ozszqx = 1;
            } else {
                ozszqx = 2;
                break;
            }
        }
        json.put("txyj", txyj);
        json.put("cftyj", cftyj);
        json.put("fjsj", fjsj);
        json.put("zgszqx", zgszqx);
        json.put("ozszqx", ozszqx);
        return AjaxResult.success(json);
    }

    /**
     * Table数据处理
     */
    public static void setTableField(double value, int i, TableField tableField) {
        if (i == 0) {
            tableField.setJanuary(value);
        } else if (i == 1) {
            tableField.setFebruary(value);
        } else if (i == 2) {
            tableField.setMarch(value);
        } else if (i == 3) {
            tableField.setApril(value);
        } else if (i == 4) {
            tableField.setMay(value);
        } else if (i == 5) {
            tableField.setJune(value);
        } else if (i == 6) {
            tableField.setJuly(value);
        } else if (i == 7) {
            tableField.setAugust(value);
        } else if (i == 8) {
            tableField.setSeptember(value);
        } else if (i == 9) {
            tableField.setOctober(value);
        } else if (i == 10) {
            tableField.setNovember(value);
        } else if (i == 11) {
            tableField.setDecember(value);
        }
    }

    /**
     * map添加值,空开同一时间内中间缺失数据,解决后面的值向前错位
     */
    public static void addMapValue(LinkedMultiValueMap<String, Double> map, Double d, String dataTime, int i, int size) {
        if (i == 0) {
            map.add(dataTime, d);
        } else {
            if (map.containsKey(dataTime)) {
                if (Objects.requireNonNull(map.get(dataTime)).size() < i) {
                    for (int j = 0; j < size; j++) {
                        if (Objects.requireNonNull(map.get(dataTime)).size() == i) {
                            break;
                        }
                        map.add(dataTime, null);
                    }
                }
                map.add(dataTime, d);
            } else {
                for (int j = 0; j < i; j++) {
                    map.add(dataTime, null);
                }
                map.add(dataTime, d);
            }
        }
    }
}