千峰商城-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;
    }
}
复制代码

 

 

 

 

posted @   临易  阅读(112)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示