【代码优化】商城:优化分销提现的逻辑(余额)
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package cn.iocoder.yudao.module.pay.api.wallet;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.pay.api.wallet.dto.PayWalletCreateReqDto;
|
||||
import cn.iocoder.yudao.module.pay.api.wallet.dto.PayWalletRespDTO;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.iocoder.yudao.module.pay.api.wallet.dto.PayWalletAddBalanceReqDTO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
|
||||
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
|
||||
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -21,13 +20,14 @@ public class PayWalletApiImpl implements PayWalletApi {
|
||||
private PayWalletService payWalletService;
|
||||
|
||||
@Override
|
||||
public void addWallet(PayWalletCreateReqDto reqDTO) {
|
||||
payWalletService.addWalletBalance(reqDTO.getWalletId(), reqDTO.getBizId(), reqDTO.getBizType(), reqDTO.getPrice());
|
||||
public void addWalletBalance(PayWalletAddBalanceReqDTO reqDTO) {
|
||||
// 创建或获取钱包
|
||||
PayWalletDO wallet = payWalletService.getOrCreateWallet(reqDTO.getUserId(), reqDTO.getUserType());
|
||||
Assert.notNull(wallet, "钱包({}/{})不存在", reqDTO.getUserId(), reqDTO.getUserType());
|
||||
|
||||
// 增加余额
|
||||
PayWalletBizTypeEnum bizType = PayWalletBizTypeEnum.valueOf(reqDTO.getBizType());
|
||||
payWalletService.addWalletBalance(wallet.getId(), reqDTO.getBizId(), bizType, reqDTO.getPrice());
|
||||
}
|
||||
|
||||
@Override
|
||||
public PayWalletRespDTO getWalletByUserId(Long userId) {
|
||||
PayWalletDO orCreateWallet = payWalletService.getOrCreateWallet(userId, UserTypeEnum.MEMBER.getValue());
|
||||
return BeanUtils.toBean(orCreateWallet, PayWalletRespDTO.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +68,19 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
|
||||
return update(null, lambdaUpdateWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加余额的时候,更新钱包
|
||||
*
|
||||
* @param id 钱包 id
|
||||
* @param price 钱包金额
|
||||
*/
|
||||
default void updateWhenAdd(Long id, Integer price) {
|
||||
LambdaUpdateWrapper<PayWalletDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<PayWalletDO>()
|
||||
.setSql(" balance = balance + " + price)
|
||||
.eq(PayWalletDO::getId, id);
|
||||
update(null, lambdaUpdateWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 冻结钱包部分余额
|
||||
*
|
||||
|
||||
@@ -187,7 +187,7 @@ public class PayWalletServiceImpl implements PayWalletService {
|
||||
|
||||
// 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯)
|
||||
return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> {
|
||||
// 2. 更新钱包金额
|
||||
// 3. 更新钱包金额
|
||||
switch (bizType) {
|
||||
case PAYMENT_REFUND: { // 退款更新
|
||||
walletMapper.updateWhenConsumptionRefund(payWallet.getId(), price);
|
||||
@@ -198,19 +198,15 @@ public class PayWalletServiceImpl implements PayWalletService {
|
||||
break;
|
||||
}
|
||||
case UPDATE_BALANCE: // 更新余额
|
||||
walletMapper.updateWhenRecharge(payWallet.getId(), price);
|
||||
break;
|
||||
// TODO @luchi:1)不能使用 updateWhenRecharge,它是充值哈。应该增加余额就 ok 了。。ps:顺便帮我把 UPDATE_BALANCE 也改改哈;2)其实不用 WITHDRAW 枚举,复用 UPDATE_BALANCE 就好了。
|
||||
case WITHDRAW:
|
||||
walletMapper.updateWhenRecharge(payWallet.getId(), price);
|
||||
case BROKERAGE_WITHDRAW: // 分佣提现
|
||||
walletMapper.updateWhenAdd(payWallet.getId(), price);
|
||||
break;
|
||||
default: {
|
||||
// TODO 其它类型待实现
|
||||
throw new UnsupportedOperationException("待实现");
|
||||
throw new UnsupportedOperationException("待实现:" + bizType);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 生成钱包流水
|
||||
// 4. 生成钱包流水
|
||||
WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO()
|
||||
.setWalletId(payWallet.getId()).setPrice(price).setBalance(payWallet.getBalance() + price)
|
||||
.setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription());
|
||||
|
||||
Reference in New Issue
Block a user