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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.telemetry.domain.TelemetryData;
import com.zksy.base.telemetry.service.TelemetryDataService;
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/telemetry/data")
@Api(tags = "平升遥测终端消防压力")
@Anonymous
public class TelemetryDataController {

    @Autowired
    private TelemetryDataService telemetryDataService;

    /**
     * 分页查询遥测终端数据
     */
    @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<TelemetryData> page = new Page<>(pageNum, pageSize);
            IPage<TelemetryData> resultPage = telemetryDataService.queryPage(page, deviceCode, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 按设备统计流速/压力/温度等关键指标
     */
    @GetMapping("/visualization/stat/device")
    @ApiOperation("按设备统计流速/压力/温度等关键指标")
    public AjaxResult statTelemetryByDevice(
            @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 = telemetryDataService.statTelemetryByDevice(startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 按小时统计设备流量/压力/温度趋势
     */
    @GetMapping("/visualization/stat/trend")
    @ApiOperation("按小时统计设备流量/压力/温度趋势")
    public AjaxResult statTelemetryTrendByHour(
            @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 = telemetryDataService.statTelemetryTrendByHour(deviceCode, startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 查询异常数据（压力/温度/流速超过阈值）
     */
    @GetMapping("/visualization/abnormal")
    @ApiOperation("查询异常数据（压力/温度/流速超过阈值）")
    public AjaxResult queryAbnormalData(
            @RequestParam Double pressureThreshold,
            @RequestParam Double tempThreshold,
            @RequestParam Double speedThreshold,
            @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<TelemetryData> page = new Page<>(pageNum, pageSize);
            IPage<TelemetryData> resultPage = telemetryDataService.queryAbnormalData(page, pressureThreshold, tempThreshold, speedThreshold, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

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

    /**
     * 获取所有遥测终端设备编码
     */
    @GetMapping("/visualization/devices")
    @ApiOperation("获取所有遥测终端设备编码")
    public AjaxResult getAllDeviceCodes() {
        try {
            List<String> data = telemetryDataService.getAllDeviceCodes();
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取所有遥测终端设备分组
     */
    @GetMapping("/visualization/groups")
    @ApiOperation("获取所有遥测终端设备分组")
    public AjaxResult getDeviceGroup() {
        try {
            List<String> data = telemetryDataService.getDeviceGroup();
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
}