Ver código fonte

修改站内信的查询逻辑

liudongzhi 10 horas atrás
pai
commit
dba00c570f

+ 6 - 3
alien-entity/src/main/java/shop/alien/mapper/LifeNoticeMapper.java

@@ -10,7 +10,10 @@ import shop.alien.entity.store.LifeNotice;
 public interface LifeNoticeMapper extends BaseMapper<LifeNotice> {
 
     @Select("select count(id) noReadNum from life_notice " +
-            "where delete_flag = 0 and is_read = 0 and receiver_id = #{receiverId} ")
-    int selectNoReadCount(@Param("receiverId") String receiverId);
+            "where delete_flag = 0 and is_read = 0 " +
+            "and receiver_user_type = #{receiverUserType} " +
+            "and receiver_ref_id = #{receiverRefId} ")
+    int selectNoReadCount(@Param("receiverUserType") Integer receiverUserType,
+                          @Param("receiverRefId") Integer receiverRefId);
 
-}
+}

+ 61 - 0
alien-entity/src/main/java/shop/alien/util/type/LifeNoticeUtil.java

@@ -1,5 +1,7 @@
 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;
@@ -65,4 +67,63 @@ public class LifeNoticeUtil {
             notice.setReceiverRefId(result.getId());
         }
     }
+
+    /**
+     * 按接收人 receiver_user_type + receiver_ref_id 查询。
+     */
+    public void applyReceiverQuery(LambdaQueryWrapper<LifeNotice> wrapper, String receiverId) {
+        applyReceiverIdentity(wrapper, receiverId);
+    }
+
+    /**
+     * 按接收人 receiver_user_type + receiver_ref_id 更新条件。
+     */
+    public void applyReceiverUpdate(LambdaUpdateWrapper<LifeNotice> wrapper, String receiverId) {
+        applyReceiverIdentity(wrapper, receiverId);
+    }
+
+    /**
+     * 按发送人 sender_user_type + sender_ref_id 查询。
+     */
+    public void applySenderQuery(LambdaQueryWrapper<LifeNotice> wrapper, String senderId) {
+        if (!StringUtils.hasText(senderId) || SYSTEM_SENDER_ID.equals(senderId)) {
+            wrapper.apply("1 = 0");
+            return;
+        }
+        PhoneTypeIdResult resolved = typeUtil.resolveTypeAndId(senderId);
+        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
+            wrapper.eq(LifeNotice::getSenderUserType, resolved.getType())
+                    .eq(LifeNotice::getSenderRefId, resolved.getId());
+        } else {
+            wrapper.apply("1 = 0");
+        }
+    }
+
+    private void applyReceiverIdentity(LambdaQueryWrapper<LifeNotice> wrapper, String receiverId) {
+        if (!StringUtils.hasText(receiverId)) {
+            wrapper.apply("1 = 0");
+            return;
+        }
+        PhoneTypeIdResult resolved = typeUtil.resolveTypeAndId(receiverId);
+        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
+            wrapper.eq(LifeNotice::getReceiverUserType, resolved.getType())
+                    .eq(LifeNotice::getReceiverRefId, resolved.getId());
+        } else {
+            wrapper.apply("1 = 0");
+        }
+    }
+
+    private void applyReceiverIdentity(LambdaUpdateWrapper<LifeNotice> wrapper, String receiverId) {
+        if (!StringUtils.hasText(receiverId)) {
+            wrapper.apply("1 = 0");
+            return;
+        }
+        PhoneTypeIdResult resolved = typeUtil.resolveTypeAndId(receiverId);
+        if (resolved != null && resolved.getType() != null && resolved.getId() != null) {
+            wrapper.eq(LifeNotice::getReceiverUserType, resolved.getType())
+                    .eq(LifeNotice::getReceiverRefId, resolved.getId());
+        } else {
+            wrapper.apply("1 = 0");
+        }
+    }
 }

+ 7 - 4
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerNoticeServiceImpl.java

@@ -20,6 +20,7 @@ import shop.alien.lawyer.service.LawyerNoticeService;
 import shop.alien.mapper.LawyerUserViolationMapper;
 import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeNoticeMapper;
+import shop.alien.util.type.LifeNoticeUtil;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -90,6 +91,8 @@ public class LawyerNoticeServiceImpl extends ServiceImpl<LifeNoticeMapper, LifeN
 
     private final LawyerUserViolationMapper lawyerUserViolationMapper;
 
