Commit f1f4d776 by 叶立庆

修复订单退款审核多次调用问题,增加退款申请子订单可退款金额校验

1 parent 0e3268fa
...@@ -82,6 +82,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager ...@@ -82,6 +82,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -1502,6 +1503,24 @@ public class RefundApplyRecordService { ...@@ -1502,6 +1503,24 @@ public class RefundApplyRecordService {
RpcAssert.assertNotNull(applyRecord.getRefundApplyDetailList(), DefaultErrorEnum.DATA_NULL,"退款子订单明细不能为空"); RpcAssert.assertNotNull(applyRecord.getRefundApplyDetailList(), DefaultErrorEnum.DATA_NULL,"退款子订单明细不能为空");
RpcAssert.assertNotNull(applyRecord.getRefundType(), DefaultErrorEnum.DATA_NULL,"退款类型不能为空"); RpcAssert.assertNotNull(applyRecord.getRefundType(), DefaultErrorEnum.DATA_NULL,"退款类型不能为空");
List<GoodsSubOrderDTO> subOrderList = goodsSubOrderService.getByOrderCode(applyRecord.getOrderCode());
if (CollectionUtils.isNotEmpty(subOrderList)) {
Map<String, BigDecimal> subOrderFinalPriceMap = subOrderList.stream()
.collect(Collectors.toMap(GoodsSubOrderDTO::getSubOrderCode,
so -> ObjectUtil.isNotNull(so.getFinalPrice()) ? so.getFinalPrice() : BigDecimal.ZERO));
for (RefundApplyRecordDetailsDTO detail : applyRecord.getRefundApplyDetailList()) {
BigDecimal actualFinalPrice = subOrderFinalPriceMap.get(detail.getSubOrderCode());
if (ObjectUtil.isNull(actualFinalPrice)) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "子订单" + detail.getSubOrderCode() + "不存在");
}
if (ObjectUtil.isNotNull(detail.getRefundAmount())
&& detail.getRefundAmount().compareTo(actualFinalPrice) > 0) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR,
"子订单" + detail.getSubOrderCode() + "退款金额不能大于实际支付金额");
}
}
}
GoodsOrder goodsOrder = goodsOrderService.getBaseByOrderCode(applyRecord.getOrderCode()); GoodsOrder goodsOrder = goodsOrderService.getBaseByOrderCode(applyRecord.getOrderCode());
if(Objects.isNull(goodsOrder) || ( if(Objects.isNull(goodsOrder) || (
!Objects.equals(goodsOrder.getStatus(), CarLifeEnum.OrderStatus.PAYED.getValue()) && !Objects.equals(goodsOrder.getStatus(), CarLifeEnum.OrderStatus.PAYED.getValue()) &&
...@@ -2053,8 +2072,10 @@ public class RefundApplyRecordService { ...@@ -2053,8 +2072,10 @@ public class RefundApplyRecordService {
* @author: lqWang * @author: lqWang
* @date: 2025/7/9 21:14 * @date: 2025/7/9 21:14
*/ */
@GlsxDistributeLockable(name="'RefundApplyRecordService_update'+ #refundApplyRecordDTO.id",timeoutException=RpcServiceException.class,time = 10,timeUnit = TimeUnit.MINUTES)
@Transactional(propagation= Propagation.REQUIRED,rollbackFor= RpcServiceException.class) @Transactional(propagation= Propagation.REQUIRED,rollbackFor= RpcServiceException.class)
public Boolean updateRefundApplyRecord(RefundApplyRecordDTO refundApplyRecordDTO) { public Boolean updateRefundApplyRecord(RefundApplyRecordDTO refundApplyRecordDTO) {
log.info("请求参数:{},{}",refundApplyRecordDTO.getId(),refundApplyRecordDTO.getAuditStatus());
RpcAssert.assertNotNull(refundApplyRecordDTO, DefaultErrorEnum.DATA_NULL, "refundApplyRecordDTO not be null"); RpcAssert.assertNotNull(refundApplyRecordDTO, DefaultErrorEnum.DATA_NULL, "refundApplyRecordDTO not be null");
RpcAssert.assertNotNull(refundApplyRecordDTO.getId(), DefaultErrorEnum.DATA_NULL, "refundApplyRecordDTO.id not be null"); RpcAssert.assertNotNull(refundApplyRecordDTO.getId(), DefaultErrorEnum.DATA_NULL, "refundApplyRecordDTO.id not be null");
RpcAssert.assertNotNull(refundApplyRecordDTO.getAuditStatus(), DefaultErrorEnum.DATA_NULL, "auditStatus not be null"); RpcAssert.assertNotNull(refundApplyRecordDTO.getAuditStatus(), DefaultErrorEnum.DATA_NULL, "auditStatus not be null");
...@@ -2082,6 +2103,7 @@ public class RefundApplyRecordService { ...@@ -2082,6 +2103,7 @@ public class RefundApplyRecordService {
this.markAsRefundedOfOfflinePay(refundApplyRecordDTO); this.markAsRefundedOfOfflinePay(refundApplyRecordDTO);
} }
//修改审核状态 //修改审核状态
log.info("修改审核状态:{}",refundApplyRecordDTO.getAuditStatus());
refundApplyRecordMapper.updateRefundApplyRecord(refundApplyRecordDTO); refundApplyRecordMapper.updateRefundApplyRecord(refundApplyRecordDTO);
//保存操作日志 //保存操作日志
RefundApplyRecordLog recordLog = RefundApplyRecordLog.builder() RefundApplyRecordLog recordLog = RefundApplyRecordLog.builder()
...@@ -2675,4 +2697,4 @@ public class RefundApplyRecordService { ...@@ -2675,4 +2697,4 @@ public class RefundApplyRecordService {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, e.getMessage()); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
} }
\ No newline at end of file \ No newline at end of file
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 分布式锁超时时间设置过长(健壮性与可用性风险)

    • 问题描述:在 updateRefundApplyRecord 方法上添加的分布式锁注解 @GlsxDistributeLockable,其超时时间被设置为 10 分钟(time = 10, timeUnit = TimeUnit.MINUTES)。
    • 影响分析:10 分钟的锁超时时间对于一个常规的业务更新操作来说过长。如果持有锁的节点发生宕机、网络假死或因异常未能正常释放锁(取决于底层锁实现的可靠性),该退款记录将被锁死 10 分钟。在此期间,任何对该记录的重试或并发操作都会被阻塞或直接失败,严重影响系统的可用性和用户体验。
    • 优化建议:建议将超时时间缩短至合理范围(如 10~30 秒)。如果担心业务执行时间较长,应依赖分布式锁底层的“看门狗(WatchDog)”机制来自动续期,而不是直接设置一个极长的绝对超时时间。 java // 建议修改为较短的超时时间,例如 30 秒 @GlsxDistributeLockable(name="'RefundApplyRecordService_update'+ #refundApplyRecordDTO.id", timeoutException=RpcServiceException.class, time = 30, timeUnit = TimeUnit.SECONDS)

    2. Collectors.toMap 存在 IllegalStateException 风险(健壮性风险)

    • 问题描述:在将 subOrderList 转换为 Map 时,使用了两个参数的 Collectors.toMap 方法。
    • 影响分析:如果由于历史脏数据或上游逻辑缺陷,导致 subOrderList 中出现了重复的 subOrderCode,Collectors.toMap 会直接抛出 IllegalStateException: Duplicate key 异常。这会导致退款流程直接阻断,且对外抛出非业务定义的系统异常。
    • 优化建议:使用带有合并函数(merge function)的三个参数的 toMap 方法,优雅处理可能出现的重复键问题。 java Map<String, BigDecimal> subOrderFinalPriceMap = subOrderList.stream() .collect(Collectors.toMap( GoodsSubOrderDTO::getSubOrderCode, so -> ObjectUtil.isNotNull(so.getFinalPrice()) ? so.getFinalPrice() : BigDecimal.ZERO, (existing, replacement) -> existing // 遇到重复的 subOrderCode 时保留第一个,或根据业务抛出自定义业务异常 ));

    3. 缺少对 orderCode 的空值校验(正确性与健壮性)

    • 问题描述:在新增的退款金额校验逻辑中,直接调用了 goodsSubOrderService.getByOrderCode(applyRecord.getOrderCode()),但并未对 applyRecord.getOrderCode() 进行非空校验。
    • 影响分析:虽然前文对 RefundApplyDetailList 和 RefundType 进行了 RpcAssert.assertNotNull 校验,但如果调用方遗漏了 orderCode 参数,直接透传给下游查询服务可能导致无效的数据库全表扫描或抛出 NullPointerException。
    • 优化建议:在执行查询前,增加对 orderCode 的断言校验。 java RpcAssert.assertNotNull(applyRecord.getOrderCode(), DefaultErrorEnum.DATA_NULL, "订单编号不能为空"); List<GoodsSubOrderDTO> subOrderList = goodsSubOrderService.getByOrderCode(applyRecord.getOrderCode());

    二、评分明细

    • 功能实现的正确性与健壮性(33/40分): 核心的退款金额校验逻辑正确,有效拦截了退款金额大于实际支付金额的问题。未触犯 MyBatis 子查询及 Dubbo 接口变更的红线。扣分项在于 Collectors.toMap 缺乏防重保护以及缺失对关键参数 orderCode 的前置非空校验。
    • 安全性与潜在风险(28/30分): 未发现 SQL 注入、越权等严重安全漏洞。扣分项主要在于分布式锁超时时间过长,在极端情况下存在导致单点业务长时间不可用的风险。
    • 最佳实践与可维护性(16/20分): 代码使用了 Java 8 Stream API,逻辑清晰,新增的日志打印有助于排查问题。但长时间的分布式锁不符合高并发系统的最佳实践,且 AOP 锁注解与 @Transactional 同用时需确保锁在事务外围生效(通常锁切面优先级应高于事务切面,需团队内部确认)。
    • 性能与资源利用(3/5分): 引入了合理的局部变量和 Map 结构降低了时间复杂度(O(N)),表现良好。但 10 分钟的锁占用了不必要的逻辑资源空间。
    • 提交信息质量(5/5分): Commit 信息(修复订单退款审核多次调用问题,增加退款申请子订单可退款金额校验)非常清晰,准确描述了修改的背景(修复多次调用)和具体措施(增加金额校验),具有良好的可追溯性。

    总分:85分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!