From 5fc1101900cf929d1dde909254ac469610126db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E8=B4=9E?= Date: Thu, 31 Mar 2022 09:11:02 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=85=B3=E8=81=94=E8=A1=A8=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=A2=9D=E5=A4=96=E7=9A=84service=E3=80=82=202?= =?UTF-8?q?=E3=80=81service=E4=B8=AD=E5=88=A0=E9=99=A4mybatis=20plus=20?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C=EF=BC=8C=E6=8A=80=E6=9C=AF=E5=92=8C?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E8=A7=A3=E8=80=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/dal/mysql/dept/UserPostMapper.java | 43 ++++++++++ .../system/service/dept/UserPostService.java | 16 ---- .../service/dept/UserPostServiceImpl.java | 19 ---- .../service/user/AdminUserServiceImpl.java | 86 +++++++++++-------- .../src/test/resources/sql/clean.sql | 1 + .../src/test/resources/sql/create_tables.sql | 14 +++ 6 files changed, 107 insertions(+), 72 deletions(-) delete mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostService.java delete mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostServiceImpl.java diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java index 0e9345a861..a5f1fce840 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java @@ -1,10 +1,53 @@ package cn.iocoder.yudao.module.system.dal.mysql.dept; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; import org.apache.ibatis.annotations.Mapper; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + @Mapper public interface UserPostMapper extends BaseMapperX { + default List selectIdList(Long id) { + return selectList(new LambdaQueryWrapperX() + .eq(UserPostDO::getUserId, id) + .select(UserPostDO::getPostId) + ) + .stream() + .map(UserPostDO::getPostId) + .collect(Collectors.toList()); + + } + + default void insertList(Long userId, Collection createPostIds) { + List list = createPostIds + .stream() + .map(postId -> { + UserPostDO entity = new UserPostDO(); + entity.setUserId(userId); + entity.setPostId(postId); + return entity; + }) + .collect(Collectors.toList()); + insertBatch(list); + } + + default void deleteByUserAndPost(Long userId, Collection deletePostIds) { + delete(new LambdaQueryWrapperX() + .eq(UserPostDO::getUserId, userId) + .in(UserPostDO::getPostId, deletePostIds)); + } + + default List getUserIdByPostIds(Collection postIds) { + return selectList(new LambdaQueryWrapperX() + .in(UserPostDO::getPostId, postIds)) + .stream() + .map(UserPostDO::getUserId) + .distinct() + .collect(Collectors.toList()); + } } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostService.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostService.java deleted file mode 100644 index 4299c5cbe2..0000000000 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostService.java +++ /dev/null @@ -1,16 +0,0 @@ -package cn.iocoder.yudao.module.system.service.dept; - -import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; -import com.baomidou.mybatisplus.extension.service.IService; - -/** - *

- * 工作流的表单定义 Mapper 接口 - *

- * - * @author anzhen - * @since 2022-03-03 - */ -public interface UserPostService extends IService { - -} diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostServiceImpl.java deleted file mode 100644 index 5be2e17d08..0000000000 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostServiceImpl.java +++ /dev/null @@ -1,19 +0,0 @@ -package cn.iocoder.yudao.module.system.service.dept; - -import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; -import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import org.springframework.stereotype.Service; - -/** - *

- * 工作流的表单定义 Mapper 接口 - *

- * - * @author anzhen - * @since 2022-03-03 - */ -@Service -public class UserPostServiceImpl extends ServiceImpl implements UserPostService { - -} diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java index 6457e84e86..29b8426769 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.system.service.user; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; @@ -10,15 +11,20 @@ import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.module.infra.api.file.FileApi; import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; -import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; +import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserCreateReqVO; +import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserExportReqVO; +import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO; +import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO; +import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserPageReqVO; +import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserUpdateReqVO; import cn.iocoder.yudao.module.system.convert.user.UserConvert; import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; +import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper; import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; import cn.iocoder.yudao.module.system.service.dept.DeptService; import cn.iocoder.yudao.module.system.service.dept.PostService; -import cn.iocoder.yudao.module.system.service.dept.UserPostService; import cn.iocoder.yudao.module.system.service.permission.PermissionService; import cn.iocoder.yudao.module.system.service.tenant.TenantService; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -31,15 +37,28 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.io.InputStream; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_COUNT_MAX; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_EMAIL_EXISTS; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IS_DISABLE; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_MOBILE_EXISTS; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_PASSWORD_FAILED; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_USERNAME_EXISTS; /** * 后台用户 Service 实现类 - * * @author 芋道源码 */ @Service("adminUserService") @@ -64,7 +83,7 @@ public class AdminUserServiceImpl implements AdminUserService { private TenantService tenantService; @Resource - private UserPostService userPostService; + private UserPostMapper userPostMapper; @Resource private FileApi fileApi; @@ -81,7 +100,7 @@ public class AdminUserServiceImpl implements AdminUserService { }); // 校验正确性 this.checkCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), - reqVO.getDeptId(), reqVO.getPostIds()); + reqVO.getDeptId(), reqVO.getPostIds()); // 插入用户 AdminUserDO user = UserConvert.INSTANCE.convert(reqVO); user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启 @@ -89,14 +108,14 @@ public class AdminUserServiceImpl implements AdminUserService { userMapper.insert(user); Set postIds = user.getPostIds(); if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { - ArrayList userPostList = new ArrayList<>(); + List userPostList = new ArrayList<>(); for (Long postId : postIds) { UserPostDO entity = new UserPostDO(); entity.setUserId(entity.getUserId()); entity.setPostId(postId); userPostList.add(entity); } - userPostService.saveBatch(userPostList); + userPostMapper.insertBatch(userPostList); } return user.getId(); } @@ -106,23 +125,25 @@ public class AdminUserServiceImpl implements AdminUserService { public void updateUser(UserUpdateReqVO reqVO) { // 校验正确性 this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), - reqVO.getDeptId(), reqVO.getPostIds()); + reqVO.getDeptId(), reqVO.getPostIds()); // 更新用户 AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); userMapper.updateById(updateObj); + // 更新岗位 Set postIds = updateObj.getPostIds(); - if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { - for (Long postId : postIds) { - UserPostDO entity = new UserPostDO(); - entity.setUserId(reqVO.getId()); - entity.setPostId(postId); - userPostService.saveOrUpdate(entity, - Wrappers.lambdaUpdate(UserPostDO.class) - .eq(UserPostDO::getUserId, entity.getUserId()) - .eq(UserPostDO::getPostId, entity.getPostId()) - ); - } + Long userId = reqVO.getId(); + List dbPostIds = userPostMapper.selectIdList(userId); + // 计算新增和删除的岗位编号 + Collection createPostIds = CollUtil.subtract(postIds, dbPostIds); + Collection deletePostIds = CollUtil.subtract(dbPostIds, postIds); + // 执行新增和删除。对于已经授权的菜单,不用做任何处理 + if (!CollectionUtil.isEmpty(createPostIds)) { + userPostMapper.insertList(userId, createPostIds); } + if (!CollectionUtil.isEmpty(deletePostIds)) { + userPostMapper.deleteByUserAndPost(userId, deletePostIds); + } + } @Override @@ -194,8 +215,8 @@ public class AdminUserServiceImpl implements AdminUserService { userMapper.deleteById(id); // 删除用户关联数据 permissionService.processUserDeleted(id); - - userPostService.remove(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, id)); + // 删除用户岗位 + userPostMapper.delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, id)); } @Override @@ -226,14 +247,7 @@ public class AdminUserServiceImpl implements AdminUserService { if (CollUtil.isEmpty(postIds)) { return Collections.emptyList(); } - List userIdList = userPostService - .lambdaQuery() - .in(UserPostDO::getPostId, postIds) - .list() - .stream() - .map(UserPostDO::getUserId) - .distinct() - .collect(Collectors.toList()); + List userIdList = userPostMapper.getUserIdByPostIds(postIds); if (userIdList.isEmpty()) { return Collections.emptyList(); } @@ -289,7 +303,6 @@ public class AdminUserServiceImpl implements AdminUserService { /** * 获得部门条件:查询指定部门的子部门编号们,包括自身 - * * @param deptId 部门编号 * @return 部门编号集合 */ @@ -298,7 +311,7 @@ public class AdminUserServiceImpl implements AdminUserService { return Collections.emptySet(); } Set deptIds = CollectionUtils.convertSet(deptService.getDeptsByParentIdFromCache( - deptId, true), DeptDO::getId); + deptId, true), DeptDO::getId); deptIds.add(deptId); // 包括自身 return deptIds; } @@ -386,7 +399,6 @@ public class AdminUserServiceImpl implements AdminUserService { /** * 校验旧密码 - * * @param id 用户 id * @param oldPassword 旧密码 */ @@ -408,12 +420,12 @@ public class AdminUserServiceImpl implements AdminUserService { throw exception(USER_IMPORT_LIST_IS_EMPTY); } UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()) - .updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); + .updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); importUsers.forEach(importUser -> { // 校验,判断是否有不符合的原因 try { checkCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), - importUser.getDeptId(), null); + importUser.getDeptId(), null); } catch (ServiceException ex) { respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage()); return; @@ -422,7 +434,7 @@ public class AdminUserServiceImpl implements AdminUserService { AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); if (existUser == null) { userMapper.insert(UserConvert.INSTANCE.convert(importUser) - .setPassword(passwordEncoder.encode(userInitPassword))); // 设置默认密码 + .setPassword(passwordEncoder.encode(userInitPassword))); // 设置默认密码 respVO.getCreateUsernames().add(importUser.getUsername()); return; } diff --git a/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/clean.sql b/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/clean.sql index c709513b11..23e8cda253 100644 --- a/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/clean.sql +++ b/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/clean.sql @@ -4,6 +4,7 @@ DELETE FROM "system_role"; DELETE FROM "system_role_menu"; DELETE FROM "system_menu"; DELETE FROM "system_user_role"; +DELETE FROM "system_user_post"; DELETE FROM "system_dict_type"; DELETE FROM "system_user_session"; DELETE FROM "system_post"; diff --git a/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/create_tables.sql b/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/create_tables.sql index 5db3692e5a..cef6b1424f 100644 --- a/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/create_tables.sql +++ b/yudao-module-system/yudao-module-system-impl/src/test/resources/sql/create_tables.sql @@ -145,6 +145,20 @@ CREATE TABLE IF NOT EXISTS "system_post" ( PRIMARY KEY ("id") ) COMMENT '岗位信息表'; +CREATE TABLE IF NOT EXISTS `system_user_post`( + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, + "user_id" bigint DEFAULT NULL, + "post_id" bigint DEFAULT NULL, + "creator" varchar(64) DEFAULT '', + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updater" varchar(64) DEFAULT '', + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "deleted" bit NOT NULL DEFAULT FALSE, + "tenant_id" bigint not null default '0', + PRIMARY KEY (`id`) +) COMMENT ='部门表'; + + CREATE TABLE IF NOT EXISTS "system_notice" ( "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "title" varchar(50) NOT NULL COMMENT '公告标题',