This commit is contained in:
tianfeng 2025-10-31 20:23:29 +08:00
parent 27ea713593
commit d197793839
4 changed files with 133 additions and 9 deletions

View File

@ -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")));
}
/**
* 测试方法手动触发.
*/

View File

@ -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")));
}
/**
* 测试方法手动触发.
*/

View File

@ -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<List<TeamBillCycle>> execute, Integer limit) {
scan(execute, TeamBillCycleStatus.PAY_OUT, limit);

View File

@ -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")));
}
}