From a55a247526755a24df9b0ea3131f3ecd9c94c093 Mon Sep 17 00:00:00 2001 From: cst61 Date: Sun, 8 Feb 2026 18:59:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=AE=A2=E6=88=B7=E6=95=B0?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=97=B6=E7=9A=84=E7=A7=9F=E6=88=B7=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomerStatisticsScheduler.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/com/rj/scheduler/CustomerStatisticsScheduler.java 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; + } + } +} +