From 28cdd9c014806f1f150861b657ef1704aa87ba82 Mon Sep 17 00:00:00 2001 From: lishangbu Date: Mon, 28 Dec 2020 23:29:44 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E8=8F=9C=E5=8D=95=E6=A0=91=E6=9E=84=E5=BB=BA=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=87=B3=E8=8F=9C=E5=8D=95=E6=9C=8D=E5=8A=A1=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=B1=BB=E4=B8=AD=EF=BC=8C=E4=BF=9D=E6=8C=81TreeUtil?= =?UTF-8?q?=E7=9A=84=E7=BA=AF=E5=87=80=E6=80=A7,=E4=BD=BF=E7=94=A8Spring?= =?UTF-8?q?=E6=96=AD=E8=A8=80=E5=B7=A5=E5=85=B7=E5=BF=AB=E9=80=9F=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=EF=BC=8C=E4=B8=8D=E5=86=8D=E9=80=8F=E4=BC=A0=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=99=A8=E7=9A=84R=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/{TreeUtil.java => TreeUtils.java} | 29 +----------- .../pig/admin/controller/MenuController.java | 2 +- .../pig/admin/controller/UserController.java | 2 +- .../pig/admin/service/SysMenuService.java | 3 +- .../pig/admin/service/SysUserService.java | 4 +- .../service/impl/SysDeptServiceImpl.java | 4 +- .../service/impl/SysMenuServiceImpl.java | 46 +++++++++++++++---- .../service/impl/SysUserServiceImpl.java | 10 ++-- 8 files changed, 49 insertions(+), 51 deletions(-) rename pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/util/{TreeUtil.java => TreeUtils.java} (76%) diff --git a/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/util/TreeUtil.java b/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/util/TreeUtils.java similarity index 76% rename from pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/util/TreeUtil.java rename to pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/util/TreeUtils.java index 3e2d052b..66c46224 100644 --- a/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/util/TreeUtil.java +++ b/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/util/TreeUtils.java @@ -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 buildTree(List menus, int root) { - List 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); - } - } diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/MenuController.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/MenuController.java index 51a95cb0..76c94730 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/MenuController.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/MenuController.java @@ -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)); } /** diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/UserController.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/UserController.java index 3f6113aa..a08ee866 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/UserController.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/UserController.java @@ -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)); } /** diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysMenuService.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysMenuService.java index 2e5fac77..50027d79 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysMenuService.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysMenuService.java @@ -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 { * @param id 菜单ID * @return true成功,false失败 */ - R removeMenuById(Integer id); + Boolean removeMenuById(Integer id); /** * 更新菜单信息 diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysUserService.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysUserService.java index a9a482ca..3021f07f 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysUserService.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysUserService.java @@ -58,9 +58,9 @@ public interface SysUserService extends IService { /** * 更新当前用户基本信息 * @param userDto 用户信息 - * @return Boolean + * @return Boolean 操作成功返回true,操作失败返回false */ - R updateUserInfo(UserDTO userDto); + Boolean updateUserInfo(UserDTO userDto); /** * 更新指定用户信息 diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysDeptServiceImpl.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysDeptServiceImpl.java index 32ced0d6..4e963ccb 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysDeptServiceImpl.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysDeptServiceImpl.java @@ -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 impl node.setName(dept.getName()); return node; }).collect(Collectors.toList()); - return TreeUtil.build(treeList, 0); + return TreeUtils.build(treeList, 0); } } diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysMenuServiceImpl.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysMenuServiceImpl.java index c1c9e87b..7fb79820 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysMenuServiceImpl.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysMenuServiceImpl.java @@ -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 impl @Override @Transactional(rollbackFor = Exception.class) @CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true) - public R removeMenuById(Integer id) { + public Boolean removeMenuById(Integer id) { // 查询父节点为当前节点的节点 List menuList = this.list(Wrappers.query().lambda().eq(SysMenu::getParentId, id)); - if (CollUtil.isNotEmpty(menuList)) { - return R.failed("菜单含有下级不能删除"); - } + Assert.isTrue(CollUtil.isEmpty(menuList),"菜单含有下级不能删除"); sysRoleMenuMapper.delete(Wrappers.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 impl @Override public List treeMenu(boolean lazy, Integer parentId) { if (!lazy) { - return TreeUtil.buildTree( + return buildTree( baseMapper.selectList(Wrappers.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.lambdaQuery().eq(SysMenu::getParentId, parent).orderByAsc(SysMenu::getSort)), parent); @@ -121,7 +121,35 @@ public class SysMenuServiceImpl extends ServiceImpl impl List 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 buildTree(List menus, int root) { + List 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); } } diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysUserServiceImpl.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysUserServiceImpl.java index 887c4dfa..c59b6587 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysUserServiceImpl.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysUserServiceImpl.java @@ -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 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