package com.zksy.web.controller.warning;

import com.zksy.base.warning.service.IEarlyWarningDisposeStatusService;
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.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 预警统计-处置状态分析接口
 */
@RestController
@RequestMapping("/warning/disposeStatus")
@Api(tags = "预警信息-处置状态分析模块")
public class EarlyWarningDisposeStatusController {

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

    @Autowired
    private IEarlyWarningDisposeStatusService disposeStatusService;

    @GetMapping("/stat")
    @ApiOperation(value = "处置状态统计分析",
            notes = "按预警处置状态分组统计数量，支持时间范围、预警专项筛选，返回数据适配饼图/柱状图")
    @ApiResponses({
            @ApiResponse(code = 200, message = "统计成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "统计异常", response = AjaxResult.class)
    })
    public AjaxResult getDisposeStatusStat(
            @ApiParam(value = "发布开始时间 yyyy-MM-dd HH:mm:ss", example = "2026-01-01 00:00:00")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,

            @ApiParam(value = "发布结束时间 yyyy-MM-dd HH:mm:ss", example = "2026-06-01 23:59:59")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,

            @ApiParam(value = "预警专项", example = "管网压力专项")
            @RequestParam(required = false) String warningSpecial) {
        try {
            List<Map<String, Object>> result = disposeStatusService.getWarningStatusStat(startTime, endTime, warningSpecial);
            return AjaxResult.success("处置状态统计查询成功", result);
        } catch (Exception e) {
            log.error("处置状态统计查询异常", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }
}