看下接口:

返回值:

门户商品搜索功能的实现:

根据分类id进行搜索,根据关键词进行搜索,并按照一定的顺序排序

业务逻辑:

1、查询分类是否存在。

2、如果分类存在,则递归分类,展示父类商品,子类商品,孙子类商品,递归获取商品的分类id,获取到该id下面的子类商品

3、根据关键字和分类id查询商品

 //前端显示商品列表,并按照一定的顺序排序
@Override
public ServerResponse<PageInfo> getPortalProductList(Integer categoryId, String keyword, String orderBy, Integer pageNum, Integer pageSize) {
if (StringUtils.isBlank(keyword) && categoryId == null) {
return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
}
List<Integer> categoryIdList = Lists.newArrayList();
//这里需要根据商品id来判断这个类别是否存在,如果分类不存在,则返回给前台一个空即可
if (categoryId != null) {
mmall_category category = categoryMapper.selectByPrimaryKey(categoryId);
if (category == null && StringUtils.isBlank(keyword)) {
//如果分类为空,则返回该类别为空的结果集,不报错
PageHelper.startPage(pageNum, pageSize);
List<ProductListVo> list = Lists.newArrayList();
PageInfo info = new PageInfo(list);
return ServerResponse.createBySuccess(info);
}
//商品展示的时候,当我们在搜索某一类商品的时候,它会有很多子类,比如手机类别,有华为型号的,华为型号下面又有很多子类,所以递归函数来调用 categoryIdList = categoryService.getDeepCategory(category.getId()).getData();
}
//接下来判断关键字是否为空
if (keyword != null) {
keyword = new StringBuilder().append("%").append(keyword).append("%").toString();
}
//排序处理
PageHelper.startPage(pageNum, pageSize);
/* if (StringUtils.isNotBlank(orderBy)){
//分页的排序
if (Const.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
//进行分割
String[] orderArray=orderBy.split("_");
//排序
PageHelper.orderBy(orderArray[0]+" "+orderArray[1]);
}
}*/
List<mmall_product> productList = productMapper.selectProtalProduct(StringUtils.isBlank(keyword) ? null : keyword, categoryIdList.size() == 0 ? null : categoryIdList);
List<ProductListVo> productListVoList = Lists.newArrayList();
if (!CollectionUtils.isEmpty(productList)) {
for (mmall_product product : productList) {
ProductListVo productListVo = this.productConvertVo(product);
productListVoList.add(productListVo);
}
}
PageInfo info = new PageInfo(productListVoList);
return ServerResponse.createBySuccess(info);
}

  

递归的代码:

//这里递归获取子节点,即当前节点下的所以子节点以及子节点的节点都要列出
@Override
public ServerResponse<List<Integer>> getDeepCategory(Integer categoryId) {
Set<mmall_category> categorySet= Sets.newHashSet();//这是guava缓存的技巧
//在这里进行初始化Set集合
findChildrenCategory(categorySet,categoryId);
List<Integer> list= Lists.newArrayList();
if (categoryId!=null){
for (mmall_category categoryItem:categorySet) {
list.add(categoryItem.getId());
}
}
return ServerResponse.createBySuccess(list);
}
//递归代码的实现
public Set<mmall_category> findChildrenCategory(Set<mmall_category> categorySet,Integer categoryId){
mmall_category category=mmall_categoryMapper.selectByPrimaryKey(categoryId);
if (category!=null){
categorySet.add(category);
}
//categorySet其实是用来存储这些列表数据的
//查找子节点递归函数必须有一个终止条件
List<mmall_category> categoryList=mmall_categoryMapper.selectCategoryByParentId(categoryId);
for (mmall_category categoryItem: categoryList) {
findChildrenCategory(categorySet,categoryItem.getId());
}
return categorySet;
}

  

最新文章

  1. linq to js使用汇总
  2. Redis集群(三):主从配置一
  3. Mysql5.7修改默认密码
  4. Hadoop IPC的代码结构分析
  5. 认识Swift
  6. Python中的正斜杠与反斜杠
  7. Robotium原则的实施源代码分析
  8. Restify —— 在Node.js中构建正确的REST Web服务
  9. 2017-3-10 SQL server 数据库 T--SQL语句
  10. 理解python的元类
  11. 用系统为centos6的主机,搭建PXE服务器,实现批量安装centos6,7系统
  12. ubantu安全卸载火狐浏览器
  13. 常用font-family
  14. ECharts教程
  15. (转)Spring Boot (十九):使用 Spring Boot Actuator 监控应用
  16. django中的数据库迁移
  17. HSTS 与 307 状态码
  18. ql Server 2012完全卸载方法
  19. jsp如何往js里传值
  20. Liunx一些命令

热门文章

  1. 算法笔记(c++)--01背包问题
  2. 当Kubernets遇上阿里云 -之七层负载均衡(一).
  3. Your funds transfer has been delayed
  4. C++ map 遍历
  5. 测试与优化bugbugbugbug
  6. Linux发行版本应用场景
  7. MOOK学习
  8. windows+ubuntu时间修改问题
  9. webService —— soap
  10. 使用rand替换random模块