瀏覽代碼

新增用户端申请退款接口

zhangchen 3 周之前
父節點
當前提交
6051dfa8ad

+ 6 - 1
alien-entity/src/main/java/shop/alien/entity/store/LawyerConsultationOrder.java

@@ -149,11 +149,16 @@ public class LawyerConsultationOrder extends Model<LawyerConsultationOrder> {
 
     @ApiModelProperty(value = "申请退款状态,0-未申请,1-已申请,2-律师已拒绝,3-律师已同意")
     @TableField("apply_refund_status")
-    private  String applyRefundStatus;
+    private String applyRefundStatus;
 
     @ApiModelProperty(value = "申请退款时间")
     @TableField(value = "apply_refund_time", fill = FieldFill.UPDATE)
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private Date applyRefundTime;
+
+    @ApiModelProperty(value = "申请退款原因")
+    @TableField("apply_refund_reason")
+    private String applyRefundReason;
+
 }
 

+ 13 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/controller/LawyerConsultationOrderController.java

@@ -364,5 +364,18 @@ public class LawyerConsultationOrderController {
         return consultationOrderService.getOrderIncome(lawyerConsultationOrderVO);
     }
 
+    @ApiOperation("用户申请退款")
+    @ApiOperationSupport(order = 17)
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "clientUserId", value = "客户端用户ID", dataType = "Integer", paramType = "query", required = true),
+            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Integer", paramType = "query", required = true),
+            @ApiImplicitParam(name = "applyRefundReason", value = "申请退款原因", dataType = "String", paramType = "query", required = true)
+    })
+    @PostMapping("/applyRefund")
+    public R<Boolean> applyRefund(@RequestParam Integer clientUserId, @RequestParam Integer orderId, @RequestParam String applyRefundReason) {
+        log.info("LawyerConsultationOrderController.applyRefund?clientUserId={},orderId{}", clientUserId, orderId);
+        return consultationOrderService.applyRefund(clientUserId, orderId, applyRefundReason);
+    }
+
 }
 

+ 11 - 1
alien-lawyer/src/main/java/shop/alien/lawyer/service/LawyerConsultationOrderService.java

@@ -129,9 +129,19 @@ public interface LawyerConsultationOrderService extends IService<LawyerConsultat
    /**
    * 获取律师收入
    *
-   * @param lawyerUserId
+   * @param lawyerConsultationOrderVO
    * @return
    */
    R<Map<String, Object>> getOrderIncome(LawyerConsultationOrderVO lawyerConsultationOrderVO);
+
+
+    /**
+     * 用户端申请退款
+     *
+     * @param clientUserId
+     * @param orderId
+     * @return
+     */
+    R<Boolean> applyRefund(Integer clientUserId, Integer orderId, String applyRefundReason);
 }
 

+ 88 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerConsultationOrderServiceImpl.java

