From 37b4cf40185bf157b07aeeb29c66db66caf22c42 Mon Sep 17 00:00:00 2001 From: shahaibo <1023316923@qq.com> Date: Tue, 7 May 2024 15:38:17 +0800 Subject: [PATCH] TASK:mixi; --- .../common/tasks/MiTuExportScheduledTask.java | 76 ++++++++++++++----- 1 file changed, 56 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java b/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java index a71e4d6..7f95532 100644 --- a/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java +++ b/src/main/java/com/mixi/common/tasks/MiTuExportScheduledTask.java @@ -26,6 +26,7 @@ import java.nio.file.Paths; import java.sql.*; import java.time.*; import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAdjusters; import java.util.*; import java.util.Date; import java.util.stream.Collectors; @@ -54,11 +55,11 @@ public class MiTuExportScheduledTask { private MiTuProductSellNumMapper miTuProductSellNumMapper; @PostConstruct public void executeWeeklyHeavyStockReport() { -// customerPurchaseReport(); -// NewJoinVIPReport(); -// weeklySellThrReport(); -// WeeklyHeavyStockReport(); -// QuarterlyProductGroupingReport(); + customerPurchaseReport(); + NewJoinVIPReport(); + weeklySellThrReport(); + WeeklyHeavyStockReport(); + QuarterlyProductGroupingReport(); // customerTypeAnalysis(); // getBestSell(); } @@ -315,7 +316,7 @@ public class MiTuExportScheduledTask { * Customer purchase report */ @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) -// @Scheduled(cron = "0 0 22 L * ?") + @Scheduled(cron = "0 0 23 L * ?") public void customerPurchaseReport() { MiTuExport miTuExport = createMiTuExport("Customer purchase report", "month"); try { @@ -347,22 +348,30 @@ public class MiTuExportScheduledTask { case "week": startTime = currentDateTime.with(DayOfWeek.MONDAY).with(LocalTime.MIN); endTime = currentDateTime.with(DayOfWeek.SUNDAY).with(LocalTime.MAX); - exportName = fileName + "_week_" + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); + exportName = fileName + " week " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); break; case "month": startTime = currentDateTime.withDayOfMonth(1).with(LocalTime.MIN); endTime = currentDateTime.withDayOfMonth(currentDateTime.toLocalDate().lengthOfMonth()).with(LocalTime.MAX); - exportName = fileName + "_month_" + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); + exportName = fileName + " month " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); break; case "year": startTime = currentDateTime.withDayOfYear(1).with(LocalTime.MIN); endTime = currentDateTime.withDayOfYear(currentDateTime.toLocalDate().lengthOfYear()).with(LocalTime.MAX); - exportName = fileName + "_year_" + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); + exportName = fileName + " year " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); break; case "quarter": - startTime = currentDateTime.withDayOfYear(1).with(LocalTime.MIN); - endTime = currentDateTime.withDayOfYear(currentDateTime.toLocalDate().lengthOfYear()).with(LocalTime.MAX); - exportName = fileName + "_quarter_" + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); + // 获取当前季度的第一个月和最后一个月 + int currentQuarter = (currentDateTime.getMonthValue() - 1) / 3 + 1; + int firstMonthOfQuarter = (currentQuarter - 1) * 3 + 1; + int lastMonthOfQuarter = currentQuarter * 3; + + // 获取当前季度的起始时间和结束时间 + startTime = currentDateTime.withMonth(firstMonthOfQuarter).withDayOfMonth(1).with(LocalTime.MIN); + endTime = currentDateTime.withMonth(lastMonthOfQuarter).with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); + + // 设置导出名称 + exportName = fileName + " quarter " + currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")); break; default: throw new IllegalArgumentException("Invalid period argument: " + period); @@ -408,7 +417,7 @@ public class MiTuExportScheduledTask { * New Join VIP report */ @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) -// @Scheduled(cron = "0 0 22 L * ?") + @Scheduled(cron = "0 0 23 ? * SUN") public void NewJoinVIPReport() { MiTuExport miTuExport = createMiTuExport("New join vip report", "week"); try { @@ -484,7 +493,7 @@ public class MiTuExportScheduledTask { * Weekly Sell Through Report */ @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) -// @Scheduled(cron = "0 0 22 L * ?") + @Scheduled(cron = "0 0 23 ? * SUN") public void weeklySellThrReport() { MiTuExport miTuExport = createMiTuExport("Weekly sell thr report", "week"); try { @@ -585,7 +594,7 @@ public class MiTuExportScheduledTask { * WeeklyHeavyStockReport */ @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) -// @Scheduled(cron = "0 0 22 L * ?") + @Scheduled(cron = "0 0 23 ? * SUN") public void WeeklyHeavyStockReport() { MiTuExport miTuExport = createMiTuExport("Weekly heavy stock report", "week"); try { @@ -677,7 +686,7 @@ public class MiTuExportScheduledTask { * Quarterly Product Grouping Report */ @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) -// @Scheduled(cron = "0 0 22 L * ?") + @Scheduled(cron = "#{@calculateLastDayOfQuarterCron}") public void QuarterlyProductGroupingReport() { MiTuExport miTuExport = createMiTuExport("Quarterly product grouping report", "quarter"); try { @@ -695,6 +704,23 @@ public class MiTuExportScheduledTask { } } + public String calculateLastDayOfQuarterCron() { + // 获取当前日期 + LocalDate currentDate = LocalDate.now(); + + // 计算下一个季度的最后一天 + Month currentMonth = currentDate.getMonth(); + Month nextQuarterStartMonth = currentMonth.plus(3 - currentMonth.getValue() % 3); + LocalDate nextQuarterStartDate = LocalDate.of(currentDate.getYear(), nextQuarterStartMonth, 1); + LocalDate lastDayOfNextQuarter = nextQuarterStartDate.with(TemporalAdjusters.lastDayOfMonth()); + + // 将日期转换成 Cron 表达式的格式 + int year = lastDayOfNextQuarter.getYear(); + int month = lastDayOfNextQuarter.getMonthValue(); + int dayOfMonth = lastDayOfNextQuarter.getDayOfMonth(); + return String.format("0 0 22 %d %d ?", dayOfMonth, month); + } + private void exportQuarterlyProductGrouping(List weeklyHeavyStockList, String weeklyHeavyStockReportName) { String currentPath = Paths.get("").toAbsolutePath().toString(); File file = new File(currentPath + "/" + weeklyHeavyStockReportName); @@ -1116,6 +1142,17 @@ public class MiTuExportScheduledTask { stmt = conn.createStatement(); String sql; + // 获取当前日期 + LocalDate currentDate = LocalDate.now(); + + // 获取当前星期的第一天(星期一)和最后一天(星期日) + LocalDate firstDayOfWeek = currentDate.with(DayOfWeek.MONDAY); + LocalDate lastDayOfWeek = currentDate.with(DayOfWeek.SUNDAY); + + // 格式化日期字符串 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + String firstDayOfWeekStr = formatter.format(firstDayOfWeek); + String lastDayOfWeekStr = formatter.format(lastDayOfWeek); // 构建完整的 SQL 查询语句 sql = "SELECT TOP 30\n" + " PLU_CODE,\n" + @@ -1134,8 +1171,8 @@ public class MiTuExportScheduledTask { "FROM\n" + " v_MZG020B\n" + "WHERE\n" + - " TRX_DATE >= '2024-01-08'\n" + - " AND TRX_DATE <= '2024-01-14'\n" + + " TRX_DATE >= '" + firstDayOfWeekStr + "' \n" + + " AND TRX_DATE <= '" + lastDayOfWeekStr + "' \n" + "GROUP BY\n" + " PLU_CODE, ITEM_NAME, CATEGORY, SUB_CAT, PRICE_ORIGINAL\n" + "ORDER BY \n" + @@ -1681,7 +1718,6 @@ public class MiTuExportScheduledTask { String sql; sql = "SELECT \n" + " user_member,\n" + - " SUM(CASE WHEN trx_date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AND trx_date <= GETDATE() THEN pay_bas_amt ELSE 0 END) AS thisMonth,\n" + " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) THEN pay_bas_amt ELSE 0 END) AS month1,\n" + " SUM(CASE WHEN trx_date >= DATEADD(MONTH, -2, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) AND trx_date < DATEADD(MONTH, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) THEN pay_bas_amt ELSE 0 END) AS month2,\n" + @@ -1759,7 +1795,7 @@ public class MiTuExportScheduledTask { } @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000)) -// @Scheduled(cron = "0 0 22 L * ?") + @Scheduled(cron = "#{@calculateLastDayOfQuarterCron}") public void customerTypeAnalysis() { try { // 根据近三个月购买记录获取顾客member_code