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.EarlyWarning;
import com.zksy.base.warning.service.IEarlyWarningTodayService;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 今日预警统计接口
 */
@RestController
@RequestMapping("/warning/today")
@Api(tags = "预警信息-今日预警统计接口")
public class EarlyWarningTodayController {

    private static final Logger log = LoggerFactory.getLogger(EarlyWarningTodayController.class);

    @Autowired
    private IEarlyWarningTodayService earlyWarningTodayService;

    @GetMapping("/stats")
    @ApiOperation(value = "获取今日预警统计指标", notes = "返回今日预警总数、已处置数、已解除数三个核心指标")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "服务器内部错误，查询失败", response = AjaxResult.class)
    })
    public AjaxResult getTodayStats() {
        try {
            Map<String, Object> stats = earlyWarningTodayService.getTodayWarningStats();
            return AjaxResult.success("查询成功", stats);
        } catch (Exception e) {
            log.error("查询今日预警统计异常", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询今日预警列表", notes = "支持按状态筛选，为空则查询所有非草稿状态的今日预警")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "服务器内部错误，查询失败", response = AjaxResult.class)
    })
    public AjaxResult queryTodayPage(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "预警状态", allowableValues = "PENDING,PROCESSING,RELEASED,HANDLED,CLOSED") @RequestParam(required = false) String status) {
        try {
            Page<EarlyWarning> page = new Page<>(pageNum, pageSize);
            IPage<EarlyWarning> resultPage = earlyWarningTodayService.queryTodayWarningPage(page, status);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            log.error("分页查询今日预警列表异常", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
}