feat: 使用余额支付
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,6 @@ public interface IUOrderService extends IService<UOrder> {
|
||||
Long saveOrder(UOrder order);
|
||||
|
||||
boolean paySuccess(Long orderId);
|
||||
|
||||
boolean payByBalance(Long orderId);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user