@@ -1336,6 +1336,94 @@ public class LawyerConsultationOrderServiceImpl extends ServiceImpl<LawyerConsul
     }
 
     /**
+     * 用户申请退款
+     * <p>
+     * 申请退款前会进行以下校验和处理:
+     * 1. 参数校验:用户ID和订单ID不能为空
+     * 2. 订单存在性校验:订单必须存在
+     * 3. 订单归属校验:订单必须属于该用户
+     * 4. 订单状态校验:只有进行中(2)和已完成(3)状态的订单可以申请退款
+     * 5. 退款状态校验:已申请退款的订单不允许重复申请
+     * 6. 更新订单的申请退款状态为已申请(1),并更新退款申请时间
+     * </p>
+     *
+     * @param clientUserId 客户端用户ID
+     * @param orderId      订单ID
+     * @return 申请退款结果
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R<Boolean> applyRefund(Integer clientUserId, Integer orderId, String applyRefundReason) {
+        log.info("开始申请退款,用户ID={}, 订单ID={}", clientUserId, orderId);
+
+        // 1. 参数校验
+        if (clientUserId == null || clientUserId <= 0) {
+            log.warn("申请退款失败:用户ID为空或无效,clientUserId={}", clientUserId);
+            return R.fail("用户ID不能为空");
+        }
+
+        if (orderId == null || orderId <= 0) {
+            log.warn("申请退款失败:订单ID为空或无效,orderId={}", orderId);
+            return R.fail("订单ID不能为空");
+        }
+
+        // 2. 查询订单信息
+        LawyerConsultationOrder order = consultationOrderMapper.selectById(orderId);
+        if (order == null) {
+            log.warn("申请退款失败:订单不存在,订单ID={}", orderId);
+            return R.fail("订单不存在");
+        }
+
+        // 3. 订单归属校验:订单必须属于该用户
+        if (!clientUserId.equals(order.getClientUserId())) {
+            log.warn("申请退款失败:订单不属于该用户,订单ID={}, 订单用户ID={}, 请求用户ID={}",
+                    orderId, order.getClientUserId(), clientUserId);
+            return R.fail("订单不属于该用户,无法申请退款");
+        }
+
+        // 4. 订单状态校验:只有进行中(2)和已完成(3)状态的订单可以申请退款
+        Integer orderStatus = order.getOrderStatus();
+        Integer inProgressStatus = LawyerStatusEnum.INPROGRESS.getStatus(); // 2:进行中
+        Integer completeStatus = LawyerStatusEnum.COMPLETE.getStatus(); // 3:已完成
+
+        if (!inProgressStatus.equals(orderStatus) && !completeStatus.equals(orderStatus)) {
+            log.warn("申请退款失败:订单状态不允许申请退款,订单ID={}, 订单编号={}, 订单状态={}",
+                    orderId, order.getOrderNumber(), orderStatus);
+            return R.fail("只有进行中或已完成的订单可以申请退款");
+        }
+
+        // 5. 退款状态校验:已申请退款的订单不允许重复申请
+        String applyRefundStatus = order.getApplyRefundStatus();
+        String appliedStatus = "1"; // 1:已申请
+        String lawyerAgreedStatus = "3"; // 3:律师已同意
+        if (appliedStatus.equals(applyRefundStatus) || lawyerAgreedStatus.equals(applyRefundStatus)) {
+            log.warn("申请退款失败:订单已申请退款,不允许重复申请,订单ID={}, 订单编号={}, 退款状态={}",
+                    orderId, order.getOrderNumber(), applyRefundStatus);
+            return R.fail("订单已申请退款,不允许重复申请");
+        }
+
+        // 6. 更新订单的申请退款状态为已申请(1),并更新退款申请时间
+        LambdaUpdateWrapper<LawyerConsultationOrder> updateWrapper = new LambdaUpdateWrapper<>();
+        updateWrapper.eq(LawyerConsultationOrder::getId, orderId)
+                .set(LawyerConsultationOrder::getApplyRefundStatus, appliedStatus)
+                .set(LawyerConsultationOrder::getApplyRefundTime, new Date())
+                .set(LawyerConsultationOrder::getUpdatedTime, new Date()).set(LawyerConsultationOrder::getApplyRefundReason, applyRefundReason);
+
+        int updateCount = consultationOrderMapper.update(null, updateWrapper);
+        boolean success = updateCount > 0;
+
+        // 7. 记录操作结果
+        if (success) {
+            log.info("申请退款成功,订单ID={}, 订单编号={}, 订单状态={}, 退款状态=已申请",
+                    orderId, order.getOrderNumber(), orderStatus);
+            return R.data(true, "退款申请已提交,请等待律师处理");
+        } else {
+            log.error("申请退款失败:更新数据库失败,订单ID={}, 订单编号={}", orderId, order.getOrderNumber());
+            return R.fail("申请退款失败,请稍后重试");
+        }
+    }
+
+    /**
      * 格式化收益金额(分转元)
      *
      * @param revenueInCents 收益金额(单位:分)