|
|
@@ -240,17 +240,17 @@ public class SettlementEngine {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- Boolean applied = applyInTransaction(current, plan, bizDate);
|
|
|
- if (Boolean.TRUE.equals(applied)) {
|
|
|
+ Long journalId = applyInTransaction(current, plan, bizDate);
|
|
|
+ if (journalId != null) {
|
|
|
tradeSettlementProducer.sendTradeSettledEvent(
|
|
|
- current.getId(),
|
|
|
+ journalId,
|
|
|
current.getUserId(),
|
|
|
plan.profitAmount(),
|
|
|
"BEX",
|
|
|
"STAKING");
|
|
|
return SettleResult.success(orderNo);
|
|
|
}
|
|
|
- // applied == false:乐观锁版本冲突,重新查询后重试
|
|
|
+ // journalId == null:乐观锁版本冲突,重新查询后重试
|
|
|
log.debug("订单结算乐观锁冲突, 第 {} 次重试, orderNo={}", attempt, orderNo);
|
|
|
} catch (DuplicateKeyException e) {
|
|
|
// 并发下唯一约束 uk_order_date 兜底:视为已结算跳过(需求 9.7)
|
|
|
@@ -276,16 +276,16 @@ public class SettlementEngine {
|
|
|
* 在单个本地事务内应用账本更新与流水插入(订单更新 → 流水插入,保证原子一致)。
|
|
|
*
|
|
|
* <p>先以乐观锁更新订单账本:{@code updateById} 携带 {@code version},返回 0 表示版本冲突,此时事务内
|
|
|
- * 未发生任何变更,直接返回 {@code false},由调用方重读后重试;返回 1 后插入收益流水
|
|
|
+ * 未发生任何变更,直接返回 {@code null},由调用方重读后重试;返回 1 后插入收益流水
|
|
|
* ({@link ProfitStatus#UNCLAIMED}),若命中唯一约束 {@code uk_order_date} 抛 {@link DuplicateKeyException},
|
|
|
* 触发整个事务回滚(订单账本更新一并撤销),由调用方按"已结算"幂等跳过处理。
|
|
|
*
|
|
|
* @param current 当前订单快照(提供 id 与 version)
|
|
|
* @param plan 账本累加计划
|
|
|
* @param bizDate 业务日期 {@code YYYYMMDD}
|
|
|
- * @return {@code true} 表示成功落库;{@code false} 表示乐观锁版本冲突需重试
|
|
|
+ * @return 收益流水ID 表示成功落库;{@code null} 表示乐观锁版本冲突需重试
|
|
|
*/
|
|
|
- private Boolean applyInTransaction(StakingOrder current, SettlementPlan plan, int bizDate) {
|
|
|
+ private Long applyInTransaction(StakingOrder current, SettlementPlan plan, int bizDate) {
|
|
|
return transactionTemplate.execute(status -> {
|
|
|
// 3.1 乐观锁更新订单账本(需求 9.5)
|
|
|
StakingOrder update = new StakingOrder();
|
|
|
@@ -299,12 +299,13 @@ public class SettlementEngine {
|
|
|
int rows = orderMapper.updateById(update);
|
|
|
if (rows == 0) {
|
|
|
// 版本冲突:事务内无变更,回滚(实为空操作)后由调用方重试
|
|
|
- return Boolean.FALSE;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
// 3.2 插入收益流水(status=UNCLAIMED);uk_order_date 冲突将抛 DuplicateKeyException 触发回滚
|
|
|
- profitJournalMapper.insert(buildJournal(current, plan, bizDate));
|
|
|
- return Boolean.TRUE;
|
|
|
+ StakingProfitJournal journal = buildJournal(current, plan, bizDate);
|
|
|
+ profitJournalMapper.insert(journal);
|
|
|
+ return journal.getId();
|
|
|
});
|
|
|
}
|
|
|
|