ソースを参照

修改life_message表 查询以及插入部分的逻辑

liudongzhi 9 時間 前
コミット
3ac84dd040

+ 43 - 16
alien-entity/src/main/java/shop/alien/mapper/LifeMessageMapper.java

@@ -32,10 +32,14 @@ public interface LifeMessageMapper extends BaseMapper<LifeMessage> {
 
     @Select("with message_num as ( " +
             "    with message as ( " +
-            "        select id, type, content, created_time, if(sender_id = #{phoneId}, receiver_id, sender_id) phoneId, sender_id, receiver_id, is_read " +
+            "        select id, type, content, created_time, " +
+            "               if(sender_user_type = #{participantUserType} and sender_ref_id = #{participantRefId}, receiver_id, sender_id) phoneId, " +
+            "               sender_id, receiver_id, is_read " +
             "        from life_message " +
-            "        where delete_flag = 0 and (sender_id = #{phoneId} or receiver_id = #{phoneId}) " +
-            "               and (instr(delete_phone_id, #{phoneId}) is null or instr(delete_phone_id, #{phoneId}) = 0)" +
+            "        where delete_flag = 0 " +
+            "          and ((sender_user_type = #{participantUserType} and sender_ref_id = #{participantRefId}) " +
+            "               or (receiver_user_type = #{participantUserType} and receiver_ref_id = #{participantRefId})) " +
+            "          and (instr(delete_phone_id, #{phoneId}) is null or instr(delete_phone_id, #{phoneId}) = 0) " +
             "    ) " +
             "    select *, row_number() over (partition by phoneId order by created_time desc ) num, " +
             "           substring_index(phoneId, '_', 1) flag, substring_index(phoneId, '_', -1) phone " +
@@ -67,37 +71,60 @@ public interface LifeMessageMapper extends BaseMapper<LifeMessage> {
             "left join lawyer_user luser on message.flag = 'lawyer' and message.phone = luser.phone and luser.delete_flag = 0 "+
             "left join store_img img on img.store_id = suser.store_id and img.img_type = '10' and img.delete_flag = 0 " +
             "${ew.customSqlSegment}")
-    List<LifeMessageVo> getLifeMessagePageByPhoneId(@Param("phoneId") String phoneId, @Param(Constants.WRAPPER) QueryWrapper<LifeFansVo> dynamicsWrapper);
+    List<LifeMessageVo> getLifeMessagePageByPhoneId(@Param("phoneId") String phoneId,
+                                                    @Param("participantUserType") Integer participantUserType,
+                                                    @Param("participantRefId") Integer participantRefId,
+                                                    @Param(Constants.WRAPPER) QueryWrapper<LifeFansVo> dynamicsWrapper);
 
     @Update("update life_message set delete_phone_id = (if(delete_phone_id is null, #{receiverId}, concat(delete_phone_id, ',', #{receiverId}))) " +
-            "where ((receiver_id = #{senderId} and sender_id = #{receiverId}) or (sender_id = #{senderId} and receiver_id = #{receiverId})) " +
+            "where ((receiver_user_type = #{receiverUserType} and receiver_ref_id = #{receiverRefId} " +
+            "        and sender_user_type = #{senderUserType} and sender_ref_id = #{senderRefId}) " +
+            "    or (sender_user_type = #{receiverUserType} and sender_ref_id = #{receiverRefId} " +
+            "        and receiver_user_type = #{senderUserType} and receiver_ref_id = #{senderRefId})) " +
             "   and (instr(delete_phone_id, #{receiverId}) is null or instr(delete_phone_id, #{receiverId}) = 0)")
-    int deleteMessageByPhoneId(@Param("senderId") String senderId, @Param("receiverId") String receiverId);
+    int deleteMessageByPhoneId(@Param("senderUserType") Integer senderUserType,
+                               @Param("senderRefId") Integer senderRefId,
+                               @Param("receiverUserType") Integer receiverUserType,
+                               @Param("receiverRefId") Integer receiverRefId,
+                               @Param("senderId") String senderId,
+                               @Param("receiverId") String receiverId);
 
     @Select("select lm.id,lm.updated_time,lm.created_user_id,lm.receiver_name,lm.is_read,lm.type,lm.content,lm.current_time,lm.delete_flag," +
             "       lm.sender_id,lm.sender_name,lm.receiver_id,lm.delete_phone_id,lm.created_time,lm.updated_user_id,lu.user_image " +
             "from life_message lm " +
             "left join life_user lu on SUBSTRING(lm.sender_id, INSTR(lm.sender_id, '_') + 1) = lu.user_phone " +
             "where lm.delete_flag = 0 " +
-            "   and (((lm.receiver_id = #{senderId} and lm.sender_id = #{receiverId}) or (lm.sender_id = #{senderId} and lm.receiver_id = #{receiverId}))) " +
-            "   and (instr(delete_phone_id, #{receiverId}) is null or instr(delete_phone_id, #{receiverId}) = 0) " +
+            "   and (((lm.receiver_user_type = #{receiverUserType} and lm.receiver_ref_id = #{receiverRefId} " +
+            "         and lm.sender_user_type = #{senderUserType} and lm.sender_ref_id = #{senderRefId}) " +
+            "      or (lm.sender_user_type = #{receiverUserType} and lm.sender_ref_id = #{receiverRefId} " +
+            "         and lm.receiver_user_type = #{senderUserType} and lm.receiver_ref_id = #{senderRefId}))) " +
+            "   and (instr(delete_phone_id, #{receiverPhoneId}) is null or instr(delete_phone_id, #{receiverPhoneId}) = 0) " +
             "order by lm.created_time asc ")
-    List<LifeMessageVo> getMessageListByReceiverId(@Param("receiverId") String receiverId, @Param("senderId") String senderId);
+    List<LifeMessageVo> getMessageListByReceiverId(@Param("receiverPhoneId") String receiverPhoneId,
+                                                   @Param("senderUserType") Integer senderUserType,
+                                                   @Param("senderRefId") Integer senderRefId,
+                                                   @Param("receiverUserType") Integer receiverUserType,
+                                                   @Param("receiverRefId") Integer receiverRefId);
 
     @Select("select user.id, user.user_name name, user.user_image image, concat('user_', user.user_phone) phoneId, user.jianjie blurb " +
             "from life_user user " +
             "where user_phone in ( " +
             "    select substring_index(sender_id, '_', -1) phone from life_message " +
-            "    where sender_id in " +
-            "          (select receiver_id from life_message " +
-            "            where sender_id = #{senderId} and receiver_id != #{senderId} and delete_flag = 0 " +
-            "            and left(receiver_id, 5) = 'user_' and receiver_id not in ( " +
+            "    where sender_id in ( " +
+            "          select receiver_id from life_message " +
+            "            where sender_user_type = #{senderUserType} and sender_ref_id = #{senderRefId} " +
+            "            and delete_flag = 0 " +
+            "            and not (receiver_user_type = #{senderUserType} and receiver_ref_id = #{senderRefId}) " +
+            "            and receiver_user_type = 1 " +
+            "            and receiver_id not in ( " +
             "                    select blocked_phone_id from life_blacklist " +
             "                    where blocker_phone_id = #{senderId} and delete_flag = 0 " +
             "                ) " +
             "            group by receiver_id) " +
-            "    and receiver_id = #{senderId} and delete_flag = 0 " +
+            "    and receiver_user_type = #{senderUserType} and receiver_ref_id = #{senderRefId} and delete_flag = 0 " +
             "    group by sender_id) " +
             "and delete_flag = 0 ")
-    List<LifeFansVo> getTalkedUserList(@Param("senderId") String senderId);
-}
+    List<LifeFansVo> getTalkedUserList(@Param("senderId") String senderId,
+                                       @Param("senderUserType") Integer senderUserType,
+                                       @Param("senderRefId") Integer senderRefId);
+}

