feat 分账订单接口和字段微调

This commit is contained in:
bootx
2024-04-07 23:14:35 +08:00
parent 6825ce4cfe
commit d1010c2572
12 changed files with 341 additions and 29 deletions

View File

@@ -0,0 +1,79 @@
package cn.bootx.platform.daxpay.admin.controller.allocation;
import cn.bootx.platform.common.core.rest.PageResult;
import cn.bootx.platform.common.core.rest.Res;
import cn.bootx.platform.common.core.rest.ResResult;
import cn.bootx.platform.common.core.rest.dto.LabelValue;
import cn.bootx.platform.common.core.rest.param.PageParam;
import cn.bootx.platform.daxpay.param.pay.allocation.AllocationStartParam;
import cn.bootx.platform.daxpay.service.core.order.allocation.service.AllocationOrderService;
import cn.bootx.platform.daxpay.service.core.payment.allocation.service.AllocationService;
import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDetailDto;
import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDto;
import cn.bootx.platform.daxpay.service.param.order.AllocationOrderQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 对账订单控制器
* @author xxm
* @since 2024/4/7
*/
@Tag(name = "对账订单控制器")
@RestController
@RequestMapping("/allocation/order")
@RequiredArgsConstructor
public class AllocationOrderController {
private final AllocationOrderService allocationOrderService;
private final AllocationService allocationService;
@Operation(summary = "分页")
@GetMapping("/page")
public ResResult<PageResult<AllocationOrderDto>> page(PageParam pageParam, AllocationOrderQuery param){
return Res.ok(allocationOrderService.page(pageParam,param));
}
@Operation(summary = "分账明细列表")
@GetMapping("/detail/findAll")
public ResResult<List<AllocationOrderDetailDto>> findDetailsByOrderId(Long orderId){
return Res.ok(allocationOrderService.findDetailsByOrderId(orderId));
}
@Operation(summary = "查询详情")
@GetMapping("/findById")
public ResResult<AllocationOrderDto> findById(Long id){
return Res.ok(allocationOrderService.findById(id));
}
@Operation(summary = "查询明细详情")
@GetMapping("/detail/findById")
public ResResult<AllocationOrderDetailDto> findDetailById(Long id){
return Res.ok(allocationOrderService.findDetailById(id));
}
@Operation(summary = "获取可以分账的通道")
@GetMapping("/findChannels")
public ResResult<List<LabelValue>> findChannels(){
return Res.ok(allocationOrderService.findChannels());
}
@Operation(summary = "发起分账")
@PostMapping("/start")
public ResResult<Void> start(Long paymentId){
AllocationStartParam param = new AllocationStartParam();
param.setPaymentId(paymentId);
allocationService.allocation(param);
return Res.ok();
}
}

View File

