在之前的博文中,我們已經完成了用戶模塊的所有的功能,那麼在接下來的幾篇博文中,我們來完成分類管理功能模塊。
先來看一下後台的分類管理都有哪些功能點
後台品類管理其實就是對商品的一個管理,主要分為增加品類、更新品類名稱、獲取同級品類結點和獲取品類id及子節點品類
一、分類管理模塊-增加品類功能的實現
先來看Service層
// 添加品類public ServerResponse addCategory(String categoryName, Integer parentId){ if(parentId == null || StringUtils.isBlank(categoryName)){ return ServerResponse.createByErrorMessage("參數錯誤"); } Category category = new Category(); category.setName(categoryName); category.setParentId(parentId); category.setStatus(true); int rowCount = categoryMapper.insert(category); if(rowCount > 0){ return ServerResponse.createBySuceessMessage("添加品類成功"); } return ServerResponse.createByErrorMessage("添加品類失敗"); }添加品類相對來說還是比較簡單的。和之前的註冊邏輯有點相似。首先校驗前端傳過來的categoryName和parentId是否存在,如果不存在則提示參數錯誤,否則就繼續使用JavaBean的實例來增加品類。同樣的,在用JavaBean增加完之後,將結果插入到數據庫中,如果返回的生效行數大於0,則添加品類成功,否則添加品類失敗。
再來看Controller層
/** * 管理品類-增加品類* @param categoryName * @param parentId * @param session * @return */ @RequestMapping(value = "add_category.do") @ResponseBody public ServerResponse addCategory(String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId, HttpSession session) { User user = (User) session.getAttribute(Const.CURRENT_USER); if (user == null) { return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用戶未登錄,請登錄"); } // 校驗是否是管理員if (iUserService.checkAdmin(user).isSuccess()) { return iCategoryService.addCategory(categoryName, parentId); } else { return ServerResponse.createByErrorMessage("無權限操作,請登錄管理員"); } }首先有一個不同的地方在與RequestMapping的value值,只有一個接口名稱,而沒有規定接口請求的方法,是因為品類管理模塊是網站管理員進行後台管理,屬於後台模塊。針對於後台模塊,其是公司內部員工使用,不需要對外界進行公開,所以使用默認的GET方式請求就可以。
後台功能管理的通用邏輯就是首先驗證用戶是否處於登錄狀態,如果用戶處於登錄狀態,再來驗證當前登錄的是不是網站管理員,如果不是管理員,則無權進行相關的管理操作,如果是管理員,就可以進行後台的管理。在進行後台功能管理的邏輯中,一般的直接返回在Service層中方法處理結果就可以了。
在上述方法中,需要判斷用戶的登錄狀態,所以需要引入用戶服務,然後直接調用相應的方法即可。
二、分類管理模塊-更新品類名稱功能的實現
先來看Service層
// 更新品類名稱public ServerResponse updateCategoryName(String categoryName, Integer categoryId){ if(categoryId == null || StringUtils.isBlank(categoryName)){ return ServerResponse.createByErrorMessage("更新品類參數錯誤"); } Category category = new Category(); category.setId(categoryId); category.setName(categoryName); int rowCount = categoryMapper.updateByPrimaryKeySelective(category); if(rowCount > 0){ return ServerResponse.createBySuceessMessage("更新品類名稱成功"); } return ServerResponse.createByErrorMessage("更新品類名稱失敗"); }和之前的處理邏輯完全一樣,這裡不再一一贅述。
再來看Controller層
/** * 管理品類-更新品類名稱* @param categoryName * @param categoryId * @param session * @return */ @RequestMapping(value = "update_category_name") @ResponseBody public ServerResponse updateCategoryName(String categoryName, Integer categoryId, HttpSession session){ User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用戶未登錄,請登錄"); } if(iUserService.checkAdmin(user).isSuccess()){ return iCategoryService.updateCategoryName(categoryName, categoryId); }else{ return ServerResponse.createByErrorMessage("無權限操作,請登錄管理員"); } }和之前的處理邏輯完全一樣,這裡不再一一贅述。
三、分類管理模塊-獲取平級品類結點(後台商品搜索)功能的實現
Service層
// 平級查詢品類結點public ServerResponse<List<Category>> getChildrenParalleCategory(Integer categoryId){ List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); if(CollectionUtils.isEmpty(categoryList)){ logger.info("未找到當前分類的子分類"); } return ServerResponse.createBySuccess(categoryList); }處理一組商品信息,往往使用集合的方式,根據集合不同種類,其適用長青也不一樣。這裡,我用的是List集合,一是考慮到List集合方便遍歷操作,也方便管理。因為是管理商品,所以指定List集合的泛型為Category,通過categoryMapper的selectCategoryChildrenByParentId方法來進行商品id的查詢。在邏輯判斷上,使用Java中封裝好的CollectionUtils工具類,來判斷集合的返回結果是否為空,如果為空就打印一行日誌,否則將執行成功的categoryList結果返回即可。這裡的logger是餓哦們自己封裝的日誌打印工具類,關於他的用法,簡單提一下
private org.slf4j.Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
注意,這個logger使用的是slf4j包下的,不要導錯包了,然後LoggerFactory.getLogger(classs),需要傳遞一個參數,就是當前需要打印日誌的類,例如這裡的CategoryServiceImpl.class。即可在控制台看到日誌的打印結果。
Controller層
/** * 管理品類-獲取同級品類的結點* @param categoryId * @param session * @return */ @RequestMapping(value = "get_category.do") @ResponseBody public ServerResponse getChildrenParalleCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId, HttpSession session){ User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用戶未登錄,請登錄"); } if(iUserService.checkAdmin(user).isSuccess()){ return iCategoryService.getChildrenParalleCategory(categoryId); }else { return ServerResponse.createByErrorMessage("無權限操作,請登錄管理員"); } }出於實際情況的考慮,當商品數量為0時,不需要對商品品類進行管理,所以使用RequestParam註解的defaultValue="0"來規定一個參數的默認值。其餘的邏輯處理和之前的完全一樣。
四、分類管理模塊-獲取品類id及子結點功能的實現
看Service層
public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){ Set<Category> categorySet = Sets.newHashSet(); findChildCategory(categoryId, categorySet); List<Integer> categoryIdList = Lists.newArrayList(); if(categoryId != null){ for(Category categoryItem : categorySet){ categoryIdList.add(categoryItem.getId()); } } return ServerResponse.createBySuccess(categoryIdList); } // 遞歸算法,算出子節點private Set<Category> findChildCategory(Integer categoryId, Set<Category> categorySet){ Category category = categoryMapper.selectByPrimaryKey(categoryId); if(category != null){ categorySet.add(category); } // 查找子節點List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); for(Category categoryItem : categoryList){ findChildCategory(categoryItem.getId(), categorySet); } return categorySet; }主方法是selectCategoryAndChildrenById,輔助方法為findChildCategory,通過遞歸算出子節點。在輔助方法中,通過categoryId來查詢出商品的id信息,並且加入到Set集合中,再通過foreach循環來遍歷出商品的子節點,最後返回categorySet。在主方法中通過調用輔助方法,將商品的id及子節點全部查出,然後放到List集合中,再通過foreach循環遍歷出我們想要的結果,最後直接返回categoryIdList即可。
Controller層
/** * 管理品類-獲取id及子節點品類* @param categoryId * @param session * @return */ @RequestMapping(value = "get_deep_category.do") @ResponseBody public ServerResponse getCategoryAndDeepChildrenCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId, HttpSession session){ User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用戶未登錄,請登錄"); } if(iUserService.checkAdmin(user).isSuccess()){ return iCategoryService.selectCategoryAndChildrenById(categoryId); }else{ return ServerResponse.createByErrorMessage("無權限操作,請登錄管理員"); } }和之前的獲取品類同級結點的邏輯處理完全一樣,這裡就不一一贅述了。
五、補充用戶模塊
在後台品類管理模塊中,用到了校驗當前登錄的用戶是否是管理員的方法,這個是在用戶模塊中寫到的,之前我忘記寫了,所以,在這裡做一個補充。
用戶模塊的Service層
// 用戶後台-校驗是否是管理員public ServerResponse checkAdmin(User user){ if(user != null && user.getRole().intValue() == Const.Role.ROLE_ADMIN){ return ServerResponse.createBySuccess(); } return ServerResponse.createByError(); }因為是管理員相關,所以只需要在Service層中進行邏輯處理,不需要再在Controller中聲明。該方法傳入一個user對象,通過封裝好的Role接口進行權限判定,如果返回值為ADMIN,則視為管理員,直接返回成功,否則返回失敗。
寫到這裡,後台的品類管理模塊就寫完了。因為該模塊的功能接口比較少,所以用了較長的篇幅全部寫在一篇博文中,這樣也方便大家一次性就學完後台的品類管理模塊。
在接下來的博文中,繼續推進項目進度,將為大家帶來後台商品模塊的開發,希望大家跟上進度。
如果在之前的博文中你遇到了什麼問題,歡迎留言反饋,我會盡可能為大家解決問題。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。