+    private final LifeNoticeUtil lifeNoticeUtil;
+
     @Override
     public R<IPage<LifeNoticeVo>> getLawyerNoticeList(Integer pageNum, Integer pageSize, String receiverId, Integer noticeType) {
         // 构建查询条件
@@ -142,8 +145,8 @@ public class LawyerNoticeServiceImpl extends ServiceImpl<LifeNoticeMapper, LifeN
      */
     private LambdaQueryWrapper<LifeNotice> buildQueryWrapper(String receiverId, Integer noticeType) {
         LambdaQueryWrapper<LifeNotice> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeNotice::getReceiverId, receiverId)
-                .eq(LifeNotice::getNoticeType, noticeType)
+        lifeNoticeUtil.applyReceiverQuery(queryWrapper, receiverId);
+        queryWrapper.eq(LifeNotice::getNoticeType, noticeType)
                 .eq(LifeNotice::getDeleteFlag, DELETE_FLAG_NOT_DELETED)
                 .orderByDesc(LifeNotice::getCreatedTime);
         return queryWrapper;
@@ -341,8 +344,8 @@ public class LawyerNoticeServiceImpl extends ServiceImpl<LifeNoticeMapper, LifeN
         
         // 构建查询条件:查询未读且未删除的通知,使用LIMIT 1优化性能
         LambdaQueryWrapper<LifeNotice> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeNotice::getReceiverId, receiverId)
-                .eq(LifeNotice::getIsRead, IS_READ_UNREAD)
+        lifeNoticeUtil.applyReceiverQuery(queryWrapper, receiverId);
+        queryWrapper.eq(LifeNotice::getIsRead, IS_READ_UNREAD)
                 .eq(LifeNotice::getDeleteFlag, DELETE_FLAG_NOT_DELETED)
                 .last("LIMIT 1");
         

+ 7 - 6
alien-lawyer/src/main/java/shop/alien/lawyer/service/impl/LawyerUserServiceImpl.java

@@ -2,6 +2,7 @@ package shop.alien.lawyer.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -27,6 +28,7 @@ import shop.alien.lawyer.util.ai.LawyerLicenseVerifyUtil;
 import shop.alien.mapper.*;
 import shop.alien.util.common.ListToPage;
 import shop.alien.util.excel.EasyExcelUtil;
+import shop.alien.util.type.LifeNoticeUtil;
 
 import javax.servlet.http.HttpServletResponse;
 import java.text.SimpleDateFormat;
@@ -56,6 +58,7 @@ public class LawyerUserServiceImpl extends ServiceImpl<LawyerUserMapper, LawyerU
     private final LawFirmPaymentMapper lawFirmPaymentmapper;
     private final OrderReviewMapper orderReviewMapper;
     private final LifeNoticeMapper lifeNoticeMapper;
+    private final LifeNoticeUtil lifeNoticeUtil;
     private final LawyerLicenseVerifyUtil lawyerLicenseVerifyUtil;
     private final StoreDictionaryMapper storeDictionaryMapper;
 
@@ -572,12 +575,10 @@ public class LawyerUserServiceImpl extends ServiceImpl<LawyerUserMapper, LawyerU
                         id, lawyerUser.getName(), lawyerUser.getLogoutTime());
 
 
-                lifeNoticeMapper.update(
-                        null,
-                        new UpdateWrapper<LifeNotice>()
-                                .eq("receiver_id", key)
-                                .set("delete_flag", 1)
-                );
+                LambdaUpdateWrapper<LifeNotice> noticeUpdateWrapper = new LambdaUpdateWrapper<>();
+                lifeNoticeUtil.applyReceiverUpdate(noticeUpdateWrapper, key);
+                noticeUpdateWrapper.set(LifeNotice::getDeleteFlag, 1);
+                lifeNoticeMapper.update(null, noticeUpdateWrapper);
 
                 // 构建返回结果
                 resultMap.put("id", lawyerUser.getId());

+ 6 - 3
alien-store-platform/src/main/java/shop/alien/storeplatform/service/impl/MerchantUserServiceImpl.java

@@ -13,6 +13,7 @@ import shop.alien.entity.store.vo.StoreUserVo;
 import shop.alien.mapper.*;
 import shop.alien.storeplatform.service.MerchantUserService;
 import shop.alien.storeplatform.util.LoginUserUtil;
