1、关联表删除额外的service。

2、service中删除mybatis plus 的操作,技术和业务解耦。
This commit is contained in:
安贞 2022-03-31 09:11:02 +08:00
parent 3cd135850e
commit 5fc1101900
6 changed files with 107 additions and 72 deletions

View File

@ -1,10 +1,53 @@
package cn.iocoder.yudao.module.system.dal.mysql.dept; 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.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Mapper @Mapper
public interface UserPostMapper extends BaseMapperX<UserPostDO> { public interface UserPostMapper extends BaseMapperX<UserPostDO> {
default List<Long> selectIdList(Long id) {
return selectList(new LambdaQueryWrapperX<UserPostDO>()
.eq(UserPostDO::getUserId, id)
.select(UserPostDO::getPostId)
)
.stream()
.map(UserPostDO::getPostId)
.collect(Collectors.toList());
}
default void insertList(Long userId, Collection<Long> createPostIds) {
List<UserPostDO> 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<Long> deletePostIds) {
delete(new LambdaQueryWrapperX<UserPostDO>()
.eq(UserPostDO::getUserId, userId)
.in(UserPostDO::getPostId, deletePostIds));
}
default List<Long> getUserIdByPostIds(Collection<Long> postIds) {
return selectList(new LambdaQueryWrapperX<UserPostDO>()
.in(UserPostDO::getPostId, postIds))
.stream()
.map(UserPostDO::getUserId)
.distinct()
.collect(Collectors.toList());
}
} }

View File

@ -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;
/**
* <p>
* 工作流的表单定义 Mapper 接口
* </p>
*
* @author anzhen
* @since 2022-03-03
*/
public interface UserPostService extends IService<UserPostDO> {
}

View File

