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.service.IEarlyWarningReceiveEfficiencyService;
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.*;

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

/**
 * 预警统计-接警效率分析接口
 */
@RestController
@RequestMapping("/warning/receiveEfficiency")
@Api(tags = "预警信息-接警效率统计模块")
public class EarlyWarningReceiveEfficiencyController {

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

    @Autowired
    private IEarlyWarningReceiveEfficiencyService receiveEfficiencyService;

    @GetMapping("/overview")
    @ApiOperation(value = "接警效率概览统计", notes = "返回平均、最长、最短接警时长及接警总数核心指标")
    @ApiResponses({
            @ApiResponse(code = 200, message = "统计成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "统计异常", response = AjaxResult.class)
    })
    public AjaxResult getOverview(
            @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 warningType,

            @ApiParam(value = "预警专项", example = "供水管网专项")
            @RequestParam(required = false) String warningSpecial,

            @ApiParam(value = "处置状态", example = "HANDLED")
            @RequestParam(required = false) String status) {
        try {
            Map<String, Object> result = receiveEfficiencyService.getEfficiencyOverview(
                    startTime, endTime, warningType, warningSpecial, status);
            return AjaxResult.success("概览统计查询成功", result);
        } catch (Exception e) {
            log.error("接警效率概览统计异常", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    @GetMapping("/range")
    @ApiOperation(value = "接警时效区间分布", notes = "按响应时长区间统计预警数量，适配柱状图展示")
    @ApiResponses({
            @ApiResponse(code = 200, message = "统计成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "统计异常", response = AjaxResult.class)
    })
    public AjaxResult getRange(
            @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 warningType,

            @ApiParam(value = "预警专项", example = "供水管网专项")
            @RequestParam(required = false) String warningSpecial,

            @ApiParam(value = "处置状态", example = "HANDLED")
            @RequestParam(required = false) String status) {
        try {
            List<Map<String, Object>> result = receiveEfficiencyService.getEfficiencyRange(
                    startTime, endTime, warningType, warningSpecial, status);
            return AjaxResult.success("区间分布统计查询成功", result);
        } catch (Exception e) {
            log.error("接警时效区间统计异常", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    @GetMapping("/type")
    @ApiOperation(value = "按类型接警统计接警效率", notes = "按预警类型维度统计平均接警时长，从高到低排序")
    @ApiResponses({
            @ApiResponse(code = 200, message = "统计成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "统计异常", response = AjaxResult.class)
    })
    public AjaxResult getByType(
            @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) {
        try {
            List<Map<String, Object>> result = receiveEfficiencyService.getEfficiencyByType(startTime, endTime);
            return AjaxResult.success("类型维度统计查询成功", result);
        } 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 queryPage(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,

            @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 warningType,

            @ApiParam(value = "预警专项", example = "供水管网专项")
            @RequestParam(required = false) String warningSpecial,

            @ApiParam(value = "处置状态", example = "HANDLED")
            @RequestParam(required = false) String status,

            @ApiParam(value = "排序方式", allowableValues = "asc,desc", example = "desc")
            @RequestParam(defaultValue = "desc") String sortType) {
        try {
            Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
            IPage<Map<String, Object>> resultPage = receiveEfficiencyService.queryEfficiencyPage(
                    page, startTime, endTime, warningType, warningSpecial, status, sortType);
            return AjaxResult.success("明细查询成功", resultPage);
        } catch (Exception e) {
            log.error("接警时效明细查询异常", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
}