code review:钱包充值

This commit is contained in:
YunaiV 2023-09-18 13:07:54 +08:00
parent bbc16bb937
commit 080c32b9f3
15 changed files with 104 additions and 70 deletions

View File

@ -46,6 +46,9 @@ public interface ErrorCodeConstants {
ErrorCode WALLET_TRANSACTION_NOT_FOUND = new ErrorCode(1007007002, "未找到对应的钱包交易"); ErrorCode WALLET_TRANSACTION_NOT_FOUND = new ErrorCode(1007007002, "未找到对应的钱包交易");
ErrorCode WALLET_REFUND_AMOUNT_ERROR = new ErrorCode(1007007003, "钱包退款金额不对"); ErrorCode WALLET_REFUND_AMOUNT_ERROR = new ErrorCode(1007007003, "钱包退款金额不对");
ErrorCode WALLET_REFUND_EXIST = new ErrorCode(1007007004, "已经存在钱包退款"); ErrorCode WALLET_REFUND_EXIST = new ErrorCode(1007007004, "已经存在钱包退款");
// TODO @jason把钱包充值单独搞个错误码段哈
ErrorCode WALLET_RECHARGE_NOT_FOUND = new ErrorCode(1007007005, "钱包充值记录不存在"); ErrorCode WALLET_RECHARGE_NOT_FOUND = new ErrorCode(1007007005, "钱包充值记录不存在");
ErrorCode WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007007006, "钱包充值更新支付状态失败,钱包充值记录不是【未支付】状态"); ErrorCode WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007007006, "钱包充值更新支付状态失败,钱包充值记录不是【未支付】状态");
ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR = new ErrorCode(1007007007, "钱包充值更新支付状态失败,支付单编号不匹配"); ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR = new ErrorCode(1007007007, "钱包充值更新支付状态失败,支付单编号不匹配");

View File

@ -33,13 +33,14 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLogi
public class AppPayWalletRechargeController { public class AppPayWalletRechargeController {
@Resource @Resource
private PayWalletRechargeService payWalletRechargeService; private PayWalletRechargeService walletRechargeService;
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "创建钱包充值记录") @Operation(summary = "创建钱包充值记录(发起充值)")
public CommonResult<AppPayWalletRechargeCreateRespVO> createWalletRecharge(@Valid @RequestBody AppPayWalletRechargeCreateReqVO reqVO) { public CommonResult<AppPayWalletRechargeCreateRespVO> createWalletRecharge(
PayWalletRechargeDO walletRecharge = payWalletRechargeService.createWalletRecharge(getLoginUserId(), @Valid @RequestBody AppPayWalletRechargeCreateReqVO reqVO) {
getLoginUserType(), reqVO); PayWalletRechargeDO walletRecharge = walletRechargeService.createWalletRecharge(
getLoginUserId(), getLoginUserType(), reqVO);
return success(PayWalletRechargeConvert.INSTANCE.convert(walletRecharge)); return success(PayWalletRechargeConvert.INSTANCE.convert(walletRecharge));
} }
@ -47,9 +48,12 @@ public class AppPayWalletRechargeController {
@Operation(summary = "更新钱包充值为已充值") // pay-module 支付服务进行回调可见 PayNotifyJob @Operation(summary = "更新钱包充值为已充值") // pay-module 支付服务进行回调可见 PayNotifyJob
@PermitAll // 无需登录安全由 内部校验实现 @PermitAll // 无需登录安全由 内部校验实现
@OperateLog(enable = false) // 禁用操作日志因为没有操作人 @OperateLog(enable = false) // 禁用操作日志因为没有操作人
public CommonResult<Boolean> updateWalletRechargerPaid(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) { public CommonResult<Boolean> updateWalletRechargerPaid(@Valid @RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
payWalletRechargeService.updateWalletRechargerPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()), walletRechargeService.updateWalletRechargerPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()),
notifyReqDTO.getPayOrderId()); notifyReqDTO.getPayOrderId());
return success(true); return success(true);
} }
// TODO @jason管理后台是不是可以搞个发起退款
} }

