Ver Fonte

feat(search): 添加搜索结果中的评论总数显示功能

- 在AiSearchController中注入CommonRatingService依赖
- 添加fillRatingCount方法填充评论总数到店铺信息
- 实现异常处理确保评论总数获取失败时不影响其他数据
- 优化CommonRatingServiceImpl中空列表的处理逻辑
fcw há 3 meses atrás
pai
commit
f5e86e6ea9

+ 39 - 0
alien-store/src/main/java/shop/alien/store/controller/AiSearchController.java

@@ -24,6 +24,7 @@ import shop.alien.entity.store.StoreUser;
 import shop.alien.entity.store.vo.StoreInfoVo;
 import shop.alien.mapper.StoreImgMapper;
 import shop.alien.mapper.StoreUserMapper;
+import shop.alien.store.service.CommonRatingService;
 import shop.alien.store.service.StoreImgService;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 
@@ -54,6 +55,7 @@ public class AiSearchController {
 
     private final RestTemplate restTemplate;
     private final StoreImgService storeImgService;
+    private final CommonRatingService commonRatingService;
 
     @RequestMapping("/search")
     public R search(@RequestBody Map<String,String> map) {
@@ -133,6 +135,9 @@ public class AiSearchController {
 
             // 查找图片并设置到result中(图片类型1-入口图)
             fillStoreImages(result, 1);
+            
+            // 填充评论总数
+            fillRatingCount(result);
 
             jsonObject1.put("records", result);
 
@@ -231,6 +236,40 @@ public class AiSearchController {
     }
 
     /**
+     * 填充评论总数到StoreInfoVo列表中
+     *
+     * @param result StoreInfoVo列表
+     */
+    private void fillRatingCount(List<StoreInfoVo> result) {
+        if (result == null || result.isEmpty()) {
+            return;
+        }
+        
+        for (StoreInfoVo storeInfo : result) {
+            if (storeInfo.getId() != null) {
+                try {
+                    // 调用评论服务获取评论总数,businessId传id,businessType传1
+                    Object ratingCountObj = commonRatingService.getRatingCount(storeInfo.getId(), 1);
+                    
+                    // 将返回的Object转换为Map
+                    if (ratingCountObj instanceof Map) {
+                        Map<String, Object> ratingCountMap = (Map<String, Object>) ratingCountObj;
+                        // 从map中取出totalCount字段
+                        Object totalCount = ratingCountMap.get("totalCount");
+                        if (totalCount != null) {
+                            // 赋值给totalNum字段(转为String类型)
+                            storeInfo.setTotalNum(String.valueOf(totalCount));
+                        }
+                    }
+                } catch (Exception e) {
+                    log.warn("获取评论总数失败,storeId: {}", storeInfo.getId(), e);
+                    // 如果获取失败,继续处理下一个,不影响其他数据
+                }
+            }
+        }
+    }
+
+    /**
      * 将下划线命名转换为驼峰命名
      * 例如: store_tel -> storeTel, created_time -> createdTime
      */

+ 6 - 1
alien-store/src/main/java/shop/alien/store/service/impl/CommonRatingServiceImpl.java

@@ -203,8 +203,13 @@ public class CommonRatingServiceImpl extends ServiceImpl<CommonRatingMapper, Com
         wrapper.eq(CommonRating::getIsShow, 1);
         List<CommonRating> commonRatings = commonRatingMapper.selectList(wrapper);
         List<Long> collect = commonRatings.stream().map(x -> x.getId()).collect(Collectors.toList());
+        // 如果为空直接返回
+        Map<String, Object> ratingCount = new HashMap<>();
+        if(commonRatings.isEmpty()){
+            return ratingCount;
+        }
         // 获取评价统计信息(总评论数、有图评论数、好评数、中评数、差评数)
-        Map<String, Object> ratingCount = commonRatingMapper.getRatingCount(new QueryWrapper<CommonRating>().in("id", collect));
+        ratingCount = commonRatingMapper.getRatingCount(new QueryWrapper<CommonRating>().in("id", collect));
         if(RatingBusinessTypeEnum.STORE_RATING.getBusinessType() == businessType){
             // 1店铺评分
             StoreInfo storeInfo = storeInfoMapper.selectById(businessId);