在 foodie-dev-pojo 模块下面的 com.imooc.pojo.vo 包内新建 CommentLevelCountsVO 类

    1. package com.imooc.pojo.vo;
    2. /**
    3. * 用于展示商品评价数的 VO
    4. * Created by 92578 on 2020/8/25 17:27
    5. **/
    6. public class CommentLevelCountsVO {
    7. public Integer totalCounts;
    8. public Integer goodCounts;
    9. public Integer normalCounts;
    10. public Integer badCounts;
    11. public Integer getTotalCounts() {
    12. return totalCounts;
    13. }
    14. public void setTotalCounts(Integer totalCounts) {
    15. this.totalCounts = totalCounts;
    16. }
    17. public Integer getGoodCounts() {
    18. return goodCounts;
    19. }
    20. public void setGoodCounts(Integer goodCounts) {
    21. this.goodCounts = goodCounts;
    22. }
    23. public Integer getNormalCounts() {
    24. return normalCounts;
    25. }
    26. public void setNormalCounts(Integer normalCounts) {
    27. this.normalCounts = normalCounts;
    28. }
    29. public Integer getBadCounts() {
    30. return badCounts;
    31. }
    32. public void setBadCounts(Integer badCounts) {
    33. this.badCounts = badCounts;
    34. }
    35. }

    在 foodie-dev-service 模块下面的 ItemService 接口内新建 queryCommentCounts 方法

    1. package com.imooc.service;
    2. import com.imooc.pojo.Items;
    3. import com.imooc.pojo.ItemsImg;
    4. import com.imooc.pojo.ItemsParam;
    5. import com.imooc.pojo.ItemsSpec;
    6. import com.imooc.pojo.vo.CommentLevelCountsVO;
    7. import java.util.List;
    8. /**
    9. * Created by 92578 on 2020/8/22 16:17
    10. **/
    11. public interface ItemService {
    12. /**
    13. * 根据商品 id 查询详情
    14. *
    15. * @param itemId
    16. * @return
    17. */
    18. public Items queryItemById(String itemId);
    19. /**
    20. * 根据商品 id 查询商品图片列表
    21. *
    22. * @param itemId
    23. * @return
    24. */
    25. public List<ItemsImg> queryItemImgList(String itemId);
    26. /**
    27. * 根据商品 id 查询商品规格
    28. *
    29. * @param itemId
    30. * @return
    31. */
    32. public List<ItemsSpec> queryItemSpecList(String itemId);
    33. /**
    34. * 根据商品 id 查询商品参数
    35. *
    36. * @param itemId
    37. * @return
    38. */
    39. public ItemsParam queryItemsParam(String itemId);
    40. /**
    41. * 根据商品 id 查询商品的评价等级数量
    42. * @param itemId
    43. */
    44. public CommentLevelCountsVO queryCommentCounts(String itemId);
    45. }

    在 foodie-dev-common 模块下面的 com.imooc.enums 包内新建 CommentLevel 枚举

    1. package com.imooc.enums;
    2. /**
    3. * 商品评价等级 枚举
    4. * Created by 92578 on 2020/8/16 20:38
    5. **/
    6. public enum CommentLevel {
    7. GOOD(1, "好评"),
    8. NORMAL(2, "中评"),
    9. BAD(3, "差评");
    10. public final Integer type;
    11. public final String value;
    12. CommentLevel(Integer type, String value) {
    13. this.type = type;
    14. this.value = value;
    15. }
    16. }

    在 foodie-dev-service 模块下面的 ItemServiceImpl 类中实现 queryCommentCounts 方法

    1. package com.imooc.service.impl;
    2. import com.imooc.enums.CommentLevel;
    3. import com.imooc.mapper.*;
    4. import com.imooc.pojo.*;
    5. import com.imooc.pojo.vo.CommentLevelCountsVO;
    6. import com.imooc.service.ItemService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import org.springframework.transaction.annotation.Propagation;
    10. import org.springframework.transaction.annotation.Transactional;
    11. import tk.mybatis.mapper.entity.Example;
    12. import java.util.List;
    13. /**
    14. * Created by 92578 on 2020/8/22 16:18
    15. **/
    16. @Service
    17. public class ItemServiceImpl implements ItemService {
    18. @Autowired
    19. private ItemsMapper itemsMapper;
    20. @Autowired
    21. private ItemsImgMapper itemsImgMapper;
    22. @Autowired
    23. private ItemsSpecMapper itemsSpecMapper;
    24. @Autowired
    25. private ItemsParamMapper itemsParamMapper;
    26. @Autowired
    27. private ItemsCommentsMapper itemsCommentsMapper;
    28. /**
    29. * 根据商品 id 查询详情
    30. *
    31. * @param itemId
    32. * @return
    33. */
    34. @Transactional(propagation = Propagation.SUPPORTS)
    35. @Override
    36. public Items queryItemById(String itemId) {
    37. return itemsMapper.selectByPrimaryKey(itemId);
    38. }
    39. /**
    40. * 根据商品 id 查询商品图片列表
    41. *
    42. * @param itemId
    43. * @return
    44. */
    45. @Transactional(propagation = Propagation.SUPPORTS)
    46. @Override
    47. public List<ItemsImg> queryItemImgList(String itemId) {
    48. Example itemsImgExp = new Example(ItemsImg.class);
    49. Example.Criteria criteria = itemsImgExp.createCriteria();
    50. criteria.andEqualTo("itemId", itemId);
    51. return itemsImgMapper.selectByExample(itemsImgExp);
    52. }
    53. /**
    54. * 根据商品 id 查询商品规格
    55. *
    56. * @param itemId
    57. * @return
    58. */
    59. @Transactional(propagation = Propagation.SUPPORTS)
    60. @Override
    61. public List<ItemsSpec> queryItemSpecList(String itemId) {
    62. Example itemsSpecExp = new Example(ItemsSpec.class);
    63. Example.Criteria criteria = itemsSpecExp.createCriteria();
    64. criteria.andEqualTo("itemId", itemId);
    65. return itemsSpecMapper.selectByExample(itemsSpecExp);
    66. }
    67. /**
    68. * 根据商品 id 查询商品参数
    69. *
    70. * @param itemId
    71. * @return
    72. */
    73. @Transactional(propagation = Propagation.SUPPORTS)
    74. @Override
    75. public ItemsParam queryItemsParam(String itemId) {
    76. Example itemsParamExp = new Example(ItemsParam.class);
    77. Example.Criteria criteria = itemsParamExp.createCriteria();
    78. criteria.andEqualTo("itemId", itemId);
    79. return itemsParamMapper.selectOneByExample(itemsParamExp);
    80. }
    81. /**
    82. * 根据商品 id 查询商品的评价等级数量
    83. *
    84. * @param itemId
    85. */
    86. @Transactional(propagation = Propagation.SUPPORTS)
    87. @Override
    88. public CommentLevelCountsVO queryCommentCounts(String itemId) {
    89. Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
    90. Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
    91. Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
    92. Integer totalCounts = goodCounts + normalCounts + badCounts;
    93. CommentLevelCountsVO countsVO = new CommentLevelCountsVO();
    94. countsVO.setTotalCounts(totalCounts);
    95. countsVO.setGoodCounts(goodCounts);
    96. countsVO.setNormalCounts(normalCounts);
    97. countsVO.setBadCounts(badCounts);
    98. return countsVO;
    99. }
    100. @Transactional(propagation = Propagation.SUPPORTS)
    101. Integer getCommentCounts(String itemId, Integer level) {
    102. ItemsComments condition = new ItemsComments();
    103. condition.setItemId(itemId);
    104. if (level != null) {
    105. condition.setCommentLevel(level);
    106. }
    107. return itemsCommentsMapper.selectCount(condition);
    108. }
    109. }

    在 foodie-dev-api 模块下面的 ItemsController 类中实现 commentLevel 方法

    package com.imooc.controller;
    
    import com.imooc.pojo.Items;
    import com.imooc.pojo.ItemsImg;
    import com.imooc.pojo.ItemsParam;
    import com.imooc.pojo.ItemsSpec;
    import com.imooc.pojo.vo.CommentLevelCountsVO;
    import com.imooc.pojo.vo.ItemInfoVO;
    import com.imooc.service.ItemService;
    import com.imooc.utils.IMOOCJSONResult;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * Created by 92578 on 2020/8/22 16:30
     **/
    @Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
    @RestController
    @RequestMapping("items")
    public class ItemsController {
    
        @Autowired
        private ItemService itemService;
    
        @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
        @GetMapping("/info/{itemId}")
        public IMOOCJSONResult info(
                @ApiParam(name = "itemId", value = "商品 id", required = true)
                @PathVariable String itemId) {
            if (StringUtils.isBlank(itemId)) {
                return IMOOCJSONResult.errorMsg(null);
            }
    
            Items item = itemService.queryItemById(itemId);
            List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
            List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
            ItemsParam itemsParam = itemService.queryItemsParam(itemId);
    
            ItemInfoVO itemInfoVO = new ItemInfoVO();
            itemInfoVO.setItem(item);
            itemInfoVO.setItemImgList(itemsImgList);
            itemInfoVO.setItemSpecList(itemsSpecList);
            itemInfoVO.setItemParams(itemsParam);
    
            return IMOOCJSONResult.ok(itemInfoVO);
        }
    
        @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
        @GetMapping("/commentLevel")
        public IMOOCJSONResult commentLevel(
                @ApiParam(name = "itemId", value = "商品 id", required = true)
                @RequestParam String itemId) {
            if (StringUtils.isBlank(itemId)) {
                return IMOOCJSONResult.errorMsg(null);
            }
    
            CommentLevelCountsVO countsVO = itemService.queryCommentCounts(itemId);
    
            return IMOOCJSONResult.ok(countsVO);
        }
    }
    

    启动项目,打开浏览器,访问 http://localhost:8088/doc.html
    找到查询商品评价等级接口,点击“调试”,输入参数“cake-1001”,点击“发送”并返回响应内容
    image.png
    访问 http://localhost:8080/foodie-shop/item.html?itemId=cake-1001
    找到“全部评价”,评价数量已经正确显示
    image.png