package com.zksy.base.radar.mapper;

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

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

/**
* @author Administrator
* @description 针对表【radar_data(平升雷达流量计数据表)】的数据库操作Mapper
* @createDate 2026-01-16 15:37:38
* @Entity com.zksy.base.radar.domain.RadarData
*/
public interface RadarDataMapper extends BaseMapper<RadarData> {
    /**
     * 按设备分组统计关键指标（平均流速、平均水位）
     */
    @Select("SELECT device_code, " +
            "AVG(CAST(flow_speed AS DECIMAL(10,2))) as avg_flow_speed, " +
            "AVG(CAST(water_level AS DECIMAL(10,2))) as avg_water_level, " +
            "COUNT(*) as data_count " +
            "FROM radar_data " +
            "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY device_code")
    List<Map<String, Object>> statRadarByDevice(@Param("startTime") LocalDateTime startTime,
                                                @Param("endTime") LocalDateTime endTime);

    /**
     * 按小时统计设备流量趋势
     */
    @Select("SELECT DATE_FORMAT(create_time, '%Y-%m-%d %H:00:00') as hour_time, " +
            "AVG(CAST(meter1_instant_flow AS DECIMAL(10,2))) as avg_instant_flow, " +
            "MAX(CAST(meter1_instant_flow AS DECIMAL(10,2))) as max_instant_flow, " +
            "MIN(CAST(meter1_instant_flow AS DECIMAL(10,2))) as min_instant_flow " +
            "FROM radar_data " +
            "WHERE device_code = #{deviceCode} AND create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY hour_time ORDER BY hour_time")
    List<Map<String, Object>> statFlowTrendByHour(@Param("deviceCode") String deviceCode,
                                                  @Param("startTime") LocalDateTime startTime,
                                                  @Param("endTime") LocalDateTime endTime);

    /**
     * 查询异常数据（流速/水位超过阈值）
     */
    @Select("SELECT * FROM radar_data " +
            "WHERE (CAST(flow_speed AS DECIMAL(10,2)) > #{speedThreshold} OR CAST(water_level AS DECIMAL(10,2)) > #{levelThreshold}) " +
            "AND create_time BETWEEN #{startTime} AND #{endTime} " +
            "ORDER BY create_time DESC")
    IPage<RadarData> queryAbnormalData(Page<RadarData> page,
                                       @Param("speedThreshold") Double speedThreshold,
                                       @Param("levelThreshold") Double levelThreshold,
                                       @Param("startTime") LocalDateTime startTime,
                                       @Param("endTime") LocalDateTime endTime);

    /**
     * 获取设备最新数据
     */
    @Select("SELECT * FROM radar_data " +
            "WHERE device_code = #{deviceCode} " +
            "ORDER BY create_time DESC LIMIT 1")
    RadarData getLatestDataByDevice(@Param("deviceCode") String deviceCode);
}




