feat 流水记录处理

This commit is contained in:
bootx
2024-02-19 14:14:02 +08:00
parent 0e31863e21
commit 5fa06c4504
10 changed files with 112 additions and 211 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.stereotype.Service;
public class VoucherRecordService {
private final VoucherRecordManager manager;
/**
* 分页查询
*/

View File

@@ -2,11 +2,9 @@ package cn.bootx.platform.daxpay.service.core.channel.wallet.convert;
import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.Wallet;
import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletConfig;
import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletLog;
import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletRecord;
import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletConfigDto;
import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletDto;
import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletLogDto;
import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletRecordDto;
import cn.bootx.platform.daxpay.service.param.channel.wechat.WalletConfigParam;
import org.mapstruct.Mapper;
@@ -25,8 +23,6 @@ public interface WalletConvert {
WalletDto convert(Wallet in);
WalletLogDto convert(WalletLog in);
WalletConfigDto convert(WalletConfig in);
WalletConfig convert(WalletConfigParam in);

View File

@@ -1,70 +0,0 @@
package cn.bootx.platform.daxpay.service.core.channel.wallet.entity;
import cn.bootx.platform.common.core.function.EntityBaseFunction;
import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity;
import cn.bootx.platform.daxpay.service.core.channel.wallet.convert.WalletConvert;
import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletLogDto;
import cn.bootx.table.modify.annotation.DbColumn;
import cn.bootx.table.modify.annotation.DbTable;
import cn.bootx.table.modify.mysql.annotation.DbMySqlIndex;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* 钱包日志
*
* @author xxm
* @since 2020/12/8
*/
@EqualsAndHashCode(callSuper = true)
@Data
@DbTable(comment = "钱包日志")
@Accessors(chain = true)
@TableName("pay_wallet_log")
public class WalletLog extends MpBaseEntity implements EntityBaseFunction<WalletLogDto> {
/** 钱包id */
@DbMySqlIndex(comment = "钱包索引ID")
@DbColumn(comment = "钱包id")
private Long walletId;
/** 用户id */
@DbColumn(comment = "用户id")
private Long userId;
/** 类型 */
@DbColumn(comment = "类型")
private String type;
/** 交易记录ID */
@DbColumn(comment = "交易记录ID")
private Long paymentId;
/** 业务ID */
@DbColumn(comment = "业务ID")
private String businessId;
/**
* 操作类型
*/
@DbColumn(comment = "操作类型")
private String operationSource;
/** 金额 */
@DbColumn(comment = "金额")
private Integer amount;
/** 备注 */
@DbColumn(comment = "备注")
private String remark;
@Override
public WalletLogDto toDto() {
return WalletConvert.CONVERT.convert(this);
}
}

View File

@@ -39,6 +39,17 @@ public class WalletConfigService {
return walletConfigManager.findById(ID).orElseThrow(() -> new DataNotExistException("钱包配置不存在"));
}
/**
* 获取并校验配置
*/
public WalletConfig getAndCheckConfig(){
WalletConfig config = this.getConfig();
if (!config.getEnable()){
throw new DataNotExistException("钱包配置未启用");
}
return config;
}
/**
* 更新
*/

View File

@@ -39,20 +39,7 @@ public class WalletPayService {
/**
* 关闭支付, 将支付成功的金额进行返还
*/
public void close(PayChannelOrder channelOrder) {
// 从通道扩展参数中取出钱包参数
String channelExtra = channelOrder.getChannelExtra();
WalletPayParam walletPayParam = JSONUtil.toBean(channelExtra, WalletPayParam.class);
// 获取钱包
Wallet wallet = null;
if (Objects.nonNull(walletPayParam.getWalletId())){
wallet = walletManager.findById(walletPayParam.getWalletId()).orElseThrow(DataNotExistException::new);
}
if (Objects.nonNull(walletPayParam.getUserId())){
wallet = walletManager.findByUser(walletPayParam.getUserId()).orElseThrow(DataNotExistException::new);
}
public void close(PayChannelOrder channelOrder, Wallet wallet) {
// 将订单的金额退款到钱包
wallet.setBalance(wallet.getBalance() + channelOrder.getAmount());
walletManager.updateById(wallet);

View File

@@ -5,7 +5,10 @@ import cn.bootx.platform.common.core.rest.PageResult;
import cn.bootx.platform.common.core.rest.param.PageParam;
import cn.bootx.platform.common.mybatisplus.util.MpUtil;
import cn.bootx.platform.daxpay.service.core.channel.wallet.dao.WalletRecordManager;
import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.Wallet;
import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletRecord;
import cn.bootx.platform.daxpay.service.core.order.pay.entity.PayChannelOrder;
import cn.bootx.platform.daxpay.service.core.order.refund.entity.PayRefundChannelOrder;
import cn.bootx.platform.daxpay.service.dto.channel.wallet.WalletRecordDto;
import cn.bootx.platform.daxpay.service.param.channel.wallet.WalletRecordQuery;
import lombok.RequiredArgsConstructor;
@@ -22,6 +25,71 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
public class WalletRecordService {
private final WalletRecordManager walletRecordManager;
private final WalletQueryService walletQueryService;
/**
* 创建保存
*/
public void create(Wallet wallet){
WalletRecord walletRecord = new WalletRecord()
.setAmount(wallet.getBalance())
.setNewAmount(wallet.getBalance())
.setOldAmount(0)
.setWalletId(wallet.getId());
walletRecordManager.save(walletRecord);
}
/**
* 支付保存
*/
public void pay(PayChannelOrder channelOrder, Wallet wallet){
WalletRecord walletRecord = new WalletRecord()
.setAmount(channelOrder.getAmount())
.setNewAmount(wallet.getBalance())
.setOldAmount(wallet.getBalance() - channelOrder.getAmount())
.setOrderId(String.valueOf(channelOrder.getPaymentId()))
.setWalletId(wallet.getId());
walletRecordManager.save(walletRecord);
}
/**
* 退款保存
*/
public void refund(PayRefundChannelOrder channelOrder, Wallet wallet){
WalletRecord walletRecord = new WalletRecord()
.setAmount(channelOrder.getAmount())
.setNewAmount(wallet.getBalance())
.setOldAmount(wallet.getBalance() + channelOrder.getAmount())
.setOrderId(String.valueOf(channelOrder.getRefundId()))
.setWalletId(wallet.getId());
walletRecordManager.save(walletRecord);
}
/**
* 支付关闭
*/
public void payClose(PayChannelOrder channelOrder, Wallet wallet){
WalletRecord walletRecord = new WalletRecord()
.setAmount(channelOrder.getAmount())
.setNewAmount(wallet.getBalance())
.setOldAmount(wallet.getBalance() - channelOrder.getAmount())
.setOrderId(String.valueOf(channelOrder.getPaymentId()))
.setWalletId(wallet.getId());
walletRecordManager.save(walletRecord);
}
/**
* 退款关闭
*/
public void refundClose(PayRefundChannelOrder channelOrder, Wallet wallet){
WalletRecord walletRecord = new WalletRecord()
.setAmount(channelOrder.getAmount())
.setNewAmount(wallet.getBalance())
.setOldAmount(wallet.getBalance() - channelOrder.getAmount())
.setOrderId(String.valueOf(channelOrder.getRefundId()))
.setWalletId(wallet.getId());
walletRecordManager.save(walletRecord);
}
/**
* 分页

View File

@@ -1,8 +1,13 @@
package cn.bootx.platform.daxpay.service.core.payment.close.strategy;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.param.channel.WalletPayParam;
import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.Wallet;
import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletPayService;
import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletQueryService;
import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletRecordService;
import cn.bootx.platform.daxpay.service.func.AbsPayCloseStrategy;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
@@ -21,17 +26,34 @@ import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROT
@RequiredArgsConstructor
public class WalletPayCloseStrategy extends AbsPayCloseStrategy {
private final WalletPayService walletPayService;
private final WalletQueryService walletQueryService;
private final WalletRecordService walletRecordService;
private Wallet wallet;
@Override
public PayChannelEnum getChannel() {
return PayChannelEnum.WALLET;
}
/**
* 关闭前的处理方式
*/
@Override
public void doBeforeCloseHandler() {
// 从通道扩展参数中取出钱包参数
String channelExtra = this.getChannelOrder().getChannelExtra();
WalletPayParam walletPayParam = JSONUtil.toBean(channelExtra, WalletPayParam.class);
this.wallet = walletQueryService.getWallet(walletPayParam);
}
/**
* 关闭操作
*/
@Override
public void doCloseHandler() {
walletPayService.close(this.getChannelOrder());
walletPayService.close(this.getChannelOrder(),this.wallet);
walletRecordService.payClose(this.getChannelOrder(),this.wallet);
}
}

View File

@@ -11,6 +11,7 @@ import cn.bootx.platform.daxpay.service.core.channel.wallet.entity.WalletConfig;
import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletConfigService;
import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletPayService;
import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletQueryService;
import cn.bootx.platform.daxpay.service.core.channel.wallet.service.WalletRecordService;
import cn.bootx.platform.daxpay.service.func.AbsPayStrategy;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
@@ -41,6 +42,8 @@ public class WalletPayStrategy extends AbsPayStrategy {
private final WalletQueryService walletQueryService;
private final WalletRecordService walletRecordService;
private Wallet wallet;
private WalletConfig walletConfig;
@@ -66,7 +69,7 @@ public class WalletPayStrategy extends AbsPayStrategy {
throw new PayFailureException("支付参数错误");
}
this.walletConfig = walletConfigService.getConfig();
this.walletConfig = walletConfigService.getAndCheckConfig();
// 获取钱包
this.wallet = walletQueryService.getWallet(walletPayParam);
@@ -78,6 +81,9 @@ public class WalletPayStrategy extends AbsPayStrategy {
throw new WalletBannedException();
}
// 判断是否超过限额
if (getPayChannelParam().getAmount() > this.walletConfig.getSingleLimit()){
throw new PayFailureException("钱包单次支付金额超过限额");
}
// 判断余额
if (this.wallet.getBalance() < getPayChannelParam().getAmount()) {
@@ -90,7 +96,7 @@ public class WalletPayStrategy extends AbsPayStrategy {
*/
@Override
public void doPayHandler() {
// 异步支付方式时使用冻结方式
walletPayService.pay(getPayChannelParam().getAmount(), this.wallet);
walletRecordService.pay(this.getChannelOrder(), this.wallet);
}
}

View File

@@ -1,61 +0,0 @@
package cn.bootx.platform.daxpay.service.dto.channel.voucher;
import cn.bootx.platform.common.core.rest.dto.BaseDto;
import cn.bootx.platform.daxpay.service.code.VoucherCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
/**
* 储值卡日志
*
* @author xxm
* @since 2022/3/17
*/
@Schema(title = "储值卡日志")
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class VoucherLogDto extends BaseDto {
/** 储值卡id */
@Schema(description = "储值卡id")
private Long voucherId;
/** 储值卡号 */
@Schema(description = "储值卡号")
private String voucherNo;
@Schema(description = "商户编码")
private String mchCode;
@Schema(description = "商户应用编码")
private String mchAppCode;
/** 金额 */
@Schema(description = "金额")
private BigDecimal amount;
/**
* 类型
* @see VoucherCode#LOG_PAY
*/
@Schema(description = "类型")
private String type;
/** 交易记录ID */
@Schema(description = "交易记录ID")
private Long paymentId;
/** 业务ID */
@Schema(description = "业务ID")
private String businessId;
/** 备注 */
@Schema(description = "备注")
private String remark;
}

View File

@@ -1,59 +0,0 @@
package cn.bootx.platform.daxpay.service.dto.channel.wallet;
import cn.bootx.platform.common.core.rest.dto.BaseDto;
import cn.bootx.platform.daxpay.service.code.WalletCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* @author xxm
* @since 2020/12/8
*/
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Schema(title = "钱包日志")
public class WalletLogDto extends BaseDto implements Serializable {
private static final long serialVersionUID = -2553004953931903738L;
@Schema(description = "钱包ID")
private Long walletId;
@Schema(description = "用户ID")
private Long userId;
/**
* @see WalletCode
*/
@Schema(description = "类型")
private String type;
@Schema(description = "交易记录ID")
private Long paymentId;
@Schema(description = "备注")
private String remark;
@Schema(description = "业务ID")
private String businessId;
/**
* @see WalletCode#OPERATION_SOURCE_SYSTEM
*/
@Schema(description = " 1 系统操作 2管理员操作 3用户操作")
private String operationSource;
@Schema(description = "金额")
private BigDecimal amount;
@Schema(description = "创建时间")
private LocalDateTime createTime;
}