【电商项目】---商品评价

红太狼 2022-12-22 06:27 381阅读 0赞

前言

商品评价


商品评价

在这里插入图片描述

代码编写

  • service层

    /**

    1. * 根据商品id查询商品的评价等级数量
    2. * @param itemId
    3. */
    4. public CommentLevelCountsVO queryCommentCounts(String itemId);
  • impl

    @Transactional(propagation = Propagation.SUPPORTS)

    1. @Override
    2. public CommentLevelCountsVO queryCommentCounts(String itemId) {
    3. Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
    4. Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
    5. Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
    6. Integer totalCounts = goodCounts + normalCounts + badCounts;
    7. CommentLevelCountsVO countsVO = new CommentLevelCountsVO();
    8. countsVO.setTotalCounts(totalCounts);
    9. countsVO.setGoodCounts(goodCounts);
    10. countsVO.setNormalCounts(normalCounts);
    11. countsVO.setBadCounts(badCounts);
    12. return countsVO;
    13. }

    @Transactional(propagation = Propagation.SUPPORTS)

    1. Integer getCommentCounts(String itemId, Integer level) {
    2. ItemsComments condition = new ItemsComments();
    3. condition.setItemId(itemId);
    4. if (level != null) {
    5. condition.setCommentLevel(level);
    6. }
    7. return itemsCommentsMapper.selectCount(condition);
    8. }
  • pojo

    /**

    • 用于展示商品评价数量的vo
      */
      public class CommentLevelCountsVO {

      public Integer totalCounts;
      public Integer goodCounts;
      public Integer normalCounts;
      public Integer badCounts;

      public Integer getTotalCounts() {

      1. return totalCounts;

      }

      public void setTotalCounts(Integer totalCounts) {

      1. this.totalCounts = totalCounts;

      }

      public Integer getGoodCounts() {

      1. return goodCounts;

      }

      public void setGoodCounts(Integer goodCounts) {

      1. this.goodCounts = goodCounts;

      }

      public Integer getNormalCounts() {

      1. return normalCounts;

      }

      public void setNormalCounts(Integer normalCounts) {

      1. this.normalCounts = normalCounts;

      }

      public Integer getBadCounts() {

      1. return badCounts;

      }

      public void setBadCounts(Integer badCounts) {

      1. this.badCounts = badCounts;

      }
      }

  • 枚举

    /**

    • @Desc: 商品评价等级 枚举
      */
      public enum CommentLevel {
      GOOD(1, “好评”),
      NORMAL(2, “中评”),
      BAD(3, “差评”);

      public final Integer type;
      public final String value;

      CommentLevel(Integer type, String value) {

      1. this.type = type;
      2. this.value = value;

      }
      }

    /**

    • 用于展示商品评价数量的vo
      */
      public class CommentLevelCountsVO {

      public Integer totalCounts;
      public Integer goodCounts;
      public Integer normalCounts;
      public Integer badCounts;

      public Integer getTotalCounts() {

      1. return totalCounts;

      }

      public void setTotalCounts(Integer totalCounts) {

      1. this.totalCounts = totalCounts;

      }

      public Integer getGoodCounts() {

      1. return goodCounts;

      }

      public void setGoodCounts(Integer goodCounts) {

      1. this.goodCounts = goodCounts;

      }

      public Integer getNormalCounts() {

      1. return normalCounts;

      }

      public void setNormalCounts(Integer normalCounts) {

      1. this.normalCounts = normalCounts;

      }

      public Integer getBadCounts() {

      1. return badCounts;

      }

      public void setBadCounts(Integer badCounts) {

      1. this.badCounts = badCounts;

      }
      }

  • controller

    @ApiOperation(value = “查询商品评论”, notes = “查询商品评论”, httpMethod = “GET”)

    1. @GetMapping("/comments")
    2. public ZCWJSONResult comments(
    3. @ApiParam(name = "itemId", value = "商品id", required = true)
    4. @RequestParam String itemId,
    5. @ApiParam(name = "level", value = "评价等级", required = false)
    6. @RequestParam Integer level,
    7. @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
    8. @RequestParam Integer page,
    9. @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
    10. @RequestParam Integer pageSize) {
    11. if (StringUtils.isBlank(itemId)) {
    12. return ZCWJSONResult.errorMsg(null);
    13. }
    14. if (page == null) {
    15. page = 1;
    16. }
    17. if (pageSize == null) {
    18. pageSize = COMMON_PAGE_SIZE;
    19. }
    20. PagedGridResult grid = itemService.queryPagedComments(itemId,
    21. level,
    22. page,
    23. pageSize);
    24. return ZCWJSONResult.ok(grid);
    25. }
  • 商品评价的sql语句

    SELECT

    1. ic.comment_level AS commentLevel,
    2. ic.content AS content,
    3. ic.sepc_name AS specName,
    4. ic.created_time AS createdTime,
    5. u.face AS userFace,
    6. u.nickname AS nickname

    FROM

    1. items_comments ic
    2. LEFT JOIN users u ON ic.user_id = u.id

    WHERE

    1. ic.item_id = 'cake-1001'
    2. AND ic.comment_level = 1

