From d1977938392de8a3e416719baf3ac9a4ac68e696 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 31 Oct 2025 20:23:29 +0800 Subject: [PATCH] =?UTF-8?q?bd=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BdLeaderSalarySettlementTask.java | 35 +++++++++++++++-- .../app/scheduler/BdSalarySettlementTask.java | 39 ++++++++++++++++--- .../team/impl/TeamBillCycleServiceImpl.java | 30 +++++++++++++- .../other-start/src/test/java/ApiTest.java | 38 ++++++++++++++++++ 4 files changed, 133 insertions(+), 9 deletions(-) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdLeaderSalarySettlementTask.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdLeaderSalarySettlementTask.java index e3386bbd..ba57267a 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdLeaderSalarySettlementTask.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdLeaderSalarySettlementTask.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.ZoneId; +import java.time.format.DateTimeFormatter; /** * BD Leader 工资结算定时任务. @@ -28,15 +29,18 @@ public class BdLeaderSalarySettlementTask { /** * 每月1号和16号 0点执行半月结算. */ - @Scheduled(cron = "0 0 0 1,16 * ?", zone = "Asia/Riyadh") + @Scheduled(cron = "0 0 2 1,16 * ?", zone = "Asia/Riyadh") @TaskCacheLock(key = "BD_LEADER_SALARY_SETTLEMENT", expireSecond = 86400) public void processBdLeaderSalarySettlement() { long startTime = System.currentTimeMillis(); log.warn("========== BD Leader工资结算定时任务开始 =========="); try { - // 判断是上半月还是下半月 - Integer billBelong = TeamBillCycleUtils.getCalcBillBelong(); + // 获取当前日期的账期 + Integer currentBillBelong = TeamBillCycleUtils.getCalcBillBelong(); + + // 计算需要结算的账期(往前推一个账期) + Integer billBelong = calculateSettlementBillBelong(currentBillBelong); String billTitle = TeamBillCycleUtils.parseBillBelongToDateRangeStr(billBelong); log.warn("结算周期:{} - {}", billBelong, billTitle); @@ -51,6 +55,31 @@ public class BdLeaderSalarySettlementTask { } } + /** + * 计算需要结算的账期 + * 规则: + * - 如果当前账期是本月1号(如20250101),则结算上个月16号的账期(如20241216) + * - 如果当前账期是本月16号(如20250116),则结算本月1号的账期(如20250101) + */ + private Integer calculateSettlementBillBelong(Integer currentBillBelong) { + String billStr = String.valueOf(currentBillBelong); + int year = Integer.parseInt(billStr.substring(0, 4)); + int month = Integer.parseInt(billStr.substring(4, 6)); + int day = Integer.parseInt(billStr.substring(6, 8)); + + LocalDate settlementDate; + + if (day == 1) { + // 当前是本月1号,结算上个月16号 + settlementDate = LocalDate.of(year, month, 1).minusMonths(1).withDayOfMonth(16); + } else { + // 当前是本月16号,结算本月1号 + settlementDate = LocalDate.of(year, month, 1); + } + + return Integer.parseInt(settlementDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"))); + } + /** * 测试方法(手动触发). */ diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdSalarySettlementTask.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdSalarySettlementTask.java index 81586951..6bca6e41 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdSalarySettlementTask.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/BdSalarySettlementTask.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.ZoneId; +import java.time.format.DateTimeFormatter; /** * BD 工资结算定时任务. @@ -28,18 +29,21 @@ public class BdSalarySettlementTask { /** * 每月1号和16号 0点执行半月结算. */ - @Scheduled(cron = "0 0 0 1,16 * ?", zone = "Asia/Riyadh") + @Scheduled(cron = "0 0 1 1,16 * ?", zone = "Asia/Riyadh") @TaskCacheLock(key = "BD_SALARY_SETTLEMENT", expireSecond = 86400) public void processBdSalarySettlement() { long startTime = System.currentTimeMillis(); log.warn("========== BD工资结算定时任务开始 =========="); try { - // 判断是上半月还是下半月 - Integer billBelong = TeamBillCycleUtils.getCalcBillBelong(); - String billTitle = TeamBillCycleUtils.parseBillBelongToDateRangeStr(billBelong); + // 获取当前日期的账期 + Integer currentBillBelong = TeamBillCycleUtils.getCalcBillBelong(); - log.warn("结算周期:{} - {}", billBelong, billTitle); + // 计算需要结算的账期(往前推一个账期) + Integer billBelong = calculateSettlementBillBelong(currentBillBelong); + String billTitle = TeamBillCycleUtils.parseBillBelongToDateRangeStr(billBelong); + + log.warn("当前账期:{},结算账期:{} - {}", currentBillBelong, billBelong, billTitle); // 执行 BD 工资结算 bdSalarySettlementService.processAllBdSettlement(billBelong, billTitle); @@ -51,6 +55,31 @@ public class BdSalarySettlementTask { } } + /** + * 计算需要结算的账期 + * 规则: + * - 如果当前账期是本月1号(如20250101),则结算上个月16号的账期(如20241216) + * - 如果当前账期是本月16号(如20250116),则结算本月1号的账期(如20250101) + */ + private Integer calculateSettlementBillBelong(Integer currentBillBelong) { + String billStr = String.valueOf(currentBillBelong); + int year = Integer.parseInt(billStr.substring(0, 4)); + int month = Integer.parseInt(billStr.substring(4, 6)); + int day = Integer.parseInt(billStr.substring(6, 8)); + + LocalDate settlementDate; + + if (day == 1) { + // 当前是本月1号,结算上个月16号 + settlementDate = LocalDate.of(year, month, 1).minusMonths(1).withDayOfMonth(16); + } else { + // 当前是本月16号,结算本月1号 + settlementDate = LocalDate.of(year, month, 1); + } + + return Integer.parseInt(settlementDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"))); + } + /** * 测试方法(手动触发). */ diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/team/team/impl/TeamBillCycleServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/team/team/impl/TeamBillCycleServiceImpl.java index 4a7de53f..0376b2bd 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/team/team/impl/TeamBillCycleServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/team/team/impl/TeamBillCycleServiceImpl.java @@ -21,6 +21,7 @@ import com.red.circle.tool.core.sequence.IdWorkerUtils; import com.red.circle.tool.core.text.StringUtils; import java.math.BigDecimal; +import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; @@ -459,11 +460,12 @@ public class TeamBillCycleServiceImpl implements TeamBillCycleService { public void billStatusPayOut() { // 计算当前账期的 billBelong Integer currentBillBelong = getCalcBillBelong(); + Integer billBelong = calculateSettlementBillBelong(currentBillBelong); // 将小于当前账期的 UNPAID 状态改为 PAY_OUT mongoTemplate.updateMulti( Query.query( - Criteria.where("billBelong").lt(currentBillBelong) + Criteria.where("billBelong").lt(billBelong) .and("status").is(TeamBillCycleStatus.UNPAID)), new Update() .set("updateTime", TimestampUtils.now()) @@ -471,6 +473,32 @@ public class TeamBillCycleServiceImpl implements TeamBillCycleService { TeamBillCycle.class); } + + /** + * 计算需要结算的账期 + * 规则: + * - 如果当前账期是本月1号(如20250101),则结算上个月16号的账期(如20241216) + * - 如果当前账期是本月16号(如20250116),则结算本月1号的账期(如20250101) + */ + private Integer calculateSettlementBillBelong(Integer currentBillBelong) { + String billStr = String.valueOf(currentBillBelong); + int year = Integer.parseInt(billStr.substring(0, 4)); + int month = Integer.parseInt(billStr.substring(4, 6)); + int day = Integer.parseInt(billStr.substring(6, 8)); + + LocalDate settlementDate; + + if (day == 1) { + // 当前是本月1号,结算上个月16号 + settlementDate = LocalDate.of(year, month, 1).minusMonths(1).withDayOfMonth(16); + } else { + // 当前是本月16号,结算本月1号 + settlementDate = LocalDate.of(year, month, 1); + } + + return Integer.parseInt(settlementDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"))); + } + @Override public void scanPayOut(Consumer> execute, Integer limit) { scan(execute, TeamBillCycleStatus.PAY_OUT, limit); diff --git a/rc-service/rc-service-other/other-start/src/test/java/ApiTest.java b/rc-service/rc-service-other/other-start/src/test/java/ApiTest.java index 8e14cd0d..ee4435d4 100644 --- a/rc-service/rc-service-other/other-start/src/test/java/ApiTest.java +++ b/rc-service/rc-service-other/other-start/src/test/java/ApiTest.java @@ -4,6 +4,11 @@ import com.red.circle.tool.crypto.SecurityUtils; import org.junit.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import static com.red.circle.other.infra.database.mongo.service.team.TeamBillCycleUtils.getCalcBillBelong; + public class ApiTest { @Test @@ -23,5 +28,38 @@ public class ApiTest { System.out.println(ZonedDateTimeAsiaRiyadhUtils.nowFormat("yyyyMMddHH")); } + @Test + public void test3() { + // 计算当前账期的 billBelong + Integer currentBillBelong = 20251101; + Integer billBelong = calculateSettlementBillBelong(currentBillBelong); + System.out.println(billBelong); + } + + /** + * 计算需要结算的账期 + * 规则: + * - 如果当前账期是本月1号(如20250101),则结算上个月16号的账期(如20241216) + * - 如果当前账期是本月16号(如20250116),则结算本月1号的账期(如20250101) + */ + private Integer calculateSettlementBillBelong(Integer currentBillBelong) { + String billStr = String.valueOf(currentBillBelong); + int year = Integer.parseInt(billStr.substring(0, 4)); + int month = Integer.parseInt(billStr.substring(4, 6)); + int day = Integer.parseInt(billStr.substring(6, 8)); + + LocalDate settlementDate; + + if (day == 1) { + // 当前是本月1号,结算上个月16号 + settlementDate = LocalDate.of(year, month, 1).minusMonths(1).withDayOfMonth(16); + } else { + // 当前是本月16号,结算本月1号 + settlementDate = LocalDate.of(year, month, 1); + } + + return Integer.parseInt(settlementDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"))); + } + }