package com.rj.example;

import com.rj.entity.AudioManagementStatistics;
import com.rj.scheduler.AudioStatisticsScheduler;
import com.rj.service.IAudioManagementStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 门店录音统计功能使用示例
 * 
 * @author 李中华 ,spllzh
 * @since 2025-08-07
 */
@Component
public class AudioStatisticsExample {

    @Autowired
    private IAudioManagementStatisticsService audioStatisticsService;

    @Autowired
    private AudioStatisticsScheduler audioStatisticsScheduler;

    /**
     * 示例1：手动生成指定日期的统计数据
     */
    public void exampleGenerateStatistics() {
        System.out.println("=== 示例1：手动生成指定日期的统计数据 ===");
        
        // 生成昨天的统计数据
        LocalDate yesterday = LocalDate.now().minusDays(1);
        int count = audioStatisticsScheduler.manualGenerateStatistics(yesterday);
        
        System.out.println("生成统计记录数: " + count);
        System.out.println("统计日期: " + yesterday);
    }

    /**
     * 示例2：查询最近7天的统计数据
     */
    public void exampleQueryRecentStatistics() {
        System.out.println("=== 示例2：查询最近7天的统计数据 ===");
        
        LocalDate endDate = LocalDate.now().minusDays(1); // 昨天
        LocalDate startDate = endDate.minusDays(6); // 7天前
        
        List<AudioManagementStatistics> statistics = audioStatisticsService.getStatisticsByDateRange(startDate, endDate);
        
        System.out.println("查询到统计记录数: " + statistics.size());
        System.out.println("查询日期范围: " + startDate + " 到 " + endDate);
        
        // 打印统计信息
        for (AudioManagementStatistics stat : statistics) {
            System.out.println("门店: " + stat.getDealershipName() + 
                             ", 日期: " + stat.getStatisticsDate() + 
                             ", 录音数量: " + stat.getDealershipRecordingCount() + 
                             ", 总时长: " + stat.getDealershipTotalDuration() + "秒" +
                             ", 平均时长: " + stat.getDealershipAvgDuration() + "秒");
        }
    }

    /**
     * 示例3：查询指定门店的统计数据
     */
    public void exampleQueryDealershipStatistics() {
        System.out.println("=== 示例3：查询指定门店的统计数据 ===");
        
        Long dealershipId = 1L; // 假设门店ID为1
        LocalDate startDate = LocalDate.now().minusDays(30); // 最近30天
        LocalDate endDate = LocalDate.now().minusDays(1);
        
        List<AudioManagementStatistics> statistics = audioStatisticsService.getStatisticsByDealership(dealershipId, startDate, endDate);
        
        System.out.println("门店ID: " + dealershipId);
        System.out.println("查询到统计记录数: " + statistics.size());
        
        // 计算该门店的总统计
        long totalDuration = 0;
        int totalCount = 0;
        
        for (AudioManagementStatistics stat : statistics) {
            totalDuration += stat.getDealershipTotalDuration();
            totalCount += stat.getDealershipRecordingCount();
        }
        
        System.out.println("门店总录音时长: " + totalDuration + "秒");
        System.out.println("门店总录音数量: " + totalCount);
        if (totalCount > 0) {
            System.out.println("门店平均录音时长: " + (totalDuration / totalCount) + "秒");
        }
    }

    /**
     * 示例4：查询指定销售人员的统计数据
     */
    public void exampleQuerySalesStatistics() {
        System.out.println("=== 示例4：查询指定销售人员的统计数据 ===");
        
        String salesId = "sales001"; // 假设销售人员ID
        LocalDate startDate = LocalDate.now().minusDays(30); // 最近30天
        LocalDate endDate = LocalDate.now().minusDays(1);
        
        List<AudioManagementStatistics> statistics = audioStatisticsService.getStatisticsBySales(salesId, startDate, endDate);
        
        System.out.println("销售人员ID: " + salesId);
        System.out.println("查询到统计记录数: " + statistics.size());
        
        // 计算该销售人员的总统计
        long totalDuration = 0;
        int totalCount = 0;
        
        for (AudioManagementStatistics stat : statistics) {
            totalDuration += stat.getSalesTotalDuration();
            totalCount += stat.getSalesRecordingCount();
        }
        
        System.out.println("销售人员总录音时长: " + totalDuration + "秒");
        System.out.println("销售人员总录音数量: " + totalCount);
        if (totalCount > 0) {
            System.out.println("销售人员平均录音时长: " + (totalDuration / totalCount) + "秒");
        }
    }

    /**
     * 示例6：批量生成历史统计数据
     */
    public void exampleBatchGenerateStatistics() {
        System.out.println("=== 示例6：批量生成历史统计数据 ===");
        
        // 生成最近30天的统计数据
        LocalDate endDate = LocalDate.now().minusDays(1);
        LocalDate startDate = endDate.minusDays(29);
        
        int totalCount = 0;
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            try {
                int count = audioStatisticsScheduler.manualGenerateStatistics(date);
                totalCount += count;
                System.out.println("日期 " + date + " 生成统计记录数: " + count);
            } catch (Exception e) {
                System.err.println("日期 " + date + " 统计失败: " + e.getMessage());
            }
        }
        
        System.out.println("批量生成完成，总记录数: " + totalCount);
    }


}
