This commit is contained in:
Guoqs
2025-04-16 11:49:26 +08:00
parent 1b2d17fae8
commit 6e8cd51cb4
8 changed files with 9272 additions and 1353 deletions

View File

@@ -20,12 +20,15 @@ import com.jsowell.adapay.response.PaymentReverseResponse;
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.adapay.vo.OrderSplitResult;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.enums.ykc.ScenarioEnum;
import com.jsowell.common.util.AdapayUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.AdapayMemberAccount;
import com.jsowell.pile.service.OrderBasicInfoService;
import com.jsowell.pile.service.OrderUnsplitRecordService;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -33,6 +36,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@@ -40,9 +44,8 @@ import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* 专用处理汇付支付相关
@@ -52,13 +55,18 @@ import java.util.Map;
@RunWith(SpringRunner.class)
public class PaymentTestController {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
String wechatAppId1 = "wxbb3e0d474569481d"; // 万车充
String wechatAppId2 = "wx20abc5210391649c"; // 嘉佳充电
String adapayAppId = "app_d0c80cb1-ffc8-48cb-a030-fe9bec823aaa";
@Autowired
private RedisCache redisCache;
@Autowired
private OrderUnsplitRecordService orderUnsplitRecordService;
@Autowired
private AdapayService adapayService;
@@ -242,6 +250,67 @@ public class PaymentTestController {
}
}
}
// 执行分账并处理重试
// for (PaymentConfirmParam param : paramList) {
// executeWithRetry(param, 0); // 初始重试次数为0
// }
}
/**
* 执行分账并处理重试逻辑
*/
private void executeWithRetry(PaymentConfirmParam param, int retryCount) {
PaymentConfirmResponse response = adapayService.createPaymentConfirmRequest(param);
if (!response.isFailed()) {
logger.info("分账成功: paymentId={}", param.getPaymentId());
return;
}
// 失败时记录日志
logger.error("分账失败: paymentId={}, 重试次数={}, 错误信息={}", param.getPaymentId(), retryCount, response.getError_msg());
// 如果未达到最大重试次数,将任务放入延迟队列
if (retryCount < 3) {
retryCount++;
logger.info("将分账任务放入延迟队列: paymentId={}, 重试次数={}", param.getPaymentId(), retryCount);
redisCache.setCacheObject(
CacheConstants.DELAYED_PAYMENT_CONFIRM_QUEUE + param.getPaymentId(),
param,
60, // 延迟1分钟
TimeUnit.SECONDS
);
} else {
logger.error("分账失败超过最大重试次数: paymentId={}", param.getPaymentId());
// 记录最终失败的任务,便于后续处理
recordFailedPayment(param);
}
}
/**
* 记录最终失败的分账任务
*/
private void recordFailedPayment(PaymentConfirmParam param) {
String failedKey = CacheConstants.FAILED_PAYMENT_CONFIRM_LIST;
redisCache.setCacheList(failedKey, Lists.newArrayList(param));
logger.error("记录最终失败的分账任务: paymentId={}", param.getPaymentId());
}
/**
* 处理延迟队列中的分账任务
*/
@Scheduled(fixedDelay = 60000) // 每分钟执行一次
public void processDelayedPaymentConfirm() {
String delayedQueueKey = CacheConstants.DELAYED_PAYMENT_CONFIRM_QUEUE + "*";
Collection<String> keys = redisCache.keys(delayedQueueKey);
for (String key : keys) {
PaymentConfirmParam param = redisCache.getCacheObject(key);
if (param != null) {
int retryCount = Integer.parseInt(key.split("_")[key.split("_").length - 1]);
executeWithRetry(param, retryCount);
redisCache.deleteObject(key); // 处理完成后删除任务
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -404,4 +404,7 @@ public class CacheConstants {
* 优惠券绑定
*/
public static final String BIND_PARKING_COUPON = "bindParkingCoupon_";
public static final String DELAYED_PAYMENT_CONFIRM_QUEUE = "delayed_payment_confirm_queue:";
public static final String FAILED_PAYMENT_CONFIRM_LIST = "failed_payment_confirm_list:";
}

View File

@@ -0,0 +1,58 @@
package com.jsowell.pile.domain;
import java.math.BigDecimal;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
@Data
@Accessors(chain = true)
@SuperBuilder
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OrderUnsplitRecord {
/**
* 主键
*/
private Integer id;
/**
* 支付id
*/
private String paymentId;
/**
* 状态
*/
private String status;
/**
* 订单编号
*/
private String orderCode;
/**
* 支付金额
*/
private BigDecimal payAmount;
/**
* 结算金额
*/
private BigDecimal settleAmount;
/**
* 订单时间
*/
private Date orderTime;
/**
* 记录添加时间
*/
private Date createTime;
}

View File

@@ -0,0 +1,25 @@
package com.jsowell.pile.mapper;
import com.jsowell.pile.domain.OrderUnsplitRecord;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface OrderUnsplitRecordMapper {
int deleteByPrimaryKey(Integer id);
int insertSelective(OrderUnsplitRecord record);
OrderUnsplitRecord selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(OrderUnsplitRecord record);
int updateBatch(List<OrderUnsplitRecord> list);
int updateBatchSelective(List<OrderUnsplitRecord> list);
int batchInsert(@Param("list") List<OrderUnsplitRecord> list);
int insertOrUpdate(OrderUnsplitRecord record);
int insertOrUpdateSelective(OrderUnsplitRecord record);
}

View File

@@ -0,0 +1,4 @@
package com.jsowell.pile.service;
public interface OrderUnsplitRecordService {
}

View File

@@ -0,0 +1,8 @@
package com.jsowell.pile.service.impl;
import com.jsowell.pile.service.OrderUnsplitRecordService;
import org.springframework.stereotype.Service;
@Service
public class OrderUnsplitRecordServiceImpl implements OrderUnsplitRecordService {
}

View File

@@ -0,0 +1,352 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jsowell.pile.mapper.OrderUnsplitRecordMapper">
<resultMap id="BaseResultMap" type="com.jsowell.pile.domain.OrderUnsplitRecord">
<!--@mbg.generated-->
<!--@Table order_unsplit_record-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="payment_id" jdbcType="VARCHAR" property="paymentId" />
<result column="status" jdbcType="VARCHAR" property="status" />
<result column="order_code" jdbcType="VARCHAR" property="orderCode" />
<result column="pay_amount" jdbcType="DECIMAL" property="payAmount" />
<result column="settle_amount" jdbcType="DECIMAL" property="settleAmount" />
<result column="order_time" jdbcType="TIMESTAMP" property="orderTime" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, payment_id, `status`, order_code, pay_amount, settle_amount, order_time, create_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from order_unsplit_record
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--@mbg.generated-->
delete from order_unsplit_record
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.jsowell.pile.domain.OrderUnsplitRecord" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into order_unsplit_record
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="paymentId != null">
payment_id,
</if>
<if test="status != null">
`status`,
</if>
<if test="orderCode != null">
order_code,
</if>
<if test="payAmount != null">
pay_amount,
</if>
<if test="settleAmount != null">
settle_amount,
</if>
<if test="orderTime != null">
order_time,
</if>
<if test="createTime != null">
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="paymentId != null">
#{paymentId,jdbcType=VARCHAR},
</if>
<if test="status != null">
#{status,jdbcType=VARCHAR},
</if>
<if test="orderCode != null">
#{orderCode,jdbcType=VARCHAR},
</if>
<if test="payAmount != null">
#{payAmount,jdbcType=DECIMAL},
</if>
<if test="settleAmount != null">
#{settleAmount,jdbcType=DECIMAL},
</if>
<if test="orderTime != null">
#{orderTime,jdbcType=TIMESTAMP},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.jsowell.pile.domain.OrderUnsplitRecord">
<!--@mbg.generated-->
update order_unsplit_record
<set>
<if test="paymentId != null">
payment_id = #{paymentId,jdbcType=VARCHAR},
</if>
<if test="status != null">
`status` = #{status,jdbcType=VARCHAR},
</if>
<if test="orderCode != null">
order_code = #{orderCode,jdbcType=VARCHAR},
</if>
<if test="payAmount != null">
pay_amount = #{payAmount,jdbcType=DECIMAL},
</if>
<if test="settleAmount != null">
settle_amount = #{settleAmount,jdbcType=DECIMAL},
</if>
<if test="orderTime != null">
order_time = #{orderTime,jdbcType=TIMESTAMP},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateBatch" parameterType="java.util.List">
<!--@mbg.generated-->
update order_unsplit_record
<trim prefix="set" suffixOverrides=",">
<trim prefix="payment_id = case" suffix="end,">
<foreach collection="list" index="index" item="item">
when id = #{item.id,jdbcType=INTEGER} then #{item.paymentId,jdbcType=VARCHAR}
</foreach>
</trim>
<trim prefix="`status` = case" suffix="end,">
<foreach collection="list" index="index" item="item">
when id = #{item.id,jdbcType=INTEGER} then #{item.status,jdbcType=VARCHAR}
</foreach>
</trim>
<trim prefix="order_code = case" suffix="end,">
<foreach collection="list" index="index" item="item">
when id = #{item.id,jdbcType=INTEGER} then #{item.orderCode,jdbcType=VARCHAR}
</foreach>
</trim>
<trim prefix="pay_amount = case" suffix="end,">
<foreach collection="list" index="index" item="item">
when id = #{item.id,jdbcType=INTEGER} then #{item.payAmount,jdbcType=DECIMAL}
</foreach>
</trim>
<trim prefix="settle_amount = case" suffix="end,">
<foreach collection="list" index="index" item="item">
when id = #{item.id,jdbcType=INTEGER} then #{item.settleAmount,jdbcType=DECIMAL}
</foreach>
</trim>
<trim prefix="order_time = case" suffix="end,">
<foreach collection="list" index="index" item="item">
when id = #{item.id,jdbcType=INTEGER} then #{item.orderTime,jdbcType=TIMESTAMP}
</foreach>
</trim>
<trim prefix="create_time = case" suffix="end,">
<foreach collection="list" index="index" item="item">
when id = #{item.id,jdbcType=INTEGER} then #{item.createTime,jdbcType=TIMESTAMP}
</foreach>
</trim>
</trim>
where id in
<foreach close=")" collection="list" item="item" open="(" separator=", ">
#{item.id,jdbcType=INTEGER}
</foreach>
</update>
<update id="updateBatchSelective" parameterType="java.util.List">
<!--@mbg.generated-->
update order_unsplit_record
<trim prefix="set" suffixOverrides=",">
<trim prefix="payment_id = case" suffix="end,">
<foreach collection="list" index="index" item="item">
<if test="item.paymentId != null">
when id = #{item.id,jdbcType=INTEGER} then #{item.paymentId,jdbcType=VARCHAR}
</if>
</foreach>
</trim>
<trim prefix="`status` = case" suffix="end,">
<foreach collection="list" index="index" item="item">
<if test="item.status != null">
when id = #{item.id,jdbcType=INTEGER} then #{item.status,jdbcType=VARCHAR}
</if>
</foreach>
</trim>
<trim prefix="order_code = case" suffix="end,">
<foreach collection="list" index="index" item="item">
<if test="item.orderCode != null">
when id = #{item.id,jdbcType=INTEGER} then #{item.orderCode,jdbcType=VARCHAR}
</if>
</foreach>
</trim>
<trim prefix="pay_amount = case" suffix="end,">
<foreach collection="list" index="index" item="item">
<if test="item.payAmount != null">
when id = #{item.id,jdbcType=INTEGER} then #{item.payAmount,jdbcType=DECIMAL}
</if>
</foreach>
</trim>
<trim prefix="settle_amount = case" suffix="end,">
<foreach collection="list" index="index" item="item">
<if test="item.settleAmount != null">
when id = #{item.id,jdbcType=INTEGER} then #{item.settleAmount,jdbcType=DECIMAL}
</if>
</foreach>
</trim>
<trim prefix="order_time = case" suffix="end,">
<foreach collection="list" index="index" item="item">
<if test="item.orderTime != null">
when id = #{item.id,jdbcType=INTEGER} then #{item.orderTime,jdbcType=TIMESTAMP}
</if>
</foreach>
</trim>
<trim prefix="create_time = case" suffix="end,">
<foreach collection="list" index="index" item="item">
<if test="item.createTime != null">
when id = #{item.id,jdbcType=INTEGER} then #{item.createTime,jdbcType=TIMESTAMP}
</if>
</foreach>
</trim>
</trim>
where id in
<foreach close=")" collection="list" item="item" open="(" separator=", ">
#{item.id,jdbcType=INTEGER}
</foreach>
</update>
<insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into order_unsplit_record
(payment_id, `status`, order_code, pay_amount, settle_amount, order_time, create_time
)
values
<foreach collection="list" item="item" separator=",">
(#{item.paymentId,jdbcType=VARCHAR}, #{item.status,jdbcType=VARCHAR}, #{item.orderCode,jdbcType=VARCHAR},
#{item.payAmount,jdbcType=DECIMAL}, #{item.settleAmount,jdbcType=DECIMAL}, #{item.orderTime,jdbcType=TIMESTAMP},
#{item.createTime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<insert id="insertOrUpdate" keyColumn="id" keyProperty="id" parameterType="com.jsowell.pile.domain.OrderUnsplitRecord" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into order_unsplit_record
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
payment_id,
`status`,
order_code,
pay_amount,
settle_amount,
order_time,
create_time,
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
#{paymentId,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR},
#{orderCode,jdbcType=VARCHAR},
#{payAmount,jdbcType=DECIMAL},
#{settleAmount,jdbcType=DECIMAL},
#{orderTime,jdbcType=TIMESTAMP},
#{createTime,jdbcType=TIMESTAMP},
</trim>
on duplicate key update
<trim suffixOverrides=",">
<if test="id != null">
id = #{id,jdbcType=INTEGER},
</if>
payment_id = #{paymentId,jdbcType=VARCHAR},
`status` = #{status,jdbcType=VARCHAR},
order_code = #{orderCode,jdbcType=VARCHAR},
pay_amount = #{payAmount,jdbcType=DECIMAL},
settle_amount = #{settleAmount,jdbcType=DECIMAL},
order_time = #{orderTime,jdbcType=TIMESTAMP},
create_time = #{createTime,jdbcType=TIMESTAMP},
</trim>
</insert>
<insert id="insertOrUpdateSelective" keyColumn="id" keyProperty="id" parameterType="com.jsowell.pile.domain.OrderUnsplitRecord" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into order_unsplit_record
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="paymentId != null">
payment_id,
</if>
<if test="status != null">
`status`,
</if>
<if test="orderCode != null">
order_code,
</if>
<if test="payAmount != null">
pay_amount,
</if>
<if test="settleAmount != null">
settle_amount,
</if>
<if test="orderTime != null">
order_time,
</if>
<if test="createTime != null">
create_time,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="paymentId != null">
#{paymentId,jdbcType=VARCHAR},
</if>
<if test="status != null">
#{status,jdbcType=VARCHAR},
</if>
<if test="orderCode != null">
#{orderCode,jdbcType=VARCHAR},
</if>
<if test="payAmount != null">
#{payAmount,jdbcType=DECIMAL},
</if>
<if test="settleAmount != null">
#{settleAmount,jdbcType=DECIMAL},
</if>
<if test="orderTime != null">
#{orderTime,jdbcType=TIMESTAMP},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
</trim>
on duplicate key update
<trim suffixOverrides=",">
<if test="id != null">
id = #{id,jdbcType=INTEGER},
</if>
<if test="paymentId != null">
payment_id = #{paymentId,jdbcType=VARCHAR},
</if>
<if test="status != null">
`status` = #{status,jdbcType=VARCHAR},
</if>
<if test="orderCode != null">
order_code = #{orderCode,jdbcType=VARCHAR},
</if>
<if test="payAmount != null">
pay_amount = #{payAmount,jdbcType=DECIMAL},
</if>
<if test="settleAmount != null">
settle_amount = #{settleAmount,jdbcType=DECIMAL},
</if>
<if test="orderTime != null">
order_time = #{orderTime,jdbcType=TIMESTAMP},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
</mapper>