♻️ Refactoring code. 调整菜单树构建代码至菜单服务实现类中,保持TreeUtil的纯净性,使用Spring断言工具快速失败,不再透传控制器的R对象

This commit is contained in:
lishangbu 2020-12-28 23:29:44 +08:00
parent 758e1271d8
commit 28cdd9c014
8 changed files with 49 additions and 51 deletions

View File

@ -36,7 +36,7 @@ import java.util.List;
* @date 2020-02-09
*/
@UtilityClass
public class TreeUtil {
public class TreeUtils {
/**
* 两层循环实现建树
@ -98,31 +98,4 @@ public class TreeUtil {
return treeNode;
}
/**
* 通过sysMenu创建树形节点
* @param menus
* @param root
* @return
*/
public List<MenuTree> buildTree(List<SysMenu> menus, int root) {
List<MenuTree> trees = new ArrayList<>();
MenuTree node;
for (SysMenu menu : menus) {
node = new MenuTree();
node.setId(menu.getMenuId());
node.setParentId(menu.getParentId());
node.setName(menu.getName());
node.setPath(menu.getPath());
node.setPermission(menu.getPermission());
node.setLabel(menu.getName());
node.setIcon(menu.getIcon());
node.setType(menu.getType());
node.setSort(menu.getSort());
node.setHasChildren(false);
node.setKeepAlive(menu.getKeepAlive());
trees.add(node);
}
return TreeUtil.build(trees, root);
}
}

View File

@ -112,7 +112,7 @@ public class MenuController {
@DeleteMapping("/{id}")
@PreAuthorize("@pms.hasPermission('sys_menu_del')")
public R removeById(@PathVariable Integer id) {
return sysMenuService.removeMenuById(id);
return R.ok(sysMenuService.removeMenuById(id));
}
/**

View File

@ -151,7 +151,7 @@ public class UserController {
@SysLog("修改个人信息")
@PutMapping("/edit")
public R updateUserInfo(@Valid @RequestBody UserDTO userDto) {
return userService.updateUserInfo(userDto);
return R.ok(userService.updateUserInfo(userDto));
}
/**

View File

@ -20,7 +20,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pig.admin.api.dto.MenuTree;
import com.pig4cloud.pig.admin.api.entity.SysMenu;
import com.pig4cloud.pig.admin.api.vo.MenuVO;
import com.pig4cloud.pig.common.core.util.R;
import java.util.List;
import java.util.Set;
@ -47,7 +46,7 @@ public interface SysMenuService extends IService<SysMenu> {
* @param id 菜单ID
* @return true成功,false失败
*/
R removeMenuById(Integer id);
Boolean removeMenuById(Integer id);
/**
* 更新菜单信息

View File

@ -58,9 +58,9 @@ public interface SysUserService extends IService<SysUser> {
/**
* 更新当前用户基本信息
* @param userDto 用户信息
* @return Boolean
* @return Boolean 操作成功返回true,操作失败返回false
*/
R updateUserInfo(UserDTO userDto);
Boolean updateUserInfo(UserDTO userDto);
/**
* 更新指定用户信息

View File

@ -22,7 +22,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.dto.DeptTree;
import com.pig4cloud.pig.admin.api.entity.SysDept;
import com.pig4cloud.pig.admin.api.entity.SysDeptRelation;
import com.pig4cloud.pig.admin.api.util.TreeUtil;
import com.pig4cloud.pig.admin.api.util.TreeUtils;
import com.pig4cloud.pig.admin.mapper.SysDeptMapper;
import com.pig4cloud.pig.admin.service.SysDeptRelationService;
import com.pig4cloud.pig.admin.service.SysDeptService;
@ -143,7 +143,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
node.setName(dept.getName());
return node;
}).collect(Collectors.toList());
return TreeUtil.build(treeList, 0);
return TreeUtils.build(treeList, 0);
}
}

View File

@ -22,7 +22,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.dto.MenuTree;
import com.pig4cloud.pig.admin.api.entity.SysMenu;
import com.pig4cloud.pig.admin.api.entity.SysRoleMenu;
import com.pig4cloud.pig.admin.api.util.TreeUtil;
import com.pig4cloud.pig.admin.api.util.TreeUtils;
import com.pig4cloud.pig.admin.api.vo.MenuVO;
import com.pig4cloud.pig.admin.mapper.SysMenuMapper;
import com.pig4cloud.pig.admin.mapper.SysRoleMenuMapper;
@ -36,7 +36,9 @@ import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
@ -70,17 +72,15 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
@Override
@Transactional(rollbackFor = Exception.class)
@CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
public R removeMenuById(Integer id) {
public Boolean removeMenuById(Integer id) {
// 查询父节点为当前节点的节点
List<SysMenu> menuList = this.list(Wrappers.<SysMenu>query().lambda().eq(SysMenu::getParentId, id));
if (CollUtil.isNotEmpty(menuList)) {
return R.failed("菜单含有下级不能删除");
}
Assert.isTrue(CollUtil.isEmpty(menuList),"菜单含有下级不能删除");
sysRoleMenuMapper.delete(Wrappers.<SysRoleMenu>query().lambda().eq(SysRoleMenu::getMenuId, id));
// 删除当前菜单及其子菜单
return R.ok(this.removeById(id));
return this.removeById(id);
}
@Override
@ -98,13 +98,13 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
@Override
public List<MenuTree> treeMenu(boolean lazy, Integer parentId) {
if (!lazy) {
return TreeUtil.buildTree(
return buildTree(
baseMapper.selectList(Wrappers.<SysMenu>lambdaQuery().orderByAsc(SysMenu::getSort)),
CommonConstants.MENU_TREE_ROOT_ID);
}
Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
return TreeUtil.buildTree(
return buildTree(
baseMapper.selectList(
Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, parent).orderByAsc(SysMenu::getSort)),
parent);
@ -121,7 +121,35 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
List<MenuTree> menuTreeList = all.stream().filter(vo -> MenuTypeEnum.LEFT_MENU.getType().equals(vo.getType()))
.map(MenuTree::new).sorted(Comparator.comparingInt(MenuTree::getSort)).collect(Collectors.toList());
Integer parent = parentId == null ? CommonConstants.MENU_TREE_ROOT_ID : parentId;
return TreeUtil.build(menuTreeList, parent);
return TreeUtils.build(menuTreeList, parent);
}
/**
* 通过sysMenu创建树形节点
* @param menus
* @param root
* @return
*/
private List<MenuTree> buildTree(List<SysMenu> menus, int root) {
List<MenuTree> trees = new ArrayList<>();
MenuTree node;
for (SysMenu menu : menus) {
node = new MenuTree();
node.setId(menu.getMenuId());
node.setParentId(menu.getParentId());
node.setName(menu.getName());
node.setPath(menu.getPath());
node.setPermission(menu.getPermission());
node.setLabel(menu.getName());
node.setIcon(menu.getIcon());
node.setType(menu.getType());
node.setSort(menu.getSort());
node.setHasChildren(false);
node.setKeepAlive(menu.getKeepAlive());
trees.add(node);
}
return TreeUtils.build(trees, root);
}
}

View File

@ -34,7 +34,6 @@ import com.pig4cloud.pig.admin.mapper.SysUserMapper;
import com.pig4cloud.pig.admin.service.*;
import com.pig4cloud.pig.common.core.constant.CacheConstants;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.util.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
@ -44,6 +43,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.time.LocalDateTime;
import java.util.HashSet;
@ -154,19 +154,17 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override
@CacheEvict(value = CacheConstants.USER_DETAILS, key = "#userDto.username")
public R updateUserInfo(UserDTO userDto) {
public Boolean updateUserInfo(UserDTO userDto) {
UserVO userVO = baseMapper.getUserVoByUsername(userDto.getUsername());
if (!ENCODER.matches(userDto.getPassword(), userVO.getPassword())) {
return R.failed("原密码错误,修改失败");
}
Assert.isTrue(ENCODER.matches(userDto.getPassword(), userVO.getPassword()),"原密码错误,修改失败");
SysUser sysUser = new SysUser();
sysUser.setPassword(ENCODER.encode(userDto.getNewpassword1()));
sysUser.setPhone(userDto.getPhone());
sysUser.setUserId(userVO.getUserId());
sysUser.setAvatar(userDto.getAvatar());
return R.ok(this.updateById(sysUser));
return this.updateById(sysUser);
}
@Override