Commit 3cd135850ec0f6d5d43027870de03a2b191388e1
1 parent
56ea65e5
新建一个关联表维护用户岗位关系
Showing
7 changed files
with
154 additions
and
6 deletions
sql/ruoyi-vue-pro.sql
| @@ -2414,6 +2414,25 @@ CREATE TABLE `pay_refund` ( | @@ -2414,6 +2414,25 @@ CREATE TABLE `pay_refund` ( | ||
| 2414 | BEGIN; | 2414 | BEGIN; |
| 2415 | COMMIT; | 2415 | COMMIT; |
| 2416 | 2416 | ||
| 2417 | + | ||
| 2418 | +-- ---------------------------- | ||
| 2419 | +-- Table structure for system_user_post | ||
| 2420 | +-- ---------------------------- | ||
| 2421 | +DROP TABLE IF EXISTS `system_user_post`; | ||
| 2422 | +CREATE TABLE `system_user_post` ( | ||
| 2423 | + `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id', | ||
| 2424 | + `user_id` bigint NOT NULL DEFAULT '0' COMMENT '用户ID', | ||
| 2425 | + `post_id` bigint NOT NULL DEFAULT '0' COMMENT '岗位ID', | ||
| 2426 | + `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者', | ||
| 2427 | + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | ||
| 2428 | + `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '更新者', | ||
| 2429 | + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', | ||
| 2430 | + `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', | ||
| 2431 | + `tenant_id` bigint NOT NULL DEFAULT '0' COMMENT '租户编号', | ||
| 2432 | + PRIMARY KEY (`id`) USING BTREE | ||
| 2433 | +) ENGINE=InnoDB AUTO_INCREMENT=112 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='部门表'; | ||
| 2434 | + | ||
| 2435 | + | ||
| 2417 | -- ---------------------------- | 2436 | -- ---------------------------- |
| 2418 | -- Table structure for system_dept | 2437 | -- Table structure for system_dept |
| 2419 | -- ---------------------------- | 2438 | -- ---------------------------- |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/dept/UserPostDO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.dal.dataobject.dept; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | ||
| 4 | +import com.baomidou.mybatisplus.annotation.DbType; | ||
| 5 | +import com.baomidou.mybatisplus.annotation.KeySequence; | ||
| 6 | +import com.baomidou.mybatisplus.annotation.TableId; | ||
| 7 | +import com.baomidou.mybatisplus.annotation.TableName; | ||
| 8 | +import lombok.Data; | ||
| 9 | +import lombok.EqualsAndHashCode; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * 用户和岗位关联 | ||
| 13 | + * | ||
| 14 | + * @author ruoyi | ||
| 15 | + */ | ||
| 16 | +@TableName("system_user_post") | ||
| 17 | +@Data | ||
| 18 | +@EqualsAndHashCode(callSuper = true) | ||
| 19 | +public class UserPostDO extends BaseDO { | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * 自增主键 | ||
| 23 | + */ | ||
| 24 | + @TableId | ||
| 25 | + private Long id; | ||
| 26 | + /** | ||
| 27 | + * 用户 ID | ||
| 28 | + */ | ||
| 29 | + private Long userId; | ||
| 30 | + /** | ||
| 31 | + * 角色 ID | ||
| 32 | + */ | ||
| 33 | + private Long postId; | ||
| 34 | + | ||
| 35 | +} |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/UserPostMapper.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.dal.mysql.dept; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | ||
| 4 | +import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; | ||
| 5 | +import org.apache.ibatis.annotations.Mapper; | ||
| 6 | + | ||
| 7 | +@Mapper | ||
| 8 | +public interface UserPostMapper extends BaseMapperX<UserPostDO> { | ||
| 9 | + | ||
| 10 | +} |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/user/AdminUserMapper.java
| @@ -61,5 +61,8 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> { | @@ -61,5 +61,8 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> { | ||
| 61 | return selectList(AdminUserDO::getDeptId, deptIds); | 61 | return selectList(AdminUserDO::getDeptId, deptIds); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | + default List<AdminUserDO> selectListByIds(List<Long> userIdList) { | ||
| 65 | + return selectList(new LambdaQueryWrapperX<AdminUserDO>().in(AdminUserDO::getId, userIdList)); | ||
| 66 | + } | ||
| 64 | } | 67 | } |
| 65 | 68 |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.service.dept; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; | ||
| 4 | +import com.baomidou.mybatisplus.extension.service.IService; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * <p> | ||
| 8 | + * 工作流的表单定义 Mapper 接口 | ||
| 9 | + * </p> | ||
| 10 | + * | ||
| 11 | + * @author anzhen | ||
| 12 | + * @since 2022-03-03 | ||
| 13 | + */ | ||
| 14 | +public interface UserPostService extends IService<UserPostDO> { | ||
| 15 | + | ||
| 16 | +} |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/UserPostServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.service.dept; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; | ||
| 4 | +import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper; | ||
| 5 | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||
| 6 | +import org.springframework.stereotype.Service; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * <p> | ||
| 10 | + * 工作流的表单定义 Mapper 接口 | ||
| 11 | + * </p> | ||
| 12 | + * | ||
| 13 | + * @author anzhen | ||
| 14 | + * @since 2022-03-03 | ||
| 15 | + */ | ||
| 16 | +@Service | ||
| 17 | +public class UserPostServiceImpl extends ServiceImpl<UserPostMapper, UserPostDO> implements UserPostService { | ||
| 18 | + | ||
| 19 | +} |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
| @@ -13,12 +13,15 @@ import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfi | @@ -13,12 +13,15 @@ import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfi | ||
| 13 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; | 13 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; |
| 14 | import cn.iocoder.yudao.module.system.convert.user.UserConvert; | 14 | import cn.iocoder.yudao.module.system.convert.user.UserConvert; |
| 15 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; | 15 | import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; |
| 16 | +import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; | ||
| 16 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | 17 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 17 | import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; | 18 | import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; |
| 18 | import cn.iocoder.yudao.module.system.service.dept.DeptService; | 19 | import cn.iocoder.yudao.module.system.service.dept.DeptService; |
| 19 | import cn.iocoder.yudao.module.system.service.dept.PostService; | 20 | import cn.iocoder.yudao.module.system.service.dept.PostService; |
| 21 | +import cn.iocoder.yudao.module.system.service.dept.UserPostService; | ||
| 20 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; | 22 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; |
| 21 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; | 23 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; |
| 24 | +import com.baomidou.mybatisplus.core.toolkit.Wrappers; | ||
| 22 | import com.google.common.annotations.VisibleForTesting; | 25 | import com.google.common.annotations.VisibleForTesting; |
| 23 | import lombok.extern.slf4j.Slf4j; | 26 | import lombok.extern.slf4j.Slf4j; |
| 24 | import org.springframework.beans.factory.annotation.Value; | 27 | import org.springframework.beans.factory.annotation.Value; |
| @@ -29,6 +32,7 @@ import org.springframework.transaction.annotation.Transactional; | @@ -29,6 +32,7 @@ import org.springframework.transaction.annotation.Transactional; | ||
| 29 | import javax.annotation.Resource; | 32 | import javax.annotation.Resource; |
| 30 | import java.io.InputStream; | 33 | import java.io.InputStream; |
| 31 | import java.util.*; | 34 | import java.util.*; |
| 35 | +import java.util.stream.Collectors; | ||
| 32 | 36 | ||
| 33 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 37 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 34 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | 38 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| @@ -60,10 +64,13 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -60,10 +64,13 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 60 | private TenantService tenantService; | 64 | private TenantService tenantService; |
| 61 | 65 | ||
| 62 | @Resource | 66 | @Resource |
| 67 | + private UserPostService userPostService; | ||
| 68 | + | ||
| 69 | + @Resource | ||
| 63 | private FileApi fileApi; | 70 | private FileApi fileApi; |
| 64 | 71 | ||
| 65 | @Override | 72 | @Override |
| 66 | - | 73 | + @Transactional(rollbackFor = Exception.class) |
| 67 | public Long createUser(UserCreateReqVO reqVO) { | 74 | public Long createUser(UserCreateReqVO reqVO) { |
| 68 | // 校验账户配合 | 75 | // 校验账户配合 |
| 69 | tenantService.handleTenantInfo(tenant -> { | 76 | tenantService.handleTenantInfo(tenant -> { |
| @@ -80,10 +87,22 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -80,10 +87,22 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 80 | user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启 | 87 | user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启 |
| 81 | user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码 | 88 | user.setPassword(passwordEncoder.encode(reqVO.getPassword())); // 加密密码 |
| 82 | userMapper.insert(user); | 89 | userMapper.insert(user); |
| 90 | + Set<Long> postIds = user.getPostIds(); | ||
| 91 | + if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { | ||
| 92 | + ArrayList<UserPostDO> userPostList = new ArrayList<>(); | ||
| 93 | + for (Long postId : postIds) { | ||
| 94 | + UserPostDO entity = new UserPostDO(); | ||
| 95 | + entity.setUserId(entity.getUserId()); | ||
| 96 | + entity.setPostId(postId); | ||
| 97 | + userPostList.add(entity); | ||
| 98 | + } | ||
| 99 | + userPostService.saveBatch(userPostList); | ||
| 100 | + } | ||
| 83 | return user.getId(); | 101 | return user.getId(); |
| 84 | } | 102 | } |
| 85 | 103 | ||
| 86 | @Override | 104 | @Override |
| 105 | + @Transactional(rollbackFor = Exception.class) | ||
| 87 | public void updateUser(UserUpdateReqVO reqVO) { | 106 | public void updateUser(UserUpdateReqVO reqVO) { |
| 88 | // 校验正确性 | 107 | // 校验正确性 |
| 89 | this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), | 108 | this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), |
| @@ -91,6 +110,19 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -91,6 +110,19 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 91 | // 更新用户 | 110 | // 更新用户 |
| 92 | AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); | 111 | AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); |
| 93 | userMapper.updateById(updateObj); | 112 | userMapper.updateById(updateObj); |
| 113 | + Set<Long> postIds = updateObj.getPostIds(); | ||
| 114 | + if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { | ||
| 115 | + for (Long postId : postIds) { | ||
| 116 | + UserPostDO entity = new UserPostDO(); | ||
| 117 | + entity.setUserId(reqVO.getId()); | ||
| 118 | + entity.setPostId(postId); | ||
| 119 | + userPostService.saveOrUpdate(entity, | ||
| 120 | + Wrappers.lambdaUpdate(UserPostDO.class) | ||
| 121 | + .eq(UserPostDO::getUserId, entity.getUserId()) | ||
| 122 | + .eq(UserPostDO::getPostId, entity.getPostId()) | ||
| 123 | + ); | ||
| 124 | + } | ||
| 125 | + } | ||
| 94 | } | 126 | } |
| 95 | 127 | ||
| 96 | @Override | 128 | @Override |
| @@ -154,6 +186,7 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -154,6 +186,7 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 154 | } | 186 | } |
| 155 | 187 | ||
| 156 | @Override | 188 | @Override |
| 189 | + @Transactional(rollbackFor = Exception.class) | ||
| 157 | public void deleteUser(Long id) { | 190 | public void deleteUser(Long id) { |
| 158 | // 校验用户存在 | 191 | // 校验用户存在 |
| 159 | this.checkUserExists(id); | 192 | this.checkUserExists(id); |
| @@ -161,6 +194,8 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -161,6 +194,8 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 161 | userMapper.deleteById(id); | 194 | userMapper.deleteById(id); |
| 162 | // 删除用户关联数据 | 195 | // 删除用户关联数据 |
| 163 | permissionService.processUserDeleted(id); | 196 | permissionService.processUserDeleted(id); |
| 197 | + | ||
| 198 | + userPostService.remove(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, id)); | ||
| 164 | } | 199 | } |
| 165 | 200 | ||
| 166 | @Override | 201 | @Override |
| @@ -191,11 +226,22 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -191,11 +226,22 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 191 | if (CollUtil.isEmpty(postIds)) { | 226 | if (CollUtil.isEmpty(postIds)) { |
| 192 | return Collections.emptyList(); | 227 | return Collections.emptyList(); |
| 193 | } | 228 | } |
| 194 | - // 过滤不符合条件的 | ||
| 195 | - // TODO 芋艿:暂时只能内存过滤。解决方案:1、新建一个关联表;2、基于 where + 函数;3、json 字段,适合 mysql 8+ 版本 | ||
| 196 | - List<AdminUserDO> users = userMapper.selectList(); | ||
| 197 | - users.removeIf(user -> !CollUtil.containsAny(user.getPostIds(), postIds)); | ||
| 198 | - return users; | 229 | + List<Long> userIdList = userPostService |
| 230 | + .lambdaQuery() | ||
| 231 | + .in(UserPostDO::getPostId, postIds) | ||
| 232 | + .list() | ||
| 233 | + .stream() | ||
| 234 | + .map(UserPostDO::getUserId) | ||
| 235 | + .distinct() | ||
| 236 | + .collect(Collectors.toList()); | ||
| 237 | + if (userIdList.isEmpty()) { | ||
| 238 | + return Collections.emptyList(); | ||
| 239 | + } | ||
| 240 | + return userMapper | ||
| 241 | + .selectListByIds(userIdList) | ||
| 242 | + .stream() | ||
| 243 | + .peek(user -> user.setPassword(null)) | ||
| 244 | + .collect(Collectors.toList()); | ||
| 199 | } | 245 | } |
| 200 | 246 | ||
| 201 | @Override | 247 | @Override |