refactor(tenant): 优化租户相关代码

This commit is contained in:
2025-07-17 22:15:13 +08:00
parent 6e7d371565
commit 7e9a950694
29 changed files with 333 additions and 151 deletions

View File

@@ -29,13 +29,13 @@ import org.springframework.stereotype.Service;
import top.continew.admin.auth.model.query.OnlineUserQuery;
import top.continew.admin.auth.model.resp.OnlineUserResp;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.config.TenantExtensionProperties;
import top.continew.admin.common.context.UserContext;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.common.context.UserExtraContext;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.tenant.autoconfigure.TenantProperties;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
import java.time.LocalDateTime;
@@ -52,7 +52,7 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class OnlineUserServiceImpl implements OnlineUserService {
private final TenantProperties tenantProperties;
private final TenantExtensionProperties tenantExtensionProperties;
@Override
@AutoOperate(type = OnlineUserResp.class, on = "list")
@@ -92,8 +92,8 @@ public class OnlineUserServiceImpl implements OnlineUserService {
.isMatchClientId(query.getClientId(), userContext)) {
continue;
}
//租户数据过滤
if (tenantProperties.isEnabled()) {
// 只显示本租户数据
if (tenantExtensionProperties.isEnabled()) {
if (!TenantContextHolder.getTenantId().equals(userContext.getTenantId())) {
continue;
}

View File

@@ -20,11 +20,11 @@ import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import top.continew.admin.common.base.model.entity.TenantBaseDO;
import top.continew.admin.common.config.mybatis.BCryptEncryptor;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.enums.GenderEnum;
import top.continew.starter.extension.crud.annotation.DictModel;
import top.continew.admin.common.base.model.entity.BaseDO;
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
import java.io.Serial;
@@ -39,7 +39,7 @@ import java.time.LocalDateTime;
@Data
@DictModel(labelKey = "nickname", extraKeys = {"username"})
@TableName("sys_user")
public class UserDO extends BaseDO {
public class UserDO extends TenantBaseDO {
@Serial
private static final long serialVersionUID = 1L;
@@ -108,10 +108,4 @@ public class UserDO extends BaseDO {
* 部门 ID
*/
private Long deptId;
/**
* 租户 ID
*/
@TableField(select = false)
private Long tenantId;
}

View File

@@ -62,6 +62,6 @@ public class MenuQuery implements Serializable {
* 排除的菜单 ID 列表
*/
@Schema(hidden = true, description = "菜单 ID 列表", example = "[9000]")
@Query(columns = "id", type = QueryType.NOT_IN)
@Query(columns = {"id", "parent_id"}, type = QueryType.NOT_IN)
private List<Long> excludeMenuIdList;
}

View File

@@ -21,6 +21,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import top.continew.admin.common.base.service.BaseServiceImpl;
import top.continew.admin.common.enums.DisEnableStatusEnum;
@@ -53,10 +54,10 @@ import java.util.Optional;
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptResp, DeptResp, DeptQuery, DeptReq> implements DeptService {
private final RoleDeptService roleDeptService;
private final DataSource dataSource;
@Lazy
@Resource
private UserService userService;
@Resource
private DataSource dataSource;
@Override
public void beforeCreate(DeptReq req) {

View File

@@ -28,6 +28,7 @@ import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.ProgressListener;
import org.dromara.x.file.storage.core.upload.UploadPretreatment;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.common.base.service.BaseServiceImpl;
@@ -64,6 +65,7 @@ import java.util.stream.Collectors;
public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileResp, FileResp, FileQuery, FileReq> implements FileService {
private final FileStorageService fileStorageService;
@Lazy
@Resource
private StorageService storageService;

View File

@@ -73,7 +73,7 @@ public class SmsConfigServiceImpl extends BaseServiceImpl<SmsConfigMapper, SmsCo
return;
}
// 启用状态才能设为默认配置
CheckUtils.throwIfNotEqual(DisEnableStatusEnum.ENABLE, smsConfig.getStatus(), "请先启用所选配置");
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, smsConfig.getStatus(), "请先启用所选配置");
baseMapper.lambdaUpdate().eq(SmsConfigDO::getIsDefault, true).set(SmsConfigDO::getIsDefault, false).update();
baseMapper.lambdaUpdate().eq(SmsConfigDO::getId, id).set(SmsConfigDO::getIsDefault, true).update();
}

View File

@@ -155,7 +155,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
return;
}
// 启用状态才能设为默认存储
CheckUtils.throwIfNotEqual(DisEnableStatusEnum.ENABLE, storage.getStatus(), "请先启用所选存储");
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, storage.getStatus(), "请先启用所选存储");
baseMapper.lambdaUpdate().eq(StorageDO::getIsDefault, true).set(StorageDO::getIsDefault, false).update();
baseMapper.lambdaUpdate().eq(StorageDO::getId, id).set(StorageDO::getIsDefault, true).update();
}