From 5717d03d01f8f052688bef873def83a3b8defc21 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 17 Nov 2024 00:47:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=AE=A1=E7=90=86=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=8F=8A=E5=88=9D=E5=A7=8B=E8=8F=9C=E5=8D=95=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.优化方法排序 2.优化前端模板 3.完善菜单 --- .../admin/system/service/RoleService.java | 16 +++--- .../admin/system/service/UserService.java | 16 +++--- .../system/service/impl/DeptServiceImpl.java | 52 +++++++++--------- .../service/impl/DictItemServiceImpl.java | 4 +- .../system/service/impl/DictServiceImpl.java | 6 +-- .../system/service/impl/MenuServiceImpl.java | 2 +- .../system/service/impl/RoleServiceImpl.java | 22 ++++---- .../service/impl/StorageServiceImpl.java | 6 +-- .../system/service/impl/UserServiceImpl.java | 53 ++++++++++++------- .../main/resources/mapper/DictItemMapper.xml | 2 +- .../resources/templates/frontend/AddModal.ftl | 2 +- .../templates/frontend/DetailDrawer.ftl | 4 +- .../resources/templates/frontend/index.ftl | 5 +- .../controller/system/RoleController.java | 14 ++--- .../db/changelog/mysql/main_data.sql | 42 ++++++++------- .../db/changelog/postgresql/main_data.sql | 42 ++++++++------- 16 files changed, 156 insertions(+), 132 deletions(-) diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java b/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java index d369e6396..4c26ea0d5 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/RoleService.java @@ -36,6 +36,14 @@ */ public interface RoleService extends BaseService, IService { + /** + * 分配角色给用户 + * + * @param id 角色 ID + * @param userIds 用户 ID 列表 + */ + void assignToUsers(Long id, List userIds); + /** * 根据用户 ID 查询权限码 * @@ -91,12 +99,4 @@ public interface RoleService extends BaseService roleNames); - - /** - * 分配角色给用户 - * - * @param id 角色 ID - * @param userIds 用户 ID 列表 - */ - void assignToUsers(Long id, List userIds); } diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/UserService.java b/continew-module-system/src/main/java/top/continew/admin/system/service/UserService.java index 09d942ae3..d99b69253 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/UserService.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/UserService.java @@ -39,14 +39,6 @@ */ public interface UserService extends BaseService, IService { - /** - * 新增 - * - * @param user 用户信息 - * @return ID - */ - Long add(UserDO user); - /** * 下载导入模板 * @@ -132,6 +124,14 @@ public interface UserService extends BaseService implements DeptService { + private final RoleDeptService roleDeptService; @Resource private UserService userService; - private final RoleDeptService roleDeptService; @Resource private DataSource dataSource; @Override - public List listChildren(Long id) { - DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(dataSource, DatabaseType.MYSQL); - return baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).list(); - } - - @Override - public List listByNames(List list) { - if (CollUtil.isEmpty(list)) { - return Collections.emptyList(); - } - return this.list(Wrappers.lambdaQuery().in(DeptDO::getName, list)); - } - - @Override - public int countByNames(List deptNames) { - if (CollUtil.isEmpty(deptNames)) { - return 0; - } - return (int)this.count(Wrappers.lambdaQuery().in(DeptDO::getName, deptNames)); - } - - @Override - protected void beforeAdd(DeptReq req) { + public void beforeAdd(DeptReq req) { String name = req.getName(); boolean isExists = this.isNameExists(name, req.getParentId(), null); CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", name); @@ -89,7 +67,7 @@ protected void beforeAdd(DeptReq req) { } @Override - protected void beforeUpdate(DeptReq req, Long id) { + public void beforeUpdate(DeptReq req, Long id) { String name = req.getName(); boolean isExists = this.isNameExists(name, req.getParentId(), id); CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", name); @@ -124,7 +102,7 @@ protected void beforeUpdate(DeptReq req, Long id) { } @Override - protected void beforeDelete(List ids) { + public void beforeDelete(List ids) { List list = baseMapper.lambdaQuery() .select(DeptDO::getName, DeptDO::getIsSystem) .in(DeptDO::getId, ids) @@ -138,6 +116,28 @@ protected void beforeDelete(List ids) { roleDeptService.deleteByDeptIds(ids); } + @Override + public List listChildren(Long id) { + DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(dataSource, DatabaseType.MYSQL); + return baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).list(); + } + + @Override + public List listByNames(List list) { + if (CollUtil.isEmpty(list)) { + return Collections.emptyList(); + } + return this.list(Wrappers.lambdaQuery().in(DeptDO::getName, list)); + } + + @Override + public int countByNames(List deptNames) { + if (CollUtil.isEmpty(deptNames)) { + return 0; + } + return (int)this.count(Wrappers.lambdaQuery().in(DeptDO::getName, deptNames)); + } + /** * 名称是否存在 * diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictItemServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictItemServiceImpl.java index 6a385c7de..d7662aebb 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictItemServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictItemServiceImpl.java @@ -55,14 +55,14 @@ public class DictItemServiceImpl extends BaseServiceImpl> ENUM_DICT_CACHE = new ConcurrentHashMap<>(); @Override - protected void beforeAdd(DictItemReq req) { + public void beforeAdd(DictItemReq req) { String value = req.getValue(); CheckUtils.throwIf(this.isValueExists(value, null, req.getDictId()), "新增失败,字典值 [{}] 已存在", value); RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK); } @Override - protected void beforeUpdate(DictItemReq req, Long id) { + public void beforeUpdate(DictItemReq req, Long id) { String value = req.getValue(); CheckUtils.throwIf(this.isValueExists(value, id, req.getDictId()), "修改失败,字典值 [{}] 已存在", value); RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK); diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictServiceImpl.java index c807c07c3..36302463e 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/DictServiceImpl.java @@ -45,7 +45,7 @@ public class DictServiceImpl extends BaseServiceImpl ids) { + public void beforeDelete(List ids) { List list = baseMapper.lambdaQuery() .select(DictDO::getName, DictDO::getIsSystem) .in(DictDO::getId, ids) diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/MenuServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/MenuServiceImpl.java index 0c4ed99fe..8b997b620 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/MenuServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/MenuServiceImpl.java @@ -105,7 +105,7 @@ public Set listPermissionByUserId(Long userId) { public List listByRoleCode(String roleCode) { List menuList = baseMapper.selectListByRoleCode(roleCode); List list = BeanUtil.copyToList(menuList, MenuResp.class); - list.forEach(this::fill); + list.forEach(super::fill); return list; } diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java index b36317280..b0e361ff4 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java @@ -106,7 +106,7 @@ public void update(RoleReq req, Long id) { } @Override - protected void beforeDelete(List ids) { + public void beforeDelete(List ids) { List list = baseMapper.lambdaQuery() .select(RoleDO::getName, RoleDO::getIsSystem) .in(RoleDO::getId, ids) @@ -122,7 +122,16 @@ protected void beforeDelete(List ids) { } @Override - protected void fill(Object obj) { + public void assignToUsers(Long id, List userIds) { + super.getById(id); + // 保存用户和角色关联 + userRoleService.assignRoleToUsers(id, userIds); + // 更新用户上下文 + this.updateUserContext(id); + } + + @Override + public void fill(Object obj) { super.fill(obj); if (obj instanceof RoleDetailResp detail) { Long roleId = detail.getId(); @@ -188,15 +197,6 @@ public int countByNames(List roleNames) { return (int)this.count(Wrappers.lambdaQuery().in(RoleDO::getName, roleNames)); } - @Override - public void assignToUsers(Long id, List userIds) { - super.getById(id); - // 保存用户和角色关联 - userRoleService.assignRoleToUsers(id, userIds); - // 更新用户上下文 - this.updateUserContext(id); - } - /** * 名称是否存在 * diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java index 8af4d1fab..11ed03ba7 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java @@ -65,7 +65,7 @@ public class StorageServiceImpl extends BaseServiceImpl ids) { + public void beforeDelete(List ids) { CheckUtils.throwIf(fileService.countByStorageIds(ids) > 0, "所选存储存在文件关联,请删除文件后重试"); List storageList = baseMapper.lambdaQuery().in(StorageDO::getId, ids).list(); storageList.forEach(s -> { diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java index aa48bdc6b..c1b14ec52 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java @@ -209,13 +209,6 @@ public void delete(List ids) { ids.forEach(onlineUserService::kickOut); } - @Override - public Long add(UserDO user) { - user.setStatus(DisEnableStatusEnum.ENABLE); - baseMapper.insert(user); - return user.getId(); - } - @Override public void downloadImportTemplate(HttpServletResponse response) throws IOException { try { @@ -360,19 +353,6 @@ public UserImportResp importUser(UserImportReq req) { return new UserImportResp(insertList.size() + updateList.size(), insertList.size(), updateList.size()); } - public void doImportUser(List insertList, List updateList, List userRoleDOList) { - if (CollUtil.isNotEmpty(insertList)) { - baseMapper.insert(insertList); - } - if (CollUtil.isNotEmpty(updateList)) { - this.updateBatchById(updateList); - userRoleService.deleteByUserIds(updateList.stream().map(UserDO::getId).toList()); - } - if (CollUtil.isNotEmpty(userRoleDOList)) { - userRoleService.saveBatch(userRoleDOList); - } - } - @Override public void resetPassword(UserPasswordResetReq req, Long id) { super.getById(id); @@ -459,6 +439,13 @@ public void updateEmail(String newEmail, String oldPassword, Long id) { baseMapper.lambdaUpdate().set(UserDO::getEmail, newEmail).eq(UserDO::getId, id).update(); } + @Override + public Long add(UserDO user) { + user.setStatus(DisEnableStatusEnum.ENABLE); + baseMapper.insert(user); + return user.getId(); + } + @Override public UserDO getByUsername(String username) { return baseMapper.selectByUsername(username); @@ -523,6 +510,26 @@ protected QueryWrapper buildQueryWrapper(UserQuery query) { .in(CollUtil.isNotEmpty(userIdList), "t1.id", userIdList); } + /** + * 导入用户 + * + * @param insertList 新增用户 + * @param updateList 修改用户 + * @param userRoleDOList 用户角色关联 + */ + private void doImportUser(List insertList, List updateList, List userRoleDOList) { + if (CollUtil.isNotEmpty(insertList)) { + baseMapper.insert(insertList); + } + if (CollUtil.isNotEmpty(updateList)) { + this.updateBatchById(updateList); + userRoleService.deleteByUserIds(updateList.stream().map(UserDO::getId).toList()); + } + if (CollUtil.isNotEmpty(userRoleDOList)) { + userRoleService.saveBatch(userRoleDOList); + } + } + /** * 判断是否跳过导入 * @@ -681,6 +688,12 @@ private boolean isPhoneExists(String phone, Long id) { return null != count && count > 0; } + /** + * 根据用户名获取用户列表 + * + * @param usernames 用户名列表 + * @return 用户列表 + */ private List listByUsernames(List usernames) { return this.list(Wrappers.lambdaQuery() .in(UserDO::getUsername, usernames) diff --git a/continew-module-system/src/main/resources/mapper/DictItemMapper.xml b/continew-module-system/src/main/resources/mapper/DictItemMapper.xml index d6d51aab5..8deee471a 100644 --- a/continew-module-system/src/main/resources/mapper/DictItemMapper.xml +++ b/continew-module-system/src/main/resources/mapper/DictItemMapper.xml @@ -2,7 +2,7 @@