Forráskód Böngészése

Merge remote-tracking branch 'origin/dev' into dev

qxy 3 hete
szülő
commit
4eb46918d4

+ 96 - 5
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerUserViolationServiceImpl.java

@@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 import shop.alien.entity.store.*;
 import shop.alien.entity.store.dto.LawyerUserViolationDto;
@@ -21,10 +22,13 @@ import shop.alien.mapper.*;
 import shop.alien.lawyer.config.WebSocketProcess;
 import shop.alien.lawyer.service.LawyerUserViolationService;
 import shop.alien.lawyer.service.LawyerUserService;
+import shop.alien.lawyer.util.ali.AliApi;
 import shop.alien.util.common.EnumUtil;
 import shop.alien.util.common.constant.LawyerStatusEnum;
 
 import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -120,6 +124,8 @@ public class LawyerUserViolationServiceImpl extends ServiceImpl<LawyerUserViolat
 
     private final LawyerConsultationOrderMapper consultationOrderMapper;
 
+    private final AliApi aliApi;
+
 
     /**
      * 用户举报处理
@@ -971,8 +977,10 @@ public class LawyerUserViolationServiceImpl extends ServiceImpl<LawyerUserViolat
      * <p>
      * 处理举报审批,包括:
      * 1. 更新举报处理状态
-     * 2. 向举报人发送审批结果通知
-     * 3. 向被举报人发送通知(如需要)
+     * 2. 更新订单状态为已退款
+     * 3. 调用支付宝退款接口进行退款(如果退款失败,会回滚订单状态更新)
+     * 4. 向举报人发送审批结果通知
+     * 5. 向被举报人发送通知(如需要)
      * </p>
      *
      * @param id               举报记录ID
@@ -982,6 +990,7 @@ public class LawyerUserViolationServiceImpl extends ServiceImpl<LawyerUserViolat
      * @since 2025-01-XX
      */
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void approve(int id, String processingStatus, String reportResult) {
         // 参数校验
         if (id <= 0 || StringUtils.isBlank(processingStatus)) {
@@ -1006,11 +1015,20 @@ public class LawyerUserViolationServiceImpl extends ServiceImpl<LawyerUserViolat
                 LambdaUpdateWrapper<LawyerConsultationOrder> lawyerConsultationOrderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                 lawyerConsultationOrderLambdaUpdateWrapper.eq(LawyerConsultationOrder::getOrderNumber, violation.getOrderNumber());
                 lawyerConsultationOrderLambdaUpdateWrapper.set(LawyerConsultationOrder::getOrderStatus, LawyerStatusEnum.REFUNDED.getStatus());
-                consultationOrderMapper.update(null, lawyerConsultationOrderLambdaUpdateWrapper);
+               int result = consultationOrderMapper.update(null, lawyerConsultationOrderLambdaUpdateWrapper);
+               if (result > 0) {
+                   log.info("订单状态更新成功,订单编号:{}", violation.getOrderNumber());
+
+                   // 订单状态更新成功后进行退款
+                   processRefund(violation.getOrderNumber());
+
+                   // 构建并发送通知消息
+                   sendApprovalNotifications(violation, processingStatus, reportResult);
+               } else {
+                   log.warn("订单状态更新失败,订单编号:{}", violation.getOrderNumber());
+               }
             }
 
-            // 构建并发送通知消息
-            sendApprovalNotifications(violation, processingStatus, reportResult);
 
             log.info("审批举报处理完成,举报ID:{},处理状态:{}", id, processingStatus);
 
@@ -1036,6 +1054,79 @@ public class LawyerUserViolationServiceImpl extends ServiceImpl<LawyerUserViolat
     }
 
     /**
+     * 处理订单退款
+     * <p>
+     * 根据订单号查询订单信息,调用支付宝退款接口进行退款
+     * 如果退款失败,会抛出异常以触发事务回滚
+     * </p>
+     *
+     * @param orderNumber 订单号
+     * @throws RuntimeException 当退款失败时抛出异常,触发事务回滚
+     */
+    private void processRefund(String orderNumber) {
+        if (StringUtils.isEmpty(orderNumber)) {
+            log.error("处理退款失败:订单号为空");
+            throw new RuntimeException("订单号不能为空,无法处理退款");
+        }
+
+        try {
+            // 查询订单信息
+            LambdaQueryWrapper<LawyerConsultationOrder> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper.eq(LawyerConsultationOrder::getOrderNumber, orderNumber)
+                    .eq(LawyerConsultationOrder::getDeleteFlag, 0)
+                    .last("LIMIT 1");
+            LawyerConsultationOrder order = consultationOrderMapper.selectOne(queryWrapper);
+
+            if (order == null) {
+                log.error("处理退款失败:订单不存在,订单号:{}", orderNumber);
+                throw new RuntimeException("订单不存在,无法处理退款,订单号:" + orderNumber);
+            }
+
+            // 检查订单是否有支付宝交易号
+            if (StringUtils.isEmpty(order.getAlipayNo())) {
+                log.error("处理退款失败:订单无支付宝交易号,订单号:{}", orderNumber);
+                throw new RuntimeException("订单无支付宝交易号,无法处理退款,订单号:" + orderNumber);
+            }
+
+            // 检查订单金额
+            if (order.getOrderAmount() == null || order.getOrderAmount() <= 0) {
+                log.error("处理退款失败:订单金额无效,订单号:{},订单金额:{}", orderNumber, order.getOrderAmount());
+                throw new RuntimeException("订单金额无效,无法处理退款,订单号:" + orderNumber);
+            }
+
+            // 将订单金额从分转换为元
+            BigDecimal refundAmount = new BigDecimal(order.getOrderAmount())
+                    .divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
+
+            // 退款原因
+            String refundReason = "举报审核通过,订单退款";
+
+            // 调用支付宝退款接口
+            log.info("开始处理订单退款,订单号:{},支付宝交易号:{},退款金额:{}元", 
+                    orderNumber, order.getAlipayNo(), refundAmount.toString());
+            
+            String refundResult = aliApi.processRefund(order.getAlipayNo(), refundAmount.toString(), refundReason, "");
+            
+            if ("调用成功".equals(refundResult)) {
+                log.info("订单退款成功,订单号:{},退款金额:{}元", orderNumber, refundAmount.toString());
+            } else {
+                log.error("订单退款失败,订单号:{},退款结果:{}", orderNumber, refundResult);
+                // 退款失败时抛出异常,触发事务回滚
+                throw new RuntimeException("订单退款失败,订单号:" + orderNumber + ",退款结果:" + refundResult);
+            }
+
+        } catch (RuntimeException e) {
+            // 重新抛出RuntimeException,触发事务回滚
+            log.error("处理订单退款失败,订单号:{},异常信息:{}", orderNumber, e.getMessage(), e);
+            throw e;
+        } catch (Exception e) {
+            // 其他异常也转换为RuntimeException,触发事务回滚
+            log.error("处理订单退款异常,订单号:{},异常信息:{}", orderNumber, e.getMessage(), e);
+            throw new RuntimeException("处理订单退款时发生异常,订单号:" + orderNumber + ",异常信息:" + e.getMessage(), e);
+        }
+    }
+
+    /**
      * 发送审批通知消息
      *
      * @param violation        举报记录