mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 10:35:57 +08:00
update 忽略租户写法去除使用注解的方式
This commit is contained in:
parent
b663197e45
commit
716d7e7435
@ -1,20 +0,0 @@
|
|||||||
package org.dromara.common.tenant.annotation;
|
|
||||||
|
|
||||||
import java.lang.annotation.Documented;
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Inherited;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 忽略租户注解
|
|
||||||
*
|
|
||||||
* @author yixiacoco
|
|
||||||
*/
|
|
||||||
@Inherited
|
|
||||||
@Target(ElementType.METHOD)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Documented
|
|
||||||
public @interface IgnoreTenant {
|
|
||||||
}
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
package org.dromara.common.tenant.aspect;
|
|
||||||
|
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
|
||||||
import org.aspectj.lang.annotation.Around;
|
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
|
||||||
import org.dromara.common.tenant.annotation.IgnoreTenant;
|
|
||||||
import org.dromara.common.tenant.helper.TenantHelper;
|
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 租户切面
|
|
||||||
*
|
|
||||||
* @author yixiacoco
|
|
||||||
*/
|
|
||||||
@Aspect
|
|
||||||
@Order(-1)
|
|
||||||
public class TenantAspect {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理忽略租户
|
|
||||||
*
|
|
||||||
* @param point 织入点
|
|
||||||
* @param ignoreTenant 忽略注解
|
|
||||||
*/
|
|
||||||
@Around("@within(ignoreTenant) || @annotation(ignoreTenant)")
|
|
||||||
public Object handleIgnore(ProceedingJoinPoint point, IgnoreTenant ignoreTenant) throws Throwable {
|
|
||||||
TenantHelper.enableIgnore();
|
|
||||||
try {
|
|
||||||
return point.proceed();
|
|
||||||
} finally {
|
|
||||||
TenantHelper.disableIgnore();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -9,7 +9,6 @@ import org.dromara.common.core.utils.reflect.ReflectUtils;
|
|||||||
import org.dromara.common.mybatis.config.MybatisPlusConfig;
|
import org.dromara.common.mybatis.config.MybatisPlusConfig;
|
||||||
import org.dromara.common.redis.config.RedisConfig;
|
import org.dromara.common.redis.config.RedisConfig;
|
||||||
import org.dromara.common.redis.config.properties.RedissonProperties;
|
import org.dromara.common.redis.config.properties.RedissonProperties;
|
||||||
import org.dromara.common.tenant.aspect.TenantAspect;
|
|
||||||
import org.dromara.common.tenant.core.TenantSaTokenDao;
|
import org.dromara.common.tenant.core.TenantSaTokenDao;
|
||||||
import org.dromara.common.tenant.handle.PlusTenantLineHandler;
|
import org.dromara.common.tenant.handle.PlusTenantLineHandler;
|
||||||
import org.dromara.common.tenant.handle.TenantKeyPrefixHandler;
|
import org.dromara.common.tenant.handle.TenantKeyPrefixHandler;
|
||||||
@ -98,11 +97,4 @@ public class TenantConfig {
|
|||||||
return new TenantSaTokenDao();
|
return new TenantSaTokenDao();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 多租户切面
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public TenantAspect tenantAspect() {
|
|
||||||
return new TenantAspect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import org.dromara.common.core.utils.StringUtils;
|
|||||||
import org.dromara.common.redis.utils.RedisUtils;
|
import org.dromara.common.redis.utils.RedisUtils;
|
||||||
import org.dromara.common.satoken.utils.LoginHelper;
|
import org.dromara.common.satoken.utils.LoginHelper;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,10 +57,10 @@ public class TenantHelper {
|
|||||||
*
|
*
|
||||||
* @param handle 处理执行方法
|
* @param handle 处理执行方法
|
||||||
*/
|
*/
|
||||||
public static void ignore(Runnable handle) {
|
public static void ignore(Consumer<Void> handle) {
|
||||||
enableIgnore();
|
enableIgnore();
|
||||||
try {
|
try {
|
||||||
handle.run();
|
handle.accept(null);
|
||||||
} finally {
|
} finally {
|
||||||
disableIgnore();
|
disableIgnore();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import org.dromara.common.core.utils.SpringUtils;
|
|||||||
import org.dromara.common.core.utils.StringUtils;
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
import org.dromara.common.mybatis.core.page.PageQuery;
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
||||||
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
||||||
import org.dromara.common.tenant.annotation.IgnoreTenant;
|
import org.dromara.common.tenant.helper.TenantHelper;
|
||||||
import org.dromara.system.domain.*;
|
import org.dromara.system.domain.*;
|
||||||
import org.dromara.system.domain.bo.SysTenantBo;
|
import org.dromara.system.domain.bo.SysTenantBo;
|
||||||
import org.dromara.system.domain.vo.SysTenantVo;
|
import org.dromara.system.domain.vo.SysTenantVo;
|
||||||
@ -111,81 +111,81 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
* 新增租户
|
* 新增租户
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@IgnoreTenant
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Boolean insertByBo(SysTenantBo bo) {
|
public Boolean insertByBo(SysTenantBo bo) {
|
||||||
|
TenantHelper.ignore(unused -> {
|
||||||
|
|
||||||
SysTenant add = MapstructUtils.convert(bo, SysTenant.class);
|
SysTenant add = MapstructUtils.convert(bo, SysTenant.class);
|
||||||
|
|
||||||
// 获取所有租户编号
|
// 获取所有租户编号
|
||||||
List<String> tenantIds = baseMapper.selectObjs(
|
List<String> tenantIds = baseMapper.selectObjs(
|
||||||
new LambdaQueryWrapper<SysTenant>().select(SysTenant::getTenantId), Convert::toStr);
|
new LambdaQueryWrapper<SysTenant>().select(SysTenant::getTenantId), Convert::toStr);
|
||||||
String tenantId = generateTenantId(tenantIds);
|
String tenantId = generateTenantId(tenantIds);
|
||||||
add.setTenantId(tenantId);
|
add.setTenantId(tenantId);
|
||||||
boolean flag = baseMapper.insert(add) > 0;
|
boolean flag = baseMapper.insert(add) > 0;
|
||||||
if (!flag) {
|
if (!flag) {
|
||||||
throw new ServiceException("创建租户失败");
|
throw new ServiceException("创建租户失败");
|
||||||
}
|
}
|
||||||
bo.setId(add.getId());
|
bo.setId(add.getId());
|
||||||
|
|
||||||
// 根据套餐创建角色
|
// 根据套餐创建角色
|
||||||
Long roleId = createTenantRole(tenantId, bo.getPackageId());
|
Long roleId = createTenantRole(tenantId, bo.getPackageId());
|
||||||
|
|
||||||
// 创建部门: 公司名是部门名称
|
// 创建部门: 公司名是部门名称
|
||||||
SysDept dept = new SysDept();
|
SysDept dept = new SysDept();
|
||||||
dept.setTenantId(tenantId);
|
dept.setTenantId(tenantId);
|
||||||
dept.setDeptName(bo.getCompanyName());
|
dept.setDeptName(bo.getCompanyName());
|
||||||
dept.setLeader(bo.getUsername());
|
dept.setLeader(bo.getUsername());
|
||||||
dept.setParentId(Constants.TOP_PARENT_ID);
|
dept.setParentId(Constants.TOP_PARENT_ID);
|
||||||
dept.setAncestors(Constants.TOP_PARENT_ID.toString());
|
dept.setAncestors(Constants.TOP_PARENT_ID.toString());
|
||||||
deptMapper.insert(dept);
|
deptMapper.insert(dept);
|
||||||
Long deptId = dept.getDeptId();
|
Long deptId = dept.getDeptId();
|
||||||
|
|
||||||
// 角色和部门关联表
|
// 角色和部门关联表
|
||||||
SysRoleDept roleDept = new SysRoleDept();
|
SysRoleDept roleDept = new SysRoleDept();
|
||||||
roleDept.setRoleId(roleId);
|
roleDept.setRoleId(roleId);
|
||||||
roleDept.setDeptId(deptId);
|
roleDept.setDeptId(deptId);
|
||||||
roleDeptMapper.insert(roleDept);
|
roleDeptMapper.insert(roleDept);
|
||||||
|
|
||||||
// 创建系统用户
|
// 创建系统用户
|
||||||
SysUser user = new SysUser();
|
SysUser user = new SysUser();
|
||||||
user.setTenantId(tenantId);
|
user.setTenantId(tenantId);
|
||||||
user.setUserName(bo.getUsername());
|
user.setUserName(bo.getUsername());
|
||||||
user.setNickName(bo.getUsername());
|
user.setNickName(bo.getUsername());
|
||||||
user.setPassword(BCrypt.hashpw(bo.getPassword()));
|
user.setPassword(BCrypt.hashpw(bo.getPassword()));
|
||||||
user.setDeptId(deptId);
|
user.setDeptId(deptId);
|
||||||
userMapper.insert(user);
|
userMapper.insert(user);
|
||||||
|
|
||||||
// 用户和角色关联表
|
// 用户和角色关联表
|
||||||
SysUserRole userRole = new SysUserRole();
|
SysUserRole userRole = new SysUserRole();
|
||||||
userRole.setUserId(user.getUserId());
|
userRole.setUserId(user.getUserId());
|
||||||
userRole.setRoleId(roleId);
|
userRole.setRoleId(roleId);
|
||||||
userRoleMapper.insert(userRole);
|
userRoleMapper.insert(userRole);
|
||||||
|
|
||||||
String defaultTenantId = TenantConstants.DEFAULT_TENANT_ID;
|
String defaultTenantId = TenantConstants.DEFAULT_TENANT_ID;
|
||||||
List<SysDictType> dictTypeList = dictTypeMapper.selectList(
|
List<SysDictType> dictTypeList = dictTypeMapper.selectList(
|
||||||
new LambdaQueryWrapper<SysDictType>().eq(SysDictType::getTenantId, defaultTenantId));
|
new LambdaQueryWrapper<SysDictType>().eq(SysDictType::getTenantId, defaultTenantId));
|
||||||
List<SysDictData> dictDataList = dictDataMapper.selectList(
|
List<SysDictData> dictDataList = dictDataMapper.selectList(
|
||||||
new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getTenantId, defaultTenantId));
|
new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getTenantId, defaultTenantId));
|
||||||
for (SysDictType dictType : dictTypeList) {
|
for (SysDictType dictType : dictTypeList) {
|
||||||
dictType.setDictId(null);
|
dictType.setDictId(null);
|
||||||
dictType.setTenantId(tenantId);
|
dictType.setTenantId(tenantId);
|
||||||
}
|
}
|
||||||
for (SysDictData dictData : dictDataList) {
|
for (SysDictData dictData : dictDataList) {
|
||||||
dictData.setDictCode(null);
|
dictData.setDictCode(null);
|
||||||
dictData.setTenantId(tenantId);
|
dictData.setTenantId(tenantId);
|
||||||
}
|
}
|
||||||
dictTypeMapper.insertBatch(dictTypeList);
|
dictTypeMapper.insertBatch(dictTypeList);
|
||||||
dictDataMapper.insertBatch(dictDataList);
|
dictDataMapper.insertBatch(dictDataList);
|
||||||
|
|
||||||
List<SysConfig> sysConfigList = configMapper.selectList(
|
|
||||||
new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getTenantId, defaultTenantId));
|
|
||||||
for (SysConfig config : sysConfigList) {
|
|
||||||
config.setConfigId(null);
|
|
||||||
config.setTenantId(tenantId);
|
|
||||||
}
|
|
||||||
configMapper.insertBatch(sysConfigList);
|
|
||||||
|
|
||||||
|
List<SysConfig> sysConfigList = configMapper.selectList(
|
||||||
|
new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getTenantId, defaultTenantId));
|
||||||
|
for (SysConfig config : sysConfigList) {
|
||||||
|
config.setConfigId(null);
|
||||||
|
config.setTenantId(tenantId);
|
||||||
|
}
|
||||||
|
configMapper.insertBatch(sysConfigList);
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,33 +340,34 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|||||||
* 同步租户套餐
|
* 同步租户套餐
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@IgnoreTenant
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Boolean syncTenantPackage(String tenantId, String packageId) {
|
public Boolean syncTenantPackage(String tenantId, String packageId) {
|
||||||
SysTenantPackage tenantPackage = tenantPackageMapper.selectById(packageId);
|
TenantHelper.ignore(unused -> {
|
||||||
List<SysRole> roles = roleMapper.selectList(
|
SysTenantPackage tenantPackage = tenantPackageMapper.selectById(packageId);
|
||||||
new LambdaQueryWrapper<SysRole>().eq(SysRole::getTenantId, tenantId));
|
List<SysRole> roles = roleMapper.selectList(
|
||||||
List<Long> roleIds = new ArrayList<>(roles.size() - 1);
|
new LambdaQueryWrapper<SysRole>().eq(SysRole::getTenantId, tenantId));
|
||||||
List<Long> menuIds = StringUtils.splitTo(tenantPackage.getMenuIds(), Convert::toLong);
|
List<Long> roleIds = new ArrayList<>(roles.size() - 1);
|
||||||
roles.forEach(item -> {
|
List<Long> menuIds = StringUtils.splitTo(tenantPackage.getMenuIds(), Convert::toLong);
|
||||||
if (TenantConstants.TENANT_ADMIN_ROLE_KEY.equals(item.getRoleKey())) {
|
roles.forEach(item -> {
|
||||||
List<SysRoleMenu> roleMenus = new ArrayList<>(menuIds.size());
|
if (TenantConstants.TENANT_ADMIN_ROLE_KEY.equals(item.getRoleKey())) {
|
||||||
menuIds.forEach(menuId -> {
|
List<SysRoleMenu> roleMenus = new ArrayList<>(menuIds.size());
|
||||||
SysRoleMenu roleMenu = new SysRoleMenu();
|
menuIds.forEach(menuId -> {
|
||||||
roleMenu.setRoleId(item.getRoleId());
|
SysRoleMenu roleMenu = new SysRoleMenu();
|
||||||
roleMenu.setMenuId(menuId);
|
roleMenu.setRoleId(item.getRoleId());
|
||||||
roleMenus.add(roleMenu);
|
roleMenu.setMenuId(menuId);
|
||||||
});
|
roleMenus.add(roleMenu);
|
||||||
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, item.getRoleId()));
|
});
|
||||||
roleMenuMapper.insertBatch(roleMenus);
|
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, item.getRoleId()));
|
||||||
} else {
|
roleMenuMapper.insertBatch(roleMenus);
|
||||||
roleIds.add(item.getRoleId());
|
} else {
|
||||||
|
roleIds.add(item.getRoleId());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!roleIds.isEmpty()) {
|
||||||
|
roleMenuMapper.delete(
|
||||||
|
new LambdaQueryWrapper<SysRoleMenu>().in(SysRoleMenu::getRoleId, roleIds).notIn(!menuIds.isEmpty(), SysRoleMenu::getMenuId, menuIds));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (!roleIds.isEmpty()) {
|
|
||||||
roleMenuMapper.delete(
|
|
||||||
new LambdaQueryWrapper<SysRoleMenu>().in(SysRoleMenu::getRoleId, roleIds).notIn(!menuIds.isEmpty(), SysRoleMenu::getMenuId, menuIds));
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user