View File

@ -12,11 +12,14 @@ public class AppPayWalletRechargeCreateReqVO {
@Schema(description = "支付金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000") @Schema(description = "支付金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
@NotNull(message = "支付金额不能为空") @NotNull(message = "支付金额不能为空")
// TODO @jason999是不是 @Min
@DecimalMin(value = "0", inclusive = false, message = "支付金额必须大于零") @DecimalMin(value = "0", inclusive = false, message = "支付金额必须大于零")
private Integer payPrice; private Integer payPrice;
// TODO @jason这个是不是后端计算出来呀不然前端可以直接搞了
@Schema(description = "钱包赠送金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000") @Schema(description = "钱包赠送金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
@NotNull(message = "钱包赠送金额不能为空") @NotNull(message = "钱包赠送金额不能为空")
@DecimalMin(value = "0", message = "钱包赠送金额必须大于等于零") @DecimalMin(value = "0", message = "钱包赠送金额必须大于等于零")
private Integer walletBonus; private Integer walletBonus;
} }

View File

@ -12,4 +12,5 @@ public class AppPayWalletRechargeCreateRespVO {
@Schema(description = "支付订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "100") @Schema(description = "支付订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
private Long payOrderId; private Long payOrderId;
} }

View File

@ -16,6 +16,7 @@ public interface PayWalletRechargeConvert {
PayWalletRechargeDO convert(AppPayWalletRechargeCreateReqVO vo); PayWalletRechargeDO convert(AppPayWalletRechargeCreateReqVO vo);
// TODO @jason好像 price 相加可以写个表达式的通过 @Mapping
default PayWalletRechargeDO convert(Long walletId, AppPayWalletRechargeCreateReqVO vo) { default PayWalletRechargeDO convert(Long walletId, AppPayWalletRechargeCreateReqVO vo) {
PayWalletRechargeDO walletRecharge = convert(vo); PayWalletRechargeDO walletRecharge = convert(vo);
return walletRecharge.setWalletId(walletId) return walletRecharge.setWalletId(walletId)

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.pay.convert.wallet;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.transaction.AppPayWalletTransactionRespVO; import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.transaction.AppPayWalletTransactionRespVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO; import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO; import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -14,6 +14,6 @@ public interface PayWalletTransactionConvert {
PageResult<AppPayWalletTransactionRespVO> convertPage(PageResult<PayWalletTransactionDO> page); PageResult<AppPayWalletTransactionRespVO> convertPage(PageResult<PayWalletTransactionDO> page);
PayWalletTransactionDO convert(CreateWalletTransactionBO bean); PayWalletTransactionDO convert(WalletTransactionCreateReqBO bean);
} }

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.pay.dal.dataobject.wallet; package cn.iocoder.yudao.module.pay.dal.dataobject.wallet;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
@ -23,40 +25,49 @@ public class PayWalletRechargeDO extends BaseDO {
private Long id; private Long id;
/** /**
* 会员钱包 id * 钱包编号
*
* 关联 {@link PayWalletDO#getId()}
*/ */
private Long walletId; private Long walletId;
// TODO @jason要不改成 totalPrice
/** /**
* 用户实际到账余额例如充 100 20则该值是 120 * 用户实际到账余额
*
* 例如充 100 20则该值是 120
*/ */
private Integer price; private Integer price;
/** /**
* 实际支付金额 * 实际支付金额
*/ */
private Integer payPrice; private Integer payPrice;
// TODO @jasonbonusPrice 更统一一点
/** /**
* 钱包赠送金额 * 钱包赠送金额
*/ */
private Integer walletBonus; private Integer walletBonus;
/** /**
* 是否已支付[0:未支付 1:已经支付过] * 是否已支付
*
* true - 已支付
* false - 未支付
*/ */
private Boolean payStatus; private Boolean payStatus;
/** /**
* 支付订单编号 * 支付订单编号
*
* 关联 {@link PayOrderDO#getId()}
*/ */
private Long payOrderId; private Long payOrderId;
/** /**
* 支付成功的支付渠道 * 支付成功的支付渠道
*
* 冗余 {@link PayOrderDO#getChannelCode()}
*/ */
private String payChannelCode; private String payChannelCode;
/** /**
* 订单支付时间 * 订单支付时间
*/ */
@ -64,26 +75,27 @@ public class PayWalletRechargeDO extends BaseDO {
/** /**
* 支付退款单编号 * 支付退款单编号
*
* 关联 {@link PayRefundDO#getId()}
*/ */
private Long payRefundId; private Long payRefundId;
// TODO @jason要不改成 refundTotalPrice
/** /**
* 退款金额包含赠送金额 * 退款金额包含赠送金额
*/ */
private Integer refundPrice; private Integer refundPrice;
/** /**
* 退款支付金额 * 退款支付金额
*/ */
private Integer refundPayPrice; private Integer refundPayPrice;
// TODO @jason要不改成 refundBonusPrice
/** /**
* 退款钱包赠送金额 * 退款钱包赠送金额
*/ */
private Integer refundWalletBonus; private Integer refundWalletBonus;
/** /**
* 退款时间 * 退款时间
*/ */
private LocalDateTime refundTime; private LocalDateTime refundTime;
}
}

View File

@ -14,6 +14,8 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
PayWalletDO::getUserType, userType); PayWalletDO::getUserType, userType);
} }
// TODO @jason下面几个更新方法 id 放前面哈一般来说重要参数放前面
/** /**
* 当消费退款时候 更新钱包 * 当消费退款时候 更新钱包
* *
@ -55,6 +57,7 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
.eq(PayWalletDO::getId, id); .eq(PayWalletDO::getId, id);
return update(null, lambdaUpdateWrapper); return update(null, lambdaUpdateWrapper);
} }
} }

View File

@ -2,17 +2,17 @@ package cn.iocoder.yudao.module.pay.dal.mysql.wallet;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoOrderDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO; import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface PayWalletRechargeMapper extends BaseMapperX<PayWalletRechargeDO> { public interface PayWalletRechargeMapper extends BaseMapperX<PayWalletRechargeDO> {
default int updateByIdAndPaid(Long id, boolean wherePayed, PayWalletRechargeDO updateObj){ default int updateByIdAndPaid(Long id, boolean wherePayStatus, PayWalletRechargeDO updateObj){
return update(updateObj, new LambdaQueryWrapperX<PayWalletRechargeDO>() return update(updateObj, new LambdaQueryWrapperX<PayWalletRechargeDO>()
.eq(PayWalletRechargeDO::getId, id).eq(PayWalletRechargeDO::getPayStatus, wherePayed)); .eq(PayWalletRechargeDO::getId, id).eq(PayWalletRechargeDO::getPayStatus, wherePayStatus));
} }
} }

View File

@ -11,19 +11,22 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
public interface PayWalletRechargeService { public interface PayWalletRechargeService {
/** /**
* 创建钱包充值记录 * 创建钱包充值记录发起充值
*
* @param userId 用户 id * @param userId 用户 id
* @param userType 用户类型 * @param userType 用户类型
* @param vo 钱包充值请求 vo * @param createReqVO 钱包充值请求 VO
* @return 钱包充值记录 * @return 钱包充值记录
*/ */
PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType, AppPayWalletRechargeCreateReqVO vo); PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType,
AppPayWalletRechargeCreateReqVO createReqVO);
/** /**
* 更新钱包充值成功 * 更新钱包充值成功
* @param walletRechargeId 钱包充值 id *
* @param id 钱包充值记录 id
* @param payOrderId 支付订单 id * @param payOrderId 支付订单 id
*/ */
void updateWalletRechargerPaid(Long walletRechargeId, Long payOrderId); void updateWalletRechargerPaid(Long id, Long payOrderId);
} }

