浏览代码

Merge branch 'fix-panzhilin' into sit

panzhilin 2 月之前
父节点
当前提交
4a1e66cc32

+ 5 - 0
alien-entity/src/main/java/shop/alien/entity/store/CommonComment.java

@@ -80,6 +80,11 @@ public class CommonComment implements Serializable {
     @TableField("reply_count")
     private Integer replyCount;
 
+    @ApiModelProperty(value = "删除标记, 0:未删除, 1:已删除")
+    @TableField("delete_flag")
+    @TableLogic
+    private Integer deleteFlag;
+
     @ApiModelProperty(value = "创建时间")
     @TableField(value = "created_time", fill = FieldFill.INSERT)
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

+ 130 - 4
alien-store/src/main/java/shop/alien/store/service/impl/StoreCommentAppealServiceImpl.java

@@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.google.common.collect.Lists;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpHeaders;
@@ -34,6 +35,7 @@ import shop.alien.util.common.netease.TextCheckUtil;
 import shop.alien.util.common.safe.TextModerationResultVO;
 import shop.alien.util.common.safe.TextModerationUtil;
 import shop.alien.util.common.safe.TextReviewServiceEnum;
+import shop.alien.util.common.constant.CommentSourceTypeEnum;
 
 import java.net.URLEncoder;
 import java.text.SimpleDateFormat;
@@ -46,6 +48,7 @@ import java.util.stream.Collectors;
  * @author ssk
  * @since 2025-01-02
  */
+@Slf4j
 @Service
 @Transactional
 @RequiredArgsConstructor
@@ -76,6 +79,12 @@ public class StoreCommentAppealServiceImpl extends ServiceImpl<StoreCommentAppea
 
     private final WebSocketProcess webSocketProcess;
 
+    private final CommonRatingMapper commonRatingMapper;
+
+    private final CommonCommentMapper commonCommentMapper;
+
+    private final LifeLikeRecordMapper lifeLikeRecordMapper;
+
     /**
      * 懒得查, 留着导出Excel
      */
@@ -453,11 +462,74 @@ public class StoreCommentAppealServiceImpl extends ServiceImpl<StoreCommentAppea
                 break;
             case 2:
                 storeCommentAppeal.setFinalResult("已同意");
-                //删除原评论
+                // 审核通过后删除评价(common_rating)和评论(common_comment)
                 StoreCommentAppeal byId = this.getById(id);
-                storeCommentMapper.deleteById(byId.getCommentId());
-                //删除原评论下的评论
-                storeCommentMapper.update(null, new LambdaUpdateWrapper<StoreComment>().eq(StoreComment::getReplyId, byId.getCommentId()).set(StoreComment::getDeleteFlag, 1));
+                Integer ratingId = byId.getCommentId(); // commentId 实际存储的是评价ID(common_rating.id)
+                
+                if (ratingId != null) {
+                    // 1. 删除评价(逻辑删除 common_rating 表)
+                    CommonRating rating = commonRatingMapper.selectById(ratingId);
+                    if (rating != null && rating.getDeleteFlag() == 0) {
+                        rating.setDeleteFlag(1);
+                        rating.setUpdatedTime(new Date());
+                        commonRatingMapper.updateById(rating);
+                        log.info("删除评价成功,ratingId={}", ratingId);
+                    }
+                    
+                    // 2. 查询该评价下的所有评论ID
+                    LambdaQueryWrapper<CommonComment> commentQueryWrapper = new LambdaQueryWrapper<>();
+                    commentQueryWrapper.eq(CommonComment::getSourceType, CommentSourceTypeEnum.STORE_COMMENT.getType())
+                                      .eq(CommonComment::getSourceId, ratingId)
+                                      .eq(CommonComment::getDeleteFlag, 0);
+                    List<CommonComment> comments = commonCommentMapper.selectList(commentQueryWrapper);
+                    List<Long> commentIds = comments.stream().map(CommonComment::getId).collect(Collectors.toList());
+                    
+                    // 3. 删除该评价下的所有评论(逻辑删除 common_comment 表)
+                    if (!comments.isEmpty()) {
+                        LambdaUpdateWrapper<CommonComment> commentWrapper = new LambdaUpdateWrapper<>();
+                        commentWrapper.eq(CommonComment::getSourceType, CommentSourceTypeEnum.STORE_COMMENT.getType())
+                                      .eq(CommonComment::getSourceId, ratingId)
+                                      .eq(CommonComment::getDeleteFlag, 0);
+                        commentWrapper.set(CommonComment::getDeleteFlag, 1);
+                        commentWrapper.set(CommonComment::getUpdatedTime, new Date());
+                        commonCommentMapper.update(null, commentWrapper);
+                        log.info("删除评价下的评论成功,ratingId={},评论数={}", ratingId, comments.size());
+                    }
+                    
+                    // 4. 删除评价的点赞记录(type=7 表示评价点赞)
+                    LambdaUpdateWrapper<LifeLikeRecord> ratingLikeWrapper = new LambdaUpdateWrapper<>();
+                    ratingLikeWrapper.eq(LifeLikeRecord::getType, "7")
+                                    .eq(LifeLikeRecord::getHuifuId, String.valueOf(ratingId))
+                                    .eq(LifeLikeRecord::getDeleteFlag, 0);
+                    ratingLikeWrapper.set(LifeLikeRecord::getDeleteFlag, 1);
+                    ratingLikeWrapper.set(LifeLikeRecord::getUpdatedTime, new Date());
+                    lifeLikeRecordMapper.update(null, ratingLikeWrapper);
+                    log.info("删除评价的点赞记录,ratingId={}", ratingId);
+                    
+                    // 5. 删除评论的点赞记录(type=1 表示评论点赞)
+                    if (!commentIds.isEmpty()) {
+                        for (Long commentId : commentIds) {
+                            LambdaUpdateWrapper<LifeLikeRecord> commentLikeWrapper = new LambdaUpdateWrapper<>();
+                            commentLikeWrapper.eq(LifeLikeRecord::getType, "1")
+                                             .eq(LifeLikeRecord::getHuifuId, String.valueOf(commentId))
+                                             .eq(LifeLikeRecord::getDeleteFlag, 0);
+                            commentLikeWrapper.set(LifeLikeRecord::getDeleteFlag, 1);
+                            commentLikeWrapper.set(LifeLikeRecord::getUpdatedTime, new Date());
+                            lifeLikeRecordMapper.update(null, commentLikeWrapper);
+                        }
+                        log.info("删除评论的点赞记录,评论数={}", commentIds.size());
+                    }
+                    
+                    // 6. 重新统计店铺评分(删除评价后需要更新门店评分)
+                    if (rating != null && rating.getBusinessType() == 1) {
+                        updateStoreScore(rating.getBusinessId());
+                    }
+                }
+                
+                // 7. 删除申诉记录(逻辑删除)
+                byId.setDeleteFlag(1);
+                byId.setUpdatedTime(new Date());
+                this.updateById(byId);
                 break;
         }
         //商家申诉
@@ -541,4 +613,58 @@ public class StoreCommentAppealServiceImpl extends ServiceImpl<StoreCommentAppea
         }
     }
 
+    /**
+     * 更新店铺评分(删除评价后重新计算门店评分)
+     *
+     * @param businessId 店铺ID
+     */
+    private void updateStoreScore(Integer businessId) {
+        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());
+        }
+    }
 }