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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.manhole.domain.ManholeData;
import com.zksy.base.manhole.service.ManholeDataService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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("/api/manhole/data")
@Api(tags = "井盖数据接口")
@Anonymous
public class ManholeDataController {

    @Autowired
    private ManholeDataService manholeDataService;

    /**
     * 分页查询井盖数据
     */
    @GetMapping("/page")
    @ApiOperation("分页查询井盖数据")
    public AjaxResult queryPage(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "IMEI卡号") @RequestParam(required = false) String imeiCardNumber,
            @ApiParam(value = "开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            Page<ManholeData> page = new Page<>(pageNum, pageSize);
            IPage<ManholeData> resultPage = manholeDataService.queryPage(page, imeiCardNumber, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 按设备统计井盖数据
     */
    @GetMapping("/visualization/stat/device")
    @ApiOperation("按设备统计井盖数据")
    public AjaxResult statManholeByDevice(
            @ApiParam(value = "开始时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam(value = "结束时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            List<Map<String, Object>> data = manholeDataService.statManholeByDevice(startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 按小时统计报警趋势
     */
    @GetMapping("/visualization/stat/alarmTrend")
    @ApiOperation("按小时统计报警趋势")
    public AjaxResult statAlarmTrendByHour(
            @ApiParam(value = "IMEI卡号", required = true) @RequestParam String imeiCardNumber,
            @ApiParam(value = "开始时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam(value = "结束时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            List<Map<String, Object>> data = manholeDataService.statAlarmTrendByHour(imeiCardNumber, startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 按小时统计温度趋势
     */
    @GetMapping("/visualization/stat/temperatureTrend")
    @ApiOperation("按小时统计温度趋势")
    public AjaxResult statTemperatureTrendByHour(
            @ApiParam(value = "IMEI卡号", required = true) @RequestParam String imeiCardNumber,
            @ApiParam(value = "开始时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam(value = "结束时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            List<Map<String, Object>> data = manholeDataService.statTemperatureTrendByHour(imeiCardNumber, startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 查询报警数据（各种报警状态的数据）
     */
    @GetMapping("/visualization/alarm")
    @ApiOperation("查询报警数据")
    public AjaxResult queryAlarmData(
            @ApiParam(value = "IMEI卡号") @RequestParam(required = false) String imeiCardNumber,
            @ApiParam(value = "开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<ManholeData> page = new Page<>(pageNum, pageSize);
            IPage<ManholeData> resultPage = manholeDataService.queryAlarmData(page, imeiCardNumber, startTime, endTime);
            return AjaxResult.success("查询成功", resultPage);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取设备最新监测数据
     */
    @GetMapping("/latest")
    @ApiOperation("获取设备最新监测数据")
    public AjaxResult getLatestDataByImei(@ApiParam(value = "IMEI卡号", required = true) @RequestParam String imeiCardNumber) {
        try {
            Map<String, Object> data = manholeDataService.getLatestDataByImei(imeiCardNumber);
            if (data == null || data.isEmpty()) {
                return AjaxResult.warn("未查询到该设备的最新数据", null);
            }
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取所有井盖设备IMEI卡号
     */
    @GetMapping("/devices")
    @ApiOperation("获取所有井盖设备IMEI卡号")
    public AjaxResult getAllImeiCardNumbers() {
        try {
            List<String> data = manholeDataService.getAllImeiCardNumbers();
            return AjaxResult.success("查询成功", data);
        } catch (Exception e) {
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取整体统计数据
     */
    @GetMapping("/visualization/stat/overall")
    @ApiOperation("获取整体统计数据")
    public AjaxResult getOverallStatistics(
            @ApiParam(value = "开始时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam(value = "结束时间", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            Map<String, Object> data = manholeDataService.getOverallStatistics(startTime, endTime);
            return AjaxResult.success("统计成功", data);
        } catch (Exception e) {
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }
}
