package com.zksy.web.controller.warning;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.warning.domain.WarningReason;
import com.zksy.base.warning.service.WarningReasonService;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/warning/reason")
@Api(tags = "预警原因库管理")
public class WarningReasonController {

    @Autowired
    private WarningReasonService warningReasonService;

    @GetMapping("/list")
    @ApiOperation("预警原因库管理-分页查询预警原因列表")
    @PreAuthorize("@ss.hasPermi('warning:reason:list')")
    public AjaxResult queryWarningReasons(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("原因编码") @RequestParam(required = false) String reasonCode,
            @ApiParam("原因名称") @RequestParam(required = false) String reasonName,
            @ApiParam("预警类型") @RequestParam(required = false) String warningType,
            @ApiParam("预警级别") @RequestParam(required = false) String warningLevel,
            @ApiParam("状态") @RequestParam(required = false) String status) {
        Page<WarningReason> page = new Page<>(pageNum, pageSize);
        IPage<WarningReason> result = warningReasonService.queryWarningReasons(page, reasonCode, reasonName, 
                                                                                 warningType, warningLevel, status);
        return AjaxResult.success(result);
    }

    @GetMapping("/query")
    @ApiOperation("预警原因库管理-根据类型和级别查询原因列表")
    @PreAuthorize("@ss.hasPermi('warning:reason:query')")
    public AjaxResult getReasonsByTypeAndLevel(
            @ApiParam("预警类型") @RequestParam String warningType,
            @ApiParam("预警级别") @RequestParam String warningLevel) {
        List<WarningReason> result = warningReasonService.getReasonsByTypeAndLevel(warningType, warningLevel);
        return AjaxResult.success(result);
    }

    @PostMapping("/add")
    @ApiOperation("预警原因库管理-新增预警原因")
    @PreAuthorize("@ss.hasPermi('warning:reason:add')")
    public AjaxResult addWarningReason(@RequestBody WarningReason warningReason) {
        boolean success = warningReasonService.addWarningReason(warningReason);
        if (success) {
            return AjaxResult.success(warningReason);
        } else {
            return AjaxResult.error("新增预警原因失败");
        }
    }

    @PutMapping("/update")
    @ApiOperation("预警原因库管理-修改预警原因")
    @PreAuthorize("@ss.hasPermi('warning:reason:edit')")
    public AjaxResult updateWarningReason(@RequestBody WarningReason warningReason) {
        boolean success = warningReasonService.updateWarningReason(warningReason);
        if (success) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改预警原因失败");
        }
    }

    @PutMapping("/status")
    @ApiOperation("预警原因库管理-更新原因状态")
    @PreAuthorize("@ss.hasPermi('warning:reason:edit')")
    public AjaxResult updateReasonStatus(
            @ApiParam("原因ID") @RequestParam String reasonId,
            @ApiParam("状态（0-正常 1-停用）") @RequestParam String status) {
        boolean success = warningReasonService.updateReasonStatus(reasonId, status);
        if (success) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("更新状态失败");
        }
    }

    @GetMapping("/detail/{reasonId}")
    @ApiOperation("预警原因库管理-获取原因详细信息")
    @PreAuthorize("@ss.hasPermi('warning:reason:query')")
    public AjaxResult getReasonDetail(@PathVariable String reasonId) {
        WarningReason reason = warningReasonService.getReasonDetail(reasonId);
        if (reason != null) {
            return AjaxResult.success(reason);
        } else {
            return AjaxResult.error("预警原因不存在");
        }
    }
}