+import shop.alien.util.type.LifeNoticeUtil;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
@@ -44,6 +45,8 @@ public class MerchantUserServiceImpl implements MerchantUserService {
     private final LifeDiscountCouponMapper lifeDiscountCouponMapper;
     private final LifeDiscountCouponUserMapper lifeDiscountCouponUserMapper;
 
+    private final LifeNoticeUtil lifeNoticeUtil;
+
     @Override
     public StoreUserVo getMerchantByPhone(String phone) {
         log.info("MerchantUserServiceImpl.getMerchantByPhone - 查询商户用户: phone={}", phone);
@@ -370,8 +373,8 @@ public class MerchantUserServiceImpl implements MerchantUserService {
             if (StringUtils.isNotEmpty(storeUser.getPhone())) {
                 String receiverId = "store_" + storeUser.getPhone();
                 LambdaQueryWrapper<LifeNotice> noticeWrapper = new LambdaQueryWrapper<>();
-                noticeWrapper.eq(LifeNotice::getReceiverId, receiverId)
-                        .eq(LifeNotice::getTitle, "入驻店铺申请");
+                lifeNoticeUtil.applyReceiverQuery(noticeWrapper, receiverId);
+                noticeWrapper.eq(LifeNotice::getTitle, "入驻店铺申请");
                 int deletedNoticeCount = lifeNoticeMapper.delete(noticeWrapper);
                 log.info("MerchantUserServiceImpl.resetToInitialStatus - 删除入驻申请通知: count={}", deletedNoticeCount);
             }
@@ -422,7 +425,7 @@ public class MerchantUserServiceImpl implements MerchantUserService {
             if (StringUtils.isNotEmpty(storeUser.getPhone())) {
                 String receiverId = "store_" + storeUser.getPhone();
                 LambdaQueryWrapper<LifeNotice> allNoticeWrapper = new LambdaQueryWrapper<>();
-                allNoticeWrapper.eq(LifeNotice::getReceiverId, receiverId);
+                lifeNoticeUtil.applyReceiverQuery(allNoticeWrapper, receiverId);
                 int deletedAllNoticeCount = lifeNoticeMapper.delete(allNoticeWrapper);
                 log.info("MerchantUserServiceImpl.resetToInitialStatus - 删除所有通知消息: count={}", deletedAllNoticeCount);
             }

+ 6 - 3
alien-store-platform/src/main/java/shop/alien/storeplatform/service/impl/NoticeServiceImpl.java

@@ -19,6 +19,7 @@ import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeNoticeMapper;
 import shop.alien.mapper.LifeUserViolationMapper;
 import shop.alien.storeplatform.service.NoticeService;
+import shop.alien.util.type.LifeNoticeUtil;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -40,6 +41,8 @@ public class NoticeServiceImpl implements NoticeService {
     
     private final LifeUserViolationMapper lifeUserViolationMapper;
 
+    private final LifeNoticeUtil lifeNoticeUtil;
+
     @Override
     public JSONObject getNoticeStatistics(String receiverId) {
         log.info("NoticeServiceImpl.getNoticeStatistics - 查询通知统计: receiverId={}", receiverId);
@@ -48,7 +51,7 @@ public class NoticeServiceImpl implements NoticeService {
 
         // 1. 查询系统通知和订单提醒(noticeType: 1-系统通知, 2-订单提醒)
         LambdaQueryWrapper<LifeNotice> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeNotice::getReceiverId, receiverId);
+        lifeNoticeUtil.applyReceiverQuery(queryWrapper, receiverId);
         queryWrapper.in(LifeNotice::getNoticeType, 1, 2);
         List<LifeNotice> lifeNoticeList = lifeNoticeMapper.selectList(queryWrapper);
 
@@ -117,7 +120,7 @@ public class NoticeServiceImpl implements NoticeService {
 
         // 1. 查询通知列表
         LambdaQueryWrapper<LifeNotice> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeNotice::getReceiverId, receiverId);
+        lifeNoticeUtil.applyReceiverQuery(queryWrapper, receiverId);
         queryWrapper.eq(LifeNotice::getNoticeType, noticeType);
         queryWrapper.eq(LifeNotice::getDeleteFlag, 0);
         queryWrapper.orderByDesc(LifeNotice::getCreatedTime);
@@ -266,7 +269,7 @@ public class NoticeServiceImpl implements NoticeService {
         LambdaUpdateWrapper<LifeNotice> wrapper = new LambdaUpdateWrapper<>();
         
         // 1. 接收人条件(必须)
-        wrapper.eq(LifeNotice::getReceiverId, receiverId);
+        lifeNoticeUtil.applyReceiverUpdate(wrapper, receiverId);
         
         // 2. 只更新未读的通知
         wrapper.eq(LifeNotice::getIsRead, 0);

+ 23 - 5
alien-store/src/main/java/shop/alien/store/service/impl/LifeMessageServiceImpl.java

@@ -20,6 +20,8 @@ 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.LifeNoticeUtil;
+import shop.alien.util.type.PhoneTypeIdResult;
 import shop.alien.util.type.TypeUtil;
 
 import java.util.ArrayList;
@@ -48,8 +50,9 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
     private final LawyerUserMapper lawyerUserMapper;
     private final LawyerConsultationOrderMapper lawyerConsultationOrderMapper;
 
+    private final TypeUtil typeUtil;
 
-    private TypeUtil typeUtil;
+    private final LifeNoticeUtil lifeNoticeUtil;
 
     @Override
     public List<LifeMessageVo> getMessageList(String receiverId, int friendType, String search ,String userName) throws Exception {
@@ -558,17 +561,17 @@ public class LifeMessageServiceImpl extends ServiceImpl<LifeMessageMapper, LifeM
             notDisturbList.addAll(lifeBlacklistVoList);
             String notDisturbIdsStr = "'" + String.join("','", notDisturbList) + "'";
 
-            // 查询未读消息数量
+            // 查询未读消息数量(life_message 按 receiver_user_type + receiver_ref_id)
             LambdaQueryWrapper<LifeMessage> messageWrapper = new LambdaQueryWrapper<>();
-            messageWrapper.eq(LifeMessage::getReceiverId, receiverId);
+            applyReceiverIdentityFilter(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)");
             int noReadMessageCount = lifeMessageMapper.selectCount(messageWrapper);
 
-            // 查询未读通知数量
+            // 查询未读通知数量(life_notice 按 receiver_user_type + receiver_ref_id)
             LambdaQueryWrapper<LifeNotice> noticeWrapper = new LambdaQueryWrapper<>();
-            noticeWrapper.eq(LifeNotice::getReceiverId, receiverId);
+            lifeNoticeUtil.applyReceiverQuery(noticeWrapper, receiverId);
             noticeWrapper.eq(LifeNotice::getIsRead, 0);
             int noReadNoticeCount = lifeNoticeMapper.selectCount(noticeWrapper);
 
@@ -600,4 +603,19 @@ 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);
+        }
+    }
+
 }

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

@@ -19,6 +19,7 @@ import shop.alien.mapper.LifeMessageMapper;
 import shop.alien.mapper.LifeNoticeMapper;
 import shop.alien.mapper.LifeUserViolationMapper;
 import shop.alien.store.service.LifeNoticeService;
+import shop.alien.util.type.LifeNoticeUtil;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -33,11 +34,13 @@ public class LifeNoticeServiceImpl extends ServiceImpl<LifeNoticeMapper, LifeNot
 
     private final LifeUserViolationMapper lifeUserViolationMapper;
 
+    private final LifeNoticeUtil lifeNoticeUtil;
+
     @Override
     public R<IPage<LifeNoticeVo>> getNoticeList(int pageNum, int pageSize, String receiverId, int noticeType) {
         IPage<LifeNotice> ipage = new Page<>(pageNum, pageSize);
         LambdaQueryWrapper<LifeNotice> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeNotice::getReceiverId, receiverId);
+        lifeNoticeUtil.applyReceiverQuery(queryWrapper, receiverId);
         queryWrapper.eq(LifeNotice::getNoticeType, noticeType);
         queryWrapper.eq(LifeNotice::getDeleteFlag, 0);
         queryWrapper.orderByDesc(LifeNotice::getCreatedTime);
@@ -130,9 +133,9 @@ public class LifeNoticeServiceImpl extends ServiceImpl<LifeNoticeMapper, LifeNot
     public JSONObject getSystemAndOrderNoticeSum(String receiverId) {
         JSONObject jsonObject = new JSONObject();
 
-        // 查询系统通知和订单提醒
+        // 查询系统通知和订单提醒(按 receiver_user_type + receiver_ref_id)
         LambdaQueryWrapper<LifeNotice> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeNotice::getReceiverId, receiverId);
+        lifeNoticeUtil.applyReceiverQuery(queryWrapper, receiverId);
         queryWrapper.in(LifeNotice::getNoticeType, 1, 2);
         List<LifeNotice> lifeNoticeList = lifeNoticeMapper.selectList(queryWrapper);
 
@@ -175,7 +178,7 @@ public class LifeNoticeServiceImpl extends ServiceImpl<LifeNoticeMapper, LifeNot
     @Override
     public long countUnreadByReceiverIdAndNoticeType(String receiverId, Integer noticeType) {
         LambdaQueryWrapper<LifeNotice> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(LifeNotice::getReceiverId, receiverId);
+        lifeNoticeUtil.applyReceiverQuery(queryWrapper, receiverId);
         queryWrapper.eq(LifeNotice::getNoticeType, noticeType);
         queryWrapper.eq(LifeNotice::getIsRead, 0);
         return lifeNoticeMapper.selectCount(queryWrapper);