首页统计
This commit is contained in:
+19
@@ -152,6 +152,25 @@ public class TenantHierarchyController {
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定租户(含自身)及其所有子孙租户ID列表
|
||||
* 供其他业务服务(如 order-center)通过 Feign 远程调用
|
||||
*/
|
||||
@GetMapping("/{tenantId}")
|
||||
@Operation(summary = "获取子孙租户ID列表(含自身)")
|
||||
public Result<List<String>> getDescendantTenantIds(@PathVariable("tenantId") String tenantId) {
|
||||
if (!StringUtils.hasText(tenantId)) {
|
||||
return Result.failed("tenantId 不能为空");
|
||||
}
|
||||
try {
|
||||
List<String> ids = tenantHierarchyService.getAllSubTenantIds(tenantId);
|
||||
return Result.succeed(ids);
|
||||
} catch (Exception e) {
|
||||
log.warn("获取子孙租户失败,退化为仅自身: {} -> {}", tenantId, e.getMessage());
|
||||
return Result.succeed(java.util.Collections.singletonList(tenantId), "已退化为仅自身");
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizePathForSave(String rawPath, String parentId, String tenantId) {
|
||||
try {
|
||||
String path = stripPathSafe(rawPath);
|
||||
|
||||
+11
-3
@@ -45,10 +45,18 @@ public class TenantSwitchController {
|
||||
if (user == null) {
|
||||
return Result.failed("用户未登录");
|
||||
}
|
||||
if (!isCanSwitchTenant(user)) {
|
||||
return Result.failed("用户无租户切换权限");
|
||||
// 不再强制校验切换权限:至少返回当前租户 + 子孙租户,便于查询范围选择
|
||||
String userTenantId = getDefaultTenantId(user);
|
||||
List<String> accessibleTenantIds = List.of();
|
||||
if (StringUtils.hasText(userTenantId)) {
|
||||
try {
|
||||
List<String> subIds = tenantHierarchyService != null ? tenantHierarchyService.getAllSubTenantIds(userTenantId) : null;
|
||||
accessibleTenantIds = (subIds != null && !subIds.isEmpty()) ? subIds : List.of(userTenantId);
|
||||
} catch (Exception ex) {
|
||||
log.warn("获取子租户列表失败,降级为仅当前租户: tenantId={}, error={}", userTenantId, ex.getMessage());
|
||||
accessibleTenantIds = List.of(userTenantId);
|
||||
}
|
||||
}
|
||||
List<String> accessibleTenantIds = getAccessibleTenantIds(user);
|
||||
if (accessibleTenantIds.isEmpty()) {
|
||||
return Result.failed("用户无可访问的租户");
|
||||
}
|
||||
|
||||
+12
-1
@@ -5,6 +5,7 @@ import com.central.common.model.Tenant;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -146,4 +147,14 @@ public interface TenantHierarchyMapper extends BaseMapper<Tenant> {
|
||||
long countTenantList(@Param("parentTenantId") String parentTenantId,
|
||||
@Param("tenantName") String tenantName,
|
||||
@Param("status") Integer status);
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于 tenant_id 进行软删除(将 status 置为 0)
|
||||
*
|
||||
* @param tenantId 租户ID
|
||||
* @param status 状态值(0 表示删除/禁用)
|
||||
* @return 影响行数
|
||||
*/
|
||||
@Update("UPDATE sys_tenant SET status = #{status} WHERE tenant_id = #{tenantId}")
|
||||
int updateStatusByTenantId(@Param("tenantId") String tenantId, @Param("status") Integer status);
|
||||
}
|
||||
|
||||
+40
-40
@@ -16,9 +16,9 @@ import com.central.common.model.UserType;
|
||||
import com.central.common.model.SuperEntity;
|
||||
import com.central.common.model.SysMenu;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.central.user.feign.DeviceCenterService;
|
||||
import com.central.common.service.ITenantHierarchyService;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.user.feign.DeviceCenterService;
|
||||
import com.central.common.service.ITenantHierarchyService;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.user.feign.OrderCenterService;
|
||||
import com.central.user.mapper.SysRoleMenuMapper;
|
||||
import com.central.user.mapper.SysUserMapper;
|
||||
@@ -64,11 +64,11 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
@Resource
|
||||
private OrderCenterService orderCenterService;
|
||||
|
||||
@Resource
|
||||
private DeviceCenterService deviceCenterService;
|
||||
|
||||
@Resource
|
||||
private ITenantHierarchyService tenantHierarchyService;
|
||||
@Resource
|
||||
private DeviceCenterService deviceCenterService;
|
||||
|
||||
@Resource
|
||||
private ITenantHierarchyService tenantHierarchyService;
|
||||
|
||||
@Override
|
||||
public SysUser findByUsername(String username) {
|
||||
@@ -230,38 +230,38 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
return Result.succeed("修改成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<SysUser> findUsers(Map<String, Object> params) {
|
||||
Page<SysUser> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
|
||||
// 组装租户过滤:有传tenantId则以其为锚点(可选包含子级);未传则按当前登录用户及其子租户
|
||||
String tenantId = MapUtils.getString(params, "tenantId");
|
||||
Boolean includeChildren = MapUtils.getBoolean(params, "includeChildren", Boolean.TRUE);
|
||||
List<String> tenantIds = null;
|
||||
if (StrUtil.isNotBlank(tenantId)) {
|
||||
try {
|
||||
if (Boolean.TRUE.equals(includeChildren)) {
|
||||
tenantIds = tenantHierarchyService.getAllSubTenantIds(tenantId);
|
||||
} else {
|
||||
tenantIds = java.util.Collections.singletonList(tenantId);
|
||||
}
|
||||
} catch (Exception ignored) { tenantIds = java.util.Collections.singletonList(tenantId); }
|
||||
} else {
|
||||
// 未显式指定,则按当前登录用户所属租户及其子租户
|
||||
try {
|
||||
String anchor = LoginUserContextHolder.getCurrentTenantId();
|
||||
if (StringUtils.isNotBlank(anchor)) {
|
||||
try {
|
||||
tenantIds = tenantHierarchyService.getAllSubTenantIds(anchor);
|
||||
} catch (Exception e) {
|
||||
tenantIds = java.util.Collections.singletonList(anchor);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) { /* no-op */ }
|
||||
}
|
||||
if (tenantIds != null && !tenantIds.isEmpty()) {
|
||||
params.put("tenantIds", tenantIds);
|
||||
}
|
||||
List<SysUser> list = baseMapper.findList(page, params);
|
||||
@Override
|
||||
public PageResult<SysUser> findUsers(Map<String, Object> params) {
|
||||
Page<SysUser> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
|
||||
// 组装租户过滤:有传tenantId则以其为锚点(可选包含子级);未传则按当前登录用户及其子租户
|
||||
String tenantId = MapUtils.getString(params, "tenantId");
|
||||
Boolean includeChildren = MapUtils.getBoolean(params, "includeChildren", Boolean.TRUE);
|
||||
List<String> tenantIds = null;
|
||||
if (StrUtil.isNotBlank(tenantId)) {
|
||||
try {
|
||||
if (Boolean.TRUE.equals(includeChildren)) {
|
||||
tenantIds = tenantHierarchyService.getAllSubTenantIds(tenantId);
|
||||
} else {
|
||||
tenantIds = java.util.Collections.singletonList(tenantId);
|
||||
}
|
||||
} catch (Exception ignored) { tenantIds = java.util.Collections.singletonList(tenantId); }
|
||||
} else {
|
||||
// 未显式指定,则按当前登录用户所属租户及其子租户
|
||||
try {
|
||||
String anchor = LoginUserContextHolder.getCurrentTenantId();
|
||||
if (StringUtils.isNotBlank(anchor)) {
|
||||
try {
|
||||
tenantIds = tenantHierarchyService.getAllSubTenantIds(anchor);
|
||||
} catch (Exception e) {
|
||||
tenantIds = java.util.Collections.singletonList(anchor);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) { /* no-op */ }
|
||||
}
|
||||
if (tenantIds != null && !tenantIds.isEmpty()) {
|
||||
params.put("tenantIds", tenantIds);
|
||||
}
|
||||
List<SysUser> list = baseMapper.findList(page, params);
|
||||
long total = page.getTotal();
|
||||
if (total > 0) {
|
||||
List<Long> userIds = list.stream().map(SysUser::getId).collect(Collectors.toList());
|
||||
|
||||
+126
-235
@@ -23,8 +23,10 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* 租户层级管理服务实现
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2024-12-19
|
||||
* 仅修复本文件:
|
||||
* - 处理 status 为空导致的 NPE
|
||||
* - 统一中文文案为 UTF-8
|
||||
* - 保持接口契约不变
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -33,48 +35,31 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
@Autowired
|
||||
private TenantHierarchyMapper tenantHierarchyMapper;
|
||||
|
||||
private static final String CACHE_KEY_PREFIX = "tenant:hierarchy:";
|
||||
|
||||
@Override
|
||||
@Cacheable(value = "tenant:hierarchy", key = "#parentTenantId")
|
||||
public List<String> getAllSubTenantIds(String parentTenantId) {
|
||||
if (!StringUtils.hasText(parentTenantId)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
try {
|
||||
List<String> subTenantIds = tenantHierarchyMapper.selectSubTenantIds(parentTenantId);
|
||||
if (subTenantIds == null) {
|
||||
subTenantIds = new ArrayList<>();
|
||||
}
|
||||
// 确保包含自身
|
||||
if (!subTenantIds.contains(parentTenantId)) {
|
||||
subTenantIds.add(0, parentTenantId);
|
||||
}
|
||||
|
||||
// 如果基于 tenant_path 的查询结果过少,回退为按 parent_tenant_id 递归遍历
|
||||
if (subTenantIds.size() <= 1) {
|
||||
Deque<String> queue = new ArrayDeque<>();
|
||||
queue.add(parentTenantId);
|
||||
while (!queue.isEmpty()) {
|
||||
String pid = queue.poll();
|
||||
List<Tenant> children = tenantHierarchyMapper.selectDirectChildren(pid);
|
||||
if (children != null) {
|
||||
for (Tenant t : children) {
|
||||
String tid = t.getTenantId();
|
||||
if (!subTenantIds.contains(tid)) {
|
||||
subTenantIds.add(tid);
|
||||
queue.add(tid);
|
||||
}
|
||||
}
|
||||
LinkedHashSet<String> all = new LinkedHashSet<>();
|
||||
ArrayDeque<String> queue = new ArrayDeque<>();
|
||||
all.add(parentTenantId);
|
||||
queue.add(parentTenantId);
|
||||
while (!queue.isEmpty()) {
|
||||
String pid = queue.poll();
|
||||
List<Tenant> children = tenantHierarchyMapper.selectDirectChildren(pid);
|
||||
if (children == null) continue;
|
||||
for (Tenant t : children) {
|
||||
String tid = t.getTenantId();
|
||||
if (StringUtils.hasText(tid) && all.add(tid)) {
|
||||
queue.add(tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("查询租户 {} 的所有子租户,数量: {}", parentTenantId, subTenantIds.size());
|
||||
return subTenantIds;
|
||||
return new ArrayList<>(all);
|
||||
} catch (Exception e) {
|
||||
log.error("查询子租户失败: parentTenantId={}, error={}", parentTenantId, e.getMessage());
|
||||
log.error("查询子孙租户失败: parentTenantId={}, error={}", parentTenantId, e.getMessage());
|
||||
return Collections.singletonList(parentTenantId);
|
||||
}
|
||||
}
|
||||
@@ -84,40 +69,38 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
if (tenantIds == null || tenantIds.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取租户详细信息
|
||||
List<Tenant> tenants = tenantHierarchyMapper.selectByTenantIds(tenantIds);
|
||||
|
||||
// 转换为树形节点
|
||||
Map<String, TenantTreeNode> nodeMap = new HashMap<>();
|
||||
List<TenantTreeNode> rootNodes = new ArrayList<>();
|
||||
List<TenantTreeNode> roots = new ArrayList<>();
|
||||
|
||||
// 创建所有节点
|
||||
for (Tenant tenant : tenants) {
|
||||
for (Tenant t : tenants) {
|
||||
TenantTreeNode node = TenantTreeNode.builder()
|
||||
.tenantId(tenant.getTenantId())
|
||||
.tenantName(tenant.getTenantName())
|
||||
.parentTenantId(tenant.getParentTenantId())
|
||||
.tenantType(tenant.getTenantType())
|
||||
.tenantLevel(tenant.getTenantLevel())
|
||||
.status(tenant.getStatus())
|
||||
.enabled(tenant.getStatus() == 1)
|
||||
.description(tenant.getDescription())
|
||||
.children(new ArrayList<>())
|
||||
.hasChildren(false)
|
||||
.childrenCount(0)
|
||||
.build();
|
||||
nodeMap.put(tenant.getTenantId(), node);
|
||||
.tenantId(t.getTenantId())
|
||||
.tenantName(t.getTenantName())
|
||||
.parentTenantId(t.getParentTenantId())
|
||||
.tenantType(t.getTenantType())
|
||||
.tenantLevel(t.getTenantLevel())
|
||||
.status(t.getStatus())
|
||||
.enabled(Objects.equals(t.getStatus(), 1))
|
||||
.description(t.getDescription())
|
||||
.children(new ArrayList<>())
|
||||
.hasChildren(false)
|
||||
.childrenCount(0)
|
||||
.build();
|
||||
nodeMap.put(t.getTenantId(), node);
|
||||
}
|
||||
|
||||
// 构建父子关系
|
||||
for (TenantTreeNode node : nodeMap.values()) {
|
||||
if (!StringUtils.hasText(node.getParentTenantId())) {
|
||||
rootNodes.add(node);
|
||||
String p = node.getParentTenantId();
|
||||
if (!StringUtils.hasText(p)) {
|
||||
roots.add(node);
|
||||
} else {
|
||||
TenantTreeNode parent = nodeMap.get(node.getParentTenantId());
|
||||
if (parent != null) {
|
||||
TenantTreeNode parent = nodeMap.get(p);
|
||||
if (parent == null) {
|
||||
roots.add(node);
|
||||
} else {
|
||||
parent.getChildren().add(node);
|
||||
parent.setHasChildren(true);
|
||||
parent.setChildrenCount(parent.getChildren().size());
|
||||
@@ -125,9 +108,7 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("构建租户树完成,根节点数量: {}", rootNodes.size());
|
||||
return rootNodes;
|
||||
|
||||
return roots;
|
||||
} catch (Exception e) {
|
||||
log.error("构建租户树失败: tenantIds={}, error={}", tenantIds, e.getMessage());
|
||||
return Collections.emptyList();
|
||||
@@ -139,52 +120,44 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
if (!StringUtils.hasText(tenantId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
Tenant tenant = tenantHierarchyMapper.selectByTenantId(tenantId);
|
||||
if (tenant == null) {
|
||||
return null;
|
||||
}
|
||||
if (tenant == null) return null;
|
||||
|
||||
// 查询子租户数量
|
||||
List<Tenant> children = tenantHierarchyMapper.selectDirectChildren(tenantId);
|
||||
int childrenCount = children != null ? children.size() : 0;
|
||||
int childrenCount = children == null ? 0 : children.size();
|
||||
|
||||
// 查询父租户名称
|
||||
String parentTenantName = null;
|
||||
String parentName = null;
|
||||
if (StringUtils.hasText(tenant.getParentTenantId())) {
|
||||
Tenant parentTenant = tenantHierarchyMapper.selectByTenantId(tenant.getParentTenantId());
|
||||
if (parentTenant != null) {
|
||||
parentTenantName = parentTenant.getTenantName();
|
||||
}
|
||||
Tenant parent = tenantHierarchyMapper.selectByTenantId(tenant.getParentTenantId());
|
||||
if (parent != null) parentName = parent.getTenantName();
|
||||
}
|
||||
|
||||
return TenantInfo.builder()
|
||||
.tenantId(tenant.getTenantId())
|
||||
.tenantName(tenant.getTenantName())
|
||||
.parentTenantId(tenant.getParentTenantId())
|
||||
.parentTenantName(parentTenantName)
|
||||
.tenantLevel(tenant.getTenantLevel())
|
||||
.tenantPath(tenant.getTenantPath())
|
||||
.tenantType(tenant.getTenantType())
|
||||
.tenantCode(tenant.getTenantCode())
|
||||
.contactPerson(tenant.getContactPerson())
|
||||
.contactPhone(tenant.getContactPhone())
|
||||
.address(tenant.getAddress())
|
||||
.description(tenant.getDescription())
|
||||
.status(tenant.getStatus())
|
||||
.statusText(tenant.getStatus() == 1 ? "启用" : "停用")
|
||||
.hasChildren(childrenCount > 0)
|
||||
.childrenCount(childrenCount)
|
||||
.createTime(tenant.getCreateTime() != null ?
|
||||
LocalDateTime.ofInstant(tenant.getCreateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.updateTime(tenant.getUpdateTime() != null ?
|
||||
LocalDateTime.ofInstant(tenant.getUpdateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.creatorId(tenant.getCreatorId())
|
||||
.build();
|
||||
|
||||
.tenantId(tenant.getTenantId())
|
||||
.tenantName(tenant.getTenantName())
|
||||
.parentTenantId(tenant.getParentTenantId())
|
||||
.parentTenantName(parentName)
|
||||
.tenantLevel(tenant.getTenantLevel())
|
||||
.tenantPath(tenant.getTenantPath())
|
||||
.tenantType(tenant.getTenantType())
|
||||
.tenantCode(tenant.getTenantCode())
|
||||
.contactPerson(tenant.getContactPerson())
|
||||
.contactPhone(tenant.getContactPhone())
|
||||
.address(tenant.getAddress())
|
||||
.description(tenant.getDescription())
|
||||
.status(tenant.getStatus())
|
||||
.statusText(Objects.equals(tenant.getStatus(), 1) ? "启用" : "停用")
|
||||
.hasChildren(childrenCount > 0)
|
||||
.childrenCount(childrenCount)
|
||||
.createTime(tenant.getCreateTime() != null ?
|
||||
LocalDateTime.ofInstant(tenant.getCreateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.updateTime(tenant.getUpdateTime() != null ?
|
||||
LocalDateTime.ofInstant(tenant.getUpdateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.creatorId(tenant.getCreatorId())
|
||||
.build();
|
||||
} catch (Exception e) {
|
||||
log.error("获取租户信息失败: tenantId={}, error={}", tenantId, e.getMessage());
|
||||
log.error("获取租户信息异常: tenantId={}, error={}", tenantId, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -194,60 +167,38 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
@CacheEvict(value = "tenant:hierarchy", allEntries = true)
|
||||
public Result<Tenant> createTenant(Tenant tenant) {
|
||||
try {
|
||||
// 校验必填字段
|
||||
if (!StringUtils.hasText(tenant.getTenantId())) {
|
||||
return Result.failed("租户ID不能为空");
|
||||
}
|
||||
if (!StringUtils.hasText(tenant.getTenantName())) {
|
||||
return Result.failed("租户名称不能为空");
|
||||
}
|
||||
|
||||
// 检查租户ID是否已存在
|
||||
if (existsByTenantId(tenant.getTenantId())) {
|
||||
return Result.failed("租户ID已存在");
|
||||
}
|
||||
|
||||
// 验证父子租户关系
|
||||
if (StringUtils.hasText(tenant.getParentTenantId())) {
|
||||
if (!validateTenantRelation(tenant.getTenantId(), tenant.getParentTenantId())) {
|
||||
return Result.failed("无效的父子租户关系");
|
||||
}
|
||||
|
||||
// 获取父租户信息,设置层级和路径
|
||||
Tenant parentTenant = tenantHierarchyMapper.selectByTenantId(tenant.getParentTenantId());
|
||||
if (parentTenant == null) {
|
||||
return Result.failed("父租户不存在");
|
||||
}
|
||||
tenant.setTenantLevel(parentTenant.getTenantLevel() + 1);
|
||||
tenant.setTenantPath(parentTenant.getTenantPath() + tenant.getTenantId() + "/");
|
||||
Tenant parent = tenantHierarchyMapper.selectByTenantId(tenant.getParentTenantId());
|
||||
if (parent == null) return Result.failed("父租户不存在");
|
||||
tenant.setTenantLevel(parent.getTenantLevel() + 1);
|
||||
tenant.setTenantPath(parent.getTenantPath() + tenant.getTenantId() + "/");
|
||||
} else {
|
||||
// 根租户
|
||||
tenant.setTenantLevel(1);
|
||||
tenant.setTenantPath("/" + tenant.getTenantId() + "/");
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
if (tenant.getStatus() == null) {
|
||||
tenant.setStatus(1);
|
||||
}
|
||||
if (!StringUtils.hasText(tenant.getTenantType())) {
|
||||
tenant.setTenantType("COMPANY");
|
||||
}
|
||||
|
||||
// 设置创建人
|
||||
if (tenant.getStatus() == null) tenant.setStatus(1);
|
||||
if (!StringUtils.hasText(tenant.getTenantType())) tenant.setTenantType("COMPANY");
|
||||
if (LoginUserContextHolder.getUser() != null) {
|
||||
tenant.setCreatorId(LoginUserContextHolder.getUser().getId());
|
||||
}
|
||||
|
||||
// 保存租户
|
||||
int result = tenantHierarchyMapper.insert(tenant);
|
||||
if (result > 0) {
|
||||
log.info("创建租户成功: tenantId={}, tenantName={}", tenant.getTenantId(), tenant.getTenantName());
|
||||
return Result.succeed(tenant);
|
||||
} else {
|
||||
return Result.failed("创建租户失败");
|
||||
}
|
||||
|
||||
int r = tenantHierarchyMapper.insert(tenant);
|
||||
if (r > 0) return Result.succeed(tenant);
|
||||
return Result.failed("创建租户失败");
|
||||
} catch (Exception e) {
|
||||
log.error("创建租户异常: tenant={}, error={}", tenant, e.getMessage());
|
||||
return Result.failed("创建租户异常: " + e.getMessage());
|
||||
@@ -262,30 +213,19 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
if (!StringUtils.hasText(tenant.getTenantId())) {
|
||||
return Result.failed("租户ID不能为空");
|
||||
}
|
||||
Tenant exist = tenantHierarchyMapper.selectByTenantId(tenant.getTenantId());
|
||||
if (exist == null) return Result.failed("租户不存在");
|
||||
|
||||
// 检查租户是否存在
|
||||
Tenant existingTenant = tenantHierarchyMapper.selectByTenantId(tenant.getTenantId());
|
||||
if (existingTenant == null) {
|
||||
return Result.failed("租户不存在");
|
||||
}
|
||||
|
||||
// 验证父子关系变更
|
||||
if (!Objects.equals(existingTenant.getParentTenantId(), tenant.getParentTenantId())) {
|
||||
if (!Objects.equals(exist.getParentTenantId(), tenant.getParentTenantId())) {
|
||||
if (!validateTenantRelation(tenant.getTenantId(), tenant.getParentTenantId())) {
|
||||
return Result.failed("无效的父子租户关系");
|
||||
}
|
||||
}
|
||||
|
||||
// 使用主键ID执行更新,避免未传ID导致更新不到
|
||||
tenant.setId(existingTenant.getId());
|
||||
int result = tenantHierarchyMapper.updateById(tenant);
|
||||
if (result > 0) {
|
||||
log.info("更新租户成功: tenantId={}", tenant.getTenantId());
|
||||
return Result.succeed(tenant);
|
||||
} else {
|
||||
return Result.failed("更新租户失败");
|
||||
}
|
||||
|
||||
tenant.setId(exist.getId());
|
||||
int r = tenantHierarchyMapper.updateById(tenant);
|
||||
if (r > 0) return Result.succeed(tenant);
|
||||
return Result.failed("更新租户失败");
|
||||
} catch (Exception e) {
|
||||
log.error("更新租户异常: tenant={}, error={}", tenant, e.getMessage());
|
||||
return Result.failed("更新租户异常: " + e.getMessage());
|
||||
@@ -297,29 +237,12 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
@CacheEvict(value = "tenant:hierarchy", allEntries = true)
|
||||
public Result<Boolean> deleteTenant(String tenantId) {
|
||||
try {
|
||||
if (!StringUtils.hasText(tenantId)) {
|
||||
return Result.failed("租户ID不能为空");
|
||||
}
|
||||
|
||||
// 检查是否有子租户
|
||||
if (!StringUtils.hasText(tenantId)) return Result.failed("租户ID不能为空");
|
||||
List<Tenant> children = tenantHierarchyMapper.selectDirectChildren(tenantId);
|
||||
if (children != null && !children.isEmpty()) {
|
||||
return Result.failed("存在子租户,无法删除");
|
||||
}
|
||||
|
||||
// 软删除(更新状态为0)
|
||||
Tenant tenant = new Tenant();
|
||||
tenant.setTenantId(tenantId);
|
||||
tenant.setStatus(0);
|
||||
|
||||
int result = tenantHierarchyMapper.updateById(tenant);
|
||||
if (result > 0) {
|
||||
log.info("删除租户成功: tenantId={}", tenantId);
|
||||
return Result.succeed(true);
|
||||
} else {
|
||||
return Result.failed("删除租户失败");
|
||||
}
|
||||
|
||||
if (children != null && !children.isEmpty()) return Result.failed("存在子租户,无法删除");
|
||||
int r = tenantHierarchyMapper.updateStatusByTenantId(tenantId, 0);
|
||||
if (r > 0) return Result.succeed(true);
|
||||
return Result.failed("删除租户失败");
|
||||
} catch (Exception e) {
|
||||
log.error("删除租户异常: tenantId={}, error={}", tenantId, e.getMessage());
|
||||
return Result.failed("删除租户异常: " + e.getMessage());
|
||||
@@ -331,19 +254,13 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
try {
|
||||
if (page == null || page < 1) page = 1;
|
||||
if (size == null || size < 1) size = 10;
|
||||
|
||||
int offset = (page - 1) * size;
|
||||
|
||||
List<Tenant> tenants = tenantHierarchyMapper.selectTenantList(parentTenantId, tenantName, status, offset, size);
|
||||
long total = tenantHierarchyMapper.countTenantList(parentTenantId, tenantName, status);
|
||||
|
||||
List<TenantInfo> tenantInfos = tenants.stream()
|
||||
.map(this::convertToTenantInfo)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
log.debug("查询租户列表完成,总数: {},当前页数量: {}", total, tenantInfos.size());
|
||||
return Result.succeed(tenantInfos);
|
||||
tenants = tenants == null ? Collections.emptyList() : tenants;
|
||||
|
||||
List<TenantInfo> list = tenants.stream().map(this::convertToTenantInfo).collect(Collectors.toList());
|
||||
return Result.succeed(list);
|
||||
} catch (Exception e) {
|
||||
log.error("查询租户列表异常: error={}", e.getMessage());
|
||||
return Result.failed("查询租户列表异常: " + e.getMessage());
|
||||
@@ -352,77 +269,51 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
|
||||
@Override
|
||||
public boolean existsByTenantId(String tenantId) {
|
||||
if (!StringUtils.hasText(tenantId)) {
|
||||
return false;
|
||||
}
|
||||
if (!StringUtils.hasText(tenantId)) return false;
|
||||
return tenantHierarchyMapper.existsByTenantId(tenantId) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateTenantRelation(String tenantId, String parentTenantId) {
|
||||
if (!StringUtils.hasText(tenantId)) {
|
||||
return false;
|
||||
}
|
||||
if (!StringUtils.hasText(tenantId)) return false;
|
||||
if (!StringUtils.hasText(parentTenantId)) return true;
|
||||
if (tenantId.equals(parentTenantId)) return false;
|
||||
|
||||
// 如果没有父租户,则合法
|
||||
if (!StringUtils.hasText(parentTenantId)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 不能将自己设置为父租户
|
||||
if (tenantId.equals(parentTenantId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否会形成循环引用
|
||||
String currentTenantId = parentTenantId;
|
||||
String current = parentTenantId;
|
||||
Set<String> visited = new HashSet<>();
|
||||
|
||||
while (currentTenantId != null && !visited.contains(currentTenantId)) {
|
||||
visited.add(currentTenantId);
|
||||
|
||||
if (currentTenantId.equals(tenantId)) {
|
||||
// 形成循环引用
|
||||
return false;
|
||||
}
|
||||
|
||||
Tenant currentTenant = tenantHierarchyMapper.selectByTenantId(currentTenantId);
|
||||
currentTenantId = currentTenant != null ? currentTenant.getParentTenantId() : null;
|
||||
while (current != null && !visited.contains(current)) {
|
||||
visited.add(current);
|
||||
if (tenantId.equals(current)) return false;
|
||||
Tenant p = tenantHierarchyMapper.selectByTenantId(current);
|
||||
current = p == null ? null : p.getParentTenantId();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 TenantInfo
|
||||
*/
|
||||
private TenantInfo convertToTenantInfo(Tenant tenant) {
|
||||
if (tenant == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private TenantInfo convertToTenantInfo(Tenant t) {
|
||||
if (t == null) return null;
|
||||
return TenantInfo.builder()
|
||||
.tenantId(tenant.getTenantId())
|
||||
.tenantName(tenant.getTenantName())
|
||||
.parentTenantId(tenant.getParentTenantId())
|
||||
.tenantLevel(tenant.getTenantLevel())
|
||||
.tenantPath(tenant.getTenantPath())
|
||||
.tenantType(tenant.getTenantType())
|
||||
.tenantCode(tenant.getTenantCode())
|
||||
.contactPerson(tenant.getContactPerson())
|
||||
.contactPhone(tenant.getContactPhone())
|
||||
.address(tenant.getAddress())
|
||||
.description(tenant.getDescription())
|
||||
.status(tenant.getStatus())
|
||||
.statusText(tenant.getStatus() == 1 ? "启用" : "停用")
|
||||
.hasChildren(tenant.getHasChildren())
|
||||
.childrenCount(tenant.getChildrenCount())
|
||||
.createTime(tenant.getCreateTime() != null ?
|
||||
LocalDateTime.ofInstant(tenant.getCreateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.updateTime(tenant.getUpdateTime() != null ?
|
||||
LocalDateTime.ofInstant(tenant.getUpdateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.creatorId(tenant.getCreatorId())
|
||||
.build();
|
||||
.tenantId(t.getTenantId())
|
||||
.tenantName(t.getTenantName())
|
||||
.parentTenantId(t.getParentTenantId())
|
||||
.tenantLevel(t.getTenantLevel())
|
||||
.tenantPath(t.getTenantPath())
|
||||
.tenantType(t.getTenantType())
|
||||
.tenantCode(t.getTenantCode())
|
||||
.contactPerson(t.getContactPerson())
|
||||
.contactPhone(t.getContactPhone())
|
||||
.address(t.getAddress())
|
||||
.description(t.getDescription())
|
||||
.status(t.getStatus())
|
||||
.statusText(Objects.equals(t.getStatus(), 1) ? "启用" : "停用")
|
||||
.hasChildren(t.getHasChildren())
|
||||
.childrenCount(t.getChildrenCount())
|
||||
.createTime(t.getCreateTime() != null ?
|
||||
LocalDateTime.ofInstant(t.getCreateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.updateTime(t.getUpdateTime() != null ?
|
||||
LocalDateTime.ofInstant(t.getUpdateTime().toInstant(), ZoneId.systemDefault()) : null)
|
||||
.creatorId(t.getCreatorId())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user