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.ForecastReport;
import com.ruoyi.predict.service.IForecastReportService;
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/26 11:06
 * 文件说明：预测上报管理
 */
@Api(value = "预测上报管理Controller", tags = {"预测上报管理操作接口"})
@Controller
@RequestMapping("/predict/api/frmc")
public class ForecastReportingManagementController extends BaseController {

    @Autowired
    private IForecastReportService forecastReportService;

    @ApiOperation("获取预测上报信息列表")
    @PostMapping("/getForecastReportList")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalNameW或terminalNameP(筛选传值terminalNo)", value = "所属场站"),
            @ApiImplicitParam(name = "terminalType", value = "类型"),
            @ApiImplicitParam(name = "reportTime", value = "上报时间"),
            @ApiImplicitParam(name = "dataTime", value = "数据时间"),
            @ApiImplicitParam(name = "statTag", value = "上报状况(0成功 1失败)"),
            @ApiImplicitParam(name = "reportTag", value = "重新上报"),
    })
    public TableDataInfo getForecastReportList(ForecastReport forecastReport) {
        startPage();
        List<ForecastReport> list = forecastReportService.selectForecastReportList(forecastReport);
        return getDataTable(list);
    }

    @ApiOperation("添加预测上报信息")
    @PostMapping("/addForecastReport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalNo", value = "所属场站", required = true),
            @ApiImplicitParam(name = "terminalType", value = "类型", required = true),
    })
    public AjaxResult addForecastReport(ForecastReport forecastReport) {
        ForecastReport fr = forecastReportService.selectForecastReportById(forecastReport.getTerminalNo(), forecastReport.getTerminalType());
        if (ObjectUtil.isNotNull(fr)) {
            return error("新增预测上报信息失败，预测上报信息已存在");
        }
        forecastReportService.insertForecastReport(forecastReport);
        return AjaxResult.success();
    }

    @ApiOperation("修改预测上报信息")
    @PostMapping("/updateForecastReport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalNo", value = "所属场站", required = true),
            @ApiImplicitParam(name = "terminalType", value = "类型", required = true),
            @ApiImplicitParam(name = "reportTime", value = "上报时间"),
            @ApiImplicitParam(name = "dataTime", value = "数据时间"),
            @ApiImplicitParam(name = "statTag", value = "上报状况(0成功 1失败)"),
            @ApiImplicitParam(name = "reportTag", value = "重新上报"),

    })
    public AjaxResult updateForecastReport(ForecastReport forecastReport) {
        ForecastReport fr = forecastReportService.selectForecastReportById(forecastReport.getTerminalNo(), forecastReport.getTerminalType());
        if (ObjectUtil.isNull(fr)) {
            return error("修改预测上报信息失败，数据不存在");
        }
        forecastReportService.updateForecastReport(forecastReport);
        return AjaxResult.success();
    }

    @ApiOperation("删除预测上报信息")
    @PostMapping("/deleteForecastReport")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminalNo", value = "所属场站", required = true),
            @ApiImplicitParam(name = "terminalType", value = "类型", required = true),
    })
    public AjaxResult deleteForecastReport(@RequestParam(value = "terminalNo") String terminalNo,
                                           @RequestParam(value = "terminalType") String terminalType) {
        forecastReportService.deleteForecastReportById(Convert.toLong(terminalNo), Convert.toLong(terminalType));
        return AjaxResult.success();
    }

}
