diff --git a/src/main/java/com/rj/scheduler/CustomerStatisticsScheduler.java b/src/main/java/com/rj/scheduler/CustomerStatisticsScheduler.java new file mode 100644 index 0000000..2d20c59 --- /dev/null +++ b/src/main/java/com/rj/scheduler/CustomerStatisticsScheduler.java @@ -0,0 +1,70 @@ +package com.rj.scheduler; + +import com.rj.config.AppConfig; +import com.rj.service.ICustomerManagementStatisticsService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.time.LocalDate; + +/** + * 客户统计定时任务 + * + * @author 李中华 ,spllzh + * @since 2025-08-07 + */ +@Slf4j +@Component +public class CustomerStatisticsScheduler { + + @Autowired + private ICustomerManagementStatisticsService customerStatisticsService; + + @Autowired + private AppConfig appConfig; + + /** + * 每天凌晨2点执行客户统计 + * 统计昨天的客户数据 + * 根据租户id、经销商id、项目id、销售人员id,统计客户总数,每天生成一条记录 + */ + @Scheduled(cron = "0 0 2 * * ?") + public void generateDailyCustomerStatistics() { + try { + // 检查调度器是否启用 + if (!appConfig.getScheduler().isStart()) { + log.info("调度器未启用,跳过客户统计任务执行"); + return; + } + + log.info("开始执行客户统计任务..."); + + LocalDate yesterday = LocalDate.now().minusDays(1); + int count = customerStatisticsService.generateStatisticsByDate(yesterday); + + log.info("客户统计任务执行完成,统计日期:{},生成记录数:{}", yesterday, count); + } catch (Exception e) { + log.error("客户统计任务执行失败", e); + } + } + + /** + * 手动触发统计任务(用于测试或补录数据) + * @param date 要统计的日期 + * @return 生成的记录数 + */ + public int manualGenerateStatistics(LocalDate date) { + try { + log.info("手动执行客户统计任务,统计日期:{}", date); + int count = customerStatisticsService.generateStatisticsByDate(date); + log.info("手动客户统计任务执行完成,统计日期:{},生成记录数:{}", date, count); + return count; + } catch (Exception e) { + log.error("手动客户统计任务执行失败,统计日期:{}", date, e); + throw e; + } + } +} +