|
|
@@ -14,6 +14,8 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.dromara.common.core.constant.CacheNames;
|
|
|
import org.dromara.common.core.constant.SystemConstants;
|
|
|
+import org.dromara.common.core.context.PlatformContext;
|
|
|
+import org.dromara.common.core.enums.SysPlatformCode;
|
|
|
import org.dromara.common.core.exception.ServiceException;
|
|
|
import org.dromara.common.core.utils.*;
|
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
@@ -241,6 +243,7 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
public boolean checkUserNameUnique(SysUserBo user) {
|
|
|
boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysUser>()
|
|
|
.eq(SysUser::getUserName, user.getUserName())
|
|
|
+ .eq(SysUser::getPlatformCode, user.getPhonenumber())
|
|
|
.ne(ObjectUtil.isNotNull(user.getUserId()), SysUser::getUserId, user.getUserId()));
|
|
|
return !exist;
|
|
|
}
|
|
|
@@ -254,6 +257,7 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
public boolean checkPhoneUnique(SysUserBo user) {
|
|
|
boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysUser>()
|
|
|
.eq(SysUser::getPhonenumber, user.getPhonenumber())
|
|
|
+ .eq(SysUser::getPlatformCode, user.getPlatformCode())
|
|
|
.ne(ObjectUtil.isNotNull(user.getUserId()), SysUser::getUserId, user.getUserId()));
|
|
|
return !exist;
|
|
|
}
|
|
|
@@ -267,6 +271,7 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
public boolean checkEmailUnique(SysUserBo user) {
|
|
|
boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysUser>()
|
|
|
.eq(SysUser::getEmail, user.getEmail())
|
|
|
+ .eq(SysUser::getPlatformCode, user.getPlatformCode())
|
|
|
.ne(ObjectUtil.isNotNull(user.getUserId()), SysUser::getUserId, user.getUserId()));
|
|
|
return !exist;
|
|
|
}
|
|
|
@@ -311,9 +316,14 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public int insertUser(SysUserBo user) {
|
|
|
SysUser sysUser = MapstructUtils.convert(user, SysUser.class);
|
|
|
-
|
|
|
+ // 1. 校验当前是否为总控平台(main),否则不能指定 platformCode
|
|
|
+ String currentPlatform = PlatformContext.getPlatform();
|
|
|
+ String targetPlatform = currentPlatform;
|
|
|
+ if (SysPlatformCode.MAIN.getCode().equals(currentPlatform)) {
|
|
|
+ targetPlatform = user.getPlatformCode();
|
|
|
+ }
|
|
|
//在 "main" 平台上下文中执行插入,并获取返回值
|
|
|
- Integer rows = PlatformContextUtil.executeWithPlatform("main", () -> {
|
|
|
+ Integer rows = PlatformContextUtil.executeWithPlatform(targetPlatform, () -> {
|
|
|
return baseMapper.insert(sysUser); // MyBatis-Plus insert 返回 int(实际是 Integer)
|
|
|
});
|
|
|
|
|
|
@@ -323,10 +333,9 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
// 后续操作(岗位、角色)是否也需要在 "main" 平台下执行?
|
|
|
// 如果这些表也受 platform_code 控制,则同样需要包裹!
|
|
|
|
|
|
- PlatformContextUtil.runWithPlatform("main", () -> {
|
|
|
insertUserPost(user, false);
|
|
|
- insertUserRole(user, false);
|
|
|
- });
|
|
|
+ insertUserRole(user, false,targetPlatform);
|
|
|
+
|
|
|
return rows;
|
|
|
}
|
|
|
|
|
|
@@ -355,13 +364,24 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
@CacheEvict(cacheNames = CacheNames.SYS_NICKNAME, key = "#user.userId")
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public int updateUser(SysUserBo user) {
|
|
|
+ // 1. 校验当前是否为总控平台(main),否则不能指定 platformCode
|
|
|
+ String currentPlatform = PlatformContext.getPlatform();
|
|
|
+ String targetPlatform = currentPlatform;
|
|
|
+ if (SysPlatformCode.MAIN.getCode().equals(currentPlatform)) {
|
|
|
+ targetPlatform = user.getPlatformCode();
|
|
|
+ }
|
|
|
+
|
|
|
// 新增用户与角色管理
|
|
|
- insertUserRole(user, true);
|
|
|
+ insertUserRole(user, true,targetPlatform);
|
|
|
// 新增用户与岗位管理
|
|
|
insertUserPost(user, true);
|
|
|
SysUser sysUser = MapstructUtils.convert(user, SysUser.class);
|
|
|
+
|
|
|
+ //在 "main" 平台上下文中执行插入,并获取返回值
|
|
|
// 防止错误更新后导致的数据误删除
|
|
|
- int flag = baseMapper.updateById(sysUser);
|
|
|
+ Integer flag = PlatformContextUtil.executeWithPlatform(targetPlatform, () -> {
|
|
|
+ return baseMapper.updateById(sysUser); // MyBatis-Plus insert 返回 int(实际是 Integer)
|
|
|
+ });
|
|
|
if (flag < 1) {
|
|
|
throw new ServiceException("修改用户" + user.getUserName() + "信息失败");
|
|
|
}
|
|
|
@@ -376,8 +396,8 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void insertUserAuth(Long userId, Long[] roleIds) {
|
|
|
- insertUserRole(userId, roleIds, true);
|
|
|
+ public void insertUserAuth(Long userId, Long[] roleIds,String platformCode) {
|
|
|
+ insertUserRole(userId, roleIds, true,platformCode);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -449,8 +469,8 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
* @param user 用户对象
|
|
|
* @param clear 清除已存在的关联数据
|
|
|
*/
|
|
|
- private void insertUserRole(SysUserBo user, boolean clear) {
|
|
|
- this.insertUserRole(user.getUserId(), user.getRoleIds(), clear);
|
|
|
+ private void insertUserRole(SysUserBo user, boolean clear,String platformCode) {
|
|
|
+ this.insertUserRole(user.getUserId(), user.getRoleIds(), clear,platformCode);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -484,7 +504,7 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
* @param roleIds 角色组
|
|
|
* @param clear 清除已存在的关联数据
|
|
|
*/
|
|
|
- private void insertUserRole(Long userId, Long[] roleIds, boolean clear) {
|
|
|
+ private void insertUserRole(Long userId, Long[] roleIds, boolean clear,String platformCode) {
|
|
|
if (ArrayUtil.isNotEmpty(roleIds)) {
|
|
|
List<Long> roleList = new ArrayList<>(List.of(roleIds));
|
|
|
if (!LoginHelper.isSuperAdmin(userId)) {
|
|
|
@@ -505,6 +525,7 @@ public class SysUserServiceImpl implements ISysUserService {
|
|
|
SysUserRole ur = new SysUserRole();
|
|
|
ur.setUserId(userId);
|
|
|
ur.setRoleId(roleId);
|
|
|
+ ur.setPlatformCode(platformCode);
|
|
|
return ur;
|
|
|
});
|
|
|
userRoleMapper.insertBatch(list);
|