瀏覽代碼

申请退款加入通知

zhangchen 3 周之前
父節點
當前提交
5547fe3ca6

+ 140 - 1
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerConsultationOrderServiceImpl.java

@@ -1,5 +1,6 @@
 package shop.alien.lawyer.service.impl;
 
+import com.alibaba.fastjson2.JSONObject;
 import com.alibaba.nacos.common.utils.CollectionUtils;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -19,10 +20,14 @@ import shop.alien.entity.store.LawFirm;
 import shop.alien.entity.store.LawyerConsultationOrder;
 import shop.alien.entity.store.LawyerServiceArea;
 import shop.alien.entity.store.LawyerUser;
+import shop.alien.entity.store.LifeNotice;
+import shop.alien.entity.store.LifeUser;
 import shop.alien.entity.store.dto.LawyerConsultationOrderDto;
 import shop.alien.entity.store.dto.PayStatusRequest;
 import shop.alien.entity.store.vo.LawyerConsultationOrderVO;
 import shop.alien.entity.store.vo.OrderRevenueVO;
+import shop.alien.entity.store.vo.WebSocketVo;
+import shop.alien.lawyer.config.WebSocketProcess;
 import shop.alien.lawyer.service.LawyerConsultationOrderService;
 import shop.alien.lawyer.service.LawyerUserService;
 import shop.alien.lawyer.service.OrderExpirationService;
@@ -58,6 +63,14 @@ public class LawyerConsultationOrderServiceImpl extends ServiceImpl<LawyerConsul
     private final OrderExpirationService orderExpirationService;
 //    private final AliApi aliApi;
     private final LawFirmMapper lawFirmMapper;
+    private final LifeNoticeMapper lifeNoticeMapper;
+    private final LifeUserMapper lifeUserMapper;
+    private final WebSocketProcess webSocketProcess;
+
+    /**
+     * 系统发送者ID
+     */
+    private static final String SYSTEM_SENDER_ID = "system";
 
 
     @Override
@@ -1412,10 +1425,14 @@ public class LawyerConsultationOrderServiceImpl extends ServiceImpl<LawyerConsul
         int updateCount = consultationOrderMapper.update(null, updateWrapper);
         boolean success = updateCount > 0;
 
-        // 7. 记录操作结果
+        // 7. 记录操作结果并发送通知
         if (success) {
             log.info("申请退款成功,订单ID={}, 订单编号={}, 订单状态={}, 退款状态=已申请",
                     orderId, order.getOrderNumber(), orderStatus);
+            
+            // 发送退款申请通知给申请人
+            sendRefundApplyNotice(order, clientUserId);
+            
             return R.data(true, "退款申请已提交,请等待律师处理");
         } else {
             log.error("申请退款失败:更新数据库失败,订单ID={}, 订单编号={}", orderId, order.getOrderNumber());
@@ -1516,6 +1533,128 @@ public class LawyerConsultationOrderServiceImpl extends ServiceImpl<LawyerConsul
         return revenueInYuan.toString();
     }
 
