2
0

feat: 使用余额支付

This commit is contained in:
caiyuchao
2025-02-13 15:16:35 +08:00
parent 3731aefec8
commit 9598cacfc8
5 changed files with 38 additions and 0 deletions

View File

@@ -121,4 +121,9 @@ public class UOrderController extends BaseController {
public AjaxResult paySuccess(@PathVariable("id") Long id) {
return toAjax(uOrderService.paySuccess(id));
}
@PostMapping("payBalance/{id}")
public AjaxResult payBalance(@PathVariable("id") Long id) {
return toAjax(uOrderService.payByBalance(id));
}
}

View File

@@ -16,4 +16,6 @@ public interface IUOrderService extends IService<UOrder> {
Long saveOrder(UOrder order);
boolean paySuccess(Long orderId);
boolean payByBalance(Long orderId);
}

View File

@@ -142,6 +142,33 @@ public class UOrderServiceImpl extends ServiceImpl<UOrderMapper, UOrder> impleme
return true;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean payByBalance(Long orderId) {
UOrder order = this.getById(orderId);
if (!OrderTypeEnum.PACKAGE.getCode().equals(order.getType())) {
throw new ServiceException("user.order.pay.type.error");
}
UAccount account = accountService.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, order.getUserId()), false);
BigDecimal balance;
if (ObjectUtil.isNull(account)) {
throw new ServiceException("user.order.pay.amount.error");
} else {
account.setBalance(Optional.ofNullable(account.getBalance()).orElse(BigDecimal.ZERO));
balance = account.getBalance().subtract(Optional.ofNullable(account.getBalanceUsed()).orElse(BigDecimal.ZERO));
}
if (order.getOrderAmount().compareTo(balance) > 0) {
throw new ServiceException("user.order.pay.amount.error");
}
if (paySuccess(orderId)) {
account.setBalance(account.getBalance().subtract(order.getOrderAmount()));
accountService.updateById(account);
}
return true;
}
private void callbackPackage(UOrder order, UAccount account, boolean isValid) {
if (ObjectUtil.isNull(order.getPackageId())) {
return;