mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 08:25:07 +08:00
Merge remote-tracking branch 'yudao/feature/mall_product' into feature/mall_product
This commit is contained in:
@ -5,6 +5,8 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
|
||||
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
|
||||
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.*;
|
||||
import cn.iocoder.yudao.module.pay.convert.order.PayOrderConvert;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.app.PayAppDO;
|
||||
@ -23,9 +25,7 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
@ -70,6 +70,13 @@ public class PayOrderController {
|
||||
@PostMapping("/submit")
|
||||
@Operation(summary = "提交支付订单")
|
||||
public CommonResult<PayOrderSubmitRespVO> submitPayOrder(@RequestBody PayOrderSubmitReqVO reqVO) {
|
||||
// 钱包支付需要 额外传 user_id 和 user_type
|
||||
if (Objects.equals(reqVO.getChannelCode(), PayChannelEnum.WALLET.getCode())) {
|
||||
Map<String, String> channelExtras = reqVO.getChannelExtras() == null ? new HashMap<>(8) : reqVO.getChannelExtras();
|
||||
channelExtras.put("user_id", String.valueOf(WebFrameworkUtils.getLoginUserId()));
|
||||
channelExtras.put("user_type", String.valueOf(WebFrameworkUtils.getLoginUserType()));
|
||||
reqVO.setChannelExtras(channelExtras);
|
||||
}
|
||||
PayOrderSubmitRespVO respVO = orderService.submitOrder(reqVO, getClientIP());
|
||||
return success(respVO);
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ public class AppPayWalletController {
|
||||
@Operation(summary = "获取钱包")
|
||||
@PreAuthenticated
|
||||
public CommonResult<AppPayWalletRespVO> getPayWallet() {
|
||||
PayWalletDO wallet = payWalletService.getPayWallet(getLoginUserId(), UserTypeEnum.MEMBER.getValue());
|
||||
PayWalletDO wallet = payWalletService.getOrCreatePayWallet(getLoginUserId(), UserTypeEnum.MEMBER.getValue());
|
||||
return success(PayWalletConvert.INSTANCE.convert(wallet));
|
||||
}
|
||||
|
||||
|
@ -15,9 +15,6 @@ public class AppPayWalletTransactionRespVO {
|
||||
@Schema(description = "业务分类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
private Integer bizType;
|
||||
|
||||
@Schema(description = "交易时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
|
||||
private LocalDateTime transactionTime;
|
||||
|
||||
@Schema(description = "交易金额,单位分", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
|
||||
private Long price;
|
||||
|
||||
|
@ -7,8 +7,6 @@ import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 会员钱包流水 DO
|
||||
*
|
||||
@ -24,6 +22,7 @@ public class PayWalletTransactionDO extends BaseDO {
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 流水号
|
||||
*/
|
||||
@ -42,34 +41,26 @@ public class PayWalletTransactionDO extends BaseDO {
|
||||
* 枚举 {@link PayWalletBizTypeEnum#getType()}
|
||||
*/
|
||||
private Integer bizType;
|
||||
// TODO @jason:使用 string;因为可能有业务是 string 接入哈。
|
||||
|
||||
/**
|
||||
* 关联业务编号
|
||||
*/
|
||||
private Long bizId;
|
||||
private String bizId;
|
||||
|
||||
// TODO @jason:想了下,改成 title;流水标题;因为账户明细那,会看到这个;
|
||||
/**
|
||||
* 附加说明
|
||||
* 流水说明
|
||||
*/
|
||||
private String description;
|
||||
private String title;
|
||||
|
||||
// TODO @jason:使用 price 哈。项目里,金额都是用这个为主;
|
||||
/**
|
||||
* 交易金额,单位分
|
||||
*
|
||||
* 正值表示余额增加,负值表示余额减少
|
||||
*/
|
||||
private Integer amount;
|
||||
private Integer price;
|
||||
|
||||
/**
|
||||
* 交易后余额,单位分
|
||||
*/
|
||||
private Integer balance;
|
||||
|
||||
// TODO @jason:使用 createTime 就够啦
|
||||
/**
|
||||
* 交易时间
|
||||
*/
|
||||
private LocalDateTime transactionTime;
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.pay.dal.mysql.wallet;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
|
||||
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
@ -12,6 +14,55 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
|
||||
return selectOne(PayWalletDO::getUserId, userId,
|
||||
PayWalletDO::getUserType, userType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当余额减少时候更新
|
||||
*
|
||||
* @param bizType 业务类型
|
||||
* @param balance 当前余额
|
||||
* @param totalRecharge 当前累计充值
|
||||
* @param totalExpense 当前累计支出
|
||||
* @param price 支出的金额
|
||||
* @param id 钱包 id
|
||||
*/
|
||||
default int updateWhenDecBalance(PayWalletBizTypeEnum bizType, Integer balance, Long totalRecharge,
|
||||
Long totalExpense, Integer price, Long id) {
|
||||
PayWalletDO updateDO = new PayWalletDO().setBalance(balance - price);
|
||||
if(bizType == PayWalletBizTypeEnum.PAYMENT){
|
||||
updateDO.setTotalExpense(totalExpense + price);
|
||||
}
|
||||
if (bizType == PayWalletBizTypeEnum.RECHARGE_REFUND) {
|
||||
updateDO.setTotalRecharge(totalRecharge - price);
|
||||
}
|
||||
return update(updateDO,
|
||||
new LambdaQueryWrapper<PayWalletDO>().eq(PayWalletDO::getId, id)
|
||||
.eq(PayWalletDO::getBalance, balance)
|
||||
.ge(PayWalletDO::getBalance, price));
|
||||
}
|
||||
|
||||
/**
|
||||
* 当余额增加时候更新
|
||||
*
|
||||
* @param bizType 业务类型
|
||||
* @param balance 当前余额
|
||||
* @param totalRecharge 当前累计充值
|
||||
* @param totalExpense 当前累计支出
|
||||
* @param price 金额
|
||||
* @param id 钱包 id
|
||||
*/
|
||||
default int updateWhenIncBalance(PayWalletBizTypeEnum bizType, Integer balance, Long totalRecharge,
|
||||
Long totalExpense, Integer price, Long id) {
|
||||
PayWalletDO updateDO = new PayWalletDO().setBalance(balance + price);
|
||||
if (bizType == PayWalletBizTypeEnum.PAYMENT_REFUND) {
|
||||
updateDO.setTotalExpense(totalExpense - price);
|
||||
}
|
||||
if (bizType == PayWalletBizTypeEnum.RECHARGE) {
|
||||
updateDO.setTotalExpense(totalRecharge + price);
|
||||
}
|
||||
return update(updateDO,
|
||||
new LambdaQueryWrapper<PayWalletDO>().eq(PayWalletDO::getId, id)
|
||||
.eq(PayWalletDO::getBalance, balance));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -18,9 +18,9 @@ public interface PayWalletTransactionMapper extends BaseMapperX<PayWalletTransac
|
||||
LambdaQueryWrapperX<PayWalletTransactionDO> query = new LambdaQueryWrapperX<PayWalletTransactionDO>()
|
||||
.eq(PayWalletTransactionDO::getWalletId, walletId);
|
||||
if (Objects.equals(pageReqVO.getType(), AppPayWalletTransactionPageReqVO.TYPE_INCOME)) {
|
||||
query.gt(PayWalletTransactionDO::getAmount, 0);
|
||||
query.gt(PayWalletTransactionDO::getPrice, 0);
|
||||
} else if (Objects.equals(pageReqVO.getType(), AppPayWalletTransactionPageReqVO.TYPE_EXPENSE)) {
|
||||
query.lt(PayWalletTransactionDO::getAmount, 0);
|
||||
query.lt(PayWalletTransactionDO::getPrice, 0);
|
||||
}
|
||||
query.orderByDesc(PayWalletTransactionDO::getId);
|
||||
return selectPage(pageReqVO, query);
|
||||
@ -30,9 +30,8 @@ public interface PayWalletTransactionMapper extends BaseMapperX<PayWalletTransac
|
||||
return selectOne(PayWalletTransactionDO::getNo, no);
|
||||
}
|
||||
|
||||
default PayWalletTransactionDO selectByWalletIdAndBiz(Long walletId, Long bizId, Integer bizType) {
|
||||
return selectOne(PayWalletTransactionDO::getWalletId, walletId,
|
||||
PayWalletTransactionDO::getBizId, bizId,
|
||||
default PayWalletTransactionDO selectByBiz(String bizId, Integer bizType) {
|
||||
return selectOne(PayWalletTransactionDO::getBizId, bizId,
|
||||
PayWalletTransactionDO::getBizType, bizType);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.iocoder.yudao.module.pay.framework.pay.wallet;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
|
||||
@ -9,13 +11,23 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReq
|
||||
import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient;
|
||||
import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig;
|
||||
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
|
||||
import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
|
||||
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.order.PayOrderStatusEnum;
|
||||
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.wallet.PayWalletService;
|
||||
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletTransactionService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.ORDER_EXTENSION_NOT_FOUND;
|
||||
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.REFUND_NOT_FOUND;
|
||||
|
||||
/**
|
||||
* 钱包支付的 PayClient 实现类
|
||||
@ -27,6 +39,12 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
|
||||
|
||||
private PayWalletService wallService;
|
||||
|
||||
private PayWalletTransactionService walletTransactionService;
|
||||
|
||||
private PayOrderService payOrderService;
|
||||
|
||||
private PayRefundService payRefundService;
|
||||
|
||||
public WalletPayClient(Long channelId, NonePayClientConfig config) {
|
||||
super(channelId, PayChannelEnum.WALLET.getCode(), config);
|
||||
}
|
||||
@ -36,14 +54,22 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
|
||||
if (wallService == null) {
|
||||
wallService = SpringUtil.getBean(PayWalletService.class);
|
||||
}
|
||||
if (walletTransactionService == null) {
|
||||
walletTransactionService = SpringUtil.getBean(PayWalletTransactionService.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) {
|
||||
try {
|
||||
PayWalletTransactionDO transaction = wallService.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice());
|
||||
String userId = MapUtil.getStr(reqDTO.getChannelExtras(), "user_id");
|
||||
String userType = MapUtil.getStr(reqDTO.getChannelExtras(), "user_type");
|
||||
Assert.notEmpty(userId, "用户 id 不能为空");
|
||||
Assert.notEmpty(userType, "用户类型不能为空");
|
||||
PayWalletTransactionDO transaction = wallService.orderPay(Long.valueOf(userId), Integer.valueOf(userType),
|
||||
reqDTO.getOutTradeNo(), reqDTO.getPrice());
|
||||
return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(),
|
||||
transaction.getTransactionTime(),
|
||||
transaction.getCreateTime(),
|
||||
reqDTO.getOutTradeNo(), transaction);
|
||||
} catch (Throwable ex) {
|
||||
log.error("[doUnifiedOrder] 失败", ex);
|
||||
@ -66,15 +92,39 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
|
||||
|
||||
@Override
|
||||
protected PayOrderRespDTO doGetOrder(String outTradeNo) {
|
||||
throw new UnsupportedOperationException("待实现");
|
||||
if (payOrderService == null) {
|
||||
payOrderService = SpringUtil.getBean(PayOrderService.class);
|
||||
}
|
||||
PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo);
|
||||
// 支付交易拓展单不存在, 返回关闭状态
|
||||
if (orderExtension == null) {
|
||||
return PayOrderRespDTO.closedOf(String.valueOf(ORDER_EXTENSION_NOT_FOUND.getCode()),
|
||||
ORDER_EXTENSION_NOT_FOUND.getMsg(), outTradeNo, "");
|
||||
}
|
||||
// 关闭状态
|
||||
if (PayOrderStatusEnum.isClosed(orderExtension.getStatus())) {
|
||||
return PayOrderRespDTO.closedOf(orderExtension.getChannelErrorCode(),
|
||||
orderExtension.getChannelErrorMsg(), outTradeNo, "");
|
||||
}
|
||||
// 成功状态
|
||||
if (PayOrderStatusEnum.isSuccess(orderExtension.getStatus())) {
|
||||
PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction(
|
||||
String.valueOf(orderExtension.getOrderId()), PayWalletBizTypeEnum.PAYMENT);
|
||||
Assert.notNull(walletTransaction, "支付单 {} 钱包流水不能为空", outTradeNo);
|
||||
return PayOrderRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreator(),
|
||||
walletTransaction.getCreateTime(), outTradeNo, walletTransaction);
|
||||
}
|
||||
// 其它状态为无效状态
|
||||
log.error("[doGetOrder] 支付单 {} 的状态不正确", outTradeNo);
|
||||
throw new IllegalStateException(String.format("支付单[%s] 状态不正确", outTradeNo));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
|
||||
try {
|
||||
PayWalletTransactionDO payWalletTransaction = wallService.refund(reqDTO.getOutRefundNo(),
|
||||
PayWalletTransactionDO payWalletTransaction = wallService.orderRefund(reqDTO.getOutRefundNo(),
|
||||
reqDTO.getRefundPrice(), reqDTO.getReason());
|
||||
return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(),
|
||||
return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getCreateTime(),
|
||||
reqDTO.getOutRefundNo(), payWalletTransaction);
|
||||
} catch (Throwable ex) {
|
||||
log.error("[doUnifiedRefund] 失败", ex);
|
||||
@ -97,7 +147,31 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
|
||||
|
||||
@Override
|
||||
protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) {
|
||||
throw new UnsupportedOperationException("待实现");
|
||||
if (payRefundService == null) {
|
||||
payRefundService = SpringUtil.getBean(PayRefundService.class);
|
||||
}
|
||||
PayRefundDO payRefund = payRefundService.getRefundByNo(outRefundNo);
|
||||
// 支付退款单不存在, 返回退款失败状态
|
||||
if (payRefund == null) {
|
||||
return PayRefundRespDTO.failureOf(String.valueOf(REFUND_NOT_FOUND), REFUND_NOT_FOUND.getMsg(),
|
||||
outRefundNo, "");
|
||||
}
|
||||
// 退款失败
|
||||
if (PayRefundStatusRespEnum.isFailure(payRefund.getStatus())) {
|
||||
return PayRefundRespDTO.failureOf(payRefund.getChannelErrorCode(), payRefund.getChannelErrorMsg(),
|
||||
outRefundNo, "");
|
||||
}
|
||||
// 退款成功
|
||||
if (PayRefundStatusRespEnum.isSuccess(payRefund.getStatus())) {
|
||||
PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction(
|
||||
String.valueOf(payRefund.getId()), PayWalletBizTypeEnum.PAYMENT_REFUND);
|
||||
Assert.notNull(walletTransaction, "支付退款单 {} 钱包流水不能为空", outRefundNo);
|
||||
return PayRefundRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreateTime(),
|
||||
outRefundNo, walletTransaction);
|
||||
}
|
||||
// 其它状态为无效状态
|
||||
log.error("[doGetRefund] 支付退款单 {} 的状态不正确", outRefundNo);
|
||||
throw new IllegalStateException(String.format("支付退款单[%s] 状态不正确", outRefundNo));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -345,7 +345,8 @@ public class PayOrderServiceImpl implements PayOrderService {
|
||||
.channelId(channel.getId()).channelCode(channel.getCode())
|
||||
.successTime(notify.getSuccessTime()).extensionId(orderExtension.getId()).no(orderExtension.getNo())
|
||||
.channelOrderNo(notify.getChannelOrderNo()).channelUserId(notify.getChannelUserId())
|
||||
.channelFeeRate(channel.getFeeRate()).channelFeePrice(MoneyUtils.calculateRatePrice(order.getPrice(), channel.getFeeRate()))
|
||||
.channelFeeRate(channel.getFeeRate())
|
||||
.channelFeePrice(MoneyUtils.calculateRatePrice(order.getPrice(), channel.getFeeRate()))
|
||||
.build());
|
||||
if (updateCounts == 0) { // 校验状态,必须是待支付
|
||||
throw exception(ORDER_STATUS_IS_NOT_WAITING);
|
||||
|
@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.pay.service.wallet;
|
||||
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
|
||||
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
|
||||
|
||||
/**
|
||||
* 钱包 Service 接口
|
||||
@ -10,30 +11,59 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
|
||||
*/
|
||||
public interface PayWalletService {
|
||||
|
||||
// TODO @jason:改成 getOrCreateWallet;因为目前解耦,用户注册时,不会创建钱包;需要这里兜底处理;
|
||||
/**
|
||||
* 获取钱包信息
|
||||
* 获取钱包信息,如果不存在创建钱包。由于用户注册时候不会创建钱包
|
||||
*
|
||||
* @param userId 用户编号
|
||||
* @param userType 用户类型
|
||||
*/
|
||||
PayWalletDO getPayWallet(Long userId, Integer userType);
|
||||
PayWalletDO getOrCreatePayWallet(Long userId, Integer userType);
|
||||
|
||||
/**
|
||||
* 钱包支付
|
||||
* 钱包订单支付
|
||||
*
|
||||
* @param userId 用户 id
|
||||
* @param userType 用户类型
|
||||
* @param outTradeNo 外部订单号
|
||||
* @param price 金额
|
||||
*/
|
||||
PayWalletTransactionDO pay(String outTradeNo, Integer price);
|
||||
PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price);
|
||||
|
||||
|
||||
/**
|
||||
* 钱包支付退款
|
||||
* 扣减钱包余额
|
||||
*
|
||||
* @param userId 用户 id
|
||||
* @param userType 用户类型
|
||||
* @param bizId 业务关联 id
|
||||
* @param bizType 业务关联分类
|
||||
* @param price 扣减金额
|
||||
* @return 钱包流水
|
||||
*/
|
||||
PayWalletTransactionDO reduceWalletBalance(Long userId, Integer userType,
|
||||
Long bizId, PayWalletBizTypeEnum bizType, Integer price);
|
||||
|
||||
|
||||
/**
|
||||
* 增加钱包余额
|
||||
*
|
||||
* @param userId 用户 id
|
||||
* @param userType 用户类型
|
||||
* @param bizId 业务关联 id
|
||||
* @param bizType 业务关联分类
|
||||
* @param price 增加金额
|
||||
* @return 钱包流水
|
||||
*/
|
||||
PayWalletTransactionDO addWalletBalance(Long userId, Integer userType,
|
||||
Long bizId, PayWalletBizTypeEnum bizType, Integer price);
|
||||
|
||||
/**
|
||||
* 钱包订单支付退款
|
||||
*
|
||||
* @param outRefundNo 外部退款号
|
||||
* @param refundPrice 退款金额
|
||||
* @param reason 退款原因
|
||||
*/
|
||||
PayWalletTransactionDO refund(String outRefundNo, Integer refundPrice, String reason);
|
||||
PayWalletTransactionDO orderRefund(String outRefundNo, Integer refundPrice, String reason);
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package cn.iocoder.yudao.module.pay.service.wallet;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletMapper;
|
||||
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.service.order.PayOrderService;
|
||||
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -16,9 +18,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.TOO_MANY_REQUESTS;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
|
||||
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserType;
|
||||
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum.PAYMENT;
|
||||
import static cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum.PAYMENT_REFUND;
|
||||
@ -56,81 +57,111 @@ public class PayWalletServiceImpl implements PayWalletService {
|
||||
private PayRefundService payRefundService;
|
||||
|
||||
@Override
|
||||
public PayWalletDO getPayWallet(Long userId, Integer userType) {
|
||||
return payWalletMapper.selectByUserIdAndType(userId, userType);
|
||||
public PayWalletDO getOrCreatePayWallet(Long userId, Integer userType) {
|
||||
PayWalletDO payWalletDO = payWalletMapper.selectByUserIdAndType(userId, userType);
|
||||
if (payWalletDO == null) {
|
||||
payWalletDO = new PayWalletDO();
|
||||
payWalletDO.setUserId(userId);
|
||||
payWalletDO.setUserType(userType);
|
||||
payWalletDO.setBalance(0);
|
||||
payWalletDO.setTotalExpense(0L);
|
||||
payWalletDO.setTotalRecharge(0L);
|
||||
payWalletDO.setCreateTime(LocalDateTime.now());
|
||||
payWalletMapper.insert(payWalletDO);
|
||||
}
|
||||
return payWalletDO;
|
||||
}
|
||||
|
||||
// TODO @jason:可以做的更抽象一点;pay(bizType, bizId, price);reduceWalletBalance;
|
||||
// TODO @jason:最好是,明确传入哪个 userId 或者 walletId;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public PayWalletTransactionDO pay(String outTradeNo, Integer price) {
|
||||
// 1.1 判断支付交易拓展单是否存
|
||||
public PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price) {
|
||||
// 判断支付交易拓展单是否存
|
||||
PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo);
|
||||
if (orderExtension == null) {
|
||||
throw exception(ORDER_EXTENSION_NOT_FOUND);
|
||||
}
|
||||
return reduceWalletBalance(userId, userType, orderExtension.getOrderId(), PAYMENT, price);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PayWalletTransactionDO reduceWalletBalance(Long userId, Integer userType,
|
||||
Long bizId, PayWalletBizTypeEnum bizType, Integer price) {
|
||||
// 1.1 获取钱包
|
||||
PayWalletDO payWallet = getOrCreatePayWallet(userId, userType);
|
||||
// 1.2 判断余额是否足够
|
||||
PayWalletDO payWallet = validatePayWallet();
|
||||
int afterBalance = payWallet.getBalance() - price;
|
||||
if (afterBalance < 0) {
|
||||
throw exception(WALLET_BALANCE_NOT_ENOUGH);
|
||||
}
|
||||
|
||||
// 2.1 扣除余额
|
||||
// TODO @jason:不要直接整个更新;而是 new 一个出来更新;然后要考虑并发,要 where 余额 > price,以及 - price
|
||||
payWallet.setBalance(afterBalance);
|
||||
payWallet.setTotalExpense(payWallet.getTotalExpense() + price);
|
||||
payWalletMapper.updateById(payWallet);
|
||||
|
||||
int number = payWalletMapper.updateWhenDecBalance(bizType,payWallet.getBalance(), payWallet.getTotalRecharge(),
|
||||
payWallet.getTotalExpense(), price, payWallet.getId());
|
||||
if (number == 0) {
|
||||
throw exception(TOO_MANY_REQUESTS);
|
||||
}
|
||||
// 2.2 生成钱包流水
|
||||
String walletNo = noRedisDAO.generate(WALLET_PAY_NO_PREFIX);
|
||||
String walletNo = generateWalletNo(bizType);
|
||||
PayWalletTransactionDO walletTransaction = new PayWalletTransactionDO().setWalletId(payWallet.getId())
|
||||
.setNo(walletNo).setAmount(price * -1).setBalance(afterBalance).setTransactionTime(LocalDateTime.now())
|
||||
.setBizId(orderExtension.getOrderId()).setBizType(PAYMENT.getType());
|
||||
.setNo(walletNo).setPrice(-price).setBalance(afterBalance)
|
||||
.setBizId(String.valueOf(bizId)).setBizType(bizType.getType()).setTitle(bizType.getDescription());
|
||||
payWalletTransactionService.createWalletTransaction(walletTransaction);
|
||||
return walletTransaction;
|
||||
}
|
||||
|
||||
// TODO @jason:不要在 service 里去使用用户上下文,这样和 request 就耦合了。
|
||||
private PayWalletDO validatePayWallet() {
|
||||
Long userId = getLoginUserId();
|
||||
Integer userType = getLoginUserType();
|
||||
PayWalletDO payWallet = getPayWallet(userId, userType);
|
||||
if (payWallet == null) {
|
||||
log.error("[validatePayWallet] 用户 {} 钱包不存在", userId);
|
||||
throw exception(WALLET_NOT_FOUND);
|
||||
@Override
|
||||
public PayWalletTransactionDO addWalletBalance(Long userId, Integer userType, Long bizId,
|
||||
PayWalletBizTypeEnum bizType, Integer price) {
|
||||
// 1.1 获取钱包
|
||||
PayWalletDO payWallet = getOrCreatePayWallet(userId, userType);
|
||||
|
||||
// 2.1 增加余额
|
||||
int number = payWalletMapper.updateWhenIncBalance(bizType, payWallet.getBalance(), payWallet.getTotalRecharge(),
|
||||
payWallet.getTotalExpense(), price, payWallet.getId());
|
||||
if (number == 0) {
|
||||
throw exception(TOO_MANY_REQUESTS);
|
||||
}
|
||||
return payWallet;
|
||||
|
||||
// 2.2 生成钱包流水
|
||||
String walletNo = generateWalletNo(bizType);
|
||||
PayWalletTransactionDO newWalletTransaction = new PayWalletTransactionDO().setWalletId(payWallet.getId())
|
||||
.setNo(walletNo).setPrice(price).setBalance(payWallet.getBalance()+price)
|
||||
.setBizId(String.valueOf(bizId)).setBizType(bizType.getType())
|
||||
.setTitle(bizType.getDescription());
|
||||
payWalletTransactionService.createWalletTransaction(newWalletTransaction);
|
||||
return newWalletTransaction;
|
||||
}
|
||||
|
||||
private String generateWalletNo(PayWalletBizTypeEnum bizType) {
|
||||
String no = "";
|
||||
switch(bizType){
|
||||
case PAYMENT :
|
||||
no = noRedisDAO.generate(WALLET_PAY_NO_PREFIX);
|
||||
break;
|
||||
case PAYMENT_REFUND :
|
||||
no = noRedisDAO.generate(WALLET_REFUND_NO_PREFIX);
|
||||
break;
|
||||
default :
|
||||
// TODO 待增加
|
||||
}
|
||||
return no;
|
||||
}
|
||||
|
||||
// TODO @jason:可以做的更抽象一点;pay(bizType, bizId, price);addWalletBalance;这样,如果后续充值,应该也是能复用这个方法的;
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public PayWalletTransactionDO refund(String outRefundNo, Integer refundPrice, String reason) {
|
||||
public PayWalletTransactionDO orderRefund(String outRefundNo, Integer refundPrice, String reason) {
|
||||
// 1.1 判断退款单是否存在
|
||||
PayRefundDO payRefund = payRefundService.getRefundByNo(outRefundNo);
|
||||
if (payRefund == null) {
|
||||
throw exception(REFUND_NOT_FOUND);
|
||||
}
|
||||
// 1.2 校验是否可以退款
|
||||
PayWalletDO payWallet = validatePayWallet();
|
||||
validateWalletCanRefund(payRefund.getId(), payRefund.getChannelOrderNo(), payWallet.getId(), refundPrice);
|
||||
Long walletId = validateWalletCanRefund(payRefund.getId(), payRefund.getChannelOrderNo(), refundPrice);
|
||||
|
||||
// TODO @jason:不要直接整个更新;而是 new 一个出来更新;然后要考虑并发,要 where 余额 + 金额
|
||||
Integer afterBalance = payWallet.getBalance() + refundPrice;
|
||||
payWallet.setBalance(afterBalance);
|
||||
payWallet.setTotalExpense(payWallet.getTotalExpense() + refundPrice * -1L);
|
||||
payWalletMapper.updateById(payWallet);
|
||||
|
||||
// 2.2 生成钱包流水
|
||||
String walletNo = noRedisDAO.generate(WALLET_REFUND_NO_PREFIX);
|
||||
PayWalletTransactionDO newWalletTransaction = new PayWalletTransactionDO().setWalletId(payWallet.getId())
|
||||
.setNo(walletNo).setAmount(refundPrice).setBalance(afterBalance).setTransactionTime(LocalDateTime.now())
|
||||
.setBizId(payRefund.getId()).setBizType(PAYMENT_REFUND.getType())
|
||||
.setDescription(reason);
|
||||
payWalletTransactionService.createWalletTransaction(newWalletTransaction);
|
||||
return newWalletTransaction;
|
||||
PayWalletDO payWallet = payWalletMapper.selectById(walletId);
|
||||
Assert.notNull(payWallet, "钱包 {} 不存在", walletId);
|
||||
return addWalletBalance(payWallet.getUserId(), payWallet.getUserType(),payRefund.getId(), PAYMENT_REFUND, refundPrice);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,24 +169,23 @@ public class PayWalletServiceImpl implements PayWalletService {
|
||||
*
|
||||
* @param refundId 支付退款单 id
|
||||
* @param walletPayNo 钱包支付 no
|
||||
* @param walletId 钱包 id
|
||||
*/
|
||||
// TODO @jason:不要使用基本类型;
|
||||
private void validateWalletCanRefund(long refundId, String walletPayNo, long walletId, int refundPrice) {
|
||||
private Long validateWalletCanRefund(Long refundId, String walletPayNo, Integer refundPrice) {
|
||||
// 查询钱包支付交易
|
||||
PayWalletTransactionDO payWalletTransaction = payWalletTransactionService.getWalletTransactionByNo(walletPayNo);
|
||||
if (payWalletTransaction == null) {
|
||||
throw exception(WALLET_TRANSACTION_NOT_FOUND);
|
||||
}
|
||||
// 原来的支付金额
|
||||
int amount = payWalletTransaction.getAmount() * -1; // TODO @jason:直接 - payWalletTransaction.getAmount() 即可;
|
||||
int amount = - payWalletTransaction.getPrice();
|
||||
if (refundPrice != amount) {
|
||||
throw exception(WALLET_REFUND_AMOUNT_ERROR);
|
||||
}
|
||||
PayWalletTransactionDO refundTransaction = payWalletTransactionService.getWalletTransaction(walletId, refundId, PAYMENT_REFUND);
|
||||
PayWalletTransactionDO refundTransaction = payWalletTransactionService.getWalletTransaction(
|
||||
String.valueOf(refundId), PAYMENT_REFUND);
|
||||
if (refundTransaction != null) {
|
||||
throw exception(WALLET_REFUND_EXIST);
|
||||
}
|
||||
return payWalletTransaction.getWalletId();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,11 +40,9 @@ public interface PayWalletTransactionService {
|
||||
/**
|
||||
* 获取钱包流水
|
||||
*
|
||||
* @param walletId 钱包编号
|
||||
* @param bizId 业务编号
|
||||
* @param type 业务类型
|
||||
* @return 钱包流水
|
||||
*/
|
||||
PayWalletTransactionDO getWalletTransaction(Long walletId, Long bizId, PayWalletBizTypeEnum type);
|
||||
|
||||
PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type);
|
||||
}
|
||||
|
@ -11,9 +11,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.WALLET_NOT_FOUND;
|
||||
|
||||
/**
|
||||
* 钱包流水 Service 实现类
|
||||
*
|
||||
@ -25,18 +22,13 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ
|
||||
|
||||
@Resource
|
||||
private PayWalletService payWalletService;
|
||||
|
||||
@Resource
|
||||
private PayWalletTransactionMapper payWalletTransactionMapper;
|
||||
|
||||
@Override
|
||||
public PageResult<PayWalletTransactionDO> getWalletTransactionPage(Long userId, Integer userType,
|
||||
AppPayWalletTransactionPageReqVO pageVO) {
|
||||
PayWalletDO wallet = payWalletService.getPayWallet(userId, userType);
|
||||
if (wallet == null) {
|
||||
log.error("[getWalletTransactionPage][用户({}/{}) 钱包不存在", userId, userType);
|
||||
throw exception(WALLET_NOT_FOUND);
|
||||
}
|
||||
PayWalletDO wallet = payWalletService.getOrCreatePayWallet(userId, userType);
|
||||
return payWalletTransactionMapper.selectPage(wallet.getId(), pageVO);
|
||||
}
|
||||
|
||||
@ -52,8 +44,7 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ
|
||||
}
|
||||
|
||||
@Override
|
||||
public PayWalletTransactionDO getWalletTransaction(Long walletId, Long bizId, PayWalletBizTypeEnum type) {
|
||||
return payWalletTransactionMapper.selectByWalletIdAndBiz(walletId, bizId, type.getType());
|
||||
public PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type) {
|
||||
return payWalletTransactionMapper.selectByBiz(bizId, type.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.iocoder.yudao.module.pay.util;
|
||||
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
@ -18,10 +20,20 @@ public class MoneyUtils {
|
||||
* @return 百分比金额
|
||||
*/
|
||||
public static Integer calculateRatePrice(Integer price, Double rate) {
|
||||
return new BigDecimal(price)
|
||||
.multiply(BigDecimal.valueOf(rate)) // 乘以
|
||||
.setScale(0, RoundingMode.HALF_UP) // 四舍五入
|
||||
.intValue();
|
||||
return calculateRatePrice(price, rate, 0, RoundingMode.HALF_UP).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算百分比金额
|
||||
*
|
||||
* @param price 金额
|
||||
* @param rate 百分比,例如说 56.77% 则传入 56.77
|
||||
* @param scale 保留小数位数
|
||||
* @param roundingMode 舍入模式
|
||||
*/
|
||||
public static BigDecimal calculateRatePrice(Number price, Number rate, int scale, RoundingMode roundingMode) {
|
||||
return NumberUtil.toBigDecimal(price).multiply(NumberUtil.toBigDecimal(rate)) // 乘以
|
||||
.divide(BigDecimal.valueOf(100), scale, roundingMode); // 除以 100
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user