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;
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<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;
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;
@ -89,14 +108,14 @@ public class AdminUserServiceImpl implements AdminUserService {
userMapper.insert(user);
Set<Long> postIds = user.getPostIds();
if (!org.springframework.util.CollectionUtils.isEmpty(postIds)) {
ArrayList<UserPostDO> userPostList = new ArrayList<>();
List<UserPostDO> 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();
}
@ -110,19 +129,21 @@ public class AdminUserServiceImpl implements AdminUserService {
// 更新用户
AdminUserDO updateObj = UserConvert.INSTANCE.convert(reqVO);
userMapper.updateById(updateObj);
// 更新岗位
Set<Long> 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<Long> dbPostIds = userPostMapper.selectIdList(userId);
// 计算新增和删除的岗位编号
Collection<Long> createPostIds = CollUtil.subtract(postIds, dbPostIds);
Collection<Long> 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<Long> userIdList = userPostService
.lambdaQuery()
.in(UserPostDO::getPostId, postIds)
.list()
.stream()
.map(UserPostDO::getUserId)
.distinct()
.collect(Collectors.toList());
List<Long> userIdList = userPostMapper.getUserIdByPostIds(postIds);
if (userIdList.isEmpty()) {
return Collections.emptyList();
}
@ -289,7 +303,6 @@ public class AdminUserServiceImpl implements AdminUserService {
/**
* 获得部门条件查询指定部门的子部门编号们包括自身
*
* @param deptId 部门编号
* @return 部门编号集合
*/
@ -386,7 +399,6 @@ public class AdminUserServiceImpl implements AdminUserService {
/**
* 校验旧密码
*
* @param id 用户 id
* @param oldPassword 旧密码
*/

View File

@ -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";

View File

@ -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 '公告标题',