@@ -46,6 +46,7 @@ public class AllocationReceiverController {
public ResResult<List<LabelValue>> findChannels(){
return Res.ok(receiverService.findChannels());
}
@Operation(summary = "根据通道获取分账接收方类型")
@GetMapping("/findReceiverTypeByChannel")
public ResResult<List<LabelValue>> findReceiverTypeByChannel(String channel){

View File

@@ -39,14 +39,18 @@ public class WeChatPayAllocationService {
*/
@SneakyThrows
public void allocation(AllocationOrder allocationOrder, List<AllocationOrderDetail> orderDetails, WeChatPayConfig config){
String description = allocationOrder.getDescription();
if (StrUtil.isBlank(description)){
description = "分账";
}
String finalDescription = description;
List<ReceiverModel> list = orderDetails.stream().map(o->{
AllocationReceiverTypeEnum receiverTypeEnum = AllocationReceiverTypeEnum.findByCode(o.getReceiverType());
return ReceiverModel.builder()
.type(receiverTypeEnum.getOutCode())
.account(o.getReceiverAccount())
.amount(o.getAmount())
.description(allocationOrder.getDescription())
.description(finalDescription)
.build();
}).collect(Collectors.toList());

View File

@@ -6,6 +6,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
*
* @author xxm
@@ -15,4 +17,11 @@ import org.springframework.stereotype.Repository;
@Repository
@RequiredArgsConstructor
public class AllocationOrderDetailManager extends BaseManager<AllocationOrderDetailMapper, AllocationOrderDetail> {
/**
* 根据订单ID查询
*/
public List<AllocationOrderDetail> findAllByOrderId(Long orderId) {
return findAllByField(AllocationOrderDetail::getOrderId, orderId);
}
}

View File

@@ -1,7 +1,13 @@
package cn.bootx.platform.daxpay.service.core.order.allocation.dao;
import cn.bootx.platform.common.core.rest.param.PageParam;
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.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrder;
import cn.bootx.platform.daxpay.service.param.order.AllocationOrderQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
@@ -24,4 +30,13 @@ public class AllocationOrderManager extends BaseManager<AllocationOrderMapper, A
public Optional<AllocationOrder> findByAllocationNo(String allocationNo){
return findByField(AllocationOrder::getAllocationNo, allocationNo);
}
/**
* 分页
*/
public Page<AllocationOrder> page(PageParam pageParam, AllocationOrderQuery param){
Page<AllocationOrder> mpPage = MpUtil.getMpPage(pageParam, AllocationOrder.class);
QueryWrapper<AllocationOrder> generator = QueryGenerator.generator(param);
return this.page(mpPage, generator);
}
}

View File

@@ -2,13 +2,18 @@ package cn.bootx.platform.daxpay.service.core.order.allocation.entity;
import cn.bootx.platform.common.core.function.EntityBaseFunction;
import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.service.core.order.allocation.convert.AllocationConvert;
import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDto;
import cn.bootx.table.modify.annotation.DbColumn;
import cn.bootx.table.modify.annotation.DbTable;
import cn.bootx.table.modify.mysql.annotation.DbMySqlIndex;
import cn.bootx.table.modify.mysql.constants.MySqlIndexType;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@@ -23,6 +28,8 @@ import java.time.LocalDateTime;
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@DbTable(comment = "分账订单")
@TableName("pay_allocation_order")
public class AllocationOrder extends MpBaseEntity implements EntityBaseFunction<AllocationOrderDto> {
/**
@@ -31,6 +38,12 @@ public class AllocationOrder extends MpBaseEntity implements EntityBaseFunction<
@DbColumn(comment = "支付订单ID")
private Long paymentId;
/**
* 支付订单标题
*/
@Schema(description = "支付订单标题")
private String title;
/**
* 网关支付订单号
*/
@@ -59,6 +72,7 @@ public class AllocationOrder extends MpBaseEntity implements EntityBaseFunction<
/**
* 所属通道
* @see PayChannelEnum
*/
@DbColumn(comment = "所属通道")
private String channel;
@@ -77,6 +91,7 @@ public class AllocationOrder extends MpBaseEntity implements EntityBaseFunction<
/**
* 状态
* @see AllocationStatusEnum
*/
@DbColumn(comment = "状态")
private String status;

View File

@@ -1,12 +1,14 @@
package cn.bootx.platform.daxpay.service.core.order.allocation.entity;
import cn.bootx.platform.common.core.annotation.EncryptionField;
import cn.bootx.platform.common.core.function.EntityBaseFunction;
import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity;
import cn.bootx.platform.daxpay.code.AllocationReceiverTypeEnum;
import cn.bootx.platform.daxpay.service.core.order.allocation.convert.AllocationConvert;
import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDetailDto;
import cn.bootx.platform.starter.data.perm.sensitive.SensitiveInfo;
import cn.bootx.table.modify.annotation.DbColumn;
import cn.bootx.table.modify.annotation.DbTable;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@@ -19,6 +21,8 @@ import lombok.experimental.Accessors;
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@DbTable(comment = "分账订单明细")
@TableName("pay_allocation_order_detail")
public class AllocationOrderDetail extends MpBaseEntity implements EntityBaseFunction<AllocationOrderDetailDto> {
/** 分账订单ID */
@@ -46,7 +50,7 @@ public class AllocationOrderDetail extends MpBaseEntity implements EntityBaseFun
/** 接收方账号 */
@DbColumn(comment = "接收方账号")
@SensitiveInfo
@EncryptionField
private String receiverAccount;
/** 接收方姓名 */

View File

@@ -1,6 +1,12 @@
package cn.bootx.platform.daxpay.service.core.order.allocation.service;
import cn.bootx.platform.common.core.exception.DataNotExistException;
import cn.bootx.platform.common.core.rest.PageResult;
import cn.bootx.platform.common.core.rest.dto.LabelValue;
import cn.bootx.platform.common.core.rest.param.PageParam;
import cn.bootx.platform.common.core.util.ResultConvertUtil;
import cn.bootx.platform.common.mybatisplus.util.MpUtil;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.param.pay.allocation.AllocationStartParam;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.service.core.order.allocation.dao.AllocationOrderDetailManager;
@@ -10,6 +16,8 @@ import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationO
import cn.bootx.platform.daxpay.service.core.order.allocation.entity.OrderAndDetail;
import cn.bootx.platform.daxpay.service.core.order.pay.entity.PayOrder;
import cn.bootx.platform.daxpay.service.dto.allocation.AllocationGroupReceiverResult;
import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDetailDto;
import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDto;
import cn.bootx.platform.daxpay.service.param.order.AllocationOrderQuery;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
@@ -18,6 +26,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@@ -33,13 +42,47 @@ public class AllocationOrderService {
private final AllocationOrderManager allocationOrderManager;
private final AllocationOrderDetailManager allocationOrderDetailManager;
/**
* 分页查询分账订单
*/
public void page(PageParam pageParam, AllocationOrderQuery param){
/**
* 获取可以分账的通道
*/
public List<LabelValue> findChannels(){
return Arrays.asList(
new LabelValue(PayChannelEnum.ALI.getName(),PayChannelEnum.ALI.getCode()),
new LabelValue(PayChannelEnum.WECHAT.getName(),PayChannelEnum.WECHAT.getCode())
);
}
/**
* 分页查询
*/
public PageResult<AllocationOrderDto> page(PageParam pageParam, AllocationOrderQuery param){
return MpUtil.convert2DtoPageResult(allocationOrderManager.page(pageParam, param));
}
/**
* 查询详情
*/
public AllocationOrderDto findById(Long id) {
return allocationOrderManager.findById(id).map(AllocationOrder::toDto).orElseThrow(() -> new DataNotExistException("分账订单不存在"));
}
/**
* 查询订单明细列表
*/
public List<AllocationOrderDetailDto> findDetailsByOrderId(Long orderId){
return ResultConvertUtil.dtoListConvert(allocationOrderDetailManager.findAllByOrderId(orderId));
}
/**
* 查询订单明细详情
*/
public AllocationOrderDetailDto findDetailById(Long id){
return allocationOrderDetailManager.findById(id).map(AllocationOrderDetail::toDto).orElseThrow(() -> new DataNotExistException("分账订单明细不存在"));
}
/**
* 生成分账订单
*/
@@ -78,6 +121,7 @@ public class AllocationOrderService {
// 分账订单
AllocationOrder allocationOrder = new AllocationOrder()
.setPaymentId(payOrder.getId())
.setTitle(payOrder.getTitle())
.setAllocationNo(allocationNo)
.setChannel(payOrder.getAsyncChannel())
.setGatewayPayOrderNo(payOrder.getGatewayOrderNo())

View File

@@ -20,6 +20,7 @@ import cn.bootx.platform.daxpay.service.core.payment.allocation.entity.Allocatio
import cn.bootx.platform.daxpay.service.core.payment.allocation.factory.AllocationFactory;
import cn.bootx.platform.daxpay.service.dto.allocation.AllocationGroupReceiverResult;
import cn.bootx.platform.daxpay.service.func.AbsAllocationStrategy;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -49,25 +50,12 @@ public class AllocationService {
private final AllocationOrderService allocationOrderService;
/**
* 开启分账, 使用分账组进行分账
*/
public AllocationResult allocation(AllocationStartParam param) {
// 查询支付单
PayOrder payOrder = null;
if (Objects.nonNull(param.getPaymentId())){
payOrder = payOrderManager.findById(param.getPaymentId())
.orElseThrow(() -> new DataNotExistException("未查询到支付订单"));
}
if (Objects.isNull(payOrder)){
payOrder = payOrderManager.findByBusinessNo(param.getBusinessNo())
.orElseThrow(() -> new DataNotExistException("未查询到支付订单"));
}
// 判断订单是否可以分账
if (!payOrder.isAllocation()){
throw new PayFailureException("该订单不允许分账");
}
PayOrder payOrder = this.getAndCheckPayOrder(param);
// 查询待分账的通道支付订单
PayChannelOrder channelOrder = payChannelOrderManager.findByAsyncChannel(payOrder.getId())
@@ -144,4 +132,27 @@ public class AllocationService {
allocationOrderManager.updateById(allocationOrder);
}
/**
* 获取并检查支付订单
*/
private PayOrder getAndCheckPayOrder(AllocationStartParam param) {
// 查询支付单
PayOrder payOrder = null;
if (Objects.nonNull(param.getPaymentId())){
payOrder = payOrderManager.findById(param.getPaymentId())
.orElseThrow(() -> new DataNotExistException("未查询到支付订单"));
}
if (StrUtil.isNotBlank(param.getBusinessNo())){
payOrder = payOrderManager.findByBusinessNo(param.getBusinessNo())
.orElseThrow(() -> new DataNotExistException("未查询到支付订单"));
}
// 判断订单是否可以分账
if (!payOrder.isAllocation()){
throw new PayFailureException("该订单不允许分账");
}
return payOrder;
}
}

View File

@@ -1,6 +1,8 @@
package cn.bootx.platform.daxpay.service.dto.order.allocation;
import cn.bootx.platform.common.core.rest.dto.BaseDto;
import cn.bootx.platform.daxpay.code.AllocationReceiverTypeEnum;
import cn.bootx.platform.starter.data.perm.sensitive.SensitiveInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -14,6 +16,43 @@ import lombok.experimental.Accessors;
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Schema(title = "")
@Schema(title = "分账订单详情")
public class AllocationOrderDetailDto extends BaseDto {
/** 分账订单ID */
@Schema(description = "分账订单ID")
private Long orderId;
/** 接收者ID */
@Schema(description = "接收者ID")
private Long receiverId;
/** 分账比例 */
@Schema(description = "分账比例(万分之多少)")
private Integer rate;
/** 分账金额 */
@Schema(description = "分账金额")
private Integer amount;
/**
* 分账接收方类型
* @see AllocationReceiverTypeEnum
*/
@Schema(description = "分账接收方类型")
private String receiverType;
/** 接收方账号 */
@Schema(description = "接收方账号")
@SensitiveInfo
private String receiverAccount;
/** 接收方姓名 */
@Schema(description = "接收方姓名")
private String receiverName;
/** 状态 */
@Schema(description = "状态")
private String status;
}

View File

@@ -1,11 +1,15 @@
package cn.bootx.platform.daxpay.service.dto.order.allocation;
import cn.bootx.platform.common.core.rest.dto.BaseDto;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
/**
* 分账订单
* @author xxm
@@ -16,6 +20,79 @@ import lombok.experimental.Accessors;
@Accessors(chain = true)
@Schema(title = "分账订单")
public class AllocationOrderDto extends BaseDto {
/**
* 支付订单ID
*/
@Schema(description = "支付订单ID")
private Long paymentId;
/**
* 支付订单标题
*/
@Schema(description = "支付订单标题")
private String title;
/**
* 网关支付订单号
*/
@Schema(description = "网关支付订单号")
private String gatewayPayOrderNo;
/**
* 网关分账单号
*/
@Schema(description = "网关分账单号")
private String gatewayAllocationNo;
/**
* 分账单号
*/
@Schema(description = "分账单号")
private String allocationNo;
/**
* 外部请求号
*/
@Schema(description = "外部请求号")
private String outReqNo;
/**
* 所属通道
*/
@Schema(description = "所属通道")
private String channel;
/**
* 总分账金额
*/
@Schema(description = "总分账金额")
private Integer amount;
/**
* 分账描述
*/
@Schema(description = "分账描述")
private String description;
/**
* 状态
* @see cn.bootx.platform.daxpay.service.code.AllocationStatusEnum
*/
@Schema(description = "状态")
private String status;
/**
* 错误原因
*/
@Schema(description = "错误原因")
@TableField(updateStrategy = FieldStrategy.ALWAYS)
private String errorMsg;
/**
* 完成时间
*/
@Schema(description = "完成时间")
private LocalDateTime finishTime;
}