package com.ruoyi.predict.controller;

import com.ruoyi.common.constant.UserConstants;
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.Projectplan;
import com.ruoyi.predict.service.IProjectplanService;
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/8/30 9:48
 * 文件说明：发电计划管理
 */
@Api(value = "发电计划管理Controller", tags = {"发电计划管理操作接口"})
@Controller
@RequestMapping("/predict/api/pgpmc")
public class PowerGenerationPlanManagementController extends BaseController {

    @Autowired
    private IProjectplanService projectplanService;

    @ApiOperation("获取发电计划信息列表")
    @PostMapping("/getProjectplanList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "windplantName", value = "场站名称(风电场站)"),
            @ApiImplicitParam(name = "pvStationName", value = "场站名称(光伏场站)"),
            @ApiImplicitParam(name = "year", value = "计划年"),
            @ApiImplicitParam(name = "month", value = "计划月"),
            @ApiImplicitParam(name = "generatingcapacity", value = "计划发电量"),
    })
    public TableDataInfo getProjectplanList(Projectplan projectplan) {
        startPage();
        List<Projectplan> list = projectplanService.selectProjectplanList(projectplan);
        return getDataTable(list);
    }

    @ApiOperation("添加发电计划信息")
    @PostMapping("/addProjectplan")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "windpower", value = "场站编号", required = true),
            @ApiImplicitParam(name = "projectid", value = "期次代码", required = true),
            @ApiImplicitParam(name = "year", value = "计划年", required = true),
            @ApiImplicitParam(name = "month", value = "计划月", required = true),
            @ApiImplicitParam(name = "generatingcapacity", value = "计划发电量", required = true),
    })
    public AjaxResult addProjectplan(Projectplan projectplan) {
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(projectplanService.checkProjectplanUnique(projectplan.getWindpower(), projectplan.getProjectid(), projectplan.getYear(), projectplan.getMonth()))) {
            return error("新增发电计划失败，发电计划已存在");
        }
        Long id;
        if (projectplanService.selectNewData() == null) {
            id = Convert.toLong(1);
        } else {
            id = projectplanService.selectNewData() + 1;
        }
        projectplan.setId(id);
        projectplanService.insertProjectplan(projectplan);
        return AjaxResult.success();
    }

    @ApiOperation("修改发电计划信息")
    @PostMapping("/updateProjectplan")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "发电计划编号", required = true),
            @ApiImplicitParam(name = "windpower", value = "场站编号"),
            @ApiImplicitParam(name = "projectid", value = "期次代码"),
            @ApiImplicitParam(name = "year", value = "计划年"),
            @ApiImplicitParam(name = "month", value = "计划月"),
            @ApiImplicitParam(name = "generatingcapacity"),
    })
    public AjaxResult updateProjectplan(Projectplan projectplan) {
        projectplanService.updateProjectplan(projectplan);
        return AjaxResult.success();
    }

    @ApiOperation("删除发电计划信息")
    @PostMapping("/deleteProjectplan")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "发电计划编号", required = true),
    })
    public AjaxResult deleteProjectplan(@RequestParam(value = "id") String id) {
        projectplanService.deleteProjectplanById(Convert.toLong(id));
        return AjaxResult.success();
    }
}
