package com.ruoyi.predict.controller;

import cn.hutool.core.convert.Convert;
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.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.List;

/**
 * @author hlf
 * @date 2022/1/13 9:29
 * 文件说明：区域管理
 */
@Api(value = "区域管理Controller", tags = {"区域管理操作接口"})
@Controller
@RequestMapping("/predict/api/rmc")
public class RegionalManagementController extends BaseController {

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IWindplantService windplantService;

    @Autowired
    private IPvPowerStationService pvPowerStationService;

    @Autowired
    private IAreaConfigService areaConfigService;

    @Autowired
    private IRtuService rtuService;

    @Autowired
    private IWindplantprojectService windplantprojectService;

    @Autowired
    private ICollectorwireprojectService collectorwireprojectService;

    @Autowired
    private IWturService wturService;

    @Autowired
    private IWfProjectCwWtgService wfProjectCwWtgService;

    @Autowired
    private IWtgTypeService wtgTypeService;

    @Autowired
    private IWtgTypeTemperatureDefService wtgTypeTemperatureDefService;

    @ApiOperation("获取区域信息列表")
    @PostMapping("/getAreaList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pathname", value = "区域名称"),
            @ApiImplicitParam(name = "name", value = "区域代码"),
            @ApiImplicitParam(name = "wfCnt", value = "场站数量"),
            @ApiImplicitParam(name = "installWtg", value = "机组数量"),
            @ApiImplicitParam(name = "installPower", value = "装机容量MW"),
    })
    public TableDataInfo getWindZoneList(Area area) {
        startPage();
        List<Area> list = areaService.selectAreaList(area);
        return getDataTable(list);
    }

    @ApiOperation("添加区域信息")
    @PostMapping("/addArea")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pathname", value = "区域名称", required = true),
            @ApiImplicitParam(name = "name", value = "区域代码", required = true),
    })
    public AjaxResult addWindZone(Area area) {
        Long areaNo;
        if (areaService.selectNewData() == null) {
            areaNo = Convert.toLong(1);
        } else {
            areaNo = areaService.selectNewData() + 1;
        }
        area.setAreaNo(areaNo);
        area.setWfCnt((long) 0);
        area.setInstallWtg((long) 0);
        area.setInstallPower((double) 0);
        areaService.insertArea(area);
        return AjaxResult.success();
    }

    @ApiOperation("修改区域信息")
    @PostMapping("/updateArea")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaNo", value = "区域编号", required = true),
            @ApiImplicitParam(name = "pathname", value = "区域名称"),
            @ApiImplicitParam(name = "name", value = "区域代码"),
    })
    public AjaxResult updateWindZone(Area area) {
        areaService.updateArea(area);
        return AjaxResult.success();
    }

    @ApiOperation("删除区域信息")
    @PostMapping("/deleteArea")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaNo", value = "区域编号", required = true),
    })
    public AjaxResult deleteWindZone(@RequestParam(value = "areaNo") String areaNo) {
        //查询区域下所有场站信息
        List<AreaConfig> areaConfigList = areaConfigService.selectWindplantListByAreaNo(Convert.toLong(areaNo));
        for (AreaConfig areaConfig : areaConfigList) {
            //删除风场信息
            windplantService.deleteWindplantById(areaConfig.getRtuNo());
            //删除光伏电站信息
            pvPowerStationService.deletePvPowerStationById(areaConfig.getRtuNo());
            //删除新能源场站配置信息
            rtuService.deleteRtuById(areaConfig.getRtuNo());
            //删除区域场站关联表信息
            areaConfigService.deleteAreaConfigByRtuNo(areaConfig.getRtuNo());
            //删除期次配置信息
            windplantprojectService.deleteWindplantprojectByWindplantNo(areaConfig.getRtuNo());
            //删除集电线配置信息
            collectorwireprojectService.deleteCollectorwireprojectByWindplantNo(areaConfig.getRtuNo());
            //删除风机配置信息
            List<WfProjectCwWtg> wfProjectCwWtgList = wfProjectCwWtgService.selectWfProjectCwWtgByWindplantNo(areaConfig.getRtuNo());
            for (WfProjectCwWtg wfProjectCwWtg : wfProjectCwWtgList) {
                Wtur wtur = wturService.selectWturById(wfProjectCwWtg.getWtgNo());
                if (ObjectUtil.isNotNull(wtur)) {
                    wtgTypeService.deleteWtgTypeById(wtur.getWtgType());
                }
                wtgTypeTemperatureDefService.deleteWtgTypeTemperatureDefById(wtur.getWtgType());
                wturService.deleteWturById(wfProjectCwWtg.getWtgNo());
            }
            wfProjectCwWtgService.deleteWfProjectCwWtgByWindplantNo(areaConfig.getRtuNo());
        }
        //删除区域信息
        areaService.deleteAreaById(Convert.toLong(areaNo));
        return AjaxResult.success();
    }
}
