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.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.predict.domain.Area;
import com.ruoyi.predict.domain.AreaConfig;
import com.ruoyi.predict.domain.PlanpowerDistribution;
import com.ruoyi.predict.service.IAreaConfigService;
import com.ruoyi.predict.service.IAreaService;
import com.ruoyi.predict.service.IPlanpowerDistributionService;
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/10/27 13:52
 * 文件说明：系统管理首页
 */
@Api(value = "系统管理首页Controller", tags = {"系统管理首页操作接口"})
@Controller
@RequestMapping("/predict/api/smhc")
public class SystemManagementHomeController extends BaseController {

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IAreaConfigService areaConfigService;

    @Autowired
    private IPlanpowerDistributionService planpowerDistributionService;

    @ApiOperation("获取注册区域信息列表")
    @PostMapping("/getRegistrationAreaList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "区域名称"),
            @ApiImplicitParam(name = "terminalNumber", value = "场站数量"),
            @ApiImplicitParam(name = "installWtg", value = "装机数量"),
            @ApiImplicitParam(name = "installPower", value = "装机容量"),
    })
    public AjaxResult getRegistrationAreaList() {
        JSONObject json = new JSONObject();
        List<Area> list = areaService.selectAreaList(new Area());
        for (Area obj : list) {
            List<AreaConfig> areaConfigList = areaConfigService.selectWindplantListByAreaNo(obj.getAreaNo());
            obj.setTerminalNumber(areaConfigList.size());
        }
        json.put("list", list);
        return AjaxResult.success(json);
    }

    @ApiOperation("智慧配置")
    @PostMapping("/wisdomConfiguration")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "状态(1准确率 2考核分数)", required = true),
    })
    public AjaxResult wisdomConfiguration(@RequestParam(value = "type") String type) {
        PlanpowerDistribution obj = planpowerDistributionService.selectPlanpowerDistributionById((long) -2);
        if (ObjectUtil.isNotNull(obj)) {
            PlanpowerDistribution planpowerDistribution = new PlanpowerDistribution();
            planpowerDistribution.setStationNumber((long) -2);
            planpowerDistribution.setType(Convert.toLong(type));
            planpowerDistributionService.updatePlanpowerDistribution(planpowerDistribution);
        } else {
            PlanpowerDistribution planpowerDistribution = new PlanpowerDistribution();
            planpowerDistribution.setStationNumber((long) -2);
            planpowerDistribution.setType(Convert.toLong(type));
            planpowerDistributionService.insertPlanpowerDistribution(planpowerDistribution);
        }
        return AjaxResult.success();
    }

    @ApiOperation("发电计划配置")
    @PostMapping("/generationScheduleConfiguration")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationNumber", value = "场站编号", required = true),
            @ApiImplicitParam(name = "type", value = "状态(1按预测等比分配 2按月计划等分)", required = true),
    })
    public AjaxResult generationScheduleConfiguration(@RequestParam(value = "stationNumber") String stationNumber,
                                                      @RequestParam(value = "type") String type) {
        PlanpowerDistribution obj = planpowerDistributionService.selectPlanpowerDistributionById(Convert.toLong(stationNumber));
        if (ObjectUtil.isNotNull(obj)) {
            PlanpowerDistribution planpowerDistribution = new PlanpowerDistribution();
            planpowerDistribution.setStationNumber(Convert.toLong(stationNumber));
            planpowerDistribution.setType(Convert.toLong(type));
            planpowerDistributionService.updatePlanpowerDistribution(planpowerDistribution);
        } else {
            PlanpowerDistribution planpowerDistribution = new PlanpowerDistribution();
            planpowerDistribution.setStationNumber(Convert.toLong(stationNumber));
            planpowerDistribution.setType(Convert.toLong(type));
            planpowerDistributionService.insertPlanpowerDistribution(planpowerDistribution);
        }
        return AjaxResult.success();
    }
}
