portal商品展示功能邏輯
阿新 • • 發佈:2017-12-05
create clas 手機 over uav image ger true hash
看下接口:
返回值:
門戶商品搜索功能的實現:
根據分類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; }
portal商品展示功能邏輯