package com.zksy.base.audio.mapper;

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

import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【noise_info(音频信息表)】的数据库操作Mapper
* @createDate 2026-01-16 15:23:32
* @Entity com.zksy.base.audio.domain.NoiseInfo
*/
public interface NoiseInfoMapper extends BaseMapper<NoiseInfo> {
    /**
     * 按设备分组统计功率平均值
     */
    @Select("SELECT encode, AVG(power) as avg_power, COUNT(*) as data_count " +
            "FROM noise_info " +
            "WHERE ts BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY encode")
    List<Map<String, Object>> statPowerByDevice(@Param("startTime") String startTime,
                                                @Param("endTime") String endTime);

    /**
     * 按时间区间统计功率趋势（按小时）
     */
    @Select("SELECT DATE_FORMAT(ts, '%Y-%m-%d %H:00:00') as hour_time, " +
            "AVG(power) as avg_power, MAX(power) as max_power, MIN(power) as min_power " +
            "FROM noise_info " +
            "WHERE encode = #{encode} AND ts BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY hour_time ORDER BY hour_time")
    List<Map<String, Object>> statPowerTrendByHour(@Param("encode") String encode,
                                                   @Param("startTime") String startTime,
                                                   @Param("endTime") String endTime);

    /**
     * 查询异常数据（功率超过阈值）
     */
    @Select("SELECT * FROM noise_info " +
            "WHERE power > #{threshold} AND ts BETWEEN #{startTime} AND #{endTime} " +
            "ORDER BY ts DESC")
    IPage<NoiseInfo> queryAbnormalData(Page<NoiseInfo> page,
                                       @Param("threshold") Double threshold,
                                       @Param("startTime") String startTime,
                                       @Param("endTime") String endTime);

    /**
     * 获取设备最新数据
     */
    @Select("SELECT * FROM noise_info " +
            "WHERE encode = #{encode} " +
            "ORDER BY ts DESC LIMIT 1")
    NoiseInfo getLatestDataByDevice(@Param("encode") String encode);
}




