package com.rj.mapper;

import com.rj.entity.AudioManagementStatistics;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * <p>
 * 门店录音统计表 Mapper 接口
 * </p>
 *
 * @author 李中华 ,spllzh
 * @since 2025-08-07
 */
@Mapper
public interface AudioManagementStatisticsMapper extends BaseMapper<AudioManagementStatistics> {

    /**
     * 按门店统计指定日期的录音数据
     * @param date 统计日期
     * @return 门店统计结果列表
     */
    @Select("SELECT " +
            "dealership_id, " +
            "dealership_name, " +
            "SUM(duration) as total_duration_minutes, " +
            "COUNT(*) as recording_count, " +
            "AVG(duration) as avg_duration_minutes " +
            "FROM audio_management " +
            "WHERE DATE(recording_time) = #{date} " +
            "AND dealership_id IS NOT NULL " +
            "AND dealership_id != '' " +
            "GROUP BY dealership_id, dealership_name")
    List<Map<String, Object>> getDealershipStatisticsByDate(@Param("date") LocalDate date);

    /**
     * 按销售人员统计指定日期的录音数据
     * @param date 统计日期
     * @return 销售人员统计结果列表
     */
    @Select("SELECT " +
            "sales_id, " +
            "SUM(duration) as total_duration_minutes, " +
            "COUNT(*) as recording_count, " +
            "AVG(duration) as avg_duration_minutes " +
            "FROM audio_management " +
            "WHERE DATE(recording_time) = #{date} " +
            "AND sales_id IS NOT NULL " +
            "AND sales_id != '' " +
            "GROUP BY sales_id")
    List<Map<String, Object>> getSalesStatisticsByDate(@Param("date") LocalDate date);

    /**
     * 检查指定日期的统计数据是否已存在
     * @param date 统计日期
     * @return 统计记录数量
     */
    @Select("SELECT COUNT(*) FROM audio_management_statistics WHERE statistics_date = #{date}")
    int countByStatisticsDate(@Param("date") LocalDate date);

    /**
     * 删除指定日期的统计数据
     * @param date 统计日期
     * @return 删除的记录数
     */
    int deleteByStatisticsDate(@Param("date") LocalDate date);

    /**
     * 根据销售人员和日期范围聚合查询总录音时长和平均录音时长
     * @param salesId 销售人员ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 聚合统计结果（包含总录音时长、总录音数量、平均录音时长、销售人员姓名）
     */
    @Select("SELECT " +
            "sales_id, " +
            "SUM(personal_total_duration) as total_duration, " +
            "SUM(personal_recording_count) as total_count, " +
            "CASE " +
            "  WHEN SUM(personal_recording_count) > 0 THEN " +
            "    ROUND(SUM(personal_total_duration) / SUM(personal_recording_count), 2) " +
            "  ELSE 0 " +
            "END as avg_duration " +
            "FROM audio_management_statistics " +
            "WHERE sales_id = #{salesId} " +
            "AND statistics_date >= #{startDate} " +
            "AND statistics_date <= #{endDate} " +
            "AND sales_id IS NOT NULL " +
            "AND sales_id != '' " +
            "GROUP BY sales_id")
    Map<String, Object> getSalesAggregateStatistics(@Param("salesId") String salesId, 
                                                      @Param("startDate") LocalDate startDate, 
                                                      @Param("endDate") LocalDate endDate);
}