@ -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;
/**
* <p>
* 工作流的表单定义 Mapper 接口
* </p>
*
* @author anzhen
* @since 2022-03-03
*/
@Service
public class UserPostServiceImpl extends ServiceImpl<UserPostMapper, UserPostDO> implements UserPostService {
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.system.service.user; package cn.iocoder.yudao.module.system.service.user;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; 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.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.UserProfileUpdatePasswordReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; 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.convert.user.UserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; 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.dept.UserPostDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; 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.dal.mysql.user.AdminUserMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService; 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.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.permission.PermissionService;
import cn.iocoder.yudao.module.system.service.tenant.TenantService; import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@ -31,15 +37,28 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.InputStream; 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 java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 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 实现类 * 后台用户 Service 实现类
*
* @author 芋道源码 * @author 芋道源码
*/ */
@Service("adminUserService") @Service("adminUserService")
@ -64,7 +83,7 @@ public class AdminUserServiceImpl implements AdminUserService {
private TenantService tenantService; private TenantService tenantService;
@Resource @Resource
private UserPostService userPostService; private UserPostMapper userPostMapper;
@Resource @Resource
private FileApi fileApi; private FileApi fileApi;
@ -81,7 +100,7 @@ public class AdminUserServiceImpl implements AdminUserService {
}); });
// 校验正确性 // 校验正确性
this.checkCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), this.checkCreateOrUpdate(null, reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
reqVO.getDeptId(), reqVO.getPostIds()); reqVO.getDeptId(), reqVO.getPostIds());
// 插入用户 // 插入用户
AdminUserDO user = UserConvert.INSTANCE.convert(reqVO); AdminUserDO user = UserConvert.INSTANCE.convert(reqVO);
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启 user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
@ -89,14 +108,14 @@ public class AdminUserServiceImpl implements AdminUserService {
userMapper.insert(user); userMapper.insert(user);
Set<Long> postIds = user.getPostIds(); Set<Long> postIds = user.getPostIds();
if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) {
ArrayList<UserPostDO> userPostList = new ArrayList<>(); List<UserPostDO> userPostList = new ArrayList<>();
for (Long postId : postIds) { for (Long postId : postIds) {
UserPostDO entity = new UserPostDO(); UserPostDO entity = new UserPostDO();
entity.setUserId(entity.getUserId()); entity.setUserId(entity.getUserId());
entity.setPostId(postId); entity.setPostId(postId);
userPostList.add(entity); userPostList.add(entity);
} }
userPostService.saveBatch(userPostList); userPostMapper.insertBatch(userPostList);
} }
return user.getId(); return user.getId();
} }
@ -106,23 +125,25 @@ public class AdminUserServiceImpl implements AdminUserService {
public void updateUser(UserUpdateReqVO reqVO) { public void updateUser(UserUpdateReqVO reqVO) {
// 校验正确性 // 校验正确性
this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(), this.checkCreateOrUpdate(reqVO.getId(), reqVO.getUsername(), reqVO.getMobile(), reqVO.getEmail(),
reqVO.getDeptId(), reqVO.getPostIds()); reqVO.getDeptId(), reqVO.getPostIds());
// 更新用户 // 更新用户
AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO); AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO);
userMapper.updateById(updateObj); userMapper.updateById(updateObj);
// 更新岗位
Set<Long> postIds = updateObj.getPostIds(); Set<Long> postIds = updateObj.getPostIds();
if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) { Long userId = reqVO.getId();
for (Long postId : postIds) { List<Long> dbPostIds = userPostMapper.selectIdList(userId);
UserPostDO entity = new UserPostDO(); // 计算新增和删除的岗位编号
entity.setUserId(reqVO.getId()); Collection<Long> createPostIds = CollUtil.subtract(postIds, dbPostIds);
entity.setPostId(postId); Collection<Long> deletePostIds = CollUtil.subtract(dbPostIds, postIds);
userPostService.saveOrUpdate(entity, // 执行新增和删除对于已经授权的菜单不用做任何处理
Wrappers.lambdaUpdate(UserPostDO.class) if (!CollectionUtil.isEmpty(createPostIds)) {
.eq(UserPostDO::getUserId, entity.getUserId()) userPostMapper.insertList(userId, createPostIds);
.eq(UserPostDO::getPostId, entity.getPostId())
);
}
} }
if (!CollectionUtil.isEmpty(deletePostIds)) {
userPostMapper.deleteByUserAndPost(userId, deletePostIds);
}
} }
@Override @Override
@ -194,8 +215,8 @@ public class AdminUserServiceImpl implements AdminUserService {
userMapper.deleteById(id); userMapper.deleteById(id);
// 删除用户关联数据 // 删除用户关联数据
permissionService.processUserDeleted(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 @Override
@ -226,14 +247,7 @@ public class AdminUserServiceImpl implements AdminUserService {
if (CollUtil.isEmpty(postIds)) { if (CollUtil.isEmpty(postIds)) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<Long> userIdList = userPostService List<Long> userIdList = userPostMapper.getUserIdByPostIds(postIds);
.lambdaQuery()
.in(UserPostDO::getPostId, postIds)
.list()
.stream()
.map(UserPostDO::getUserId)
.distinct()
.collect(Collectors.toList());
if (userIdList.isEmpty()) { if (userIdList.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
@ -289,7 +303,6 @@ public class AdminUserServiceImpl implements AdminUserService {
/** /**
* 获得部门条件查询指定部门的子部门编号们包括自身 * 获得部门条件查询指定部门的子部门编号们包括自身
*
* @param deptId 部门编号 * @param deptId 部门编号
* @return 部门编号集合 * @return 部门编号集合
*/ */
@ -298,7 +311,7 @@ public class AdminUserServiceImpl implements AdminUserService {
return Collections.emptySet(); return Collections.emptySet();
} }
Set<Long> deptIds = CollectionUtils.convertSet(deptService.getDeptsByParentIdFromCache( Set<Long> deptIds = CollectionUtils.convertSet(deptService.getDeptsByParentIdFromCache(
deptId, true), DeptDO::getId); deptId, true), DeptDO::getId);
deptIds.add(deptId); // 包括自身 deptIds.add(deptId); // 包括自身
return deptIds; return deptIds;
} }
@ -386,7 +399,6 @@ public class AdminUserServiceImpl implements AdminUserService {
/** /**
* 校验旧密码 * 校验旧密码
*
* @param id 用户 id * @param id 用户 id
* @param oldPassword 旧密码 * @param oldPassword 旧密码
*/ */
@ -408,12 +420,12 @@ public class AdminUserServiceImpl implements AdminUserService {
throw exception(USER_IMPORT_LIST_IS_EMPTY); throw exception(USER_IMPORT_LIST_IS_EMPTY);
} }
UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()) UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>())
.updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); .updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build();
importUsers.forEach(importUser -> { importUsers.forEach(importUser -> {
// 校验判断是否有不符合的原因 // 校验判断是否有不符合的原因
try { try {
checkCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(), checkCreateOrUpdate(null, null, importUser.getMobile(), importUser.getEmail(),
importUser.getDeptId(), null); importUser.getDeptId(), null);
} catch (ServiceException ex) { } catch (ServiceException ex) {
respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage()); respVO.getFailureUsernames().put(importUser.getUsername(), ex.getMessage());
return; return;
@ -422,7 +434,7 @@ public class AdminUserServiceImpl implements AdminUserService {
AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername());
if (existUser == null) { if (existUser == null) {
userMapper.insert(UserConvert.INSTANCE.convert(importUser) userMapper.insert(UserConvert.INSTANCE.convert(importUser)
.setPassword(passwordEncoder.encode(userInitPassword))); // 设置默认密码 .setPassword(passwordEncoder.encode(userInitPassword))); // 设置默认密码
respVO.getCreateUsernames().add(importUser.getUsername()); respVO.getCreateUsernames().add(importUser.getUsername());
return; return;
} }

View File

@ -4,6 +4,7 @@ DELETE FROM "system_role";
DELETE FROM "system_role_menu"; DELETE FROM "system_role_menu";
DELETE FROM "system_menu"; DELETE FROM "system_menu";
DELETE FROM "system_user_role"; DELETE FROM "system_user_role";
DELETE FROM "system_user_post";
DELETE FROM "system_dict_type"; DELETE FROM "system_dict_type";
DELETE FROM "system_user_session"; DELETE FROM "system_user_session";
DELETE FROM "system_post"; DELETE FROM "system_post";

View File

@ -145,6 +145,20 @@ CREATE TABLE IF NOT EXISTS "system_post" (
PRIMARY KEY ("id") PRIMARY KEY ("id")
) COMMENT '岗位信息表'; ) 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" ( CREATE TABLE IF NOT EXISTS "system_notice" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"title" varchar(50) NOT NULL COMMENT '公告标题', "title" varchar(50) NOT NULL COMMENT '公告标题',