@@ -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 = end Date. atTime ( 23 , 59 , 59 ) . format ( java . time . format . DateTimeFormatter . ofPattern ( " yyyy-MM-dd HH:mm:ss " ) ) ;
} else if ( " weekly " . equals ( statisticsType ) ) {
// 周统计:自动计算上周的开始和结束时间
LocalDate lastWeekStart = Local Date. 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 , end Date , statisticsType , createdBy ) ;
Local Date 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 ) ;