fix(points): prevent stale integral balance updates

Use atomic integral updates during balance payment so older user snapshots cannot overwrite newer self-bonus rewards.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
danaisuiyuan
2026-05-19 08:57:13 +08:00
parent 8c3ff509fc
commit 9b4020d44f
2 changed files with 25 additions and 1 deletions

View File

@@ -691,7 +691,10 @@ public class OrderPayServiceImpl implements OrderPayService {
userService.updateNowMoney(user, storeOrder.getPayPrice(), "sub"); userService.updateNowMoney(user, storeOrder.getPayPrice(), "sub");
// 扣除积分 // 扣除积分
if (storeOrder.getUseIntegral() > 0) { if (storeOrder.getUseIntegral() > 0) {
userService.updateIntegral(user, storeOrder.getUseIntegral(), "sub"); Boolean integralUpdated = userService.updateIntegral(user, storeOrder.getUseIntegral(), "sub");
if (!integralUpdated) {
throw new CrmebException("用户积分不足");
}
} }
// 添加支付成功redis队列 // 添加支付成功redis队列
redisUtil.lPush(TaskConstants.ORDER_TASK_PAY_SUCCESS_AFTER, storeOrder.getOrderId()); redisUtil.lPush(TaskConstants.ORDER_TASK_PAY_SUCCESS_AFTER, storeOrder.getOrderId());

View File

@@ -392,6 +392,27 @@ public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserS
return update(lambdaUpdateWrapper); return update(lambdaUpdateWrapper);
} }
/**
* 更新用户积分
*
* 使用数据库原子加减,避免根据调用方传入的旧 User 对象计算后覆盖最新积分。
*
* @param user 用户
* @param integral 积分
* @param type 增加add、扣减sub
* @return Boolean
*/
@Override
public Boolean updateIntegral(User user, Integer integral, String type) {
if (ObjectUtil.isNull(user)) {
throw new CrmebException("用户不存在");
}
if (ObjectUtil.isNull(integral) || integral <= 0) {
throw new CrmebException("积分必须大于0");
}
return operationIntegral(user.getUid(), BigDecimal.valueOf(integral), user.getIntegral(), type);
}
/** /**
* 会员分组 * 会员分组
* *