View File

@ -42,57 +42,61 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
@Resource @Resource
private PayWalletRechargeMapper walletRechargeMapper; private PayWalletRechargeMapper walletRechargeMapper;
@Resource @Resource
private PayWalletService payWalletService; private PayWalletService payWalletService;
@Resource @Resource
private PayOrderService payOrderService; private PayOrderService payOrderService;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType, AppPayWalletRechargeCreateReqVO vo) { public PayWalletRechargeDO createWalletRecharge(Long userId, Integer userType,
// 1. 获取钱包 AppPayWalletRechargeCreateReqVO createReqVO) {
// 1. 新增钱包充值记录
PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType); PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
// 2. 新增钱包充值记录 PayWalletRechargeDO walletRecharge = PayWalletRechargeConvert.INSTANCE.convert(wallet.getId(), createReqVO);
PayWalletRechargeDO walletRecharge = PayWalletRechargeConvert.INSTANCE.convert(wallet.getId(), vo);
walletRechargeMapper.insert(walletRecharge); walletRechargeMapper.insert(walletRecharge);
// 3.创建支付单
// 2.1 创建支付单
Long payOrderId = payOrderService.createOrder(new PayOrderCreateReqDTO() Long payOrderId = payOrderService.createOrder(new PayOrderCreateReqDTO()
.setAppId(WALLET_PAY_APP_ID).setUserIp(getClientIP()) .setAppId(WALLET_PAY_APP_ID).setUserIp(getClientIP())
.setMerchantOrderId(walletRecharge.getId().toString()) // 业务的订单编号 .setMerchantOrderId(walletRecharge.getId().toString()) // 业务的订单编号
.setSubject(WALLET_RECHARGE_ORDER_SUBJECT).setBody("").setPrice(walletRecharge.getPayPrice()) .setSubject(WALLET_RECHARGE_ORDER_SUBJECT).setBody("").setPrice(walletRecharge.getPayPrice())
.setExpireTime(addTime(Duration.ofHours(2L)))); .setExpireTime(addTime(Duration.ofHours(2L))));
// 4.更新钱包充值记录中支付订单 // 2.2 更新钱包充值记录中支付订单
walletRechargeMapper.updateById(new PayWalletRechargeDO().setPayOrderId(payOrderId) walletRechargeMapper.updateById(new PayWalletRechargeDO().setPayOrderId(payOrderId)
.setId(walletRecharge.getId())); .setId(walletRecharge.getId()));
// TODO @jason是不是你直接返回就好啦然后 payOrderId 设置下
return walletRechargeMapper.selectById(walletRecharge.getId()); return walletRechargeMapper.selectById(walletRecharge.getId());
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateWalletRechargerPaid(Long walletRechargeId, Long payOrderId) { public void updateWalletRechargerPaid(Long id, Long payOrderId) {
// 1. 获取钱包充值记录 // 1.1 获取钱包充值记录
PayWalletRechargeDO walletRecharge = walletRechargeMapper.selectById(walletRechargeId); PayWalletRechargeDO walletRecharge = walletRechargeMapper.selectById(id);
if (walletRecharge == null) { if (walletRecharge == null) {
log.error("[updateWalletRechargerPaid],钱包充值记录不存在,钱包充值 Id:{} ", walletRechargeId); log.error("[updateWalletRechargerPaid][钱包充值记录不存在,钱包充值记录 id({})]", id);
throw exception(WALLET_RECHARGE_NOT_FOUND); throw exception(WALLET_RECHARGE_NOT_FOUND);
} }
// 2. 校验钱包充值是否可以支付 // 1.2 校验钱包充值是否可以支付
PayOrderDO payOrderDO = validateWalletRechargerCanPaid(walletRecharge, payOrderId); PayOrderDO payOrderDO = validateWalletRechargerCanPaid(walletRecharge, payOrderId);
// 3. 更新钱包充值的支付状态
int updateCount = walletRechargeMapper.updateByIdAndPaid(walletRechargeId,false, new PayWalletRechargeDO().setId(walletRechargeId) // 2. 更新钱包充值的支付状态
.setPayStatus(true).setPayTime(LocalDateTime.now()) int updateCount = walletRechargeMapper.updateByIdAndPaid(id,false,
new PayWalletRechargeDO().setId(id).setPayStatus(true).setPayTime(LocalDateTime.now())
.setPayChannelCode(payOrderDO.getChannelCode())); .setPayChannelCode(payOrderDO.getChannelCode()));
if (updateCount == 0) { if (updateCount == 0) {
throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID); throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID);
} }
// 4. 更新钱包余额
payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(walletRechargeId), // 3. 更新钱包余额
// TODO @jason这样的话未来提现会不会把充值的也提现走哈类似先充 100 110然后提现 110
payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(id),
PayWalletBizTypeEnum.RECHARGE, walletRecharge.getPrice()); PayWalletBizTypeEnum.RECHARGE, walletRecharge.getPrice());
} }
private PayOrderDO validateWalletRechargerCanPaid(PayWalletRechargeDO walletRecharge, Long payOrderId) { private PayOrderDO validateWalletRechargerCanPaid(PayWalletRechargeDO walletRecharge, Long payOrderId) {
// 1.1 校验充值记录的支付状态 // 1.1 校验充值记录的支付状态
if (walletRecharge.getPayStatus()) { if (walletRecharge.getPayStatus()) {
log.error("[validateWalletRechargerCanPaid][钱包({}) 不处于未支付状态! 钱包数据是:{}]", log.error("[validateWalletRechargerCanPaid][钱包({}) 不处于未支付状态! 钱包数据是:{}]",
@ -105,6 +109,7 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
walletRecharge.getId(), payOrderId, toJsonString(walletRecharge)); walletRecharge.getId(), payOrderId, toJsonString(walletRecharge));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR); throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
} }
// 2.1 校验支付单是否存在 // 2.1 校验支付单是否存在
PayOrderDO payOrder = payOrderService.getOrder(payOrderId); PayOrderDO payOrder = payOrderService.getOrder(payOrderId);
if (payOrder == null) { if (payOrder == null) {
@ -132,4 +137,5 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
} }
return payOrder; return payOrder;
} }
} }

