Commit 151e58daa1497f1616ae76e0ad32948dda58a38c

Authored by puhui999
1 parent 5bfca56e

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

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.*; @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
18 18
19 import javax.annotation.Resource; 19 import javax.annotation.Resource;
20 import javax.validation.Valid; 20 import javax.validation.Valid;
  21 +import java.util.Map;
21 22
22 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; 23 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
23 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; 24 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@@ -41,6 +42,12 @@ public class AppCommentController { @@ -41,6 +42,12 @@ public class AppCommentController {
41 return success(ProductCommentConvert.INSTANCE.convertPage02(pageResult)); 42 return success(ProductCommentConvert.INSTANCE.convertPage02(pageResult));
42 } 43 }
43 44
  45 + @GetMapping("/get-count")
  46 + @Operation(summary = "获得商品评价分页 tab count")
  47 + public CommonResult<Map<String, Long>> getCommentPage(@Valid Long spuId) {
  48 + return success(productCommentService.getCommentPageTabsCount(spuId, Boolean.TRUE));
  49 + }
  50 +
44 @PostMapping(value = "/create") 51 @PostMapping(value = "/create")
45 @Operation(summary = "创建商品评价") 52 @Operation(summary = "创建商品评价")
46 public CommonResult<Boolean> createComment(@RequestBody AppCommentCreateReqVO createReqVO) { 53 public CommonResult<Boolean> createComment(@RequestBody AppCommentCreateReqVO createReqVO) {
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 { @@ -18,4 +18,8 @@ public class AppCommentPageReqVO extends PageParam {
18 @NotNull(message = "商品SPU编号不能为空") 18 @NotNull(message = "商品SPU编号不能为空")
19 private Long spuId; 19 private Long spuId;
20 20
  21 + @Schema(description = "app 评论页 tab 类型 (0 全部、1 好评、2 中评、3 差评)", example = "0")
  22 + @NotNull(message = "商品SPU编号不能为空")
  23 + private Integer type;
  24 +
21 } 25 }
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/comment/ProductCommentDO.java
@@ -29,6 +29,23 @@ import java.util.List; @@ -29,6 +29,23 @@ import java.util.List;
29 public class ProductCommentDO extends BaseDO { 29 public class ProductCommentDO extends BaseDO {
30 30
31 /** 31 /**
  32 + * 所有
  33 + */
  34 + public static final Integer ALL = 0;
  35 + /**
  36 + * 好评
  37 + */
  38 + public static final Integer FAVOURABLE_COMMENT = 1;
  39 + /**
  40 + * 中评
  41 + */
  42 + public static final Integer MEDIOCRE_COMMENT = 2;
  43 + /**
  44 + * 差评
  45 + */
  46 + public static final Integer NEGATIVE_COMMENT = 3;
  47 +
  48 + /**
32 * 评论编号,主键自增 49 * 评论编号,主键自增
33 */ 50 */
34 @TableId 51 @TableId
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/comment/ProductCommentMapper.java
1 package cn.iocoder.yudao.module.product.dal.mysql.comment; 1 package cn.iocoder.yudao.module.product.dal.mysql.comment;
2 2
3 3
  4 +import cn.hutool.core.util.ObjectUtil;
4 import cn.iocoder.yudao.framework.common.pojo.PageResult; 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; 6 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
6 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; 7 import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
@@ -34,11 +35,33 @@ public interface ProductCommentMapper extends BaseMapperX&lt;ProductCommentDO&gt; { @@ -34,11 +35,33 @@ public interface ProductCommentMapper extends BaseMapperX&lt;ProductCommentDO&gt; {
34 .orderByDesc(ProductCommentDO::getId)); 35 .orderByDesc(ProductCommentDO::getId));
35 } 36 }
36 37
  38 + static void appendTabQuery(LambdaQueryWrapperX<ProductCommentDO> queryWrapper, Integer type) {
  39 + // 构建好评查询语句
  40 + if (ObjectUtil.equal(type, ProductCommentDO.FAVOURABLE_COMMENT)) {
  41 + // 好评计算 (商品评分星级+服务评分星级) >= 8
  42 + queryWrapper.apply("(scores + benefitScores) >= 8");
  43 + }
  44 + // 构建中评查询语句
  45 + if (ObjectUtil.equal(type, ProductCommentDO.MEDIOCRE_COMMENT)) {
  46 + // 中评计算 (商品评分星级+服务评分星级) > 4 且 (商品评分星级+服务评分星级) < 8
  47 + queryWrapper.apply("(scores + benefitScores) > 4 and (scores + benefitScores) < 8");
  48 + }
  49 + // 构建差评查询语句
  50 + if (ObjectUtil.equal(type, ProductCommentDO.NEGATIVE_COMMENT)) {
  51 + // 差评计算 (商品评分星级+服务评分星级) <= 4
  52 + queryWrapper.apply("(scores + benefitScores) <= 4");
  53 + }
  54 + }
  55 +
37 default PageResult<ProductCommentDO> selectPage(AppCommentPageReqVO reqVO, Boolean visible) { 56 default PageResult<ProductCommentDO> selectPage(AppCommentPageReqVO reqVO, Boolean visible) {
38 - return selectPage(reqVO, new LambdaQueryWrapperX<ProductCommentDO>() 57 + LambdaQueryWrapperX<ProductCommentDO> queryWrapper = new LambdaQueryWrapperX<ProductCommentDO>()
39 .eqIfPresent(ProductCommentDO::getSpuId, reqVO.getSpuId()) 58 .eqIfPresent(ProductCommentDO::getSpuId, reqVO.getSpuId())
40 - .eqIfPresent(ProductCommentDO::getVisible, visible)  
41 - .orderByDesc(ProductCommentDO::getId)); 59 + .eqIfPresent(ProductCommentDO::getVisible, visible);
  60 + // 构建评价查询语句
  61 + appendTabQuery(queryWrapper, reqVO.getType());
  62 + // 按评价时间排序最新的显示在前面
  63 + queryWrapper.orderByDesc(ProductCommentDO::getCreateTime);
  64 + return selectPage(reqVO, queryWrapper);
42 } 65 }
43 66
44 default void updateCommentVisible(Long id, Boolean visible) { 67 default void updateCommentVisible(Long id, Boolean visible) {
@@ -74,4 +97,13 @@ public interface ProductCommentMapper extends BaseMapperX&lt;ProductCommentDO&gt; { @@ -74,4 +97,13 @@ public interface ProductCommentMapper extends BaseMapperX&lt;ProductCommentDO&gt; {
74 update(null, lambdaUpdateWrapper); 97 update(null, lambdaUpdateWrapper);
75 } 98 }
76 99
  100 + default Long selectTabCount(Long spuId, Boolean visible, Integer type) {
  101 + LambdaQueryWrapperX<ProductCommentDO> queryWrapper = new LambdaQueryWrapperX<ProductCommentDO>()
  102 + .eqIfPresent(ProductCommentDO::getSpuId, spuId)
  103 + .eqIfPresent(ProductCommentDO::getVisible, visible);
  104 + // 构建评价查询语句
  105 + appendTabQuery(queryWrapper, type);
  106 + return selectCount(queryWrapper);
  107 + }
  108 +
77 } 109 }
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; @@ -11,6 +11,8 @@ import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO;
11 import org.springframework.stereotype.Service; 11 import org.springframework.stereotype.Service;
12 import org.springframework.validation.annotation.Validated; 12 import org.springframework.validation.annotation.Validated;
13 13
  14 +import java.util.Map;
  15 +
14 /** 16 /**
15 * 商品评论 Service 接口 17 * 商品评论 Service 接口
16 * 18 *
@@ -68,4 +70,12 @@ public interface ProductCommentService { @@ -68,4 +70,12 @@ public interface ProductCommentService {
68 */ 70 */
69 void additionalComment(MemberUserRespDTO user, AppCommentAdditionalReqVO createReqVO); 71 void additionalComment(MemberUserRespDTO user, AppCommentAdditionalReqVO createReqVO);
70 72
  73 + /**
  74 + * 评论页面标签数
  75 + *
  76 + * @param spuId spu id
  77 + * @param visible 是否可见
  78 + * @return 获得商品评价分页 tab count
  79 + */
  80 + Map<String, Long> getCommentPageTabsCount(Long spuId, Boolean visible);
71 } 81 }
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; @@ -18,6 +18,8 @@ import org.springframework.util.StringUtils;
18 import org.springframework.validation.annotation.Validated; 18 import org.springframework.validation.annotation.Validated;
19 19
20 import javax.annotation.Resource; 20 import javax.annotation.Resource;
  21 +import java.util.HashMap;
  22 +import java.util.Map;
