Pārlūkot izejas kodu

fix: Bug 原因:getRatingCount 方法中计算 noReplyCount 时存在两个问题:
@TableLogic 导致 delete_flag 过滤失效:CommonRating.deleteFlag 有 @TableLogic 注解,但 getRatingWithNoReply 是自定义 XML 查询(SELECT COUNT(*) FROM common_rating ${ew.customSqlSegment}),MyBatis-Plus 不会自动为其添加逻辑删除条件。而原代码通过 wrapper.eq(CommonRating::getDeleteFlag, 0) 想手动添加该条件,但对 @TableLogic 字段,这种方式可能被框架静默忽略。最终 noReplyCount 统计包含了已删除的评价数据。
复用 wrapper 导致条件累积:原代码复用了前面 selectList 的 wrapper,虽然逻辑上条件是正确的,但 wrapper 的状态不够清晰,容易引起维护隐患。
修复方式:
创建一个全新的 noReplyWrapper,条件清晰独立
使用 .apply("delete_flag = 0") 直接写 SQL 条件,绕过 @TableLogic 对 LambdaQueryWrapper 的干扰
确保 noReplyCount 的查询条件与列表查询(getRatingList 中 this.page() 自动处理 @TableLogic)完全一致
修改后,"待回复差评" 的数字将与列表数据保持一致

李亚非 2 mēneši atpakaļ
vecāks
revīzija
90cf4168ac

+ 13 - 5
alien-store/src/main/java/shop/alien/store/service/impl/CommonRatingServiceImpl.java

@@ -388,11 +388,19 @@ 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());
-        // 查询没有回复的评价数量
-        wrapper.eq(CommonRating::getDeleteFlag,0);
-        wrapper.ge(CommonRating::getScore,0.5);
-        wrapper.le(CommonRating::getScore,2.5);
-        Integer noReplyCount = commonRatingMapper.getRatingWithNoReply(wrapper);
+        // 查询没有回复的差评数量(使用新 wrapper,避免复用导致条件累积问题)
+        // 注意:因为 deleteFlag 字段有 @TableLogic 注解,LambdaQueryWrapper.eq() 对该字段可能失效
+        // 而 getRatingWithNoReply 是自定义 XML 查询,不会自动补充逻辑删除条件
+        // 所以这里使用 .apply() 直接写 SQL 条件,确保 delete_flag = 0 生效
+        LambdaQueryWrapper<CommonRating> noReplyWrapper = new LambdaQueryWrapper<>();
+        noReplyWrapper.eq(CommonRating::getBusinessId, businessId);
+        noReplyWrapper.eq(CommonRating::getBusinessType, businessType);
+        noReplyWrapper.eq(CommonRating::getAuditStatus, 1);
+        noReplyWrapper.eq(CommonRating::getIsShow, 1);
+        noReplyWrapper.apply("delete_flag = 0");
+        noReplyWrapper.ge(CommonRating::getScore, 0.5);
+        noReplyWrapper.le(CommonRating::getScore, 2.5);
+        Integer noReplyCount = commonRatingMapper.getRatingWithNoReply(noReplyWrapper);
 
         // 如果为空直接返回
         Map<String, Object> ratingCount = new HashMap<>();