Bladeren bron

fix:完善 app 商品评论页获取评论分页接口和相关评论分类数量接口

puhui999 2 jaren geleden
bovenliggende
commit
151e58daa1

+ 7 - 0
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/comment/AppCommentController.java

@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.validation.Valid;
+import java.util.Map;
 
 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@@ -41,6 +42,12 @@ public class AppCommentController {
         return success(ProductCommentConvert.INSTANCE.convertPage02(pageResult));
     }
 
+    @GetMapping("/get-count")
+    @Operation(summary = "获得商品评价分页 tab count")
+    public CommonResult<Map<String, Long>> getCommentPage(@Valid Long spuId) {
+        return success(productCommentService.getCommentPageTabsCount(spuId, Boolean.TRUE));
+    }
+
     @PostMapping(value = "/create")
     @Operation(summary = "创建商品评价")
     public CommonResult<Boolean> createComment(@RequestBody AppCommentCreateReqVO createReqVO) {

+ 4 - 0
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/comment/vo/AppCommentPageReqVO.java

@@ -18,4 +18,8 @@ public class AppCommentPageReqVO extends PageParam {
     @NotNull(message = "商品SPU编号不能为空")
     private Long spuId;
 
+    @Schema(description = "app 评论页 tab 类型 (0 全部、1 好评、2 中评、3 差评)", example = "0")
+    @NotNull(message = "商品SPU编号不能为空")
+    private Integer type;
+
 }

+ 17 - 0
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/comment/ProductCommentDO.java

@@ -28,6 +28,23 @@ import java.util.List;
 @AllArgsConstructor
 public class ProductCommentDO extends BaseDO {
 
+    /**
+     * 所有
+     */
+    public static final Integer ALL = 0;
+    /**
+     * 好评
+     */
+    public static final Integer FAVOURABLE_COMMENT = 1;
+    /**
+     * 中评
+     */
+    public static final Integer MEDIOCRE_COMMENT = 2;
+    /**
+     * 差评
+     */
+    public static final Integer NEGATIVE_COMMENT = 3;
+
     /**
      * 评论编号,主键自增
      */

+ 35 - 3
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/comment/ProductCommentMapper.java

@@ -1,6 +1,7 @@
 package cn.iocoder.yudao.module.product.dal.mysql.comment;
 
 
+import cn.hutool.core.util.ObjectUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
@@ -34,11 +35,33 @@ public interface ProductCommentMapper extends BaseMapperX<ProductCommentDO> {
                 .orderByDesc(ProductCommentDO::getId));
     }
 
+    static void appendTabQuery(LambdaQueryWrapperX<ProductCommentDO> queryWrapper, Integer type) {
+        // 构建好评查询语句
+        if (ObjectUtil.equal(type, ProductCommentDO.FAVOURABLE_COMMENT)) {
+            // 好评计算 (商品评分星级+服务评分星级) >= 8
+            queryWrapper.apply("(scores + benefitScores) >= 8");
+        }
+        // 构建中评查询语句
+        if (ObjectUtil.equal(type, ProductCommentDO.MEDIOCRE_COMMENT)) {
+            // 中评计算 (商品评分星级+服务评分星级) > 4 且 (商品评分星级+服务评分星级) < 8
+            queryWrapper.apply("(scores + benefitScores) > 4 and (scores + benefitScores) < 8");
+        }
+        // 构建差评查询语句
+        if (ObjectUtil.equal(type, ProductCommentDO.NEGATIVE_COMMENT)) {
+            // 差评计算 (商品评分星级+服务评分星级) <= 4
+            queryWrapper.apply("(scores + benefitScores) <= 4");
+        }
+    }
+
     default PageResult<ProductCommentDO> selectPage(AppCommentPageReqVO reqVO, Boolean visible) {
-        return selectPage(reqVO, new LambdaQueryWrapperX<ProductCommentDO>()
+        LambdaQueryWrapperX<ProductCommentDO> queryWrapper = new LambdaQueryWrapperX<ProductCommentDO>()
                 .eqIfPresent(ProductCommentDO::getSpuId, reqVO.getSpuId())
-                .eqIfPresent(ProductCommentDO::getVisible, visible)
-                .orderByDesc(ProductCommentDO::getId));
+                .eqIfPresent(ProductCommentDO::getVisible, visible);
+        // 构建评价查询语句
+        appendTabQuery(queryWrapper, reqVO.getType());
+        // 按评价时间排序最新的显示在前面
+        queryWrapper.orderByDesc(ProductCommentDO::getCreateTime);
+        return selectPage(reqVO, queryWrapper);
     }
 
     default void updateCommentVisible(Long id, Boolean visible) {
@@ -74,4 +97,13 @@ public interface ProductCommentMapper extends BaseMapperX<ProductCommentDO> {
         update(null, lambdaUpdateWrapper);
     }
 
+    default Long selectTabCount(Long spuId, Boolean visible, Integer type) {
+        LambdaQueryWrapperX<ProductCommentDO> queryWrapper = new LambdaQueryWrapperX<ProductCommentDO>()
+                .eqIfPresent(ProductCommentDO::getSpuId, spuId)
+                .eqIfPresent(ProductCommentDO::getVisible, visible);
+        // 构建评价查询语句
+        appendTabQuery(queryWrapper, type);
+        return selectCount(queryWrapper);
+    }
+
 }

+ 10 - 0
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentService.java

@@ -11,6 +11,8 @@ import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
+import java.util.Map;
+
 /**
  * 商品评论 Service 接口
  *
@@ -68,4 +70,12 @@ public interface ProductCommentService {
      */
     void additionalComment(MemberUserRespDTO user, AppCommentAdditionalReqVO createReqVO);
 
+    /**
+     * 评论页面标签数
+     *
+     * @param spuId   spu id
+     * @param visible 是否可见
+     * @return 获得商品评价分页 tab count
+     */
+    Map<String, Long> getCommentPageTabsCount(Long spuId, Boolean visible);
 }

+ 16 - 0
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentServiceImpl.java

@@ -18,6 +18,8 @@ import org.springframework.util.StringUtils;
 import org.springframework.validation.annotation.Validated;
 
 import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Objects;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -62,6 +64,20 @@ public class ProductCommentServiceImpl implements ProductCommentService {
         productCommentMapper.commentReply(replyVO, loginUserId);
     }
 
+    @Override
+    public Map<String, Long> getCommentPageTabsCount(Long spuId, Boolean visible) {
+        Map<String, Long> countMap = new HashMap<>(4);
+        // 查询商品 id = spuId 的所有评论数量
+        countMap.put("allCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.ALL));
+        // 查询商品 id = spuId 的所有好评数量
+        countMap.put("favourableCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.FAVOURABLE_COMMENT));
+        // 查询商品 id = spuId 的所有中评数量
+        countMap.put("mediocreCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.MEDIOCRE_COMMENT));
+        // 查询商品 id = spuId 的所有差评数量
+        countMap.put("negativeCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.NEGATIVE_COMMENT));
+        return countMap;
+    }
+
     @Override
     public PageResult<ProductCommentDO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible) {
         return productCommentMapper.selectPage(pageVO, visible);