package com.zksy.base.water.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.water.domain.WaterMonitorData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 水位监测数据 Mapper 接口
 *
 * @author zksy
 */
public interface WaterMonitorDataMapper extends BaseMapper<WaterMonitorData> {

    /**
     * 按设备分组统计水位数据
     */
    @Select("SELECT device_code, " +
            "MAX(water_level) as max_water_level, " +
            "MIN(water_level) as min_water_level, " +
            "AVG(water_level) as avg_water_level " +
            "FROM water_monitor_data " +
            "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY device_code")
    List<Map<String, Object>> statisticsByDevice(@Param("startTime") LocalDateTime startTime,
                                                 @Param("endTime") LocalDateTime endTime);

    /**
     * 查询水位超限数据（自定义阈值）
     */
    @Select("SELECT * FROM water_monitor_data " +
            "WHERE water_level > #{threshold} " +
            "AND create_time BETWEEN #{startTime} AND #{endTime}")
    IPage<WaterMonitorData> selectOverLimitData(Page<WaterMonitorData> page,
                                                @Param("threshold") String threshold,
                                                @Param("startTime") LocalDateTime startTime,
                                                @Param("endTime") LocalDateTime endTime);

    /**
     * 按时间维度统计设备在线状态
     */
    @Select("SELECT DATE(create_time) as stat_date, " +
            "COUNT(DISTINCT device_code) as online_device_count " +
            "FROM water_monitor_data " +
            "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY DATE(create_time)")
    List<Map<String, Object>> statisticsOnlineDevice(@Param("startTime") LocalDateTime startTime,
                                                     @Param("endTime") LocalDateTime endTime);
}