mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-22 02:50:00 +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.redis.config.RedisConfig;
|
||||
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.handle.PlusTenantLineHandler;
|
||||
import org.dromara.common.tenant.handle.TenantKeyPrefixHandler;
|
||||
@ -98,11 +97,4 @@ public class TenantConfig {
|
||||
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.satoken.utils.LoginHelper;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@ -56,10 +57,10 @@ public class TenantHelper {
|
||||
*
|
||||
* @param handle 处理执行方法
|
||||
*/
|
||||
public static void ignore(Runnable handle) {
|
||||
public static void ignore(Consumer<Void> handle) {
|
||||
enableIgnore();
|
||||
try {
|
||||
handle.run();
|
||||
handle.accept(null);
|
||||
} finally {
|
||||
disableIgnore();
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ import org.dromara.common.core.utils.SpringUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.mybatis.core.page.PageQuery;
|
||||
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.bo.SysTenantBo;
|
||||
import org.dromara.system.domain.vo.SysTenantVo;
|
||||
@ -111,81 +111,81 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
||||
* 新增租户
|
||||
*/
|
||||
@Override
|
||||
@IgnoreTenant
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
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(
|
||||
new LambdaQueryWrapper<SysTenant>().select(SysTenant::getTenantId), Convert::toStr);
|
||||
String tenantId = generateTenantId(tenantIds);
|
||||
add.setTenantId(tenantId);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (!flag) {
|
||||
throw new ServiceException("创建租户失败");
|
||||
}
|
||||
bo.setId(add.getId());
|
||||
// 获取所有租户编号
|
||||
List<String> tenantIds = baseMapper.selectObjs(
|
||||
new LambdaQueryWrapper<SysTenant>().select(SysTenant::getTenantId), Convert::toStr);
|
||||
String tenantId = generateTenantId(tenantIds);
|
||||
add.setTenantId(tenantId);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (!flag) {
|
||||
throw new ServiceException("创建租户失败");
|
||||
}
|
||||
bo.setId(add.getId());
|
||||
|
||||
// 根据套餐创建角色
|
||||
Long roleId = createTenantRole(tenantId, bo.getPackageId());
|
||||
// 根据套餐创建角色
|
||||
Long roleId = createTenantRole(tenantId, bo.getPackageId());
|
||||
|
||||
// 创建部门: 公司名是部门名称
|
||||
SysDept dept = new SysDept();
|
||||
dept.setTenantId(tenantId);
|
||||
dept.setDeptName(bo.getCompanyName());
|
||||
dept.setLeader(bo.getUsername());
|
||||
dept.setParentId(Constants.TOP_PARENT_ID);
|
||||
dept.setAncestors(Constants.TOP_PARENT_ID.toString());
|
||||
deptMapper.insert(dept);
|
||||
Long deptId = dept.getDeptId();
|
||||
// 创建部门: 公司名是部门名称
|
||||
SysDept dept = new SysDept();
|
||||
dept.setTenantId(tenantId);
|
||||
dept.setDeptName(bo.getCompanyName());
|
||||
dept.setLeader(bo.getUsername());
|
||||
dept.setParentId(Constants.TOP_PARENT_ID);
|
||||
dept.setAncestors(Constants.TOP_PARENT_ID.toString());
|
||||
deptMapper.insert(dept);
|
||||
Long deptId = dept.getDeptId();
|
||||
|
||||
// 角色和部门关联表
|
||||
SysRoleDept roleDept = new SysRoleDept();
|
||||
roleDept.setRoleId(roleId);
|
||||
roleDept.setDeptId(deptId);
|
||||
roleDeptMapper.insert(roleDept);
|
||||
// 角色和部门关联表
|
||||
SysRoleDept roleDept = new SysRoleDept();
|
||||
roleDept.setRoleId(roleId);
|
||||
roleDept.setDeptId(deptId);
|
||||
roleDeptMapper.insert(roleDept);
|
||||
|
||||
// 创建系统用户
|
||||
SysUser user = new SysUser();
|
||||
user.setTenantId(tenantId);
|
||||
user.setUserName(bo.getUsername());
|
||||
user.setNickName(bo.getUsername());
|
||||
user.setPassword(BCrypt.hashpw(bo.getPassword()));
|
||||
user.setDeptId(deptId);
|
||||
userMapper.insert(user);
|
||||
// 创建系统用户
|
||||
SysUser user = new SysUser();
|
||||
user.setTenantId(tenantId);
|
||||
user.setUserName(bo.getUsername());
|
||||
user.setNickName(bo.getUsername());
|
||||
user.setPassword(BCrypt.hashpw(bo.getPassword()));
|
||||
user.setDeptId(deptId);
|
||||
userMapper.insert(user);
|
||||
|
||||
// 用户和角色关联表
|
||||
SysUserRole userRole = new SysUserRole();
|
||||
userRole.setUserId(user.getUserId());
|
||||
userRole.setRoleId(roleId);
|
||||
userRoleMapper.insert(userRole);
|
||||
// 用户和角色关联表
|
||||
SysUserRole userRole = new SysUserRole();
|
||||
userRole.setUserId(user.getUserId());
|
||||
userRole.setRoleId(roleId);
|
||||
userRoleMapper.insert(userRole);
|
||||
|
||||
String defaultTenantId = TenantConstants.DEFAULT_TENANT_ID;
|
||||
List<SysDictType> dictTypeList = dictTypeMapper.selectList(
|
||||
new LambdaQueryWrapper<SysDictType>().eq(SysDictType::getTenantId, defaultTenantId));
|
||||
List<SysDictData> dictDataList = dictDataMapper.selectList(
|
||||
new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getTenantId, defaultTenantId));
|
||||
for (SysDictType dictType : dictTypeList) {
|
||||
dictType.setDictId(null);
|
||||
dictType.setTenantId(tenantId);
|
||||
}
|
||||
for (SysDictData dictData : dictDataList) {
|
||||
dictData.setDictCode(null);
|
||||
dictData.setTenantId(tenantId);
|
||||
}
|
||||
dictTypeMapper.insertBatch(dictTypeList);
|
||||
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);
|
||||
String defaultTenantId = TenantConstants.DEFAULT_TENANT_ID;
|
||||
List<SysDictType> dictTypeList = dictTypeMapper.selectList(
|
||||
new LambdaQueryWrapper<SysDictType>().eq(SysDictType::getTenantId, defaultTenantId));
|
||||
List<SysDictData> dictDataList = dictDataMapper.selectList(
|
||||
new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getTenantId, defaultTenantId));
|
||||
for (SysDictType dictType : dictTypeList) {
|
||||
dictType.setDictId(null);
|
||||
dictType.setTenantId(tenantId);
|
||||
}
|
||||
for (SysDictData dictData : dictDataList) {
|
||||
dictData.setDictCode(null);
|
||||
dictData.setTenantId(tenantId);
|
||||
}
|
||||
dictTypeMapper.insertBatch(dictTypeList);
|
||||
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);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -340,33 +340,34 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
||||
* 同步租户套餐
|
||||
*/
|
||||
@Override
|
||||
@IgnoreTenant
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean syncTenantPackage(String tenantId, String packageId) {
|
||||
SysTenantPackage tenantPackage = tenantPackageMapper.selectById(packageId);
|
||||
List<SysRole> roles = roleMapper.selectList(
|
||||
new LambdaQueryWrapper<SysRole>().eq(SysRole::getTenantId, tenantId));
|
||||
List<Long> roleIds = new ArrayList<>(roles.size() - 1);
|
||||
List<Long> menuIds = StringUtils.splitTo(tenantPackage.getMenuIds(), Convert::toLong);
|
||||
roles.forEach(item -> {
|
||||
if (TenantConstants.TENANT_ADMIN_ROLE_KEY.equals(item.getRoleKey())) {
|
||||
List<SysRoleMenu> roleMenus = new ArrayList<>(menuIds.size());
|
||||
menuIds.forEach(menuId -> {
|
||||
SysRoleMenu roleMenu = new SysRoleMenu();
|
||||
roleMenu.setRoleId(item.getRoleId());
|
||||
roleMenu.setMenuId(menuId);
|
||||
roleMenus.add(roleMenu);
|
||||
});
|
||||
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, item.getRoleId()));
|
||||
roleMenuMapper.insertBatch(roleMenus);
|
||||
} else {
|
||||
roleIds.add(item.getRoleId());
|
||||
TenantHelper.ignore(unused -> {
|
||||
SysTenantPackage tenantPackage = tenantPackageMapper.selectById(packageId);
|
||||
List<SysRole> roles = roleMapper.selectList(
|
||||
new LambdaQueryWrapper<SysRole>().eq(SysRole::getTenantId, tenantId));
|
||||
List<Long> roleIds = new ArrayList<>(roles.size() - 1);
|
||||
List<Long> menuIds = StringUtils.splitTo(tenantPackage.getMenuIds(), Convert::toLong);
|
||||
roles.forEach(item -> {
|
||||
if (TenantConstants.TENANT_ADMIN_ROLE_KEY.equals(item.getRoleKey())) {
|
||||
List<SysRoleMenu> roleMenus = new ArrayList<>(menuIds.size());
|
||||
menuIds.forEach(menuId -> {
|
||||
SysRoleMenu roleMenu = new SysRoleMenu();
|
||||
roleMenu.setRoleId(item.getRoleId());
|
||||
roleMenu.setMenuId(menuId);
|
||||
roleMenus.add(roleMenu);
|
||||
});
|
||||
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, item.getRoleId()));
|
||||
roleMenuMapper.insertBatch(roleMenus);
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user