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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.water.domain.WaterMonitorData;
import com.zksy.base.water.service.WaterMonitorDataService;
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.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;

/**
 * 水位监测数据分析统计接口
 *
 * @author zksy
 */
@RestController
@RequestMapping("/water/monitor")
@Api(tags = "水位监测数据分析统计接口")
@Anonymous
public class WaterMonitorDataController {

    @Autowired
    private WaterMonitorDataService waterMonitorDataService;

    /**
     * 分页查询水位监测数据
     */
    @GetMapping("/visualization/page")
    @ApiOperation(value = "分页查询水位监测数据", notes = "支持设备编码模糊查询、数据类型筛选")
    public AjaxResult page(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页条数", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "设备编码") @RequestParam(required = false) String deviceCode,
            @ApiParam(value = "数据类型（0x31=上报，0x34=结束）") @RequestParam(required = false) String dataType) {
        WaterMonitorData waterMonitorData = new WaterMonitorData();
        waterMonitorData.setDeviceCode(deviceCode);
        waterMonitorData.setDataType(dataType);
        Page<WaterMonitorData> page = new Page<>(pageNum, pageSize);
        IPage<WaterMonitorData> resultPage = waterMonitorDataService.selectWaterMonitorDataPage(page, waterMonitorData);
        return AjaxResult.success("查询成功", resultPage);
    }

    /**
     * 按设备分组统计水位数据（最大值、最小值、平均值）
     */
    @GetMapping("/visualization/statistics/device")
    @ApiOperation(value = "按设备分组统计水位数据", notes = "统计指定时间段内各设备的水位最大/最小/平均值")
    public AjaxResult statisticsByDevice(
            @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) {
        List<Map<String, Object>> statistics = waterMonitorDataService.statisticsByDevice(startTime, endTime);
        return AjaxResult.success("统计成功", statistics);
    }

    /**
     * 查询水位超限数据
     */
    @GetMapping("/visualization/over-limit")
    @ApiOperation(value = "查询水位超限数据", notes = "查询指定时间段内水位超过阈值的监测数据")
    public AjaxResult selectOverLimitData(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页条数", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "水位阈值", required = true) @RequestParam String threshold,
            @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) {
        Page<WaterMonitorData> page = new Page<>(pageNum, pageSize);
        IPage<WaterMonitorData> resultPage = waterMonitorDataService.selectOverLimitData(page, threshold, startTime, endTime);
        return AjaxResult.success("查询成功", resultPage);
    }

    /**
     * 统计在线设备数量（按天）
     */
    @GetMapping("/visualization/statistics/online-device")
    @ApiOperation(value = "统计在线设备数量", notes = "统计指定时间段内每天的在线设备数量")
    public AjaxResult statisticsOnlineDevice(
            @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) {
        List<Map<String, Object>> statistics = waterMonitorDataService.statisticsOnlineDevice(startTime, endTime);
        return AjaxResult.success("统计成功", statistics);
    }

    /**
     * 获取各设备最新的水位监测数据
     */
    @GetMapping("/visualization/latest/device")
    @ApiOperation(value = "获取各设备最新数据", notes = "获取每个设备最新上报的水位监测数据")
    public AjaxResult getLatestDataByDevice() {
        List<WaterMonitorData> latestData = waterMonitorDataService.getLatestDataByDevice();
        return AjaxResult.success("查询成功", latestData);
    }
    /**
     * 获取所有设备编码
     */
    @GetMapping("/visualization/devices")
    @ApiOperation(value = "获取所有设备编码", notes = "获取所有设备编码")
    public AjaxResult getAllDeviceCodes() {
        List<String> deviceCodes = waterMonitorDataService.listObjs(new QueryWrapper<WaterMonitorData>().select("DISTINCT device_code"));
        return AjaxResult.success("查询成功", deviceCodes);
    }
}