package com.ruoyi.predict.controller;

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.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
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.web.bind.annotation.PostMapping;
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;

/**
 * @author hlf
 * @date 2022/10/24 10:54
 * 文件说明：计算模型配置
 */
@Api(value = "计算模型配置Controller", tags = {"计算模型配置操作接口"})
@Controller
@RequestMapping("/predict/api/cmc")
public class ComputationalModelConfigurationController extends BaseController {

    //风场日前功率预测模型参数表
    @Autowired
    private IWfDaPfParaService wfDaPfParaService;

    //风场超短期功率预测模型参数表
    @Autowired
    private IWfUspPfParaService wfUspPfParaService;

    //风场日前电量预测模型参数表
    @Autowired
    private IWfDaEfParaService wfDaEfParaService;

    //风场超短期电量预测模型参数表
    @Autowired
    private IWfUspEfParaService wfUspEfParaService;

    //光伏日前功率预测模型参数表
    @Autowired
    private IPvDaPfParaService pvDaPfParaService;

    //光伏超短期功率预测模型参数表
    @Autowired
    private IPvUspPfParaService pvUspPfParaService;

    @ApiOperation("获取计算模型信息列表")
    @PostMapping("/getCalculationModelList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalName(筛选传值terminalNumber)", value = "所属场站"),
            @ApiImplicitParam(name = "predictType(1风电短期功率 2风电超短期功率 3风电短期电量 4风电超短期电量 5光伏短期功率 6光伏超短期功率)", value = "预测类型"),
            @ApiImplicitParam(name = "itemName", value = "算法模型"),
            @ApiImplicitParam(name = "ifModelValid", value = "是否启用"),
            @ApiImplicitParam(name = "para", value = "参数配置"),
    })
    public TableDataInfo getCalculationModelList(CalculationModel calculationModel) {
        startPage();
        List<CalculationModel> list = new ArrayList<>();
        //风电短期功率
        WfDaPfPara wfDaPfPara = new WfDaPfPara();
        wfDaPfPara.setWindplantNo(calculationModel.getTerminalNumber());
        List<WfDaPfPara> wfDaPfParaList = wfDaPfParaService.selectWfDaPfParaList(wfDaPfPara);
        for (WfDaPfPara obj : wfDaPfParaList) {
            CalculationModel cm = new CalculationModel();
            cm.setTerminalNumber(obj.getWindplantNo());
            cm.setPredictType("风电短期功率");
            cm.setModelNo(obj.getModelNo());
            cm.setIfModelValid(obj.getIfModelValid());
            cm.setPara(obj.getPara());
            cm.setTerminalName(obj.getWindplantName());
            cm.setItemName(obj.getItemName());
            cm.setPredictTypeInt(1);
            list.add(cm);
        }
        //风电超短期功率
        WfUspPfPara wfUspPfPara = new WfUspPfPara();
        wfUspPfPara.setWindplantNo(calculationModel.getTerminalNumber());
        List<WfUspPfPara> wfUspPfParaList = wfUspPfParaService.selectWfUspPfParaList(wfUspPfPara);
        for (WfUspPfPara obj : wfUspPfParaList) {
            CalculationModel cm = new CalculationModel();
            cm.setTerminalNumber(obj.getWindplantNo());
            cm.setPredictType("风电超短期功率");
            cm.setModelNo(obj.getModelNo());
            cm.setIfModelValid(obj.getIfModelValid());
            cm.setPara(obj.getPara());
            cm.setTerminalName(obj.getWindplantName());
            cm.setItemName(obj.getItemName());
            cm.setPredictTypeInt(2);
            list.add(cm);
        }
        //风电短期电量
        WfDaEfPara wfDaEfPara = new WfDaEfPara();
        wfDaEfPara.setWindplantNo(calculationModel.getTerminalNumber());
        List<WfDaEfPara> wfDaEfParaList = wfDaEfParaService.selectWfDaEfParaList(wfDaEfPara);
        for (WfDaEfPara obj : wfDaEfParaList) {
            CalculationModel cm = new CalculationModel();
            cm.setTerminalNumber(obj.getWindplantNo());
            cm.setPredictType("风电短期电量");
            cm.setModelNo(obj.getModelNo());
            cm.setIfModelValid(obj.getIfModelValid());
            cm.setPara(obj.getPara());
            cm.setTerminalName(obj.getWindplantName());
            cm.setItemName(obj.getItemName());
            cm.setPredictTypeInt(3);
            list.add(cm);
        }
        //风电超短期电量
        WfUspEfPara wfUspEfPara = new WfUspEfPara();
        wfUspEfPara.setWindplantNo(calculationModel.getTerminalNumber());
        List<WfUspEfPara> wfUspEfParaList = wfUspEfParaService.selectWfUspEfParaList(wfUspEfPara);
        for (WfUspEfPara obj : wfUspEfParaList) {
            CalculationModel cm = new CalculationModel();
            cm.setTerminalNumber(obj.getWindplantNo());
            cm.setPredictType("风电超短期电量");
            cm.setModelNo(obj.getModelNo());
            cm.setIfModelValid(obj.getIfModelValid());
            cm.setPara(obj.getPara());
            cm.setTerminalName(obj.getWindplantName());
            cm.setItemName(obj.getItemName());
            cm.setPredictTypeInt(4);
            list.add(cm);
        }
        //光伏短期功率
        PvDaPfPara pvDaPfPara = new PvDaPfPara();
        pvDaPfPara.setWindplantNo(calculationModel.getTerminalNumber());
        List<PvDaPfPara> pvDaPfParaList = pvDaPfParaService.selectPvDaPfParaList(pvDaPfPara);
        for (PvDaPfPara obj : pvDaPfParaList) {
            CalculationModel cm = new CalculationModel();
            cm.setTerminalNumber(obj.getWindplantNo());
            cm.setPredictType("光伏短期功率");
            cm.setModelNo(obj.getModelNo());
            cm.setIfModelValid(obj.getIfModelValid());
            cm.setPara(obj.getPara());
            cm.setTerminalName(obj.getPvName());
            cm.setItemName(obj.getItemName());
            cm.setPredictTypeInt(5);
            list.add(cm);
        }
        //光伏超短期功率
        PvUspPfPara pvUspPfPara = new PvUspPfPara();
        pvUspPfPara.setPvNo(calculationModel.getTerminalNumber());
        List<PvUspPfPara> pvUspPfParaList = pvUspPfParaService.selectPvUspPfParaList(pvUspPfPara);
        for (PvUspPfPara obj : pvUspPfParaList) {
            CalculationModel cm = new CalculationModel();
            cm.setTerminalNumber(obj.getPvNo());
            cm.setPredictType("光伏超短期功率");
            cm.setModelNo(obj.getModelNo());
            cm.setIfModelValid(obj.getIfModelValid());
            cm.setPara(obj.getPara());
            cm.setTerminalName(obj.getPvName());
            cm.setItemName(obj.getItemName());
            cm.setPredictTypeInt(6);
            list.add(cm);
        }
        return getDataTable(list);
    }

    @ApiOperation("添加计算模型信息")
    @PostMapping("/addCalculationModel")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "modelNo", value = "算法模型", required = true),
            @ApiImplicitParam(name = "predictType(1风电短期功率 2风电超短期功率 3风电短期电量 4风电超短期电量 5光伏短期功率 6光伏超短期功率)传数字", value = "预测类型", required = true),
            @ApiImplicitParam(name = "ifModelValid", value = "是否启用(0不启用 1启用)", required = true),
            @ApiImplicitParam(name = "para", value = "参数配置", required = true),
    })
    public AjaxResult addCalculationModel(CalculationModel calculationModel) {
        if ("1".equals(calculationModel.getPredictType())){//1风电短期功率
            WfDaPfPara obj = wfDaPfParaService.selectWfDaPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNotNull(obj)) {
                return error("新增计算模型信息失败，计算模型信息已存在");
            }
            WfDaPfPara wfDaPfPara = new WfDaPfPara();
            wfDaPfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfDaPfPara.setModelNo(calculationModel.getModelNo());
            wfDaPfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfDaPfPara.setPara(calculationModel.getPara());
            wfDaPfParaService.insertWfDaPfPara(wfDaPfPara);
        }else if ("2".equals(calculationModel.getPredictType())){//2风电超短期功率
            WfUspPfPara obj = wfUspPfParaService.selectWfUspPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNotNull(obj)) {
                return error("新增计算模型信息失败，计算模型信息已存在");
            }
            WfUspPfPara wfUspPfPara = new WfUspPfPara();
            wfUspPfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfUspPfPara.setModelNo(calculationModel.getModelNo());
            wfUspPfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfUspPfPara.setPara(calculationModel.getPara());
            wfUspPfParaService.insertWfUspPfPara(wfUspPfPara);
        }else if ("3".equals(calculationModel.getPredictType())){//3风电短期电量
            WfDaEfPara obj = wfDaEfParaService.selectWfDaEfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNotNull(obj)) {
                return error("新增计算模型信息失败，计算模型信息已存在");
            }
            WfDaEfPara wfDaEfPara = new WfDaEfPara();
            wfDaEfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfDaEfPara.setModelNo(calculationModel.getModelNo());
            wfDaEfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfDaEfPara.setPara(calculationModel.getPara());
            wfDaEfParaService.insertWfDaEfPara(wfDaEfPara);
        }else if ("4".equals(calculationModel.getPredictType())){//4风电超短期电量
            WfUspEfPara obj = wfUspEfParaService.selectWfUspEfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNotNull(obj)) {
                return error("新增计算模型信息失败，计算模型信息已存在");
            }
            WfUspEfPara wfUspEfPara = new WfUspEfPara();
            wfUspEfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfUspEfPara.setModelNo(calculationModel.getModelNo());
            wfUspEfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfUspEfPara.setPara(calculationModel.getPara());
            wfUspEfParaService.insertWfUspEfPara(wfUspEfPara);
        }else if ("5".equals(calculationModel.getPredictType())){//5光伏短期功率
            PvDaPfPara obj = pvDaPfParaService.selectPvDaPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNotNull(obj)) {
                return error("新增计算模型信息失败，计算模型信息已存在");
            }
            PvDaPfPara pvDaPfPara = new PvDaPfPara();
            pvDaPfPara.setWindplantNo(calculationModel.getTerminalNumber());
            pvDaPfPara.setModelNo(calculationModel.getModelNo());
            pvDaPfPara.setIfModelValid(calculationModel.getIfModelValid());
            pvDaPfPara.setPara(calculationModel.getPara());
            pvDaPfParaService.insertPvDaPfPara(pvDaPfPara);
        }else if ("6".equals(calculationModel.getPredictType())){//6光伏超短期功率
            PvUspPfPara obj = pvUspPfParaService.selectPvUspPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNotNull(obj)) {
                return error("新增计算模型信息失败，计算模型信息已存在");
            }
            PvUspPfPara pvUspPfPara = new PvUspPfPara();
            pvUspPfPara.setPvNo(calculationModel.getTerminalNumber());
            pvUspPfPara.setModelNo(calculationModel.getModelNo());
            pvUspPfPara.setIfModelValid(calculationModel.getIfModelValid());
            pvUspPfPara.setPara(calculationModel.getPara());
            pvUspPfParaService.insertPvUspPfPara(pvUspPfPara);
        }
        return AjaxResult.success();
    }

    @ApiOperation("修改计算模型信息")
    @PostMapping("/updateCalculationModel")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "modelNo", value = "算法模型", required = true),
            @ApiImplicitParam(name = "predictType(1风电短期功率 2风电超短期功率 3风电短期电量 4风电超短期电量 5光伏短期功率 6光伏超短期功率)传数字", value = "预测类型", required = true),
            @ApiImplicitParam(name = "ifModelValid", value = "是否启用(0不启用 1启用)"),
            @ApiImplicitParam(name = "para", value = "参数配置"),
    })
    public AjaxResult updateCalculationModel(CalculationModel calculationModel) {
        if ("1".equals(calculationModel.getPredictType())){//1风电短期功率
            WfDaPfPara obj = wfDaPfParaService.selectWfDaPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNull(obj)) {
                return error("修改计算模型信息失败，数据不存在");
            }
            WfDaPfPara wfDaPfPara = new WfDaPfPara();
            wfDaPfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfDaPfPara.setModelNo(calculationModel.getModelNo());
            wfDaPfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfDaPfPara.setPara(calculationModel.getPara());
            wfDaPfParaService.updateWfDaPfPara(wfDaPfPara);
        }else if ("2".equals(calculationModel.getPredictType())){//2风电超短期功率
            WfUspPfPara obj = wfUspPfParaService.selectWfUspPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNull(obj)) {
                return error("修改计算模型信息失败，数据不存在");
            }
            WfUspPfPara wfUspPfPara = new WfUspPfPara();
            wfUspPfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfUspPfPara.setModelNo(calculationModel.getModelNo());
            wfUspPfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfUspPfPara.setPara(calculationModel.getPara());
            wfUspPfParaService.updateWfUspPfPara(wfUspPfPara);
        }else if ("3".equals(calculationModel.getPredictType())){//3风电短期电量
            WfDaEfPara obj = wfDaEfParaService.selectWfDaEfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNull(obj)) {
                return error("修改计算模型信息失败，数据不存在");
            }
            WfDaEfPara wfDaEfPara = new WfDaEfPara();
            wfDaEfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfDaEfPara.setModelNo(calculationModel.getModelNo());
            wfDaEfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfDaEfPara.setPara(calculationModel.getPara());
            wfDaEfParaService.updateWfDaEfPara(wfDaEfPara);
        }else if ("4".equals(calculationModel.getPredictType())){//4风电超短期电量
            WfUspEfPara obj = wfUspEfParaService.selectWfUspEfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNull(obj)) {
                return error("修改计算模型信息失败，数据不存在");
            }
            WfUspEfPara wfUspEfPara = new WfUspEfPara();
            wfUspEfPara.setWindplantNo(calculationModel.getTerminalNumber());
            wfUspEfPara.setModelNo(calculationModel.getModelNo());
            wfUspEfPara.setIfModelValid(calculationModel.getIfModelValid());
            wfUspEfPara.setPara(calculationModel.getPara());
            wfUspEfParaService.updateWfUspEfPara(wfUspEfPara);
        }else if ("5".equals(calculationModel.getPredictType())){//5光伏短期功率
            PvDaPfPara obj = pvDaPfParaService.selectPvDaPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNull(obj)) {
                return error("修改计算模型信息失败，数据不存在");
            }
            PvDaPfPara pvDaPfPara = new PvDaPfPara();
            pvDaPfPara.setWindplantNo(calculationModel.getTerminalNumber());
            pvDaPfPara.setModelNo(calculationModel.getModelNo());
            pvDaPfPara.setIfModelValid(calculationModel.getIfModelValid());
            pvDaPfPara.setPara(calculationModel.getPara());
            pvDaPfParaService.updatePvDaPfPara(pvDaPfPara);
        }else if ("6".equals(calculationModel.getPredictType())){//6光伏超短期功率
            PvUspPfPara obj = pvUspPfParaService.selectPvUspPfParaById(calculationModel.getTerminalNumber(), calculationModel.getModelNo());
            if (ObjectUtil.isNull(obj)) {
                return error("修改计算模型信息失败，数据不存在");
            }
            PvUspPfPara pvUspPfPara = new PvUspPfPara();
            pvUspPfPara.setPvNo(calculationModel.getTerminalNumber());
            pvUspPfPara.setModelNo(calculationModel.getModelNo());
            pvUspPfPara.setIfModelValid(calculationModel.getIfModelValid());
            pvUspPfPara.setPara(calculationModel.getPara());
            pvUspPfParaService.updatePvUspPfPara(pvUspPfPara);
        }
        return AjaxResult.success();
    }

    @ApiOperation("删除计算模型信息")
    @PostMapping("/deleteCalculationModel")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "modelNo", value = "算法模型", required = true),
            @ApiImplicitParam(name = "predictType(1风电短期功率 2风电超短期功率 3风电短期电量 4风电超短期电量 5光伏短期功率 6光伏超短期功率)传数字", value = "预测类型", required = true),
    })
    public AjaxResult deleteCalculationModel(@RequestParam(value = "terminalNumber") String terminalNumber,
                                             @RequestParam(value = "modelNo") String modelNo,
                                             @RequestParam(value = "predictType") String predictType) {
        if ("1".equals(predictType)){//1风电短期功率
            wfDaPfParaService.deleteWfDaPfParaById(Convert.toLong(terminalNumber), Convert.toLong(modelNo));
        }else if ("2".equals(predictType)){//2风电超短期功率
            wfUspPfParaService.deleteWfUspPfParaById(Convert.toLong(terminalNumber), Convert.toLong(modelNo));
        }else if ("3".equals(predictType)){//3风电短期电量
            wfDaEfParaService.deleteWfDaEfParaById(Convert.toLong(terminalNumber), Convert.toLong(modelNo));
        }else if ("4".equals(predictType)){//4风电超短期电量
            wfUspEfParaService.deleteWfUspEfParaById(Convert.toLong(terminalNumber), Convert.toLong(modelNo));
        }else if ("5".equals(predictType)){//5光伏短期功率
            pvDaPfParaService.deletePvDaPfParaById(Convert.toLong(terminalNumber), Convert.toLong(modelNo));
        }else if ("6".equals(predictType)){//6光伏超短期功率
            pvUspPfParaService.deletePvUspPfParaById(Convert.toLong(terminalNumber), Convert.toLong(modelNo));
        }
        return AjaxResult.success();
    }
}
