定时器 ,周期性的统计分析品牌维度

This commit is contained in:
spllzh
2025-09-21 16:53:07 +08:00
parent cd72a3680c
commit 004ba02996
26 changed files with 118 additions and 207 deletions

View File

@@ -106,36 +106,81 @@ public class CpaBrandScoreStatisticsServiceImpl extends ServiceImpl<CpaBrandScor
public int generateAndSaveStatistics(String statisticsType, LocalDate startDate, LocalDate endDate, String createdBy) {
try {
log.info("开始生成统计数据,统计类型:{},日期范围:{} - {}", statisticsType, startDate, endDate);
// 先删除已存在的统计数据
deleteByCondition(null, statisticsType, startDate, endDate);
List<Map<String, Object>> statisticsResults;
// 根据统计类型执行不同的统计SQL
if ("daily".equals(statisticsType)) {
// 日统计:按经销商和日期双重分组
// 日统计:使用传入的日期,按经销商和日期双重分组
if (startDate == null) {
throw new IllegalArgumentException("日统计需要提供统计日期");
}
// 先删除已存在的统计数据
deleteByCondition(null, statisticsType, startDate, startDate);
String startDateTime = startDate.atStartOfDay().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String endDateTime = startDate.atTime(23, 59, 59).format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
statisticsResults = cpaBrandScoreStatisticsMapper.executeDailyStatistics(startDateTime, endDateTime);
} else if ("weekly".equals(statisticsType) || "monthly".equals(statisticsType)) {
// 周统计或月统计:按日期范围统计
String startDateTime = startDate.atStartOfDay().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String endDateTime = endDate.atTime(23, 59, 59).format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} else if ("weekly".equals(statisticsType)) {
// 周统计:自动计算上周的开始和结束时间
LocalDate lastWeekStart = LocalDate.now().minusWeeks(1).with(java.time.DayOfWeek.MONDAY);
LocalDate lastWeekEnd = lastWeekStart.plusDays(6);
log.info("周统计自动计算时间范围:{} - {}", lastWeekStart, lastWeekEnd);
// 先删除已存在的统计数据
deleteByCondition(null, statisticsType, lastWeekStart, lastWeekEnd);
String startDateTime = lastWeekStart.atStartOfDay().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String endDateTime = lastWeekEnd.atTime(23, 59, 59).format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
statisticsResults = cpaBrandScoreStatisticsMapper.executeDateRangeStatistics(startDateTime, endDateTime);
} else if ("monthly".equals(statisticsType)) {
// 月统计:自动计算上个月的开始和结束时间
LocalDate lastMonthStart = LocalDate.now().minusMonths(1).withDayOfMonth(1);
LocalDate lastMonthEnd = lastMonthStart.withDayOfMonth(lastMonthStart.lengthOfMonth());
log.info("月统计自动计算时间范围:{} - {}", lastMonthStart, lastMonthEnd);
// 先删除已存在的统计数据
deleteByCondition(null, statisticsType, lastMonthStart, lastMonthEnd);
String startDateTime = lastMonthStart.atStartOfDay().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String endDateTime = lastMonthEnd.atTime(23, 59, 59).format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
statisticsResults = cpaBrandScoreStatisticsMapper.executeDateRangeStatistics(startDateTime, endDateTime);
} else if ("total".equals(statisticsType)) {
// 总计统计:统计所有历史数据
// 先删除已存在的统计数据
deleteByCondition(null, statisticsType, null, null);
statisticsResults = cpaBrandScoreStatisticsMapper.executeTotalStatistics();
} else {
throw new IllegalArgumentException("不支持的统计类型:" + statisticsType);
}
// 转换为实体对象并保存
List<CpaBrandScoreStatistics> statisticsList = convertToStatisticsList(statisticsResults, endDate, statisticsType, createdBy);
LocalDate statisticsDate;
if ("weekly".equals(statisticsType)) {
// 周统计使用上周的结束日期作为统计日期
statisticsDate = LocalDate.now().minusWeeks(1).with(java.time.DayOfWeek.MONDAY).plusDays(6);
} else if ("monthly".equals(statisticsType)) {
// 月统计使用上月的最后一天作为统计日期
statisticsDate = LocalDate.now().minusMonths(1).withDayOfMonth(1).withDayOfMonth(
LocalDate.now().minusMonths(1).withDayOfMonth(1).lengthOfMonth());
} else {
// 日统计和总计统计使用传入的日期
statisticsDate = endDate;
}
List<CpaBrandScoreStatistics> statisticsList = convertToStatisticsList(statisticsResults, statisticsDate, statisticsType, createdBy);
boolean success = batchSave(statisticsList);
if (success) {
log.info("统计数据生成完成,统计类型:{},生成记录数:{}", statisticsType, statisticsList.size());
log.info("统计数据生成完成,统计类型:{}统计日期:{}生成记录数:{}", statisticsType, statisticsDate, statisticsList.size());
return statisticsList.size();
} else {
log.error("统计数据保存失败,统计类型:{}", statisticsType);