Procházet zdrojové kódy

fix:测试 app 商品评论页获取评论分页接口和相关评论分类数量接口,修复测试时发现的问题

puhui999 před 2 roky
rodič
revize
8f7efbe4e2

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

@@ -9,7 +9,6 @@ import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentCreat
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentPageReqVO;
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentRespVO;
 import cn.iocoder.yudao.module.product.convert.comment.ProductCommentConvert;
-import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO;
 import cn.iocoder.yudao.module.product.service.comment.ProductCommentService;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
@@ -38,8 +37,7 @@ public class AppCommentController {
     @GetMapping("/page")
     @Operation(summary = "获得商品评价分页")
     public CommonResult<PageResult<AppCommentRespVO>> getCommentPage(@Valid AppCommentPageReqVO pageVO) {
-        PageResult<ProductCommentDO> pageResult = productCommentService.getCommentPage(pageVO, Boolean.TRUE);
-        return success(ProductCommentConvert.INSTANCE.convertPage02(pageResult));
+        return success(productCommentService.getCommentPage(pageVO, Boolean.TRUE));
     }
 
     @GetMapping("/get-count")

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

@@ -5,7 +5,6 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.ToString;
 
-import javax.validation.constraints.NotNull;
 import java.time.LocalDateTime;
 import java.util.List;
 
@@ -27,7 +26,7 @@ public class AppCommentRespVO extends AppCommentBaseVO {
     @Schema(description = "订单项编号", required = true, example = "24965")
     private Long id;
 
-    @Schema(description = "是否匿名:[0:不匿名 1:匿名]", required = true)
+    @Schema(description = "是否匿名", required = true)
     private Boolean anonymous;
 
     @Schema(description = "交易订单编号", required = true, example = "24428")
@@ -36,7 +35,7 @@ public class AppCommentRespVO extends AppCommentBaseVO {
     @Schema(description = "交易订单项编号", required = true, example = "8233")
     private Long orderItemId;
 
-    @Schema(description = "商家是否回复:[1:回复 0:未回复]", required = true)
+    @Schema(description = "商家是否回复", required = true)
     private Boolean replied;
 
     @Schema(description = "回复管理员编号", example = "22212")
@@ -60,4 +59,6 @@ public class AppCommentRespVO extends AppCommentBaseVO {
     @Schema(description = "创建时间", required = true)
     private LocalDateTime createTime;
 
+    @Schema(description = "最终评分", required = true)
+    private Integer finalScore;
 }

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

@@ -32,19 +32,47 @@ public class ProductCommentDO extends BaseDO {
      * 所有
      */
     public static final Integer ALL = 0;
+
+    /**
+     * 所有数量 key
+     */
+    public static final String ALL_COUNT = "allCount";
+
     /**
      * 好评
      */
     public static final Integer FAVOURABLE_COMMENT = 1;
+
+    /**
+     * 好评数量 key
+     */
+    public static final String FAVOURABLE_COMMENT_COUNT = "favourableCommentCount";
+
     /**
      * 中评
      */
     public static final Integer MEDIOCRE_COMMENT = 2;
+
+    /**
+     * 中评数量 key
+     */
+    public static final String MEDIOCRE_COMMENT_COUNT = "mediocreCommentCount";
+
     /**
      * 差评
      */
     public static final Integer NEGATIVE_COMMENT = 3;
 
+    /**
+     * 差评数量 key
+     */
+    public static final String NEGATIVE_COMMENT_COUNT = "negativeCommentCount";
+
+    /**
+     * 默认匿名昵称
+     */
+    public static final String ANONYMOUS_NICKNAME = "匿名用户";
+
     /**
      * 评论编号,主键自增
      */

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

@@ -39,17 +39,17 @@ public interface ProductCommentMapper extends BaseMapperX<ProductCommentDO> {
         // 构建好评查询语句
         if (ObjectUtil.equal(type, ProductCommentDO.FAVOURABLE_COMMENT)) {
             // 好评计算 (商品评分星级+服务评分星级) >= 8
-            queryWrapper.apply("(scores + benefitScores) >= 8");
+            queryWrapper.apply("(scores + benefit_scores) >= 8");
         }
         // 构建中评查询语句
         if (ObjectUtil.equal(type, ProductCommentDO.MEDIOCRE_COMMENT)) {
             // 中评计算 (商品评分星级+服务评分星级) > 4 且 (商品评分星级+服务评分星级) < 8
-            queryWrapper.apply("(scores + benefitScores) > 4 and (scores + benefitScores) < 8");
+            queryWrapper.apply("(scores + benefit_scores) > 4 and (scores + benefit_scores) < 8");
         }
         // 构建差评查询语句
         if (ObjectUtil.equal(type, ProductCommentDO.NEGATIVE_COMMENT)) {
             // 差评计算 (商品评分星级+服务评分星级) <= 4
-            queryWrapper.apply("(scores + benefitScores) <= 4");
+            queryWrapper.apply("(scores + benefit_scores) <= 4");
         }
     }
 

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

@@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommen
 import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommentUpdateVisibleReqVO;
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentAdditionalReqVO;
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentPageReqVO;
+import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentRespVO;
 import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
@@ -52,7 +53,7 @@ public interface ProductCommentService {
      * @param visible 是否可见
      * @return 商品评价分页
      */
-    PageResult<ProductCommentDO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible);
+    PageResult<AppCommentRespVO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible);
 
     /**
      * 创建商品评论

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

@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.module.product.service.comment;
 
+import cn.hutool.core.util.ObjectUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
 import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommentPageReqVO;
@@ -7,6 +8,8 @@ import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommen
 import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommentUpdateVisibleReqVO;
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentAdditionalReqVO;
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentPageReqVO;
+import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentRespVO;
+import cn.iocoder.yudao.module.product.convert.comment.ProductCommentConvert;
 import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO;
 import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
 import cn.iocoder.yudao.module.product.dal.mysql.comment.ProductCommentMapper;
@@ -18,6 +21,8 @@ import org.springframework.util.StringUtils;
 import org.springframework.validation.annotation.Validated;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
@@ -68,19 +73,30 @@ public class ProductCommentServiceImpl implements ProductCommentService {
     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));
+        countMap.put(ProductCommentDO.ALL_COUNT, productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.ALL));
         // 查询商品 id = spuId 的所有好评数量
-        countMap.put("favourableCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.FAVOURABLE_COMMENT));
+        countMap.put(ProductCommentDO.FAVOURABLE_COMMENT_COUNT, productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.FAVOURABLE_COMMENT));
         // 查询商品 id = spuId 的所有中评数量
-        countMap.put("mediocreCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.MEDIOCRE_COMMENT));
+        countMap.put(ProductCommentDO.MEDIOCRE_COMMENT_COUNT, productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.MEDIOCRE_COMMENT));
         // 查询商品 id = spuId 的所有差评数量
-        countMap.put("negativeCommentCount", productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.NEGATIVE_COMMENT));
+        countMap.put(ProductCommentDO.NEGATIVE_COMMENT_COUNT, productCommentMapper.selectTabCount(spuId, visible, ProductCommentDO.NEGATIVE_COMMENT));
         return countMap;
     }
 
     @Override
-    public PageResult<ProductCommentDO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible) {
-        return productCommentMapper.selectPage(pageVO, visible);
+    public PageResult<AppCommentRespVO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible) {
+        PageResult<AppCommentRespVO> result = ProductCommentConvert.INSTANCE.convertPage02(productCommentMapper.selectPage(pageVO, visible));
+        result.getList().forEach(item -> {
+            // 判断用户是否选择匿名
+            if (ObjectUtil.equal(item.getAnonymous(), true)) {
+                item.setUserNickname(ProductCommentDO.ANONYMOUS_NICKNAME);
+            }
+            // 计算评价最终综合评分 最终星数 = (商品评星 + 服务评星) / 2
+            BigDecimal sumScore = new BigDecimal(item.getScores() + item.getBenefitScores());
+            BigDecimal divide = sumScore.divide(BigDecimal.valueOf(2L), 0, RoundingMode.DOWN);
+            item.setFinalScore(divide.intValue());
+        });
+        return result;
     }
 
     @Override

+ 50 - 3
yudao-module-mall/yudao-module-product-biz/src/test/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentServiceImplTest.java

@@ -11,15 +11,22 @@ import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommen
 import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommentUpdateVisibleReqVO;
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentAdditionalReqVO;
 import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentPageReqVO;
+import cn.iocoder.yudao.module.product.controller.app.comment.vo.AppCommentRespVO;
 import cn.iocoder.yudao.module.product.convert.comment.ProductCommentConvert;
 import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO;
 import cn.iocoder.yudao.module.product.dal.mysql.comment.ProductCommentMapper;
 import cn.iocoder.yudao.module.product.enums.comment.ProductCommentScoresEnum;
+import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
+import cn.iocoder.yudao.module.trade.api.order.TradeOrderApi;
 import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.Lazy;
 
 import javax.annotation.Resource;
+import java.time.LocalDateTime;
 import java.util.Date;
+import java.util.Map;
 
 import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
@@ -39,8 +46,14 @@ public class ProductCommentServiceImplTest extends BaseDbUnitTest {
     private ProductCommentMapper productCommentMapper;
 
     @Resource
+    @Lazy
     private ProductCommentServiceImpl productCommentService;
 
+    @MockBean
+    private TradeOrderApi tradeOrderApi;
+    @MockBean
+    private ProductSpuService productSpuService;
+
     public String generateNo() {
         return DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomInt(100000, 999999);
     }
@@ -70,6 +83,23 @@ public class ProductCommentServiceImplTest extends BaseDbUnitTest {
             o.setScores(ProductCommentScoresEnum.FOUR.getScores());
             o.setReplied(Boolean.TRUE);
             o.setVisible(Boolean.TRUE);
+            o.setId(generateId());
+            o.setUserId(generateId());
+            o.setAnonymous(Boolean.TRUE);
+            o.setOrderId(generateId());
+            o.setOrderItemId(generateId());
+            o.setSpuId(generateId());
+            o.setSkuId(generateId());
+            o.setDescriptionScores(ProductCommentScoresEnum.FOUR.getScores());
+            o.setBenefitScores(ProductCommentScoresEnum.FOUR.getScores());
+            o.setDeliveryScores(ProductCommentScoresEnum.FOUR.getScores());
+            o.setContent("真好吃");
+            o.setReplyUserId(generateId());
+            o.setReplyContent("确实");
+            o.setReplyTime(LocalDateTime.now());
+            o.setAdditionalTime(LocalDateTime.now());
+            o.setCreateTime(LocalDateTime.now());
+            o.setUpdateTime(LocalDateTime.now());
         });
         productCommentMapper.insert(productComment);
 
@@ -77,7 +107,7 @@ public class ProductCommentServiceImplTest extends BaseDbUnitTest {
         Long spuId = productComment.getSpuId();
 
         // 测试 userNickname 不匹配
-        productCommentMapper.insert(cloneIgnoreId(productComment, o -> o.setUserNickname("王三")));
+        productCommentMapper.insert(cloneIgnoreId(productComment, o -> o.setUserNickname("王三").setScores(ProductCommentScoresEnum.ONE.getScores())));
         // 测试 orderId 不匹配
         productCommentMapper.insert(cloneIgnoreId(productComment, o -> o.setOrderId(generateId())));
         // 测试 spuId 不匹配
@@ -107,8 +137,25 @@ public class ProductCommentServiceImplTest extends BaseDbUnitTest {
         PageResult<ProductCommentDO> all = productCommentService.getCommentPage(new ProductCommentPageReqVO());
         assertEquals(8, all.getTotal());
 
-        PageResult<ProductCommentDO> visible = productCommentService.getCommentPage(new AppCommentPageReqVO(), Boolean.TRUE);
-        assertEquals(7, visible.getTotal());
+        // 测试获取所有商品分页评论数据
+        PageResult<AppCommentRespVO> result1 = productCommentService.getCommentPage(new AppCommentPageReqVO(), Boolean.TRUE);
+        assertEquals(7, result1.getTotal());
+
+        // 测试获取所有商品分页中评数据
+        PageResult<AppCommentRespVO> result2 = productCommentService.getCommentPage(new AppCommentPageReqVO().setType(ProductCommentDO.MEDIOCRE_COMMENT), Boolean.TRUE);
+        assertEquals(2, result2.getTotal());
+
+        // 测试获取指定 spuId 商品分页中评数据
+        PageResult<AppCommentRespVO> result3 = productCommentService.getCommentPage(new AppCommentPageReqVO().setSpuId(spuId).setType(ProductCommentDO.MEDIOCRE_COMMENT), Boolean.TRUE);
+        assertEquals(2, result3.getTotal());
+
+        // 测试分页 tab count
+        Map<String, Long> tabsCount = productCommentService.getCommentPageTabsCount(spuId, Boolean.TRUE);
+        assertEquals(6, tabsCount.get(ProductCommentDO.ALL_COUNT));
+        assertEquals(4, tabsCount.get(ProductCommentDO.FAVOURABLE_COMMENT_COUNT));
+        assertEquals(2, tabsCount.get(ProductCommentDO.MEDIOCRE_COMMENT_COUNT));
+        assertEquals(0, tabsCount.get(ProductCommentDO.NEGATIVE_COMMENT_COUNT));
+
     }
 
     @Test