View File

@ -9,7 +9,7 @@ import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletMapper;
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum; import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService; import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService; import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO; import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -84,6 +84,7 @@ public class PayWalletServiceImpl implements PayWalletService {
Long walletId = validateWalletCanRefund(payRefund.getId(), payRefund.getChannelOrderNo(), refundPrice); Long walletId = validateWalletCanRefund(payRefund.getId(), payRefund.getChannelOrderNo(), refundPrice);
PayWalletDO wallet = walletMapper.selectById(walletId); PayWalletDO wallet = walletMapper.selectById(walletId);
Assert.notNull(wallet, "钱包 {} 不存在", walletId); Assert.notNull(wallet, "钱包 {} 不存在", walletId);
// 2. 增加余额 // 2. 增加余额
return addWalletBalance(walletId, String.valueOf(payRefund.getId()), PAYMENT_REFUND, refundPrice); return addWalletBalance(walletId, String.valueOf(payRefund.getId()), PAYMENT_REFUND, refundPrice);
} }
@ -137,42 +138,39 @@ public class PayWalletServiceImpl implements PayWalletService {
} }
// 2.2 生成钱包流水 // 2.2 生成钱包流水
Integer afterBalance = payWallet.getBalance() - price; Integer afterBalance = payWallet.getBalance() - price;
CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId()) WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId())
.setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId)) .setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId))
.setBizType(bizType.getType()).setTitle(bizType.getDescription()); .setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(bo); return walletTransactionService.createWalletTransaction(bo);
} }
@Override @Override
public PayWalletTransactionDO addWalletBalance(Long walletId, public PayWalletTransactionDO addWalletBalance(Long walletId, String bizId,
String bizId, PayWalletBizTypeEnum bizType, Integer price) { PayWalletBizTypeEnum bizType, Integer price) {
// 1. 获取钱包 // 1.1 获取钱包
PayWalletDO payWallet = getWallet(walletId); PayWalletDO payWallet = getWallet(walletId);
if (payWallet == null) { if (payWallet == null) {
log.error("[addWalletBalance],用户钱包({})不存在.", walletId); log.error("[addWalletBalance],用户钱包({})不存在.", walletId);
throw exception(WALLET_NOT_FOUND); throw exception(WALLET_NOT_FOUND);
} }
// 1.2 更新钱包金额
switch (bizType) { switch (bizType) {
case PAYMENT_REFUND: { case PAYMENT_REFUND: { // 退款更新
// 退款更新
walletMapper.updateWhenConsumptionRefund(price, payWallet.getId()); walletMapper.updateWhenConsumptionRefund(price, payWallet.getId());
break; break;
} }
case RECHARGE: { case RECHARGE: { // 充值更新
// 充值更新
walletMapper.updateWhenRecharge(price, payWallet.getId()); walletMapper.updateWhenRecharge(price, payWallet.getId());
break; break;
} }
// TODO 其它类型 // TODO 其它类型这里可以先跑异常避免有业务搞错
} }
// 2. 生成钱包流水 // 2. 生成钱包流水
CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId()) WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO()
.setPrice(price).setBalance(payWallet.getBalance()+price).setBizId(bizId) .setWalletId(payWallet.getId()).setPrice(price).setBalance(payWallet.getBalance() + price)
.setBizType(bizType.getType()).setTitle(bizType.getDescription()); .setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(bo); return walletTransactionService.createWalletTransaction(transactionCreateReqBO);
} }
} }

