千峰商城-springboot项目搭建-70-商品评论统计接口实现
评价统计接口实现:
1.数据库实现
统计当前商品的总记录数
统计当前商品的好评、中评、差评
2.业务层实现
ProductCommentsService :
public interface ProductCommentsService { //根据商品id实现评论的分页查询 //productId:商品id pageNum:页码 limit:每页显示条数 public ResultVO listCommentsByProductId(String productId,int pageNum,int limit); //根据商品id统计当前商品评价信息 public ResultVO getCommentsCountByProductId(String productId); }
ProductCommentsServiceImpl:
@Service public class ProductCommentsServiceImpl implements ProductCommentsService { @Autowired private ProductCommentsMapper productCommentsMapper; public ResultVO listCommentsByProductId(String productId,int pageNum,int limit) { // List<ProductCommentsVO> productCommentsVOS = productCommentsMapper.selectCommontsByProductId(productId); // // ResultVO resultVO = new ResultVO(ResStatus.OK, "success", productCommentsVOS); //分页查询 //1.根据商品id查询总记录数 Example example = new Example(ProductComments.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("productId",productId); int count = productCommentsMapper.selectCountByExample(example); //2.计算出总页数(必须确定每页显示多少条 pageSize=limit) int pageCount = count%limit==0? count/limit : count/limit+1; //3.查询当前页的数据(因为评论中需要用户信息,因此需要联表查询---自定义) int start = (pageNum-1)*limit; List<ProductCommentsVO> list = productCommentsMapper.selectCommontsByProductId(productId, start, limit); ResultVO resultVO = new ResultVO(ResStatus.OK, "success", new PageHelper<ProductCommentsVO>(count,pageCount,list)); return resultVO; } @Override public ResultVO getCommentsCountByProductId(String productId) { //1.查询当前商品评价总数 Example example = new Example(ProductComments.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("productId",productId); int total = productCommentsMapper.selectCountByExample(example); //2.查询好评评价数量 criteria.andEqualTo("cpmmType",1); int goodTotal = productCommentsMapper.selectCountByExample(example); //3.查询中评评价数量 criteria.andEqualTo("cpmmType",0); int midTotal = productCommentsMapper.selectCountByExample(example); //4.查询差评评价数量 criteria.andEqualTo("cpmmType",-1); int badTotal = productCommentsMapper.selectCountByExample(example); //5.计算好评率 double percent = Double.parseDouble(goodTotal+"")/Double.parseDouble(total+""); HashMap<String,Object> map = new HashMap<>(); map.put("total",total); map.put("goodTotal",goodTotal); map.put("midTotal",midTotal); map.put("badTotal",badTotal); map.put("percent",percent); ResultVO resultVO = new ResultVO(ResStatus.OK, "success", map); return resultVO; } }
3.控制层实现:
ProductController :
@RestController @CrossOrigin @RequestMapping("/product") @Api(value = "提供商品信息相关的接口",tags = "商品管理") public class ProductController { @Autowired private ProductService productService; @Autowired private ProductCommentsService productCommentsService; @ApiOperation("商品基本信息查询接口") @GetMapping("/detail-info/{pid}") public ResultVO getProductBasicInfo(@PathVariable("pid") String pid){ return productService.getProductBasicInfo(pid); } @ApiOperation("商品参数信息查询接口") @GetMapping("/detail-params/{pid}") public ResultVO getProductParams(@PathVariable("pid") String pid){ return productService.getProductParamsById(pid); } @ApiOperation("商品评论信息查询接口") @GetMapping("/detail-commonts/{pid}") @ApiImplicitParams({ @ApiImplicitParam(dataType = "int",name = "pageNum",value = "当前页码",required = true), @ApiImplicitParam(dataType = "int",name = "limit",value = "每页显示条数",required = true) }) public ResultVO getProductCommonts(@PathVariable("pid") String pid,int pageNum,int limit){ return productCommentsService.listCommentsByProductId(pid,pageNum,limit); } @ApiOperation("商品评价统计查询接口") @GetMapping("/detail-commontscount/{pid}") public ResultVO getProductCommentsCount(@PathVariable("pid") String pid){ return productCommentsService.getCommentsCountByProductId(pid); } }
4.好评率和好评数、中评数、差评数量:
ProductCommentsServiceImpl :
@Service public class ProductCommentsServiceImpl implements ProductCommentsService { @Autowired private ProductCommentsMapper productCommentsMapper; public ResultVO listCommentsByProductId(String productId,int pageNum,int limit) { // List<ProductCommentsVO> productCommentsVOS = productCommentsMapper.selectCommontsByProductId(productId); // // ResultVO resultVO = new ResultVO(ResStatus.OK, "success", productCommentsVOS); //分页查询 //1.根据商品id查询总记录数 Example example = new Example(ProductComments.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("productId",productId); int count = productCommentsMapper.selectCountByExample(example); //2.计算出总页数(必须确定每页显示多少条 pageSize=limit) int pageCount = count%limit==0? count/limit : count/limit+1; //3.查询当前页的数据(因为评论中需要用户信息,因此需要联表查询---自定义) int start = (pageNum-1)*limit; List<ProductCommentsVO> list = productCommentsMapper.selectCommontsByProductId(productId, start, limit); ResultVO resultVO = new ResultVO(ResStatus.OK, "success", new PageHelper<ProductCommentsVO>(count,pageCount,list)); return resultVO; } @Override public ResultVO getCommentsCountByProductId(String productId) { //1.查询当前商品评价总数 Example example = new Example(ProductComments.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("productId",productId); int total = productCommentsMapper.selectCountByExample(example); //2.查询好评评价数量 criteria.andEqualTo("commType",1); int goodTotal = productCommentsMapper.selectCountByExample(example); //3.查询中评评价数量 Example example1 = new Example(ProductComments.class); Example.Criteria criteria1 = example1.createCriteria(); criteria1.andEqualTo("productId",productId); criteria1.andEqualTo("commType",0); int midTotal = productCommentsMapper.selectCountByExample(example1); //4.查询差评评价数量 Example example2 = new Example(ProductComments.class); Example.Criteria criteria2 = example2.createCriteria(); criteria2.andEqualTo("productId",productId); criteria2.andEqualTo("commType",-1); int badTotal = productCommentsMapper.selectCountByExample(example2); //5.计算好评率 double percent =( Double.parseDouble(goodTotal+"")/Double.parseDouble(total+"") )*100; String percentValue = (percent+"").substring(0,(percent+"").lastIndexOf(".")+3); HashMap<String,Object> map = new HashMap<>(); map.put("total",total); map.put("goodTotal",goodTotal); map.put("midTotal",midTotal); map.put("badTotal",badTotal); map.put("percent",percentValue); ResultVO resultVO = new ResultVO(ResStatus.OK, "success", map); return resultVO; } }

分类:
千峰商城项目
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性