21 import java.util.Objects; 23 import java.util.Objects;
22 24
23 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 25 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -63,6 +65,20 @@ public class ProductCommentServiceImpl implements ProductCommentService { @@ -63,6 +65,20 @@ public class ProductCommentServiceImpl implements ProductCommentService {
63 } 65 }
64 66
65 @Override 67 @Override
  68 + public Map<String, Long> getCommentPageTabsCount(Long spuId, Boolean visible) {
  69 + Map<String, Long> countMap = new HashMap<>(4);
  70 + // 查询商品 id = spuId 的所有评论数量
  71 + countMap.put("allCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.ALL));
  72 + // 查询商品 id = spuId 的所有好评数量
  73 + countMap.put("favourableCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.FAVOURABLE_COMMENT));
  74 + // 查询商品 id = spuId 的所有中评数量
  75 + countMap.put("mediocreCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.MEDIOCRE_COMMENT));
  76 + // 查询商品 id = spuId 的所有差评数量
  77 + countMap.put("negativeCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.NEGATIVE_COMMENT));
  78 + return countMap;
  79 + }
  80 +
  81 + @Override
66 public PageResult<ProductCommentDO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible) { 82 public PageResult<ProductCommentDO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible) {
67 return productCommentMapper.selectPage(pageVO, visible); 83 return productCommentMapper.selectPage(pageVO, visible);
68 } 84 }