评价统计接口实现:
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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128101.html