在 foodie-dev-pojo 模块下面的 com.imooc.pojo.vo 包内新建 CommentLevelCountsVO 类
package com.imooc.pojo.vo;
/**
* 用于展示商品评价数的 VO
* Created by 92578 on 2020/8/25 17:27
**/
public class CommentLevelCountsVO {
public Integer totalCounts;
public Integer goodCounts;
public Integer normalCounts;
public Integer badCounts;
public Integer getTotalCounts() {
return totalCounts;
}
public void setTotalCounts(Integer totalCounts) {
this.totalCounts = totalCounts;
}
public Integer getGoodCounts() {
return goodCounts;
}
public void setGoodCounts(Integer goodCounts) {
this.goodCounts = goodCounts;
}
public Integer getNormalCounts() {
return normalCounts;
}
public void setNormalCounts(Integer normalCounts) {
this.normalCounts = normalCounts;
}
public Integer getBadCounts() {
return badCounts;
}
public void setBadCounts(Integer badCounts) {
this.badCounts = badCounts;
}
}
在 foodie-dev-service 模块下面的 ItemService 接口内新建 queryCommentCounts 方法
package com.imooc.service;
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 java.util.List;
/**
* Created by 92578 on 2020/8/22 16:17
**/
public interface ItemService {
/**
* 根据商品 id 查询详情
*
* @param itemId
* @return
*/
public Items queryItemById(String itemId);
/**
* 根据商品 id 查询商品图片列表
*
* @param itemId
* @return
*/
public List<ItemsImg> queryItemImgList(String itemId);
/**
* 根据商品 id 查询商品规格
*
* @param itemId
* @return
*/
public List<ItemsSpec> queryItemSpecList(String itemId);
/**
* 根据商品 id 查询商品参数
*
* @param itemId
* @return
*/
public ItemsParam queryItemsParam(String itemId);
/**
* 根据商品 id 查询商品的评价等级数量
* @param itemId
*/
public CommentLevelCountsVO queryCommentCounts(String itemId);
}
在 foodie-dev-common 模块下面的 com.imooc.enums 包内新建 CommentLevel 枚举
package com.imooc.enums;
/**
* 商品评价等级 枚举
* Created by 92578 on 2020/8/16 20:38
**/
public enum CommentLevel {
GOOD(1, "好评"),
NORMAL(2, "中评"),
BAD(3, "差评");
public final Integer type;
public final String value;
CommentLevel(Integer type, String value) {
this.type = type;
this.value = value;
}
}
在 foodie-dev-service 模块下面的 ItemServiceImpl 类中实现 queryCommentCounts 方法
package com.imooc.service.impl;
import com.imooc.enums.CommentLevel;
import com.imooc.mapper.*;
import com.imooc.pojo.*;
import com.imooc.pojo.vo.CommentLevelCountsVO;
import com.imooc.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/**
* Created by 92578 on 2020/8/22 16:18
**/
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemsMapper itemsMapper;
@Autowired
private ItemsImgMapper itemsImgMapper;
@Autowired
private ItemsSpecMapper itemsSpecMapper;
@Autowired
private ItemsParamMapper itemsParamMapper;
@Autowired
private ItemsCommentsMapper itemsCommentsMapper;
/**
* 根据商品 id 查询详情
*
* @param itemId
* @return
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public Items queryItemById(String itemId) {
return itemsMapper.selectByPrimaryKey(itemId);
}
/**
* 根据商品 id 查询商品图片列表
*
* @param itemId
* @return
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<ItemsImg> queryItemImgList(String itemId) {
Example itemsImgExp = new Example(ItemsImg.class);
Example.Criteria criteria = itemsImgExp.createCriteria();
criteria.andEqualTo("itemId", itemId);
return itemsImgMapper.selectByExample(itemsImgExp);
}
/**
* 根据商品 id 查询商品规格
*
* @param itemId
* @return
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<ItemsSpec> queryItemSpecList(String itemId) {
Example itemsSpecExp = new Example(ItemsSpec.class);
Example.Criteria criteria = itemsSpecExp.createCriteria();
criteria.andEqualTo("itemId", itemId);
return itemsSpecMapper.selectByExample(itemsSpecExp);
}
/**
* 根据商品 id 查询商品参数
*
* @param itemId
* @return
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public ItemsParam queryItemsParam(String itemId) {
Example itemsParamExp = new Example(ItemsParam.class);
Example.Criteria criteria = itemsParamExp.createCriteria();
criteria.andEqualTo("itemId", itemId);
return itemsParamMapper.selectOneByExample(itemsParamExp);
}
/**
* 根据商品 id 查询商品的评价等级数量
*
* @param itemId
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public CommentLevelCountsVO queryCommentCounts(String itemId) {
Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
Integer totalCounts = goodCounts + normalCounts + badCounts;
CommentLevelCountsVO countsVO = new CommentLevelCountsVO();
countsVO.setTotalCounts(totalCounts);
countsVO.setGoodCounts(goodCounts);
countsVO.setNormalCounts(normalCounts);
countsVO.setBadCounts(badCounts);
return countsVO;
}
@Transactional(propagation = Propagation.SUPPORTS)
Integer getCommentCounts(String itemId, Integer level) {
ItemsComments condition = new ItemsComments();
condition.setItemId(itemId);
if (level != null) {
condition.setCommentLevel(level);
}
return itemsCommentsMapper.selectCount(condition);
}
}
在 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”,点击“发送”并返回响应内容
访问 http://localhost:8080/foodie-shop/item.html?itemId=cake-1001
找到“全部评价”,评价数量已经正确显示