分类管理
管理员可以增加,修改分类,在backend下新建CategoryManageController
项目结构
RequestMapping注解声明url路径,分别新建添加分类方法,更新分类名方法,查询子分类,根据categoryId递归查询方法,自动注入IUserService对象和ICategoryService对象
@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {
....
}
添加分类
UserService里要新增一个检验是否是管理员的方法
public ServerResponse checkAdminRole(User user){
if(user!=null && user.getRole()==Const.Role.ROLE_ADMIN){
return ServerResponse.createBySuccess();
}else {
return ServerResponse.createByError();
}
}
判断完身份后执行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.createBySuccess("添加品类成功");
}
return ServerResponse.createByErrorMessage("添加品类失败");
}
controller代码
@RequestMapping("add_category.do")
@ResponseBody
public ServerResponse addCategory(
HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") int parentId){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
}
//校验一下是否是管理员
if(iUserService.checkAdminRole(user).isSuccess()){
//是管理员
//增加我们处理分类的逻辑
return iCategoryService.addCategory(categoryName,parentId);
}else{
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
}
更新分类
方法类似,详见代码
查询子分类
在dao层新增一个根据父分类id查询该分类下子分类的方法返回一个List,在CategoryMapper.xml里写sql实现
<select id="selectCategoryChildrenByParentId" resultMap="BaseResultMap" parameterType="int">
select
<include refid="Base_Column_List"/>
from mmall_category
where parent_id=#{parentId}
</select>
service调用方法查询之后直接返回给controller
public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){
List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
if(CollectionUtils.isEmpty(categoryList)){
logger.info("未找到当前分类的子分类");
}
return ServerResponse.createBySuccess(categoryList);
}
@RequestMapping("get_category.do")
@ResponseBody
public ServerResponse getChildrenParallelCategory(
HttpSession session,@RequestParam(value = "categoryId" ,defaultValue = "0") Integer categoryId){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMessage(
ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
}
if(iUserService.checkAdminRole(user).isSuccess()){
//查询子节点的category信息,并且不递归,保持平级
return iCategoryService.getChildrenParallelCategory(categoryId);
}else{
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
}
递归查询分类
service里写一个递归查询,子分类为空时退出
//递归算法,算出子节点
private Set<Category> findChildCategory(Set<Category> categorySet , Integer categoryId){
Category category = categoryMapper.selectByPrimaryKey(categoryId);
if(category != null){
categorySet.add(category);
}
//查找子节点,递归算法一定要有一个退出的条件
List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
for(Category categoryItem : categoryList){
findChildCategory(categorySet,categoryItem.getId());
}
return categorySet;
}
得到所有的分类信息,调用上面的方法把查到的分类传给controller
public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){
Set<Category> categorySet = Sets.newHashSet();
findChildCategory(categorySet,categoryId);
List<Integer> categoryIdList = Lists.newArrayList();
if(categoryId != null){
for(Category categoryItem : categorySet){
categoryIdList.add(categoryItem.getId());
}
}
return ServerResponse.createBySuccess(categoryIdList);
}
其中,Sets和Lists是Guava提供的工具包,提供了一些方便的功能例如集合的互斥、 交集、 并集等
....
@RequestMapping("get_deep_category.do")
@ResponseBody
public ServerResponse getCategoryAndDeepChildrenCategory(
HttpSession session,@RequestParam(value = "categoryId" ,defaultValue = "0") Integer categoryId){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
}
if(iUserService.checkAdminRole(user).isSuccess()){
//查询当前节点的id和递归子节点的id
// 0->10000->100000
return iCategoryService.selectCategoryAndChildrenById(categoryId);
}else{
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
}
网友评论