package com.zksy.web.controller.base.radar;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.radar.domain.RadarData;
import com.zksy.base.radar.service.RadarDataService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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("/api/radar/data")
@Api(tags = "雷达流量计数据接口")
@Anonymous
public class RadarDataController {

    @Autowired
    private RadarDataService radarDataService;

    /**
     * 分页查询雷达流量计数据
     */
    @GetMapping("/page")
    @ApiOperation("分页查询雷达流量计数据")
    public AjaxResult queryPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String deviceCode,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        try {
            Page<RadarData> page = new Page<>(pageNum, pageSize);
            IPage<RadarData> resultPage = radarDataService.queryPage(page, deviceCode, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 按设备统计流速/水位数据
     */
    @GetMapping("/visualization/stat/device")
    @ApiOperation("按设备统计流速/水位数据")
    public AjaxResult statRadarByDevice(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        try {
            List<Map<String, Object>> data = radarDataService.statRadarByDevice(startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 按小时统计设备瞬时流量趋势
     */
    @GetMapping("/visualization/stat/trend")
    @ApiOperation("按小时统计设备瞬时流量趋势")
    public AjaxResult statFlowTrendByHour(
            @RequestParam String deviceCode,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        try {
            List<Map<String, Object>> data = radarDataService.statFlowTrendByHour(deviceCode, startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 查询异常数据（流速/水位超过阈值）
     */
    @GetMapping("/visualization/abnormal")
    @ApiOperation("查询异常数据（流速/水位超过阈值）")
    public AjaxResult queryAbnormalData(
            @RequestParam Double speedThreshold,
            @RequestParam Double levelThreshold,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        try {
            Page<RadarData> page = new Page<>(pageNum, pageSize);
            IPage<RadarData> resultPage = radarDataService.queryAbnormalData(page, speedThreshold, levelThreshold, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取设备最新监测数据
     */
    @GetMapping("/latest")
    @ApiOperation("获取设备最新监测数据")
    public AjaxResult getLatestDataByDevice(@RequestParam String deviceCode) {
        try {
            RadarData data = radarDataService.getLatestDataByDevice(deviceCode);
            if (data == null) {
                return AjaxResult.warn("未查询到该设备的最新数据", null);
            }
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取所有雷达设备编码
     */
    @GetMapping("/devices")
    @ApiOperation("获取所有雷达设备编码")
    public AjaxResult getAllDeviceCodes() {
        try {
            List<String> data = radarDataService.getAllDeviceCodes();
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
    //根据设备编码分组查询
    @GetMapping("/group")
    @ApiOperation("根据设备编码分组查询")
    public AjaxResult getDeviceGroup() {
        try {
            List<String> data = radarDataService.getDeviceGroup();
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
}