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.WarningItem;
import com.zksy.base.warning.service.WarningItemService;
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.*;

@Slf4j
@RestController
@RequestMapping("/warning/item")
@Api(tags = "预警清单管理")
public class WarningItemController {

    @Autowired
    private WarningItemService warningItemService;

    @GetMapping("/list")
    @ApiOperation("事项信息查询-分页查询预警事项列表")
    @PreAuthorize("@ss.hasPermi('warning:item:list')")
    public AjaxResult queryWarningItems(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("事项编码") @RequestParam(required = false) String itemCode,
            @ApiParam("事项名称") @RequestParam(required = false) String itemName,
            @ApiParam("预警类型") @RequestParam(required = false) String warningType,
            @ApiParam("预警级别") @RequestParam(required = false) String warningLevel,
            @ApiParam("预警专项") @RequestParam(required = false) String warningSpecial,
            @ApiParam("状态") @RequestParam(required = false) String status) {
        Page<WarningItem> page = new Page<>(pageNum, pageSize);
        IPage<WarningItem> result = warningItemService.queryWarningItems(page, itemCode, itemName, 
                                                                          warningType, warningLevel, warningSpecial, status);
        return AjaxResult.success(result);
    }

    @PostMapping("/add")
    @ApiOperation("预警事项管理-新增预警事项")
    @PreAuthorize("@ss.hasPermi('warning:item:add')")
    public AjaxResult addWarningItem(@RequestBody WarningItem warningItem) {
        boolean success = warningItemService.addWarningItem(warningItem);
        if (success) {
            return AjaxResult.success(warningItem);
        } else {
            return AjaxResult.error("新增预警事项失败");
        }
    }

    @PutMapping("/update")
    @ApiOperation("预警事项管理-修改预警事项")
    @PreAuthorize("@ss.hasPermi('warning:item:edit')")
    public AjaxResult updateWarningItem(@RequestBody WarningItem warningItem) {
        boolean success = warningItemService.updateWarningItem(warningItem);
        if (success) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("修改预警事项失败");
        }
    }

    @PutMapping("/status")
    @ApiOperation("预警事项管理-更新事项状态（启用/禁用/作废）")
    @PreAuthorize("@ss.hasPermi('warning:item:edit')")
    public AjaxResult updateItemStatus(
            @ApiParam("事项ID") @RequestParam String itemId,
            @ApiParam("状态（ENABLED-启用 DISABLED-禁用 INVALID-作废）") @RequestParam String status) {
        boolean success = warningItemService.updateItemStatus(itemId, status);
        if (success) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("更新状态失败");
        }
    }

    @GetMapping("/detail/{itemId}")
    @ApiOperation("预警事项详情-获取事项详细信息")
    @PreAuthorize("@ss.hasPermi('warning:item:query')")
    public AjaxResult getItemDetail(@PathVariable String itemId) {
        WarningItem item = warningItemService.getItemDetail(itemId);
        if (item != null) {
            return AjaxResult.success(item);
        } else {
            return AjaxResult.error("预警事项不存在");
        }
    }

    @GetMapping("/code/generate")
    @ApiOperation("事项编码生成-自动生成事项编码")
    @PreAuthorize("@ss.hasPermi('warning:item:query')")
    public AjaxResult generateItemCode(
            @ApiParam("预警类型") @RequestParam String warningType,
            @ApiParam("预警级别") @RequestParam String warningLevel) {
        String itemCode = warningItemService.generateItemCode(warningType, warningLevel);
        return AjaxResult.success(itemCode);
    }
}