View File

@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.transaction.AppPayWalletTransactionPageReqVO; import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.transaction.AppPayWalletTransactionPageReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO; import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum; import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO; import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import javax.validation.Valid; import javax.validation.Valid;
@ -31,7 +31,7 @@ public interface PayWalletTransactionService {
* @param bo 创建钱包流水 bo * @param bo 创建钱包流水 bo
* @return 新建的钱包 do * @return 新建的钱包 do
*/ */
PayWalletTransactionDO createWalletTransaction(@Valid CreateWalletTransactionBO bo); PayWalletTransactionDO createWalletTransaction(@Valid WalletTransactionCreateReqBO bo);
/** /**
* 根据 no获取钱包余流水 * 根据 no获取钱包余流水
@ -48,5 +48,5 @@ public interface PayWalletTransactionService {
* @return 钱包流水 * @return 钱包流水
*/ */
PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type); PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type);
} }

View File

@ -8,7 +8,7 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletTransactionMapper; import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletTransactionMapper;
import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO; import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum; import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO; import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -43,7 +43,7 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ
} }
@Override @Override
public PayWalletTransactionDO createWalletTransaction(CreateWalletTransactionBO bo) { public PayWalletTransactionDO createWalletTransaction(WalletTransactionCreateReqBO bo) {
PayWalletTransactionDO transaction = PayWalletTransactionConvert.INSTANCE.convert(bo) PayWalletTransactionDO transaction = PayWalletTransactionConvert.INSTANCE.convert(bo)
.setNo(noRedisDAO.generate(WALLET_NO_PREFIX)); .setNo(noRedisDAO.generate(WALLET_NO_PREFIX));
payWalletTransactionMapper.insert(transaction); payWalletTransactionMapper.insert(transaction);

View File

@ -9,7 +9,7 @@ import lombok.Data;
* @author jason * @author jason
*/ */
@Data @Data
public class CreateWalletTransactionBO { public class WalletTransactionCreateReqBO {
// TODO @jasonbo 的话最好加个参数校验哈 // TODO @jasonbo 的话最好加个参数校验哈