package com.zksy.base.telemetry.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.telemetry.domain.TelemetryData;
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 针对表【telemetry_data(平升遥测终端协议数据表)】的数据库操作Mapper
* @createDate 2026-01-16 15:43:42
* @Entity com.zksy.base.telemetry.domain.TelemetryData
*/
public interface TelemetryDataMapper extends BaseMapper<TelemetryData> {
    /**
     * 按设备分组统计关键指标（平均流速、压力、温度、瞬时流量）
     */
    @Select("SELECT device_code, " +
            "AVG(CAST(flow_speed AS DECIMAL(10,2))) as avg_flow_speed, " +
            "AVG(CAST(pressure AS DECIMAL(10,2))) as avg_pressure, " +
            "AVG(CAST(temperature AS DECIMAL(10,2))) as avg_temperature, " +
            "AVG(CAST(meter1_instant_flow AS DECIMAL(10,2))) as avg_instant_flow, " +
            "COUNT(*) as data_count " +
            "FROM telemetry_data " +
            "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY device_code")
    List<Map<String, Object>> statTelemetryByDevice(@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, " +
            "AVG(CAST(pressure AS DECIMAL(10,2))) as avg_pressure, " +
            "AVG(CAST(temperature AS DECIMAL(10,2))) as avg_temperature " +
            "FROM telemetry_data " +
            "WHERE device_code = #{deviceCode} AND create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY hour_time ORDER BY hour_time")
    List<Map<String, Object>> statTelemetryTrendByHour(@Param("deviceCode") String deviceCode,
                                                       @Param("startTime") LocalDateTime startTime,
                                                       @Param("endTime") LocalDateTime endTime);

    /**
     * 查询异常数据（压力/温度/流速超过阈值）
     */
    @Select("SELECT * FROM telemetry_data " +
            "WHERE (CAST(pressure AS DECIMAL(10,2)) > #{pressureThreshold} " +
            "OR CAST(temperature AS DECIMAL(10,2)) > #{tempThreshold} " +
            "OR CAST(flow_speed AS DECIMAL(10,2)) > #{speedThreshold}) " +
            "AND create_time BETWEEN #{startTime} AND #{endTime} " +
            "ORDER BY create_time DESC")
    IPage<TelemetryData> queryAbnormalData(Page<TelemetryData> page,
                                           @Param("pressureThreshold") Double pressureThreshold,
                                           @Param("tempThreshold") Double tempThreshold,
                                           @Param("speedThreshold") Double speedThreshold,
                                           @Param("startTime") LocalDateTime startTime,
                                           @Param("endTime") LocalDateTime endTime);

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




