Переглянути джерело

Fix:店铺评价申诉通过后删除评价优化

panzhilin 2 місяців тому
батько
коміт
4a42ec8b6e

+ 23 - 1
alien-store/src/main/java/shop/alien/store/controller/CommonRatingController.java

@@ -111,15 +111,37 @@ public class CommonRatingController {
         }
     }
 
-    /**删除评价
+    /**
+     * 删除评价(并重新统计店铺评分)
+     * 
      * @param ratingId 评价id
      * @return 0:成功, 1:失败
      */
     @ApiOperation(value = "删除评价", notes = "0:成功, 1:失败")
     @GetMapping("/deleteRating")
     public R deleteRating(@RequestParam Long ratingId) {
+        log.info("删除评价,ratingId={}", ratingId);
+        
+        // 先获取评价信息(用于后续更新评分)
+        CommonRating rating = commonRatingService.getById(ratingId);
+        if (rating == null) {
+            return R.fail("评价不存在");
+        }
+        
+        // 删除评价
         boolean b = commonRatingService.removeById(ratingId);
+        
         if (b) {
+            // 删除成功后,重新统计店铺评分(仅店铺评价类型)
+            if (rating.getBusinessType() != null && rating.getBusinessType() == 1) {
+                try {
+                    commonRatingService.updateStoreScoreAfterDelete(rating.getBusinessId());
+                    log.info("更新店铺评分成功,businessId={}", rating.getBusinessId());
+                } catch (Exception e) {
+                    log.error("更新店铺评分失败,businessId={},error={}", rating.getBusinessId(), e.getMessage());
+                    // 评分更新失败不影响删除结果
+                }
+            }
             return R.success("删除评价成功");
         }
         return R.fail("删除评价失败");

+ 7 - 0
alien-store/src/main/java/shop/alien/store/service/CommonRatingService.java

@@ -54,6 +54,13 @@ public interface CommonRatingService extends IService<CommonRating> {
      */
     Object getRatingDetail(Integer ratingId, Long userId);
 
+    /**
+     * 删除评价后更新店铺评分
+     * 
+     * @param businessId 店铺ID
+     */
+    void updateStoreScoreAfterDelete(Integer businessId);
+
 
   /*  /**
      * 根据业务类型和业务ID获取平均评分

+ 60 - 0
alien-store/src/main/java/shop/alien/store/service/impl/CommonRatingServiceImpl.java

@@ -665,6 +665,66 @@ public class CommonRatingServiceImpl extends ServiceImpl<CommonRatingMapper, Com
         IPage<CommonRatingVo> emptyResult = new Page<>(page1.getCurrent(), page1.getSize(), 0);
         return R.data(emptyResult);
     }
+    /**
+     * 删除评价后更新店铺评分
+     * 
+     * @param businessId 店铺ID
+     */
+    @Override
+    public void updateStoreScoreAfterDelete(Integer businessId) {
+        if (businessId == null) {
+            return;
+        }
+        
+        try {
+            // 查询该店铺的所有有效评价
+            LambdaQueryWrapper<CommonRating> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(CommonRating::getBusinessType, 1)
+                   .eq(CommonRating::getBusinessId, businessId)
+                   .eq(CommonRating::getDeleteFlag, 0)
+                   .eq(CommonRating::getIsShow, 1)
+                   .eq(CommonRating::getAuditStatus, 1);
+            List<CommonRating> ratings = commonRatingMapper.selectList(wrapper);
+            
+            int total = ratings.size();
+            if (total == 0) {
+                // 没有评价,设置默认评分为0
+                StoreInfo storeInfo = new StoreInfo();
+                storeInfo.setId(businessId);
+                storeInfo.setScoreAvg(0.0);
+                storeInfo.setScoreOne(0.0);
+                storeInfo.setScoreTwo(0.0);
+                storeInfo.setScoreThree(0.0);
+                storeInfoMapper.updateById(storeInfo);
+                log.info("店铺无有效评价,重置评分为0,businessId={}", businessId);
+                return;
+            }
+            
+            // 计算平均评分
+            double scoreSum = ratings.stream().mapToDouble(r -> r.getScore() != null ? r.getScore() : 0.0).sum();
+            double scoreOneSum = ratings.stream().mapToDouble(r -> r.getScoreOne() != null ? r.getScoreOne() : 0.0).sum();
+            double scoreTwoSum = ratings.stream().mapToDouble(r -> r.getScoreTwo() != null ? r.getScoreTwo() : 0.0).sum();
+            double scoreThreeSum = ratings.stream().mapToDouble(r -> r.getScoreThree() != null ? r.getScoreThree() : 0.0).sum();
+            
+            double scoreAvg = Math.round((scoreSum / total) * 100.0) / 100.0;
+            double scoreOne = Math.round((scoreOneSum / total) * 100.0) / 100.0;
+            double scoreTwo = Math.round((scoreTwoSum / total) * 100.0) / 100.0;
+            double scoreThree = Math.round((scoreThreeSum / total) * 100.0) / 100.0;
+            
+            StoreInfo storeInfo = new StoreInfo();
+            storeInfo.setId(businessId);
+            storeInfo.setScoreAvg(scoreAvg);
+            storeInfo.setScoreOne(scoreOne);
+            storeInfo.setScoreTwo(scoreTwo);
+            storeInfo.setScoreThree(scoreThree);
+            storeInfoMapper.updateById(storeInfo);
+            
+            log.info("更新店铺评分成功,businessId={},评价数={},scoreAvg={}", businessId, total, scoreAvg);
+        } catch (Exception e) {
+            log.error("更新店铺评分失败,businessId={},error={}", businessId, e.getMessage());
+        }
+    }
+
 /*
     @Override
     public Double getAverageScore(Integer businessType, Long businessId) {