ref 分账优化

This commit is contained in:
DaxPay
2024-05-20 23:01:54 +08:00
parent d0da636394
commit 6b5513b638
22 changed files with 225 additions and 56 deletions

View File

@@ -10,7 +10,7 @@ import cn.daxpay.single.code.AllocDetailResultEnum;
import cn.daxpay.single.code.AllocOrderStatusEnum;
import cn.daxpay.single.code.PayChannelEnum;
import cn.daxpay.single.code.PayOrderAllocStatusEnum;
import cn.daxpay.single.param.payment.allocation.AllocationStartParam;
import cn.daxpay.single.param.payment.allocation.AllocStartParam;
import cn.daxpay.single.service.core.order.allocation.dao.AllocationOrderDetailManager;
import cn.daxpay.single.service.core.order.allocation.dao.AllocationOrderManager;
import cn.daxpay.single.service.core.order.allocation.entity.AllocationOrder;
@@ -92,7 +92,7 @@ public class AllocationOrderService {
* 生成分账订单
*/
@Transactional(rollbackFor = Exception.class)
public OrderAndDetail createAndUpdate(AllocationStartParam param, PayOrder payOrder, int orderAmount, List<AllocationGroupReceiverResult> receiversByGroups){
public OrderAndDetail createAndUpdate(AllocStartParam param, PayOrder payOrder, int orderAmount, List<AllocationGroupReceiverResult> receiversByGroups){
long orderId = IdUtil.getSnowflakeNextId();
// 订单明细

View File

@@ -1,8 +1,9 @@
package cn.daxpay.single.service.core.payment.allocation.convert;
import cn.daxpay.single.param.payment.allocation.AllocReceiverAddParam;
import cn.daxpay.single.service.core.payment.allocation.entity.AllocationReceiver;
import cn.daxpay.single.service.dto.allocation.AllocationReceiverDto;
import cn.daxpay.single.service.param.allocation.group.AllocationReceiverParam;
import cn.daxpay.single.service.param.allocation.receiver.AllocationReceiverParam;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@@ -18,4 +19,6 @@ public interface AllocationReceiverConvert {
AllocationReceiverDto convert(AllocationReceiver in);
AllocationReceiver convert(AllocationReceiverParam in);
AllocationReceiver convert(AllocReceiverAddParam in);
}

View File

@@ -38,4 +38,9 @@ public class AllocationGroupReceiverManager extends BaseManager<AllocationGroupR
public void deleteByGroupId(Long groupId){
deleteByField(AllocationGroupReceiver::getGroupId, groupId);
}
/**
* 判断是否存在分账接收者
*/
}

View File

@@ -5,7 +5,7 @@ import cn.bootx.platform.common.mybatisplus.impl.BaseManager;
import cn.bootx.platform.common.mybatisplus.util.MpUtil;
import cn.bootx.platform.common.query.generator.QueryGenerator;
import cn.daxpay.single.service.core.payment.allocation.entity.AllocationReceiver;
import cn.daxpay.single.service.param.allocation.group.AllocationReceiverQuery;
import cn.daxpay.single.service.param.allocation.receiver.AllocationReceiverQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;

View File

@@ -29,6 +29,10 @@ import lombok.experimental.Accessors;
@TableName(value = "pay_allocation_receiver",autoResultMap = true)
public class AllocationReceiver extends MpBaseEntity implements EntityBaseFunction<AllocationReceiverDto> {
/** 分账接收方编号, 需要保证唯一 */
@DbColumn(comment = "分账接收方编号")
private String receiverNo;
@DbColumn(comment = "账号别名")
private String name;

View File

@@ -9,6 +9,7 @@ import cn.bootx.platform.common.mybatisplus.util.MpUtil;
import cn.daxpay.single.code.AllocReceiverTypeEnum;
import cn.daxpay.single.code.PayChannelEnum;
import cn.daxpay.single.exception.pay.PayFailureException;
import cn.daxpay.single.param.payment.allocation.AllocReceiverAddParam;
import cn.daxpay.single.service.core.payment.allocation.convert.AllocationReceiverConvert;
import cn.daxpay.single.service.core.payment.allocation.dao.AllocationGroupReceiverManager;
import cn.daxpay.single.service.core.payment.allocation.dao.AllocationReceiverManager;
@@ -16,9 +17,11 @@ import cn.daxpay.single.service.core.payment.allocation.entity.AllocationReceive
import cn.daxpay.single.service.core.payment.allocation.factory.AllocationReceiverFactory;
import cn.daxpay.single.service.dto.allocation.AllocationReceiverDto;
import cn.daxpay.single.service.func.AbsAllocationReceiverStrategy;
import cn.daxpay.single.service.param.allocation.group.AllocationReceiverParam;
import cn.daxpay.single.service.param.allocation.group.AllocationReceiverQuery;
import cn.daxpay.single.service.param.allocation.receiver.AllocationReceiverParam;
import cn.daxpay.single.service.param.allocation.receiver.AllocationReceiverQuery;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -42,6 +45,8 @@ public class AllocationReceiverService {
private final AllocationGroupReceiverManager groupReceiverManager;
private final LockTemplate lockTemplate;
/**
* 分页
*/
@@ -175,10 +180,29 @@ public class AllocationReceiverService {
}
/**
* 接口添加
* 添加分账接收方并同步到三方支付系统中
*/
public void add(){
public void addAndSync(AllocReceiverAddParam param){
// 判断是否已经添加
AllocationReceiver receiver = AllocationReceiverConvert.CONVERT.convert(param);
// 获取策略
PayChannelEnum channelEnum = PayChannelEnum.findByCode(param.getChannel());
AbsAllocationReceiverStrategy receiverStrategy = AllocationReceiverFactory.create(channelEnum);
// 校验
receiverStrategy.setAllocationReceiver(receiver);
if (!receiverStrategy.validation()){
throw new PayFailureException("接收方信息校验失败");
}
LockInfo lock = lockTemplate.lock("payment:receiver:" + param.getReceiverNo(),10000,200);
try {
// 先添加到三方支付系统中, 然后保存到本地
receiverStrategy.doBeforeHandler();
receiverStrategy.bind();
manager.save(receiver);
} finally {
lockTemplate.releaseLock(lock);
}
}
/**

View File

@@ -7,10 +7,11 @@ import cn.daxpay.single.code.AllocOrderResultEnum;
import cn.daxpay.single.code.AllocOrderStatusEnum;
import cn.daxpay.single.code.PayOrderAllocStatusEnum;
import cn.daxpay.single.exception.pay.PayFailureException;
import cn.daxpay.single.param.payment.allocation.AllocationFinishParam;
import cn.daxpay.single.param.payment.allocation.AllocationStartParam;
import cn.daxpay.single.param.payment.allocation.AllocationSyncParam;
import cn.daxpay.single.param.payment.allocation.AllocFinishParam;
import cn.daxpay.single.param.payment.allocation.AllocStartParam;
import cn.daxpay.single.param.payment.allocation.AllocSyncParam;
import cn.daxpay.single.result.allocation.AllocationResult;
import cn.daxpay.single.result.allocation.AllocationSyncResult;
import cn.daxpay.single.service.common.local.PaymentContextLocal;
import cn.daxpay.single.service.core.order.allocation.dao.AllocationOrderDetailManager;
import cn.daxpay.single.service.core.order.allocation.dao.AllocationOrderManager;
@@ -65,7 +66,7 @@ public class AllocationService {
/**
* 开启分账, 使用分账组进行分账
*/
public AllocationResult allocation(AllocationStartParam param) {
public AllocationResult allocation(AllocStartParam param) {
// 判断是否已经有分账订单
AllocationOrder allocationOrder = allocationOrderManager.findByBizAllocationNo(param.getBizAllocationNo())
.orElse(null);
@@ -83,7 +84,7 @@ public class AllocationService {
/**
* 开启分账, 未传输分账组号, 则使用默认该通道默认分账组
*/
public AllocationResult allocation(AllocationStartParam param,PayOrder payOrder) {
public AllocationResult allocation(AllocStartParam param, PayOrder payOrder) {
LockInfo lock = lockTemplate.lock("payment:allocation:" + payOrder.getId(),10000,200);
if (Objects.isNull(lock)){
throw new RepetitiveOperationException("分账发起处理中,请勿重复操作");
@@ -183,7 +184,7 @@ public class AllocationService {
/**
* 分账完结
*/
public void finish(AllocationFinishParam param) {
public AllocationResult finish(AllocFinishParam param) {
AllocationOrder allocationOrder;
if (Objects.nonNull(param.getAllocationNo())){
allocationOrder = allocationOrderManager.findByAllocationNo(param.getAllocationNo())
@@ -192,13 +193,13 @@ public class AllocationService {
allocationOrder = allocationOrderManager.findByBizAllocationNo(param.getBizAllocationNo())
.orElseThrow(() -> new DataNotExistException("未查询到分账单信息"));
}
this.finish(allocationOrder);
return this.finish(allocationOrder);
}
/**
* 分账完结
*/
public void finish(AllocationOrder allocationOrder) {
public AllocationResult finish(AllocationOrder allocationOrder) {
// 只有分账结束后才可以完结
if (!AllocOrderStatusEnum.ALLOCATION_END.getCode().equals(allocationOrder.getStatus())){
throw new PayFailureException("分账单状态错误");
@@ -224,13 +225,16 @@ public class AllocationService {
.setErrorMsg(e.getMessage());
}
allocationOrderManager.updateById(allocationOrder);
return new AllocationResult()
.setAllocationNo(allocationOrder.getAllocationNo())
.setStatus(allocationOrder.getStatus());
}
/**
* 分账同步, 开启一个新的事务, 不受外部抛出异常的影响
*/
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void sync(AllocationSyncParam param) {
public AllocationSyncResult sync(AllocSyncParam param) {
// 获取分账订单
AllocationOrder allocationOrder = null;
if (Objects.nonNull(param.getAllocationNo())){
@@ -242,6 +246,7 @@ public class AllocationService {
.orElseThrow(() -> new DataNotExistException("分账单不存在"));
}
this.sync(allocationOrder);
return new AllocationSyncResult();
}
/**
@@ -315,7 +320,7 @@ public class AllocationService {
/**
* 获取并检查支付订单
*/
private PayOrder getAndCheckPayOrder(AllocationStartParam param) {
private PayOrder getAndCheckPayOrder(AllocStartParam param) {
// 查询支付单
PayOrder payOrder = payOrderQueryService.findByBizOrOrderNo(param.getOrderNo(), param.getBizOrderNo())
.orElseThrow(() -> new DataNotExistException("支付单不存在"));
@@ -329,4 +334,13 @@ public class AllocationService {
}
return payOrder;
}
/**
* 查询分账结果
*/
public void query(AllocFinishParam param) {
// 查询分账单
// 查询分账单明细
}
}

View File

@@ -1,4 +1,4 @@
package cn.daxpay.single.service.param.allocation.group;
package cn.daxpay.single.service.param.allocation.receiver;
import cn.daxpay.single.code.AllocReceiverTypeEnum;
import cn.daxpay.single.code.AllocRelationTypeEnum;

View File

@@ -1,4 +1,4 @@
package cn.daxpay.single.service.param.allocation.group;
package cn.daxpay.single.service.param.allocation.receiver;
import cn.bootx.platform.common.core.annotation.QueryParam;
import cn.daxpay.single.code.AllocRelationTypeEnum;

View File

@@ -1,6 +1,6 @@
package cn.daxpay.single.service.task;
import cn.daxpay.single.param.payment.allocation.AllocationStartParam;
import cn.daxpay.single.param.payment.allocation.AllocStartParam;
import cn.daxpay.single.service.core.order.pay.dao.PayOrderManager;
import cn.daxpay.single.service.core.order.pay.entity.PayOrder;
import cn.daxpay.single.service.core.payment.allocation.service.AllocationService;
@@ -27,7 +27,7 @@ public class AllocationAutoStartTask implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
for (PayOrder payOrder : payOrderManager.findAutoAllocation()) {
AllocationStartParam param = new AllocationStartParam();
AllocStartParam param = new AllocStartParam();
param.setBizAllocationNo(OrderNoGenerateUtil.allocation());
try {
allocationService.allocation(param, payOrder);