在这里插入图片描述

  • 编写自定义Mapper进行数据查询

    <?xml version=”1.0” encoding=”UTF-8” ?>
    <!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd“ >

    1. <select id="searchItems" parameterType="Map" resultType="com.imooc.pojo.vo.SearchItemsVO">
    2. SELECT
    3. i.id as itemId,
    4. i.item_name as itemName,
    5. i.sell_counts as sellCounts,
    6. ii.url as imgUrl,
    7. tempSpec.price_discount as price
    8. FROM
    9. items i
    10. LEFT JOIN
    11. items_img ii
    12. on
    13. i.id = ii.item_id
    14. LEFT JOIN
    15. (SELECT item_id,MIN(price_discount) as price_discount from items_spec GROUP BY item_id) tempSpec
    16. on
    17. i.id = tempSpec.item_id
    18. WHERE
    19. ii.is_main = 1
    20. <if test=" paramsMap.keywords != null and paramsMap.keywords != '' ">
    21. AND i.item_name like '%${paramsMap.keywords}%'
    22. </if>
    23. order by
    24. <choose>
    25. <when test=" paramsMap.sort == "c" ">
    26. i.sell_counts desc
    27. </when>
    28. <when test=" paramsMap.sort == "p" ">
    29. tempSpec.price_discount asc
    30. </when>
    31. <otherwise>
    32. i.item_name asc
    33. </otherwise>
    34. </choose>
    35. </select>
    36. <!-- k: 默认,代表默认排序,根据name-->
    37. <!-- c: 根据销量排序-->
    38. <!-- p: 根据价格排序-->
  1. <select id="searchItemsByThirdCat" parameterType="Map" resultType="com.imooc.pojo.vo.SearchItemsVO">
  2. SELECT
  3. i.id as itemId,
  4. i.item_name as itemName,
  5. i.sell_counts as sellCounts,
  6. ii.url as imgUrl,
  7. tempSpec.price_discount as price
  8. FROM
  9. items i
  10. LEFT JOIN
  11. items_img ii
  12. on
  13. i.id = ii.item_id
  14. LEFT JOIN
  15. (SELECT item_id,MIN(price_discount) as price_discount from items_spec GROUP BY item_id) tempSpec
  16. on
  17. i.id = tempSpec.item_id
  18. WHERE
  19. ii.is_main = 1
  20. and
  21. i.cat_id = #{paramsMap.catId}
  22. order by
  23. <choose>
  24. <when test=" paramsMap.sort == "c" ">
  25. i.sell_counts desc
  26. </when>
  27. <when test=" paramsMap.sort == "p" ">
  28. tempSpec.price_discount asc
  29. </when>
  30. <otherwise>
  31. i.item_name asc
  32. </otherwise>
  33. </choose>
  34. </select>
  35. <select id="queryItemsBySpecIds" parameterType="List" resultType="com.imooc.pojo.vo.ShopcartVO">
  36. SELECT
  37. t_items.id as itemId,
  38. t_items.item_name as itemName,
  39. t_items_img.url as itemImgUrl,
  40. t_items_spec.id as specId,
  41. t_items_spec.`name` as specName,
  42. t_items_spec.price_discount as priceDiscount,
  43. t_items_spec.price_normal as priceNormal
  44. FROM
  45. items_spec t_items_spec
  46. LEFT JOIN
  47. items t_items
  48. ON
  49. t_items.id = t_items_spec.item_id
  50. LEFT JOIN
  51. items_img t_items_img
  52. on
  53. t_items_img.item_id = t_items.id
  54. WHERE
  55. t_items_img.is_main = 1
  56. AND
  57. t_items_spec.id IN
  58. <foreach collection="paramsList" index="index" item="specId" open="(" separator="," close=")">
  59. #{specId}
  60. </foreach>
  61. </select>
  62. <update id="decreaseItemSpecStock">
  63. update
  64. items_spec
  65. set
  66. stock = stock - #{pendingCounts}
  67. where
  68. id = #{specId}
  69. and
  70. stock >= #{pendingCounts}
  71. </update>
  72. </mapper>
  73. public interface ItemsMapperCustom {
  74. public List<ItemCommentVO> queryItemComments(@Param("paramsMap") Map<String, Object> map);
  75. public List<SearchItemsVO> searchItems(@Param("paramsMap") Map<String, Object> map);
  76. public List<SearchItemsVO> searchItemsByThirdCat(@Param("paramsMap") Map<String, Object> map);
  77. public List<ShopcartVO> queryItemsBySpecIds(@Param("paramsList") List specIdsList);
  78. public int decreaseItemSpecStock(@Param("specId") String specId,
  79. @Param("pendingCounts") int pendingCounts);
  80. }
  81. /**
  82. * 用于展示商品评价的VO
  83. */
  84. public class ItemCommentVO {
  85. private Integer commentLevel;
  86. private String content;
  87. private String specName;
  88. private Date createdTime;
  89. private String userFace;
  90. private String nickname;
  91. public Integer getCommentLevel() {
  92. return commentLevel;
  93. }
  94. public void setCommentLevel(Integer commentLevel) {
  95. this.commentLevel = commentLevel;
  96. }
  97. public String getContent() {
  98. return content;
  99. }
  100. public void setContent(String content) {
  101. this.content = content;
  102. }
  103. public String getSpecName() {
  104. return specName;
  105. }
  106. public void setSpecName(String specName) {
  107. this.specName = specName;
  108. }
  109. public Date getCreatedTime() {
  110. return createdTime;
  111. }
  112. public void setCreatedTime(Date createdTime) {
  113. this.createdTime = createdTime;
  114. }
  115. public String getUserFace() {
  116. return userFace;
  117. }
  118. public void setUserFace(String userFace) {
  119. this.userFace = userFace;
  120. }
  121. public String getNickname() {
  122. return nickname;
  123. }
  124. public void setNickname(String nickname) {
  125. this.nickname = nickname;
  126. }
  127. }
  • service

    /**

    1. * 根据商品id查询商品的评价(分页)
    2. * @param itemId
    3. * @param level
    4. * @return
    5. */
    6. public PagedGridResult queryPagedComments(String itemId, Integer level,
    7. Integer page, Integer pageSize);

    @Transactional(propagation = Propagation.SUPPORTS)

    1. @Override
    2. public PagedGridResult queryPagedComments(String itemId,
    3. Integer level,
    4. Integer page,
    5. Integer pageSize) {
    6. Map<String, Object> map = new HashMap<>();
    7. map.put("itemId", itemId);
    8. map.put("level", level);
    9. // mybatis-pagehelper
    10. /**
    11. * page: 第几页
    12. * pageSize: 每页显示条数
    13. */
    14. PageHelper.startPage(page, pageSize);
    15. List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map);
    16. for (ItemCommentVO vo : list) {
    17. vo.setNickname(DesensitizationUtil.commonDisplay(vo.getNickname()));
    18. }
    19. return setterPagedGrid(list, page);
    20. }
  • 分页对象

    public class PagedGridResult {

    1. private int page; // 当前页数
    2. private int total; // 总页数
    3. private long records; // 总记录数
    4. private List<?> rows; // 每行显示的内容
    5. public int getPage() {
    6. return page;
    7. }
    8. public void setPage(int page) {
    9. this.page = page;
    10. }
    11. public int getTotal() {
    12. return total;
    13. }
    14. public void setTotal(int total) {
    15. this.total = total;
    16. }
    17. public long getRecords() {
    18. return records;
    19. }
    20. public void setRecords(long records) {
    21. this.records = records;
    22. }
    23. public List<?> getRows() {
    24. return rows;
    25. }
    26. public void setRows(List<?> rows) {
    27. this.rows = rows;
    28. }

    }

  • 需要在配置文件中,添加分页对象的一些配置

    分页插件配置

    pagehelper:
    helperDialect: mysql
    supportMethodsArguments: true

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读

    相关 商品指标分析

    商品是电商系统中最重要的业务模型,某种程度上说,电商就是围绕于商品进行的,不管在电商供应链、电商营销、还是推荐,商品都有其非常重要的地位。那么我们该如何评估商品带来的价值及反馈