+ 215 - 0
alien-entity/src/main/java/shop/alien/util/type/LifeMessageUtil.java

@@ -0,0 +1,215 @@
+package shop.alien.util.type;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+import shop.alien.entity.store.LifeMessage;
+
+import java.util.List;
+
+/**
+ * LifeMessage 发送人/接收人 userType、refId 回填与查询工具。
+ */
+@Component
+@RequiredArgsConstructor
+public class LifeMessageUtil {
+
+    private final TypeUtil typeUtil;
+
+    public void fillUserTypeAndRefId(LifeMessage message) {
+        if (message == null) {
+            return;
+        }
+        fillSender(message);
+        fillReceiver(message);
+        fillDeletePhone(message);
+    }
+
+    private void fillSender(LifeMessage message) {
+        String senderId = message.getSenderId();
+        if (!StringUtils.hasText(senderId)) {
+            return;
+        }
+        if (message.getSenderUserType() != null && message.getSenderRefId() != null) {
+            return;
+        }
+        if (typeUtil.containsUnderscore(senderId)) {
+            applyTypeId(message, typeUtil.resolveTypeAndId(senderId), true);
+        }
+    }
+
+    private void fillReceiver(LifeMessage message) {
+        String receiverId = message.getReceiverId();
+        if (!StringUtils.hasText(receiverId)) {
+            return;
+        }
+        if (message.getReceiverUserType() != null && message.getReceiverRefId() != null) {
+            return;
+        }
+        if (typeUtil.containsUnderscore(receiverId)) {
+            applyTypeId(message, typeUtil.resolveTypeAndId(receiverId), false);
+        }
+    }
+
+    private void fillDeletePhone(LifeMessage message) {
+        String deletePhoneId = message.getDeletePhoneId();
+        if (!StringUtils.hasText(deletePhoneId)) {
+            return;
+        }
+        if (message.getDeletePhoneUserType() != null && message.getDeletePhoneRefId() != null) {
+            return;
+        }
+        PhoneTypeIdResult result = typeUtil.resolveTypeAndId(deletePhoneId);
+        if (result != null) {
+            message.setDeletePhoneUserType(result.getType());
+            message.setDeletePhoneRefId(result.getId());
+        }
+    }
+
+    private void applyTypeId(LifeMessage message, PhoneTypeIdResult result, boolean sender) {
+        if (result == null) {
+            return;
+        }
+        if (sender) {
+            message.setSenderUserType(result.getType());
+            message.setSenderRefId(result.getId());
+        } else {
+            message.setReceiverUserType(result.getType());
+            message.setReceiverRefId(result.getId());
+        }
+    }
+
+    public PhoneTypeIdResult resolveIdentity(String phoneId) {
+        if (!StringUtils.hasText(phoneId)) {
+            return null;
+        }
+        return typeUtil.resolveTypeAndId(phoneId);
+    }
+
+    public void applyReceiverQuery(LambdaQueryWrapper<LifeMessage> wrapper, String receiverId) {
+        applyReceiverIdentity(wrapper, receiverId);
+    }
+
+    public void applyReceiverUpdate(LambdaUpdateWrapper<LifeMessage> wrapper, String receiverId) {
+        applyReceiverIdentity(wrapper, receiverId);
+    }
+
+    public void applySenderQuery(LambdaQueryWrapper<LifeMessage> wrapper, String senderId) {
+        applySenderIdentity(wrapper, senderId);
+    }
+
+    public void applySenderReceiverPairQuery(LambdaQueryWrapper<LifeMessage> wrapper, String senderId, String receiverId) {
+        PhoneTypeIdResult sender = resolveIdentity(senderId);
+        PhoneTypeIdResult receiver = resolveIdentity(receiverId);
+        if (sender == null || receiver == null) {
+            wrapper.apply("1 = 0");
+            return;
+        }
+        wrapper.eq(LifeMessage::getSenderUserType, sender.getType())
+                .eq(LifeMessage::getSenderRefId, sender.getId())
+                .eq(LifeMessage::getReceiverUserType, receiver.getType())
+                .eq(LifeMessage::getReceiverRefId, receiver.getId());
+    }
+
+    public void applySenderReceiverPairUpdate(LambdaUpdateWrapper<LifeMessage> wrapper, String senderId, String receiverId) {
+        PhoneTypeIdResult sender = resolveIdentity(senderId);
+        PhoneTypeIdResult receiver = resolveIdentity(receiverId);
+        if (sender == null || receiver == null) {
+            wrapper.apply("1 = 0");
+            return;
+        }
+        wrapper.eq(LifeMessage::getSenderUserType, sender.getType())
+                .eq(LifeMessage::getSenderRefId, sender.getId())
+                .eq(LifeMessage::getReceiverUserType, receiver.getType())
+                .eq(LifeMessage::getReceiverRefId, receiver.getId());
+    }
+
+    public void applyConversationQuery(LambdaQueryWrapper<LifeMessage> wrapper, String phoneId1, String phoneId2) {
+        PhoneTypeIdResult first = resolveIdentity(phoneId1);
+        PhoneTypeIdResult second = resolveIdentity(phoneId2);
+        if (first == null || second == null) {
+            wrapper.apply("1 = 0");
+            return;
+        }
+        wrapper.and(w -> w.nested(n -> n.eq(LifeMessage::getSenderUserType, first.getType())
+                        .eq(LifeMessage::getSenderRefId, first.getId())
+                        .eq(LifeMessage::getReceiverUserType, second.getType())
+                        .eq(LifeMessage::getReceiverRefId, second.getId()))
+                .or()
+                .nested(n -> n.eq(LifeMessage::getSenderUserType, second.getType())
+                        .eq(LifeMessage::getSenderRefId, second.getId())
+                        .eq(LifeMessage::getReceiverUserType, first.getType())
+                        .eq(LifeMessage::getReceiverRefId, first.getId())));
+    }
+
+    public void applySenderInQuery(LambdaQueryWrapper<LifeMessage> wrapper, List<String> senderPhoneIds) {
+        if (senderPhoneIds == null || senderPhoneIds.isEmpty()) {
+            wrapper.apply("1 = 0");
+            return;
+        }
+        wrapper.and(w -> {
+            boolean added = false;
+            for (String senderPhoneId : senderPhoneIds) {
+                PhoneTypeIdResult sender = resolveIdentity(senderPhoneId);
+                if (sender == null) {
+                    continue;
+                }
+                if (added) {
+                    w.or();
+                }
+                w.nested(n -> n.eq(LifeMessage::getSenderUserType, sender.getType())
+                        .eq(LifeMessage::getSenderRefId, sender.getId()));
+                added = true;
+            }
+            if (!added) {
+                w.apply("1 = 0");
+            }
+        });
+    }
+
+    /**
+     * 子查询:当前用户作为发送方时,聊过的对方 receiver_id 列表。
+     */
+    public String sqlDistinctReceiverIdsBySender(String senderId) {
+        PhoneTypeIdResult sender = resolveIdentity(senderId);
+        if (sender == null) {
+            return "select null where 1 = 0";
+        }
+        return "select receiver_id from life_message where delete_flag = 0 "
+                + "and sender_user_type = " + sender.getType()
+                + " and sender_ref_id = " + sender.getId()
+                + " group by receiver_id";
+    }
+
+    private void applyReceiverIdentity(LambdaQueryWrapper<LifeMessage> wrapper, String receiverId) {
+        PhoneTypeIdResult resolved = resolveIdentity(receiverId);
+        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
+            wrapper.eq(LifeMessage::getReceiverUserType, resolved.getType())
+                    .eq(LifeMessage::getReceiverRefId, resolved.getId());
+        } else {
+            wrapper.apply("1 = 0");
+        }
+    }
+
+    private void applySenderIdentity(LambdaQueryWrapper<LifeMessage> wrapper, String senderId) {
+        PhoneTypeIdResult resolved = resolveIdentity(senderId);
+        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
+            wrapper.eq(LifeMessage::getSenderUserType, resolved.getType())
+                    .eq(LifeMessage::getSenderRefId, resolved.getId());
+        } else {
+            wrapper.apply("1 = 0");
+        }
+    }
+
+    private void applyReceiverIdentity(LambdaUpdateWrapper<LifeMessage> wrapper, String receiverId) {
+        PhoneTypeIdResult resolved = resolveIdentity(receiverId);
+        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
+            wrapper.eq(LifeMessage::getReceiverUserType, resolved.getType())
+                    .eq(LifeMessage::getReceiverRefId, resolved.getId());
+        } else {
+            wrapper.apply("1 = 0");
+        }
+    }
+}

