package com.zksy.web.controller.warning;

import com.zksy.base.warning.service.IEarlyWarningTrendService;
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/trend")
@Api(tags = "预警信息-预警发展趋势接口")
public class EarlyWarningTrendController {

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

    @Autowired
    private IEarlyWarningTrendService earlyWarningTrendService;

    @GetMapping("/day")
    @ApiOperation(value = "按日统计预警发展趋势", notes = "按日期维度统计各专项预警数量趋势，支持时间范围、预警专项筛选")
    @ApiResponses({
            @ApiResponse(code = 200, message = "统计成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "服务器内部错误，统计失败", response = AjaxResult.class)
    })
    public AjaxResult getTrendByDay(
            @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-12-31 23:59:59")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam(value = "预警专项")
            @RequestParam(required = false) String warningSpecial) {
        try {
            List<Map<String, Object>> list = earlyWarningTrendService.getTrendByDay(startTime, endTime, warningSpecial);
            return AjaxResult.success("统计成功", list);
        } catch (Exception e) {
            log.error("按日统计预警发展趋势异常", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    @GetMapping("/month")
    @ApiOperation(value = "按月统计预警发展趋势", notes = "按月份维度统计各专项预警数量趋势，支持时间范围、预警专项筛选")
    @ApiResponses({
            @ApiResponse(code = 200, message = "统计成功", response = AjaxResult.class),
            @ApiResponse(code = 500, message = "服务器内部错误，统计失败", response = AjaxResult.class)
    })
    public AjaxResult getTrendByMonth(
            @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-12-31 23:59:59")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam(value = "预警专项")
            @RequestParam(required = false) String warningSpecial) {
        try {
            List<Map<String, Object>> list = earlyWarningTrendService.getTrendByMonth(startTime, endTime, warningSpecial);
            return AjaxResult.success("统计成功", list);
        } catch (Exception e) {
            log.error("按月统计预警发展趋势异常", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }
}