多租户与景区关系
This commit is contained in:
+145
-83
@@ -1,83 +1,145 @@
|
||||
package com.central.user.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.central.user.mapper.SysRoleMapper;
|
||||
import com.central.user.mapper.SysRoleMenuMapper;
|
||||
import com.central.user.mapper.SysUserRoleMapper;
|
||||
import com.central.user.service.ISysRoleService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 作者 owen E-mail: 624191343@qq.com
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
|
||||
private final static String LOCK_KEY_ROLECODE = "rolecode:";
|
||||
|
||||
@Resource
|
||||
private SysUserRoleMapper userRoleMapper;
|
||||
|
||||
@Resource
|
||||
private SysRoleMenuMapper roleMenuMapper;
|
||||
|
||||
@Autowired
|
||||
private DistributedLock lock;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveRole(SysRole sysRole) throws Exception {
|
||||
String roleCode = sysRole.getCode();
|
||||
super.saveIdempotency(sysRole, lock
|
||||
, LOCK_KEY_ROLECODE+roleCode, new QueryWrapper<SysRole>().eq("code", roleCode), "角色code已存在");
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void deleteRole(Long id) {
|
||||
baseMapper.deleteById(id);
|
||||
roleMenuMapper.delete(id, null);
|
||||
userRoleMapper.deleteUserRole(null, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<SysRole> findRoles(Map<String, Object> params) {
|
||||
Integer curPage = MapUtils.getInteger(params, "page");
|
||||
Integer limit = MapUtils.getInteger(params, "limit");
|
||||
Page<SysRole> page = new Page<>(curPage == null ? 0 : curPage, limit == null ? -1 : limit);
|
||||
List<SysRole> list = baseMapper.findList(page, params);
|
||||
return PageResult.<SysRole>builder().data(list).resp_code(0).count(page.getTotal()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Result saveOrUpdateRole(SysRole sysRole) throws Exception {
|
||||
if (sysRole.getId() == null) {
|
||||
sysRole.setCreatorId(LoginUserContextHolder.getUser().getId());
|
||||
this.saveRole(sysRole);
|
||||
} else {
|
||||
baseMapper.updateById(sysRole);
|
||||
}
|
||||
return Result.succeed("操作成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> findAll() {
|
||||
return baseMapper.findAll();
|
||||
}
|
||||
}
|
||||
package com.central.user.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.central.user.mapper.SysRoleMapper;
|
||||
import com.central.user.mapper.SysRoleMenuMapper;
|
||||
import com.central.user.mapper.SysUserRoleMapper;
|
||||
import com.central.user.service.ISysRoleService;
|
||||
import com.central.common.service.ITenantHierarchyService;
|
||||
import com.central.common.datascope.annotation.IgnoreTenantScope;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 作者 owen E-mail: 624191343@qq.com
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
|
||||
private final static String LOCK_KEY_ROLECODE = "rolecode:";
|
||||
|
||||
@Resource
|
||||
private SysUserRoleMapper userRoleMapper;
|
||||
|
||||
@Resource
|
||||
private SysRoleMenuMapper roleMenuMapper;
|
||||
|
||||
@Autowired
|
||||
private DistributedLock lock;
|
||||
|
||||
@Resource
|
||||
private ITenantHierarchyService tenantHierarchyService;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveRole(SysRole sysRole) throws Exception {
|
||||
String roleCode = sysRole.getCode();
|
||||
super.saveIdempotency(sysRole, lock
|
||||
, LOCK_KEY_ROLECODE+roleCode, new QueryWrapper<SysRole>().eq("code", roleCode), "角色code已存在");
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void deleteRole(Long id) {
|
||||
baseMapper.deleteById(id);
|
||||
roleMenuMapper.delete(id, null);
|
||||
userRoleMapper.deleteUserRole(null, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@IgnoreTenantScope
|
||||
public PageResult<SysRole> findRoles(Map<String, Object> params) {
|
||||
Integer curPage = MapUtils.getInteger(params, "page");
|
||||
Integer limit = MapUtils.getInteger(params, "limit");
|
||||
// 以租户为主:支持按租户及其子租户过滤
|
||||
String tenantId = MapUtils.getString(params, "tenantId");
|
||||
Object incRaw = params.get("includeChildren");
|
||||
boolean includeChildren = true;
|
||||
if (incRaw != null) {
|
||||
try {
|
||||
if (incRaw instanceof Boolean) {
|
||||
includeChildren = (Boolean) incRaw;
|
||||
} else if (incRaw instanceof Number) {
|
||||
includeChildren = ((Number) incRaw).intValue() != 0;
|
||||
} else if (incRaw instanceof CharSequence) {
|
||||
String s = incRaw.toString().trim();
|
||||
includeChildren = ("true".equalsIgnoreCase(s) || "1".equals(s) || "yes".equalsIgnoreCase(s));
|
||||
}
|
||||
} catch (Exception ignored) { /* 使用默认 true */ }
|
||||
}
|
||||
java.util.List<String> tenantIds = null;
|
||||
if (StringUtils.isNotBlank(tenantId)) {
|
||||
try {
|
||||
if (includeChildren) {
|
||||
// 展开包含所有子级(确保包含自身)
|
||||
tenantIds = tenantHierarchyService.getAllSubTenantIds(tenantId);
|
||||
if (tenantIds == null || tenantIds.isEmpty()) {
|
||||
tenantIds = new java.util.ArrayList<>();
|
||||
}
|
||||
if (!tenantIds.contains(tenantId)) {
|
||||
tenantIds = new java.util.ArrayList<>(tenantIds);
|
||||
tenantIds.add(0, tenantId);
|
||||
}
|
||||
} else {
|
||||
tenantIds = java.util.Collections.singletonList(tenantId);
|
||||
}
|
||||
} catch (Exception ignored) { tenantIds = java.util.Collections.singletonList(tenantId); }
|
||||
|
||||
// 与当前用户可访问租户集求交集,避免越权和误查全量
|
||||
// 与用户可访问租户不再做强制求交集,保持与用户列表查询一致的行为,
|
||||
// 以显式传入的 tenantId/includeChildren 为准,由接口鉴权控制访问边界。
|
||||
|
||||
if (tenantIds == null || tenantIds.isEmpty()) {
|
||||
// 无可访问租户,直接返回空结果
|
||||
Page<SysRole> empty = new Page<>(curPage == null ? 0 : curPage, limit == null ? -1 : limit);
|
||||
return PageResult.<SysRole>builder().data(java.util.Collections.emptyList()).resp_code(0).count(0L).build();
|
||||
}
|
||||
params.put("tenantIds", tenantIds);
|
||||
}
|
||||
|
||||
Page<SysRole> page = new Page<>(curPage == null ? 0 : curPage, limit == null ? -1 : limit);
|
||||
List<SysRole> list = baseMapper.findList(page, params);
|
||||
return PageResult.<SysRole>builder().data(list).resp_code(0).count(page.getTotal()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Result saveOrUpdateRole(SysRole sysRole) throws Exception {
|
||||
if (sysRole.getId() == null) {
|
||||
// 新增时补齐租户ID(以当前上下文或表单传参为准)
|
||||
if (StringUtils.isBlank(sysRole.getTenantId())) {
|
||||
try {
|
||||
String ctxTenant = com.central.common.context.TenantContextHolder.getTenant();
|
||||
if (StringUtils.isNotBlank(ctxTenant)) {
|
||||
sysRole.setTenantId(ctxTenant);
|
||||
}
|
||||
} catch (Exception ignored) { /* no-op */ }
|
||||
}
|
||||
sysRole.setCreatorId(LoginUserContextHolder.getUser().getId());
|
||||
this.saveRole(sysRole);
|
||||
} else {
|
||||
baseMapper.updateById(sysRole);
|
||||
}
|
||||
return Result.succeed("操作成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysRole> findAll() {
|
||||
return baseMapper.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
+40
-7
@@ -16,7 +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.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;
|
||||
@@ -62,8 +64,11 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
@Resource
|
||||
private OrderCenterService orderCenterService;
|
||||
|
||||
@Resource
|
||||
private DeviceCenterService deviceCenterService;
|
||||
@Resource
|
||||
private DeviceCenterService deviceCenterService;
|
||||
|
||||
@Resource
|
||||
private ITenantHierarchyService tenantHierarchyService;
|
||||
|
||||
@Override
|
||||
public SysUser findByUsername(String username) {
|
||||
@@ -225,10 +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"));
|
||||
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());
|
||||
|
||||
+34
-6
@@ -44,6 +44,33 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("查询租户 {} 的所有子租户,数量: {}", parentTenantId, subTenantIds.size());
|
||||
return subTenantIds;
|
||||
} catch (Exception e) {
|
||||
@@ -167,7 +194,7 @@ 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不能为空");
|
||||
}
|
||||
@@ -180,7 +207,7 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
return Result.failed("租户ID已存在");
|
||||
}
|
||||
|
||||
// 验证父租户关系
|
||||
// 验证父子租户关系
|
||||
if (StringUtils.hasText(tenant.getParentTenantId())) {
|
||||
if (!validateTenantRelation(tenant.getTenantId(), tenant.getParentTenantId())) {
|
||||
return Result.failed("无效的父子租户关系");
|
||||
@@ -242,14 +269,14 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
return Result.failed("租户不存在");
|
||||
}
|
||||
|
||||
// 验证父租户关系变更
|
||||
// 验证父子关系变更
|
||||
if (!Objects.equals(existingTenant.getParentTenantId(), tenant.getParentTenantId())) {
|
||||
if (!validateTenantRelation(tenant.getTenantId(), tenant.getParentTenantId())) {
|
||||
return Result.failed("无效的父子租户关系");
|
||||
}
|
||||
}
|
||||
|
||||
// 使用主键ID执行更新,避免未传ID导致返回0
|
||||
// 使用主键ID执行更新,避免未传ID导致更新不到
|
||||
tenant.setId(existingTenant.getId());
|
||||
int result = tenantHierarchyMapper.updateById(tenant);
|
||||
if (result > 0) {
|
||||
@@ -314,7 +341,7 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
.map(this::convertToTenantInfo)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
log.debug("查询租户列表完成,总数: {}, 当前页: {}", total, tenantInfos.size());
|
||||
log.debug("查询租户列表完成,总数: {},当前页数量: {}", total, tenantInfos.size());
|
||||
return Result.succeed(tenantInfos);
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -367,7 +394,7 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为TenantInfo
|
||||
* 转换为 TenantInfo
|
||||
*/
|
||||
private TenantInfo convertToTenantInfo(Tenant tenant) {
|
||||
if (tenant == null) {
|
||||
@@ -398,3 +425,4 @@ public class TenantHierarchyServiceImpl implements ITenantHierarchyService {
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,17 +12,28 @@
|
||||
data_scope
|
||||
</sql>
|
||||
|
||||
<sql id="where">
|
||||
<where>
|
||||
<if test="r.name != null and r.name != ''">
|
||||
and t.name like concat('%', #{r.name}, '%')
|
||||
</if>
|
||||
|
||||
<if test="r.searchKey != null and r.searchKey != '' and r.searchKey=='name'">
|
||||
and t.name like concat('%', #{r.searchValue}, '%')
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
<sql id="where">
|
||||
<where>
|
||||
<if test="r.name != null and r.name != ''">
|
||||
and t.name like concat('%', #{r.name}, '%')
|
||||
</if>
|
||||
|
||||
<if test="r.searchKey != null and r.searchKey != '' and r.searchKey=='name'">
|
||||
and t.name like concat('%', #{r.searchValue}, '%')
|
||||
</if>
|
||||
|
||||
<!-- 按租户过滤(以租户为主):优先使用租户ID集合,其次使用单个租户ID -->
|
||||
<if test="r.tenantIds != null and r.tenantIds.size > 0">
|
||||
and t.tenant_id in
|
||||
<foreach collection="r.tenantIds" item="tid" open="(" separator="," close=")">
|
||||
#{tid}
|
||||
</foreach>
|
||||
</if>
|
||||
<if test="(r.tenantIds == null or r.tenantIds.size == 0) and r.tenantId != null and r.tenantId != ''">
|
||||
and t.tenant_id = #{r.tenantId}
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
|
||||
<select id="findList" resultType="com.central.common.model.SysRole">
|
||||
select <include refid="Column_List"/>
|
||||
@@ -36,4 +47,4 @@
|
||||
from sys_role
|
||||
order by create_time desc
|
||||
</select>
|
||||
</mapper>
|
||||
</mapper>
|
||||
|
||||
@@ -2,12 +2,18 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysUserMapper">
|
||||
<sql id="where">
|
||||
<where>
|
||||
t.is_del=0
|
||||
<if test="u.id != null and u.id != ''">
|
||||
and t.id like concat('%', #{u.id}, '%')
|
||||
</if>
|
||||
<sql id="where">
|
||||
<where>
|
||||
t.is_del=0
|
||||
<if test="u.tenantIds != null and u.tenantIds.size > 0">
|
||||
and t.company in
|
||||
<foreach collection="u.tenantIds" item="tid" open="(" separator="," close=")">
|
||||
#{tid}
|
||||
</foreach>
|
||||
</if>
|
||||
<if test="u.id != null and u.id != ''">
|
||||
and t.id like concat('%', #{u.id}, '%')
|
||||
</if>
|
||||
<if test="u.username != null and u.username != ''">
|
||||
and t.username like concat('%', #{u.username}, '%')
|
||||
</if>
|
||||
@@ -40,4 +46,4 @@
|
||||
<include refid="where" />
|
||||
order by t.id desc
|
||||
</select>
|
||||
</mapper>
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user