+    /**
+     * 发送退款申请通知给申请人
+     *
+     * @param order        订单对象
+     * @param clientUserId 客户端用户ID
+     */
+    private void sendRefundApplyNotice(LawyerConsultationOrder order, Integer clientUserId) {
+        try {
+            LifeNotice lifeNotice = createRefundApplyNotice(order, clientUserId);
+            if (lifeNotice == null) {
+                log.warn("生成退款申请通知失败,订单ID={}, 用户ID={}", order.getId(), clientUserId);
+                return;
+            }
+
+            int noticeResult = lifeNoticeMapper.insert(lifeNotice);
+            if (noticeResult <= 0) {
+                log.warn("保存退款申请通知失败,订单ID={}, 用户ID={}", order.getId(), clientUserId);
+                return;
+            }
+
+            // 发送WebSocket消息
+            WebSocketVo webSocketVo = buildWebSocketVo(lifeNotice);
+            webSocketProcess.sendMessage(lifeNotice.getReceiverId(),
+                    JSONObject.from(webSocketVo).toJSONString());
+
+            log.info("退款申请通知发送成功,接收人ID={}, 订单编号={}", lifeNotice.getReceiverId(), order.getOrderNumber());
+
+        } catch (Exception e) {
+            log.error("发送退款申请通知异常,订单ID={}, 用户ID={}, 异常信息={}",
+                    order.getId(), clientUserId, e.getMessage(), e);
+        }
+    }
+
+    /**
+     * 创建退款申请通知对象
+     *
+     * @param order        订单对象
+     * @param clientUserId 客户端用户ID
+     * @return 通知对象,如果生成失败返回null
+     */
+    private LifeNotice createRefundApplyNotice(LawyerConsultationOrder order, Integer clientUserId) {
+        if (order == null || clientUserId == null) {
+            log.warn("创建退款申请通知失败,订单或用户ID为空");
+            return null;
+        }
+
+        try {
+            LifeNotice lifeNotice = new LifeNotice();
+            lifeNotice.setSenderId(SYSTEM_SENDER_ID);
+            lifeNotice.setBusinessId(order.getId());
+            lifeNotice.setTitle("退款申请通知");
+
+            // 获取申请人接收ID
+            String receiverId = getClientReceiverId(clientUserId);
+            if (org.apache.commons.lang3.StringUtils.isEmpty(receiverId)) {
+                log.warn("获取申请人接收ID失败,用户ID={}", clientUserId);
+                return null;
+            }
+            lifeNotice.setReceiverId(receiverId);
+
+            // 构建通知消息内容
+            String orderNumber = order.getOrderNumber();
+            String message = String.format("您的编号为%s的订单,申请退款的信息已提交给律师,等待律师同意。律师同意或48小时未处理,系统会将订单金额原路返还,请注意查收。", 
+                    orderNumber != null ? orderNumber : "");
+
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("title", "退款申请通知");
+            jsonObject.put("message", message);
+            lifeNotice.setContext(jsonObject.toJSONString());
+            lifeNotice.setNoticeType(1);
+
+            return lifeNotice;
+
+        } catch (Exception e) {
+            log.error("创建退款申请通知异常,订单ID={}, 用户ID={}, 异常信息={}",
+                    order.getId(), clientUserId, e.getMessage(), e);
+            return null;
+        }
+    }
+
+    /**
+     * 获取客户端用户接收ID
+     *
+     * @param clientUserId 客户端用户ID
+     * @return 接收人ID,格式:user_ + 手机号
+     */
+    private String getClientReceiverId(Integer clientUserId) {
+        if (clientUserId == null || clientUserId <= 0) {
+            log.warn("客户端用户ID为空或无效");
+            return null;
+        }
+
+        try {
+            LifeUser lifeUser = lifeUserMapper.selectById(clientUserId);
+            if (lifeUser != null && org.apache.commons.lang3.StringUtils.isNotEmpty(lifeUser.getUserPhone())) {
+                return "user_" + lifeUser.getUserPhone();
+            }
+        } catch (Exception e) {
+            log.error("获取客户端用户手机号异常,用户ID={}, 异常信息={}", clientUserId, e.getMessage(), e);
+        }
+
+        log.warn("获取客户端用户手机号失败,用户ID={}", clientUserId);
+        return null;
+    }
+
+    /**
+     * 构建WebSocket消息对象
+     *
+     * @param lifeNotice 通知对象
+     * @return WebSocketVo对象
+     */
+    private WebSocketVo buildWebSocketVo(LifeNotice lifeNotice) {
+        WebSocketVo webSocketVo = new WebSocketVo();
+        webSocketVo.setSenderId(SYSTEM_SENDER_ID);
+        webSocketVo.setReceiverId(lifeNotice.getReceiverId());
+        webSocketVo.setCategory("notice");
+        webSocketVo.setNoticeType("1");
+        webSocketVo.setIsRead(0);
+        webSocketVo.setText(JSONObject.from(lifeNotice).toJSONString());
+        return webSocketVo;
+    }
+
 
 }