+ 5 - 0
alien-job/src/main/java/shop/alien/job/second/SecondGoodsTradeXxlJob.java

@@ -20,6 +20,7 @@ import shop.alien.entity.store.vo.WebSocketVo;
 import shop.alien.job.feign.AlienStoreFeign;
 import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeNoticeMapper;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.type.LifeNoticeUtil;
 import shop.alien.mapper.LifeUserMapper;
 import shop.alien.mapper.second.SecondGoodsMapper;
@@ -44,6 +45,8 @@ public class SecondGoodsTradeXxlJob {
 
     private final LifeNoticeUtil lifeNoticeUtil;
 
+    private final LifeMessageUtil lifeMessageUtil;
+
     private final LifeUserMapper lifeUserMapper;
 
     private final LifeMessageMapper lifeMessageMapper;
@@ -94,6 +97,7 @@ public class SecondGoodsTradeXxlJob {
                 lifeMessage.setReceiverId(buyerPhoneId);
                 lifeMessage.setContent(message.toJSONString());
                 lifeMessage.setType("5");
+                lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                 lifeMessageMapper.insert(lifeMessage);
 
                 // 给买家推送消息
@@ -142,6 +146,7 @@ public class SecondGoodsTradeXxlJob {
                 lifeMessage.setReceiverId(sellerPhoneId);
                 lifeMessage.setContent(message.toJSONString());
                 lifeMessage.setType("5");
+                lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                 lifeMessageMapper.insert(lifeMessage);
 
                 // 给卖家推送消息

+ 4 - 0
alien-job/src/main/java/shop/alien/job/store/LifeReverseGroupBuyingJob.java

@@ -13,6 +13,7 @@ import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeReverseGroupBuyingMapper;
 import shop.alien.mapper.LifeUserMapper;
 import shop.alien.mapper.LifeUserOrderMapper;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.common.AlipayTradeRefund;
 import shop.alien.util.common.DateUtils;
 
@@ -41,6 +42,8 @@ public class LifeReverseGroupBuyingJob {
 
     private final LifeMessageMapper lifeMessageMapper;
 
+    private final LifeMessageUtil lifeMessageUtil;
+
     private final LifeUserMapper lifeUserMapper;
 
     /**
@@ -69,6 +72,7 @@ public class LifeReverseGroupBuyingJob {
                         lifeMessage.setReceiverId("user_" + lifeUser.getUserPhone());
                         lifeMessage.setContent("你有一笔反向团购退款失败, 请联系管理员");
                         lifeMessage.setType("2");
+                        lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                         lifeMessageMapper.insert(lifeMessage);
                     }
 

+ 5 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/config/WebSocketProcess.java

@@ -14,6 +14,7 @@ import shop.alien.entity.store.LifeMessage;
 import shop.alien.entity.store.vo.WebSocketVo;
 import shop.alien.mapper.LifeBlacklistMapper;
 import shop.alien.mapper.LifeMessageMapper;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.common.safe.ImageReviewServiceEnum;
 import shop.alien.util.common.safe.TextModerationResultVO;
 import shop.alien.util.common.safe.TextModerationUtil;
@@ -43,6 +44,7 @@ public class WebSocketProcess implements ApplicationContextAware {
     private static ApplicationContext applicationContext;
     private static LifeBlacklistMapper lifeBlacklistMapper;
     private static LifeMessageMapper lifeMessageMapper;
+    private static LifeMessageUtil lifeMessageUtil;
     private static BaseRedisService baseRedisService;
     private static TextModerationUtil textModerationUtil;
     /*
@@ -55,6 +57,7 @@ public class WebSocketProcess implements ApplicationContextAware {
         WebSocketProcess.applicationContext = context;
         WebSocketProcess.lifeBlacklistMapper = context.getBean(LifeBlacklistMapper.class);
         WebSocketProcess.lifeMessageMapper = context.getBean(LifeMessageMapper.class);
+        WebSocketProcess.lifeMessageUtil = context.getBean(LifeMessageUtil.class);
         WebSocketProcess.baseRedisService = context.getBean(BaseRedisService.class);
         WebSocketProcess.textModerationUtil = context.getBean(TextModerationUtil.class);
     }
@@ -100,11 +103,13 @@ public class WebSocketProcess implements ApplicationContextAware {
             lifeMessage.setContent(webSocketVo.getText());
             lifeMessage.setType(webSocketVo.getType());
             lifeMessage.setBusinessId(webSocketVo.getBusinessId());
+            lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
             // 查询自己是否在对方的黑名单中
             if (baseRedisService.hasKey("blackList_" + webSocketVo.getSenderId())) {
                 List<String> blackList = baseRedisService.getList("blackList_" + webSocketVo.getSenderId());
                 if (blackList.contains(webSocketVo.getReceiverId())) {
                     lifeMessage.setDeletePhoneId(webSocketVo.getReceiverId());
+                    lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                     lifeMessageMapper.insert(lifeMessage);
                     // 发送消息
                     webSocketVo.setMessageId(lifeMessage.getId());

+ 6 - 5
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerClientConsultationOrderServiceImpl.java

@@ -28,6 +28,7 @@ import shop.alien.lawyer.service.OrderExpirationService;
 import shop.alien.mapper.*;
 import shop.alien.util.common.constant.LawyerStatusEnum;
 import shop.alien.util.common.constant.OrderActionType;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.type.LifeNoticeUtil;
 
 import java.text.SimpleDateFormat;
@@ -60,6 +61,7 @@ public class LawyerClientConsultationOrderServiceImpl extends ServiceImpl<Lawyer
     private final LifeUserMapper lifeUserMapper;
     private final WebSocketProcess webSocketProcess;
     private final LifeMessageMapper lifeMessageMapper;
+    private final LifeMessageUtil lifeMessageUtil;
     private final LawFirmMapper lawFirmMapper;
 
     /**
@@ -430,10 +432,10 @@ public class LawyerClientConsultationOrderServiceImpl extends ServiceImpl<Lawyer
         String phone = lawyerUser.getPhone();
 
         LambdaQueryWrapper<LifeMessage> lifeMessageLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        lifeMessageLambdaQueryWrapper.in(LifeMessage::getSenderId, lawyerIdList);
+        lifeMessageUtil.applySenderInQuery(lifeMessageLambdaQueryWrapper, lawyerIdList);
         lifeMessageLambdaQueryWrapper.eq(LifeMessage::getDeleteFlag, 0);
         lifeMessageLambdaQueryWrapper.eq(LifeMessage::getIsRead, 0);
-        lifeMessageLambdaQueryWrapper.eq(LifeMessage::getReceiverId, "lawyer_" + phone);
+        lifeMessageUtil.applyReceiverQuery(lifeMessageLambdaQueryWrapper, "lawyer_" + phone);
         List<LifeMessage> lifeMessageList = lifeMessageMapper.selectList(lifeMessageLambdaQueryWrapper);
 
 
@@ -1884,9 +1886,8 @@ public class LawyerClientConsultationOrderServiceImpl extends ServiceImpl<Lawyer
 
             // 构建查询条件
             LambdaQueryWrapper<LifeMessage> queryWrapper = new LambdaQueryWrapper<>();
-            queryWrapper.eq(LifeMessage::getSenderId, senderId)
-                    .eq(LifeMessage::getReceiverId, receiverId)
-                    .gt(LifeMessage::getCreatedTime, acceptOrdersTime)
+            lifeMessageUtil.applySenderReceiverPairQuery(queryWrapper, senderId, receiverId);
+            queryWrapper.gt(LifeMessage::getCreatedTime, acceptOrdersTime)
                     .eq(LifeMessage::getDeleteFlag, DELETE_FLAG_NOT_DELETED)
                     .last("LIMIT 1");
 

+ 4 - 2
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerConsultationOrderServiceImpl.java

@@ -31,6 +31,7 @@ import shop.alien.lawyer.service.OrderExpirationService;
 import shop.alien.lawyer.util.DateUtils;
 import shop.alien.mapper.*;
 import shop.alien.util.common.constant.LawyerStatusEnum;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.type.LifeNoticeUtil;
 
 import java.math.BigDecimal;
@@ -66,6 +67,7 @@ public class LawyerConsultationOrderServiceImpl extends ServiceImpl<LawyerConsul
     private final WebSocketProcess webSocketProcess;
 
     private final LifeMessageMapper lifeMessageMapper;
+    private final LifeMessageUtil lifeMessageUtil;
     private final StoreCommentMapper storeCommentMapper;
     private final LawyerUserViolationMapper lawyerUserViolationMapper;
     private final CommentAppealMapper commentAppealMapper;
@@ -1002,10 +1004,10 @@ public class LawyerConsultationOrderServiceImpl extends ServiceImpl<LawyerConsul
         String phone = lifeUser.getUserPhone();
 
         LambdaQueryWrapper<LifeMessage> lifeMessageLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        lifeMessageLambdaQueryWrapper.in(LifeMessage::getSenderId, lawyerIdList);
+        lifeMessageUtil.applySenderInQuery(lifeMessageLambdaQueryWrapper, lawyerIdList);
         lifeMessageLambdaQueryWrapper.eq(LifeMessage::getDeleteFlag, 0);
         lifeMessageLambdaQueryWrapper.eq(LifeMessage::getIsRead, 0);
-        lifeMessageLambdaQueryWrapper.eq(LifeMessage::getReceiverId, "user_" + phone);
+        lifeMessageUtil.applyReceiverQuery(lifeMessageLambdaQueryWrapper, "user_" + phone);
         List<LifeMessage> lifeMessageList = lifeMessageMapper.selectList(lifeMessageLambdaQueryWrapper);
 
         // 按照senderId进行分组,返回senderId和数量的map

+ 8 - 1
alien-second/src/main/java/shop/alien/second/service/impl/SecondTradeRecordServiceImpl.java

@@ -30,6 +30,7 @@ import shop.alien.entity.second.vo.SellerEvaluationVo;
 import shop.alien.entity.store.vo.WebSocketVo;
 import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeNoticeMapper;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.type.LifeNoticeUtil;
 import shop.alien.mapper.LifeUserMapper;
 import shop.alien.mapper.StoreDictionaryMapper;
@@ -66,6 +67,7 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
     private final LifeMessageMapper lifeMessageMapper;
     private final LifeNoticeMapper lifeNoticeMapper;
 
+    private final LifeMessageUtil lifeMessageUtil;
     private final LifeNoticeUtil lifeNoticeUtil;
     private final LifeUserMapper lifeUserMapper;
     private final AlienStoreFeign alienStoreFeign;
@@ -201,6 +203,7 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
         lifeMessage.setReceiverId(receiverId);
         lifeMessage.setType(messageType);
         lifeMessage.setContent(message.toJSONString());
+        lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
         lifeMessageMapper.insert(lifeMessage);
         message.put("createdTime", lifeMessage.getCreatedTime());
 
@@ -494,6 +497,7 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
             message.setReceiverId(receiverId);
             message.setContent(messageContent.toJSONString());
             message.setType("6");
+            lifeMessageUtil.fillUserTypeAndRefId(message);
             lifeMessageMapper.insert(message);
 
             // 推送已签到消息
@@ -818,6 +822,7 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
             lifeMessage.setReceiverId(buyerPhoneId);
             lifeMessage.setContent(message.toJSONString());
             lifeMessage.setType("5");
+            lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
             lifeMessageMapper.insert(lifeMessage);
 
             // 给买家推送消息
@@ -866,6 +871,7 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
             lifeMessage.setReceiverId(sellerPhoneId);
             lifeMessage.setContent(message.toJSONString());
             lifeMessage.setType("5");
+            lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
             lifeMessageMapper.insert(lifeMessage);
 
             // 给卖家推送消息
@@ -1135,7 +1141,8 @@ public class SecondTradeRecordServiceImpl extends ServiceImpl<SecondTradeRecordM
                     LambdaQueryWrapper<LifeMessage> wrapper = new LambdaQueryWrapper<>();
                     wrapper.eq(LifeMessage::getType, 10);
                     wrapper.isNull(LifeMessage::getContent);
-                    wrapper.apply("((receiver_id = 'user_" + otherUser.getUserPhone() + "' and sender_id = 'user_" + user.getUserPhone() + "') or (receiver_id = 'user_" + user.getUserPhone() + "' and sender_id = 'user_" + otherUser.getUserPhone() + "'))");
+                    lifeMessageUtil.applyConversationQuery(wrapper,
+                            "user_" + user.getUserPhone(), "user_" + otherUser.getUserPhone());
                     List<Integer> messageIds = lifeMessageMapper.selectList(wrapper).stream().map(LifeMessage::getId).collect(Collectors.toList());
 
                     if (CollectionUtils.isNotEmpty(messageIds)) {

+ 5 - 0
alien-second/src/main/java/shop/alien/second/task/Task.java

@@ -19,6 +19,7 @@ import shop.alien.entity.store.LifeUser;
 import shop.alien.entity.store.vo.WebSocketVo;
 import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeNoticeMapper;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.type.LifeNoticeUtil;
 import shop.alien.mapper.LifeUserMapper;
 import shop.alien.mapper.second.SecondGoodsMapper;
@@ -43,6 +44,8 @@ public class Task {
 
     private final LifeNoticeUtil lifeNoticeUtil;
 
+    private final LifeMessageUtil lifeMessageUtil;
+
     private final LifeUserMapper lifeUserMapper;
 
     private final LifeMessageMapper lifeMessageMapper;
@@ -100,6 +103,7 @@ public class Task {
                 lifeMessage.setReceiverId(buyerPhoneId);
                 lifeMessage.setContent(message.toJSONString());
                 lifeMessage.setType("5");
+                lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                 lifeMessageMapper.insert(lifeMessage);
 
                 // 给买家推送消息
@@ -148,6 +152,7 @@ public class Task {
                 lifeMessage.setReceiverId(sellerPhoneId);
                 lifeMessage.setContent(message.toJSONString());
                 lifeMessage.setType("5");
+                lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                 lifeMessageMapper.insert(lifeMessage);
 
                 // 给卖家推送消息

+ 7 - 15
alien-second/src/main/java/shop/alien/second/util/AiUserViolationUtils.java

@@ -1,6 +1,7 @@
 package shop.alien.second.util;
 
 import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -20,9 +21,11 @@ import shop.alien.entity.store.LifeMessage;
 import shop.alien.entity.store.LifeUserViolation;
 import shop.alien.entity.store.vo.LifeUserVo;
 import shop.alien.mapper.LifeMessageMapper;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.mapper.LifeUserMapper;
 import shop.alien.mapper.LifeUserViolationMapper;
 import shop.alien.mapper.second.SecondGoodsMapper;
+import shop.alien.util.type.LifeMessageUtil;
 
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -38,6 +41,7 @@ public class AiUserViolationUtils {
     private final RestTemplate restTemplate;
     private final LifeUserMapper lifeUserMapper;
     private final LifeMessageMapper lifeMessageMapper;
+    private final LifeMessageUtil lifeMessageUtil;
     private final SecondGoodsMapper secondGoodsMapper;
 
     @Value("${third-party-login.base-url}")
@@ -391,22 +395,10 @@ public class AiUserViolationUtils {
      */
     public List<LifeMessage> getRecentChatMessages(String userReporterUserById, String userReportedUserById) {
         // 构建查询条件
-        QueryWrapper<LifeMessage> queryWrapper = new QueryWrapper<>();
-
-        // 查询双方的聊天记录
-        queryWrapper.and(wrapper -> wrapper
-                        .eq("sender_id", userReporterUserById)
-                        .eq("receiver_id", userReportedUserById))
-                .or(wrapper -> wrapper
-                        .eq("sender_id", userReportedUserById)
-                        .eq("receiver_id", userReporterUserById));
-
-        // 按时间倒序排列
-        queryWrapper.orderByDesc("created_time");
-
-        // 限制50条记录
+        LambdaQueryWrapper<LifeMessage> queryWrapper = new LambdaQueryWrapper<>();
+        lifeMessageUtil.applyConversationQuery(queryWrapper, userReporterUserById, userReportedUserById);
+        queryWrapper.orderByDesc(LifeMessage::getCreatedTime);
         queryWrapper.last("LIMIT 50");
-
         return lifeMessageMapper.selectList(queryWrapper);
     }
 

+ 5 - 13
alien-store/src/main/java/shop/alien/store/config/WebSocketProcess.java

@@ -26,8 +26,7 @@ import shop.alien.util.common.safe.TextModerationResultVO;
 import shop.alien.util.common.safe.TextModerationUtil;
 import shop.alien.util.common.safe.TextReviewServiceEnum;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import shop.alien.util.type.PhoneTypeIdResult;
-import shop.alien.util.type.TypeUtil;
+import shop.alien.util.type.LifeMessageUtil;
 
 import javax.websocket.*;
 import javax.websocket.server.PathParam;
@@ -54,6 +53,7 @@ public class WebSocketProcess implements ApplicationContextAware {
     private static ApplicationContext applicationContext;
     private static LifeBlacklistMapper lifeBlacklistMapper;
     private static LifeMessageMapper lifeMessageMapper;
+    private static LifeMessageUtil lifeMessageUtil;
     private static BaseRedisService baseRedisService;
     private static TextModerationUtil textModerationUtil;
     private static StoreUserMapper storeUserMapper;
@@ -70,13 +70,12 @@ public class WebSocketProcess implements ApplicationContextAware {
      */
     private static final ConcurrentHashMap<String, Map<String, String>> connectionInfoMap = new ConcurrentHashMap<>(12);
 
-    private  TypeUtil typeUtil;
-
     @Override
     public void setApplicationContext(ApplicationContext context) {
         WebSocketProcess.applicationContext = context;
         WebSocketProcess.lifeBlacklistMapper = context.getBean(LifeBlacklistMapper.class);
         WebSocketProcess.lifeMessageMapper = context.getBean(LifeMessageMapper.class);
+        WebSocketProcess.lifeMessageUtil = context.getBean(LifeMessageUtil.class);
         WebSocketProcess.baseRedisService = context.getBean(BaseRedisService.class);
         WebSocketProcess.textModerationUtil = context.getBean(TextModerationUtil.class);
         WebSocketProcess.storeUserMapper = context.getBean(StoreUserMapper.class);
@@ -137,25 +136,18 @@ public class WebSocketProcess implements ApplicationContextAware {
 
             // 保存消息记录
             LifeMessage lifeMessage = new LifeMessage();
-            //新增传入发送人的分类和id
-            PhoneTypeIdResult phoneTypeIdResult =typeUtil.resolveTypeAndId(webSocketVo.getSenderId());
-            lifeMessage.setSenderRefId(phoneTypeIdResult.getId());
-            lifeMessage.setSenderUserType(phoneTypeIdResult.getType());
-            //新增传入接收人的分类和id
-            PhoneTypeIdResult phoneTypeIdResultNew =typeUtil.resolveTypeAndId(webSocketVo.getReceiverId());
-            lifeMessage.setReceiverRefId(phoneTypeIdResultNew.getId());
-            lifeMessage.setReceiverUserType(phoneTypeIdResultNew.getType());
-
             lifeMessage.setSenderId(webSocketVo.getSenderId());
             lifeMessage.setReceiverId(webSocketVo.getReceiverId());
             lifeMessage.setContent(webSocketVo.getText());
             lifeMessage.setType(webSocketVo.getType());
             lifeMessage.setBusinessId(webSocketVo.getBusinessId());
+            lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
             // 查询自己是否在对方的黑名单中
             if (baseRedisService.hasKey("blackList_" + webSocketVo.getSenderId())) {
                 List<String> blackList = baseRedisService.getList("blackList_" + webSocketVo.getSenderId());
                 if (blackList.contains(webSocketVo.getReceiverId())) {
                     lifeMessage.setDeletePhoneId(webSocketVo.getReceiverId());
+                    lifeMessageUtil.fillUserTypeAndRefId(lifeMessage);
                     lifeMessageMapper.insert(lifeMessage);
                     // 发送消息
                     webSocketVo.setMessageId(lifeMessage.getId());

+ 59 - 62
alien-store/src/main/java/shop/alien/store/service/impl/LifeMessageServiceImpl.java

@@ -20,9 +20,9 @@ import shop.alien.store.service.LifeMessageService;
 import shop.alien.store.service.LifeUserService;
 import shop.alien.util.common.JwtUtil;
 import shop.alien.util.common.constant.LawyerStatusEnum;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.type.LifeNoticeUtil;
 import shop.alien.util.type.PhoneTypeIdResult;
-import shop.alien.util.type.TypeUtil;
 
 import java.util.ArrayList;
 import java.util.Comparator;
@@ -50,7 +50,7 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
     private final LawyerUserMapper lawyerUserMapper;
     private final LawyerConsultationOrderMapper lawyerConsultationOrderMapper;
 
-    private final TypeUtil typeUtil;
+    private final LifeMessageUtil lifeMessageUtil;
 
     private final LifeNoticeUtil lifeNoticeUtil;
 
@@ -79,23 +79,17 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
                 wrapper.apply("(user.user_name like '%" + search + "%' or suser.name like '%" + search + "%' or sinfo.store_name like '%" + search + "%')");
                 // 聊过
             } else if (1 == friendType) {
-                wrapper.apply("message.phoneId in ( " +
-                        "    select receiver_id " +
-                        "    from life_message " +
-                        "    where sender_id = '" + receiverId + "'  and delete_flag = 0 " +
-                        "    group by receiver_id " +
-                        ")");
+                wrapper.apply("message.phoneId in ( " + lifeMessageUtil.sqlDistinctReceiverIdsBySender(receiverId) + ")");
                 // 没聊过
             } else if (2 == friendType) {
-                wrapper.apply("message.phoneId not in ( " +
-                        "    select receiver_id " +
-                        "    from life_message " +
-                        "    where sender_id = '" + receiverId + "'  and delete_flag = 0 " +
-                        "    group by receiver_id " +
-                        ")");
+                wrapper.apply("message.phoneId not in ( " + lifeMessageUtil.sqlDistinctReceiverIdsBySender(receiverId) + ")");
             }
             wrapper.orderByDesc("message.created_time");
-            List<LifeMessageVo> lifeMessagePageList = lifeMessageMapper.getLifeMessagePageByPhoneId(receiverId, wrapper);
+            PhoneTypeIdResult participant = lifeMessageUtil.resolveIdentity(receiverId);
+            Integer participantUserType = participant != null ? participant.getType() : -1;
+            Integer participantRefId = participant != null ? participant.getId() : -1;
+            List<LifeMessageVo> lifeMessagePageList = lifeMessageMapper.getLifeMessagePageByPhoneId(
+                    receiverId, participantUserType, participantRefId, wrapper);
             List<LifeMessageVo> lifeMessagePageResultList = new ArrayList<>();
             if (!CollectionUtils.isEmpty(lifeMessagePageList)) {
                 boolean currentIsUser = receiverId != null && receiverId.startsWith("user_");
@@ -129,7 +123,7 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
 
                 // 查询所有未读消息
                 LambdaQueryWrapper<LifeMessage> messageWrapper = new LambdaQueryWrapper<>();
-                messageWrapper.eq(LifeMessage::getReceiverId, receiverId);
+                lifeMessageUtil.applyReceiverQuery(messageWrapper, receiverId);
                 messageWrapper.eq(LifeMessage::getIsRead, 0);
                 messageWrapper.eq(LifeMessage::getDeleteFlag, 0);
                 messageWrapper.apply("(instr(delete_phone_id, '" + receiverId + "') is null or instr(delete_phone_id, '" + receiverId + "') = 0)");
@@ -264,15 +258,14 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
                     "     where blocker_type = '" + blockerType + "' and blocker_id = '" + blockerId + "' and blocked_type = '1' and delete_flag = 0 " +
                     ") or suser.id is null)");
             // 没聊过
-            wrapper.apply("message.phoneId not in ( " +
-                    "    select receiver_id " +
-                    "    from life_message " +
-                    "    where sender_id = '" + receiverId + "' and delete_flag = 0 " +
-                    "    group by receiver_id " +
-                    ")");
+            wrapper.apply("message.phoneId not in ( " + lifeMessageUtil.sqlDistinctReceiverIdsBySender(receiverId) + ")");
             wrapper.orderByDesc("message.created_time");
 
-            List<LifeMessageVo> lifeMessagePageList = lifeMessageMapper.getLifeMessagePageByPhoneId(receiverId, wrapper);
+            PhoneTypeIdResult participant = lifeMessageUtil.resolveIdentity(receiverId);
+            Integer participantUserType = participant != null ? participant.getType() : -1;
+            Integer participantRefId = participant != null ? participant.getId() : -1;
+            List<LifeMessageVo> lifeMessagePageList = lifeMessageMapper.getLifeMessagePageByPhoneId(
+                    receiverId, participantUserType, participantRefId, wrapper);
             LifeMessageVo vo = new LifeMessageVo();
             if (!CollectionUtils.isEmpty(lifeMessagePageList)) {
                 vo.setUserName(lifeMessagePageList.get(0).getUserName());
@@ -315,10 +308,13 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
             // 当前用户的所有未关注人消息
             wrapper = new QueryWrapper<>();
             wrapper.eq("num", 1);
-            wrapper.apply("(sender_id not in (" + friendsIdsStr + ") and receiver_id not in (" + friendsIdsStr + "))");
+            wrapper.apply("message.phoneId not in (" + friendsIdsStr + ")");
             wrapper.orderByDesc("message.created_time");
-//        IPage<LifeMessageVo> ipage = new Page<>(1, Integer.MAX_VALUE);
-            List<LifeMessageVo> lifeMessageVoList = lifeMessageMapper.getLifeMessagePageByPhoneId(receiverId, wrapper);
+            PhoneTypeIdResult participant = lifeMessageUtil.resolveIdentity(receiverId);
+            Integer participantUserType = participant != null ? participant.getType() : -1;
+            Integer participantRefId = participant != null ? participant.getId() : -1;
+            List<LifeMessageVo> lifeMessageVoList = lifeMessageMapper.getLifeMessagePageByPhoneId(
+                    receiverId, participantUserType, participantRefId, wrapper);
             LifeMessageVo messageVos = null;
             if(CollectionUtils.isEmpty(lifeMessageVoList)){
                 return messageVos;
@@ -391,7 +387,18 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
                 lawyerUser1 = lawyerUserMapper.selectOne(new QueryWrapper<LawyerUser>().eq("phone", senderId.substring(7)));
             }
 
-            List<LifeMessageVo> lifeMessageVos = lifeMessageMapper.getMessageListByReceiverId(receiverId, senderId);
+            PhoneTypeIdResult receiverIdentity = lifeMessageUtil.resolveIdentity(receiverId);
+            PhoneTypeIdResult senderIdentity = lifeMessageUtil.resolveIdentity(senderId);
+            if (receiverIdentity == null || senderIdentity == null) {
+                JSONObject empty = new JSONObject();
+                empty.put("messageList", new ArrayList<>());
+                empty.put("isStranger", true);
+                return empty;
+            }
+            List<LifeMessageVo> lifeMessageVos = lifeMessageMapper.getMessageListByReceiverId(
+                    receiverId,
+                    senderIdentity.getType(), senderIdentity.getId(),
+                    receiverIdentity.getType(), receiverIdentity.getId());
             for (LifeMessageVo lifeMessageVo : lifeMessageVos) {
                 if (lifeMessageVo.getReceiverId().equals(receiverId)) {
                     if (null != lifeUser1) {
@@ -429,10 +436,9 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
             }
 
             // 查询两个人是否是陌生人(只有一条消息)
-            QueryWrapper<LifeMessage> wrapper = new QueryWrapper<>();
-            // 没聊过
-            wrapper.apply("((sender_id = '" + senderId + "' and receiver_id = '" + receiverId + "') or (sender_id = '" + receiverId + "' and receiver_id = '" + senderId + "'))");
-            wrapper.orderByDesc("created_time");
+            LambdaQueryWrapper<LifeMessage> wrapper = new LambdaQueryWrapper<>();
+            lifeMessageUtil.applyConversationQuery(wrapper, senderId, receiverId);
+            wrapper.orderByDesc(LifeMessage::getCreatedTime);
             int count = lifeMessageMapper.selectCount(wrapper);
 
             JSONObject jsonObject = new JSONObject();
@@ -460,16 +466,11 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
 //            return lifeMessageMapper.selectCount(wrapper) <= 1;
 
             // 查询我发送过的消息
-            QueryWrapper<LifeMessage> wrapper = new QueryWrapper<>();
-            //本人发送的数量
-            wrapper.apply("(sender_id = '" + senderId + "' and receiver_id = '" + receiverId + "')");
-//            wrapper.eq("sender_id", senderId);
-//            wrapper.eq("receiver_id", receiverId);
-
+            LambdaQueryWrapper<LifeMessage> wrapper = new LambdaQueryWrapper<>();
+            lifeMessageUtil.applySenderReceiverPairQuery(wrapper, senderId, receiverId);
             int senderCount = lifeMessageMapper.selectCount(wrapper);
-            //查询对方发送过的消息
-            QueryWrapper<LifeMessage> wrapper1 = new QueryWrapper<>();
-            wrapper1.apply("(sender_id = '" + receiverId + "' and receiver_id = '" + senderId + "')");
+            LambdaQueryWrapper<LifeMessage> wrapper1 = new LambdaQueryWrapper<>();
+            lifeMessageUtil.applySenderReceiverPairQuery(wrapper1, receiverId, senderId);
             int receiverCount = lifeMessageMapper.selectCount(wrapper1);
             if (senderCount == 0){
                 //本条可以发
@@ -502,8 +503,7 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
     public int read(String receiverId, String senderId) throws Exception {
         try {
             LambdaUpdateWrapper<LifeMessage> wrapper = new LambdaUpdateWrapper<>();
-            wrapper.eq(LifeMessage::getReceiverId, receiverId);
-            wrapper.eq(LifeMessage::getSenderId, senderId);
+            lifeMessageUtil.applySenderReceiverPairUpdate(wrapper, senderId, receiverId);
             wrapper.eq(LifeMessage::getDeleteFlag, 0);
             wrapper.set(LifeMessage::getIsRead, 1);
             return lifeMessageMapper.update(null, wrapper);
@@ -516,8 +516,8 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
     @Override
     public int noReadCount(String receiverId) throws Exception {
         try {
-            LambdaUpdateWrapper<LifeMessage> wrapper = new LambdaUpdateWrapper<>();
-            wrapper.eq(LifeMessage::getReceiverId, receiverId);
+            LambdaQueryWrapper<LifeMessage> wrapper = new LambdaQueryWrapper<>();
+            lifeMessageUtil.applyReceiverQuery(wrapper, receiverId);
             wrapper.eq(LifeMessage::getIsRead, 0);
             wrapper.eq(LifeMessage::getDeleteFlag, 0);
             return lifeMessageMapper.selectCount(wrapper);
@@ -530,7 +530,15 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
     @Override
     public int deleteMessageByPhoneId(String senderId, String receiverId) throws Exception {
         try {
-            return lifeMessageMapper.deleteMessageByPhoneId(senderId, receiverId);
+            PhoneTypeIdResult sender = lifeMessageUtil.resolveIdentity(senderId);
+            PhoneTypeIdResult receiver = lifeMessageUtil.resolveIdentity(receiverId);
+            if (sender == null || receiver == null) {
+                return 0;
+            }
+            return lifeMessageMapper.deleteMessageByPhoneId(
+                    sender.getType(), sender.getId(),
+                    receiver.getType(), receiver.getId(),
+                    senderId, receiverId);
         } catch (Exception e) {
             log.error("LifeMessageServiceImpl.deleteMessageByPhoneId Error Mgs={}", e.getMessage());
             throw new Exception(e);
@@ -563,7 +571,7 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
 
             // 查询未读消息数量(life_message 按 receiver_user_type + receiver_ref_id)
             LambdaQueryWrapper<LifeMessage> messageWrapper = new LambdaQueryWrapper<>();
-            applyReceiverIdentityFilter(messageWrapper, receiverId);
+            lifeMessageUtil.applyReceiverQuery(messageWrapper, receiverId);
             messageWrapper.eq(LifeMessage::getIsRead, 0);
             messageWrapper.notIn(LifeMessage::getSenderId, notDisturbIdsStr);
             messageWrapper.apply("(instr(delete_phone_id, '" + receiverId + "') is null or instr(delete_phone_id, '" + receiverId + "') = 0)");
@@ -586,7 +594,11 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
     public List<LifeFansVo> getTalkedUserList() throws Exception {
         try {
             String phoneId = Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getString("userType") + "_" + JwtUtil.getCurrentUserInfo().getString("phone");
-            return lifeMessageMapper.getTalkedUserList(phoneId);
+            PhoneTypeIdResult participant = lifeMessageUtil.resolveIdentity(phoneId);
+            if (participant == null) {
+                return new ArrayList<>();
+            }
+            return lifeMessageMapper.getTalkedUserList(phoneId, participant.getType(), participant.getId());
         } catch (Exception e) {
             log.error("LifeMessageServiceImpl.getTalkedUserList Error Mgs={}", e.getMessage());
             throw new Exception(e);
@@ -603,19 +615,4 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
         return lifeUserMapper.selectOne(wrapper).getIsBanned() == 1;
     }
 
-    /**
-     * 按接收人 userType + refId 过滤消息;解析失败时回退 receiver_id。
-     */
-    private void applyReceiverIdentityFilter(LambdaQueryWrapper<LifeMessage> wrapper, String receiverId) {
-        PhoneTypeIdResult resolved = typeUtil.resolveTypeAndId(receiverId);
-        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
-            wrapper.and(w -> w.nested(n -> n.eq(LifeMessage::getReceiverUserType, resolved.getType())
-                            .eq(LifeMessage::getReceiverRefId, resolved.getId()))
-                    .or()
-                    .eq(LifeMessage::getReceiverId, receiverId));
-        } else {
-            wrapper.eq(LifeMessage::getReceiverId, receiverId);
-        }
-    }
-
 }

+ 9 - 1
alien-store/src/main/java/shop/alien/store/service/impl/PlatformLifeMessageServiceImpl.java

@@ -8,6 +8,8 @@ import shop.alien.entity.store.vo.LifeFansVo;
 import shop.alien.entity.store.vo.LifeMessageVo;
 import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.store.service.PlatformLifeMessageService;
+import shop.alien.util.type.LifeMessageUtil;
+import shop.alien.util.type.PhoneTypeIdResult;
 
 import java.util.List;
 
@@ -18,13 +20,19 @@ public class PlatformLifeMessageServiceImpl implements PlatformLifeMessageServic
 
     private final LifeMessageMapper lifeMessageMapper;
 
+    private final LifeMessageUtil lifeMessageUtil;
+
     @Override
     public List<LifeMessageVo> getMessageList(String receiverId) throws Exception {
         try {
             QueryWrapper<LifeFansVo> wrapper = new QueryWrapper<>();
             wrapper.eq("message.num", 1);
             wrapper.orderByDesc("message.created_time");
-            return lifeMessageMapper.getLifeMessagePageByPhoneId(receiverId, wrapper);
+            PhoneTypeIdResult participant = lifeMessageUtil.resolveIdentity(receiverId);
+            Integer participantUserType = participant != null ? participant.getType() : -1;
+            Integer participantRefId = participant != null ? participant.getId() : -1;
+            return lifeMessageMapper.getLifeMessagePageByPhoneId(
+                    receiverId, participantUserType, participantRefId, wrapper);
         } catch (Exception e) {
             log.error("LifeMessageServiceImpl.getMessageList Error Msg={}", e.getMessage());
             throw new Exception("获取消息列表失败");

+ 7 - 7
alien-store/src/main/java/shop/alien/store/service/impl/StoreRenovationRequirementServiceImpl.java

@@ -20,6 +20,7 @@ import shop.alien.store.config.WebSocketProcess;
 import shop.alien.store.service.StoreRenovationRequirementService;
 import shop.alien.store.util.ai.AiContentModerationUtil;
 import shop.alien.util.common.JwtUtil;
+import shop.alien.util.type.LifeMessageUtil;
 import shop.alien.util.type.LifeNoticeUtil;
 
 import java.text.SimpleDateFormat;
@@ -46,6 +47,8 @@ public class StoreRenovationRequirementServiceImpl extends ServiceImpl<StoreReno
 
     private final LifeNoticeUtil lifeNoticeUtil;
 
+    private final LifeMessageUtil lifeMessageUtil;
+
     private final WebSocketProcess webSocketProcess;
 
     private final StoreUserMapper storeUserMapper;
@@ -487,17 +490,14 @@ public class StoreRenovationRequirementServiceImpl extends ServiceImpl<StoreReno
                             // 判断互相都发送过消息才算沟通过
                             // 检查 currentUserPhoneId 是否给 publishingShopPhoneId 发送过消息
                             LambdaQueryWrapper<LifeMessage> messageWrapper1 = new LambdaQueryWrapper<>();
-                            messageWrapper1.eq(LifeMessage::getDeleteFlag, 0)
-                                    .eq(LifeMessage::getSenderId, currentUserPhoneId)
-                                    .eq(LifeMessage::getReceiverId, publishingShopPhoneId);
+                            messageWrapper1.eq(LifeMessage::getDeleteFlag, 0);
+                            lifeMessageUtil.applySenderReceiverPairQuery(messageWrapper1, currentUserPhoneId, publishingShopPhoneId);
                             Integer messageCount1 = lifeMessageMapper.selectCount(messageWrapper1);
                             boolean hasSentMessage1 = messageCount1 != null && messageCount1 > 0;
 
-                            // 检查 publishingShopPhoneId 是否给 currentUserPhoneId 发送过消息
                             LambdaQueryWrapper<LifeMessage> messageWrapper2 = new LambdaQueryWrapper<>();
-                            messageWrapper2.eq(LifeMessage::getDeleteFlag, 0)
-                                    .eq(LifeMessage::getSenderId, publishingShopPhoneId)
-                                    .eq(LifeMessage::getReceiverId, currentUserPhoneId);
+                            messageWrapper2.eq(LifeMessage::getDeleteFlag, 0);
+                            lifeMessageUtil.applySenderReceiverPairQuery(messageWrapper2, publishingShopPhoneId, currentUserPhoneId);
                             Integer messageCount2 = lifeMessageMapper.selectCount(messageWrapper2);
                             boolean hasSentMessage2 = messageCount2 != null && messageCount2 > 0;
 

+ 6 - 15
alien-store/src/main/java/shop/alien/store/util/AiUserViolationUtils.java

@@ -1,6 +1,7 @@
 package shop.alien.store.util;
 
 import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -19,6 +20,7 @@ import shop.alien.entity.store.LifeUserViolation;
 import shop.alien.entity.store.vo.LifeUserVo;
 import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeUserMapper;
+import shop.alien.util.type.LifeMessageUtil;
 
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -33,6 +35,7 @@ public class AiUserViolationUtils {
     private final RestTemplate restTemplate;
     private final LifeUserMapper lifeUserMapper;
     private final LifeMessageMapper lifeMessageMapper;
+    private final LifeMessageUtil lifeMessageUtil;
 
     //    @Value("${third-party-login.base-url:http://192.168.2.250:9000/ai/user-auth-core/api/v1/auth/login}")
     private String loginUrl = "http://192.168.2.250:9000/ai/user-auth-core/api/v1/auth/login";
@@ -207,22 +210,10 @@ public class AiUserViolationUtils {
      */
     public List<LifeMessage> getRecentChatMessages(String userReporterUserById, String userReportedUserById) {
         // 构建查询条件
-        QueryWrapper<LifeMessage> queryWrapper = new QueryWrapper<>();
-
-        // 查询双方的聊天记录
-        queryWrapper.and(wrapper -> wrapper
-                        .eq("sender_id", userReporterUserById)
-                        .eq("receiver_id", userReportedUserById))
-                .or(wrapper -> wrapper
-                        .eq("sender_id", userReportedUserById)
-                        .eq("receiver_id", userReporterUserById));
-
-        // 按时间倒序排列
-        queryWrapper.orderByDesc("created_time");
-
-        // 限制50条记录
+        LambdaQueryWrapper<LifeMessage> queryWrapper = new LambdaQueryWrapper<>();
+        lifeMessageUtil.applyConversationQuery(queryWrapper, userReporterUserById, userReportedUserById);
+        queryWrapper.orderByDesc(LifeMessage::getCreatedTime);
         queryWrapper.last("LIMIT 50");
-
         return lifeMessageMapper.selectList(queryWrapper);
     }