支持多层级租户,租户父子关系依托与组织机构的父子关系
This commit is contained in:
@@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.security.config.SecurityProperties;
|
||||
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
|
||||
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
||||
@@ -65,6 +66,7 @@ public class AuthController {
|
||||
@PostMapping("/login")
|
||||
@PermitAll
|
||||
@Operation(summary = "使用账号密码登录")
|
||||
@TenantIgnore
|
||||
public CommonResult<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) {
|
||||
return success(authService.login(reqVO));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.controller.admin.dept;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
|
||||
@@ -12,16 +13,19 @@ import cn.iocoder.yudao.module.system.service.dept.DeptService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Tag(name = "管理后台 - 部门")
|
||||
@RestController
|
||||
@RequestMapping("/system/dept")
|
||||
@@ -42,6 +46,7 @@ public class DeptController {
|
||||
@PutMapping("update")
|
||||
@Operation(summary = "更新部门")
|
||||
@PreAuthorize("@ss.hasPermission('system:dept:update')")
|
||||
@TenantIgnore
|
||||
public CommonResult<Boolean> updateDept(@Valid @RequestBody DeptSaveReqVO updateReqVO) {
|
||||
deptService.updateDept(updateReqVO);
|
||||
return success(true);
|
||||
|
||||
@@ -5,6 +5,9 @@ import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Schema(description = "管理后台 - 部门信息 Response VO")
|
||||
@Data
|
||||
public class DeptRespVO {
|
||||
@@ -36,4 +39,10 @@ public class DeptRespVO {
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "时间戳格式")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(description = "组织机构是否租户", example = "true")
|
||||
private Boolean isTenant = false;
|
||||
|
||||
@Schema(description = "租户编号", example = "1024")
|
||||
private Long tenantId;
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Schema(description = "管理后台 - 部门创建/修改 Request VO")
|
||||
@Data
|
||||
public class DeptSaveReqVO {
|
||||
@@ -45,4 +48,10 @@ public class DeptSaveReqVO {
|
||||
@InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "组织机构是否租户", example = "true")
|
||||
private Boolean isTenant = false;
|
||||
|
||||
@Schema(description = "租户编号", example = "1024")
|
||||
private Long tenantId;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Schema(description = "管理后台 - 部门精简信息 Response VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
|
||||
@@ -43,7 +43,7 @@ public class RoleSaveReqVO {
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "角色类型", example = "1 内置角色,2 标准角色,3 租户自定义角色")
|
||||
private String type;
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "父级角色Id", example = "-1 系统角色,0 顶级角色")
|
||||
private Long parentId;
|
||||
|
||||
@@ -10,6 +10,9 @@ import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Schema(description = "管理后台 - 租户 Response VO")
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package cn.iocoder.yudao.module.system.controller.admin.usertenant;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSimpleRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantSaveReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.usertenant.UserTenantDO;
|
||||
import cn.iocoder.yudao.module.system.service.usertenant.UserTenantService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Tag(name = "管理后台 - 用户-租户关系")
|
||||
@RestController
|
||||
@RequestMapping("/system/user-tenant")
|
||||
@Validated
|
||||
public class UserTenantController {
|
||||
|
||||
@Resource
|
||||
private UserTenantService userTenantService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建用户-租户关系")
|
||||
@PreAuthorize("@ss.hasPermission('system:user-tenant:create')")
|
||||
public CommonResult<Long> createUserTenant(@Valid @RequestBody UserTenantSaveReqVO createReqVO) {
|
||||
return success(userTenantService.createUserTenant(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新用户-租户关系")
|
||||
@PreAuthorize("@ss.hasPermission('system:user-tenant:update')")
|
||||
public CommonResult<Boolean> updateUserTenant(@Valid @RequestBody UserTenantSaveReqVO updateReqVO) {
|
||||
userTenantService.updateUserTenant(updateReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除用户-租户关系")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('system:user-tenant:delete')")
|
||||
public CommonResult<Boolean> deleteUserTenant(@RequestParam("id") Long id) {
|
||||
userTenantService.deleteUserTenant(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete-list")
|
||||
@Parameter(name = "ids", description = "编号", required = true)
|
||||
@Operation(summary = "批量删除用户-租户关系")
|
||||
@PreAuthorize("@ss.hasPermission('system:user-tenant:delete')")
|
||||
public CommonResult<Boolean> deleteUserTenantList(@RequestParam("ids") List<Long> ids) {
|
||||
userTenantService.deleteUserTenantListByIds(ids);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得用户-租户关系")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('system:user-tenant:query')")
|
||||
public CommonResult<UserTenantRespVO> getUserTenant(@RequestParam("id") Long id) {
|
||||
UserTenantDO userTenant = userTenantService.getUserTenant(id);
|
||||
return success(BeanUtils.toBean(userTenant, UserTenantRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得用户-租户关系分页")
|
||||
@PreAuthorize("@ss.hasPermission('system:user-tenant:query')")
|
||||
public CommonResult<PageResult<UserTenantRespVO>> getUserTenantPage(@Valid UserTenantPageReqVO pageReqVO) {
|
||||
PageResult<UserTenantDO> pageResult = userTenantService.getUserTenantPage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, UserTenantRespVO.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户租户关系 simpleList 查询接口
|
||||
*/
|
||||
@GetMapping("/user-tenant-simple-list")
|
||||
@Operation(summary = "获得用户-租户关系 simpleList 列表")
|
||||
public CommonResult<List<UserTenantRespVO>> getUserTenantSimpleList(@Valid UserTenantPageReqVO pageReqVO) {
|
||||
List<UserTenantDO> list = userTenantService.getUserTenantList(pageReqVO);
|
||||
// 追加当前用户归属的租户信息
|
||||
if (CollUtil.isNotEmpty(list)) {
|
||||
list.add(new UserTenantDO()
|
||||
.setUserId(getLoginUserId())
|
||||
.setTenantId(TenantContextHolder.getTenantId()));
|
||||
}
|
||||
return success(BeanUtils.toBean(list, UserTenantRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/export-excel")
|
||||
@Operation(summary = "导出用户-租户关系 Excel")
|
||||
@PreAuthorize("@ss.hasPermission('system:user-tenant:export')")
|
||||
@ApiAccessLog(operateType = EXPORT)
|
||||
public void exportUserTenantExcel(@Valid UserTenantPageReqVO pageReqVO,
|
||||
HttpServletResponse response) throws IOException {
|
||||
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
||||
List<UserTenantDO> list = userTenantService.getUserTenantPage(pageReqVO).getList();
|
||||
// 导出 Excel
|
||||
ExcelUtils.write(response, "用户-租户关系.xls", "数据", UserTenantRespVO.class,
|
||||
BeanUtils.toBean(list, UserTenantRespVO.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户 simpleList 查询接口
|
||||
*/
|
||||
@GetMapping("/user-simple-list")
|
||||
@Operation(summary = "获得用户 simpleList 列表")
|
||||
// 不使用默认的租户查询方式,此处需要获取租户以及下属租户的相关数据,在 mapper 自行实现
|
||||
@TenantIgnore
|
||||
public CommonResult<List<UserSimpleRespVO>> getUserSimpleList() {
|
||||
return success(userTenantService.getUserBelongSimpleList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 租户 simpleList 查询接口
|
||||
*/
|
||||
@GetMapping("/tenant-simple-list")
|
||||
@Operation(summary = "获得租户 simpleList 列表")
|
||||
// 不使用默认的租户查询方式,此处需要获取租户以及下属租户的相关数据,在 mapper 自行实现
|
||||
@TenantIgnore
|
||||
public CommonResult<List<TenantRespVO>> getTenantSimpleList() {
|
||||
return success(userTenantService.getTenantBelongSimpleList());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.yudao.module.system.controller.admin.usertenant.vo;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@Schema(description = "管理后台 - 用户-租户关系分页 Request VO")
|
||||
@Data
|
||||
public class UserTenantPageReqVO extends PageParam {
|
||||
|
||||
@Schema(description = "用户ID", example = "32426")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "租户ID", example = "30027")
|
||||
private Long tenantId;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
private LocalDateTime[] createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.yudao.module.system.controller.admin.usertenant.vo;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Schema(description = "管理后台 - 用户-租户关系 Response VO")
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
public class UserTenantRespVO {
|
||||
|
||||
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26967")
|
||||
@ExcelProperty("主键ID")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "用户ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "32426")
|
||||
@ExcelProperty("用户ID")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.yudao.module.system.controller.admin.usertenant.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "管理后台 - 用户-租户关系新增/修改 Request VO")
|
||||
@Data
|
||||
public class UserTenantSaveReqVO {
|
||||
|
||||
@Schema(description = "主键ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26967")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "用户ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "32426")
|
||||
@NotNull(message = "用户ID不能为空")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "租户ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "30027")
|
||||
@NotNull(message = "租户ID不能为空")
|
||||
private Long tenantId;
|
||||
|
||||
}
|
||||
@@ -13,8 +13,7 @@ import lombok.EqualsAndHashCode;
|
||||
/**
|
||||
* 部门表
|
||||
*
|
||||
* @author ruoyi
|
||||
* @author 芋道源码
|
||||
* @author 管理员
|
||||
*/
|
||||
@TableName("system_dept")
|
||||
@KeySequence("system_dept_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@@ -63,5 +62,10 @@ public class DeptDO extends TenantBaseDO {
|
||||
* 枚举 {@link CommonStatusEnum}
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 组织机构是否为租户
|
||||
*/
|
||||
private Boolean isTenant;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package cn.iocoder.yudao.module.system.dal.dataobject.user;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
@@ -23,7 +23,7 @@ import java.util.Set;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AdminUserDO extends TenantBaseDO {
|
||||
public class AdminUserDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.yudao.module.system.dal.dataobject.usertenant;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* 用户-租户关系 DO
|
||||
*
|
||||
* @author 管理员
|
||||
*/
|
||||
@TableName("system_user_tenant")
|
||||
@KeySequence("system_user_tenant_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UserTenantDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
private Long id;
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Long userId;
|
||||
/**
|
||||
* 多租户编号
|
||||
*/
|
||||
private Long tenantId;
|
||||
|
||||
}
|
||||
@@ -4,11 +4,15 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Mapper
|
||||
public interface DeptMapper extends BaseMapperX<DeptDO> {
|
||||
|
||||
@@ -34,4 +38,26 @@ public interface DeptMapper extends BaseMapperX<DeptDO> {
|
||||
return selectList(DeptDO::getLeaderUserId, id);
|
||||
}
|
||||
|
||||
default DeptDO selectByTenantIdAndIsTenant(Long tenantId) {
|
||||
return selectOne(new LambdaQueryWrapperX<DeptDO>()
|
||||
.eq(DeptDO::getTenantId, tenantId)
|
||||
.eq(DeptDO::getIsTenant, true));
|
||||
}
|
||||
|
||||
default DeptDO selectByTenantIdAndIsTenantExcludeId(Long tenantId, Long excludeId) {
|
||||
return selectOne(new LambdaQueryWrapperX<DeptDO>()
|
||||
.eq(DeptDO::getTenantId, tenantId)
|
||||
.eq(DeptDO::getIsTenant, true)
|
||||
.ne(DeptDO::getId, excludeId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新指定部门ID列表的租户ID
|
||||
*/
|
||||
default void updateTenantIdBatch(List<Long> ids,Long tenantId){
|
||||
UpdateWrapper<DeptDO> wrapper = new UpdateWrapper<>();
|
||||
wrapper.in("id", ids).set("tenant_id", tenantId);
|
||||
update(null, wrapper);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author chenbowen
|
||||
*/
|
||||
@Mapper
|
||||
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.yudao.module.system.dal.mysql.usertenant;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.usertenant.UserTenantDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 用户-租户关系 Mapper
|
||||
*
|
||||
* @author 管理员
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserTenantMapper extends BaseMapperX<UserTenantDO> {
|
||||
|
||||
default PageResult<UserTenantDO> selectPage(UserTenantPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<UserTenantDO>()
|
||||
.eqIfPresent(UserTenantDO::getUserId, reqVO.getUserId())
|
||||
.eqIfPresent(UserTenantDO::getTenantId, reqVO.getTenantId())
|
||||
.betweenIfPresent(UserTenantDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(UserTenantDO::getId));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,9 +49,20 @@ public class DeptServiceImpl implements DeptService {
|
||||
// 校验部门名的唯一性
|
||||
validateDeptNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
||||
|
||||
// 插入部门
|
||||
// 新建为主体公司时,校验租户唯一性(一个租户只能关联一个组织机构)
|
||||
if (Boolean.TRUE.equals(createReqVO.getIsTenant())) {
|
||||
DeptDO exist = deptMapper.selectByTenantIdAndIsTenant(createReqVO.getTenantId());
|
||||
if (exist != null) {
|
||||
throw exception(DEPT_TENANT_RELATION_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
DeptDO dept = BeanUtils.toBean(createReqVO, DeptDO.class);
|
||||
if (Boolean.TRUE.equals(createReqVO.getIsTenant())) {
|
||||
dept.setTenantId(createReqVO.getTenantId());
|
||||
}
|
||||
deptMapper.insert(dept);
|
||||
|
||||
return dept.getId();
|
||||
}
|
||||
|
||||
@@ -69,7 +80,33 @@ public class DeptServiceImpl implements DeptService {
|
||||
// 校验部门名的唯一性
|
||||
validateDeptNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
|
||||
|
||||
// 更新部门
|
||||
DeptDO oldDept = deptMapper.selectById(updateReqVO.getId());
|
||||
boolean oldIsTenant = oldDept.getIsTenant();
|
||||
boolean newIsTenant = updateReqVO.getIsTenant();
|
||||
|
||||
if (newIsTenant) {
|
||||
// 1. 校验租户唯一性
|
||||
DeptDO exist = deptMapper.selectByTenantIdAndIsTenantExcludeId(updateReqVO.getTenantId(), updateReqVO.getId());
|
||||
if (exist != null) {
|
||||
throw exception(DEPT_TENANT_RELATION_EXISTS);
|
||||
}
|
||||
// 2. 设置当前组织租户Id
|
||||
oldDept.setTenantId(updateReqVO.getTenantId());
|
||||
// 3. 递归设置所有下属非主体公司组织的租户Id为当前租户Id
|
||||
updateChildrenTenantId(updateReqVO.getId(), updateReqVO.getTenantId());
|
||||
} else if (oldIsTenant) {
|
||||
// 获取父级组织机构的租户Id
|
||||
Long parentTenantId = null;
|
||||
if (oldDept.getParentId() != null && !DeptDO.PARENT_ID_ROOT.equals(oldDept.getParentId())) {
|
||||
DeptDO parentDept = deptMapper.selectById(oldDept.getParentId());
|
||||
if (parentDept != null) {
|
||||
parentTenantId = parentDept.getTenantId();
|
||||
}
|
||||
}
|
||||
// 递归设置所有下属非主体公司组织的租户Id为父级组织机构的租户Id
|
||||
updateChildrenTenantId(updateReqVO.getId(), parentTenantId);
|
||||
}
|
||||
|
||||
DeptDO updateObj = BeanUtils.toBean(updateReqVO, DeptDO.class);
|
||||
deptMapper.updateById(updateObj);
|
||||
}
|
||||
@@ -84,7 +121,8 @@ public class DeptServiceImpl implements DeptService {
|
||||
if (deptMapper.selectCountByParentId(id) > 0) {
|
||||
throw exception(DEPT_EXITS_CHILDREN);
|
||||
}
|
||||
// 删除部门
|
||||
DeptDO dept = deptMapper.selectById(id);
|
||||
// 删除为主体公司时无需处理关系表,仅常规删除
|
||||
deptMapper.deleteById(id);
|
||||
}
|
||||
|
||||
@@ -220,4 +258,32 @@ public class DeptServiceImpl implements DeptService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归设置指定组织及其所有下属非主体公司组织的租户Id,批量更新。
|
||||
* 跳过所有租户组织及其下属组织(不包含自身)。
|
||||
*/
|
||||
private void updateChildrenTenantId(Long parentId, Long tenantId) {
|
||||
List<Long> updateIds = new ArrayList<>();
|
||||
Queue<DeptDO> queue = new LinkedList<>();
|
||||
List<DeptDO> children = deptMapper.selectListByParentId(Collections.singleton(parentId));
|
||||
if (children != null && !children.isEmpty()) {
|
||||
queue.addAll(children);
|
||||
}
|
||||
while (!queue.isEmpty()) {
|
||||
DeptDO current = queue.poll();
|
||||
if (current.getIsTenant() != null && current.getIsTenant()) {
|
||||
// 跳过当前租户组织及其所有下属
|
||||
continue;
|
||||
}
|
||||
updateIds.add(current.getId());
|
||||
List<DeptDO> subChildren = deptMapper.selectListByParentId(Collections.singleton(current.getId()));
|
||||
if (subChildren != null && !subChildren.isEmpty()) {
|
||||
queue.addAll(subChildren);
|
||||
}
|
||||
}
|
||||
if (!updateIds.isEmpty()) {
|
||||
deptMapper.updateTenantIdBatch(updateIds, tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -99,8 +99,8 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
createReqVO.getMobile(), createReqVO.getEmail(), createReqVO.getDeptId(), createReqVO.getPostIds());
|
||||
// 2.1 插入用户
|
||||
AdminUserDO user = BeanUtils.toBean(createReqVO, AdminUserDO.class);
|
||||
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
|
||||
user.setPassword(encodePassword(createReqVO.getPassword())); // 加密密码
|
||||
user.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
user.setPassword(encodePassword(createReqVO.getPassword()));
|
||||
userMapper.insert(user);
|
||||
// 2.2 插入关联岗位
|
||||
if (CollectionUtil.isNotEmpty(user.getPostIds())) {
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package cn.iocoder.yudao.module.system.service.usertenant;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSimpleRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantSaveReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.usertenant.UserTenantDO;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户-租户关系 Service 接口
|
||||
*
|
||||
* @author 管理员
|
||||
*/
|
||||
public interface UserTenantService {
|
||||
|
||||
/**
|
||||
* 创建用户-租户关系
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createUserTenant(@Valid UserTenantSaveReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新用户-租户关系
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updateUserTenant(@Valid UserTenantSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除用户-租户关系
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteUserTenant(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除用户-租户关系
|
||||
*
|
||||
* @param ids 编号
|
||||
*/
|
||||
void deleteUserTenantListByIds(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得用户-租户关系
|
||||
*
|
||||
* @param id 编号
|
||||
* @return 用户-租户关系
|
||||
*/
|
||||
UserTenantDO getUserTenant(Long id);
|
||||
|
||||
/**
|
||||
* 获得用户-租户关系分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @return 用户-租户关系分页
|
||||
*/
|
||||
PageResult<UserTenantDO> getUserTenantPage(UserTenantPageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 获得用户-租户关系列表
|
||||
*
|
||||
* @param pageReqVO 列表查询
|
||||
* @return 用户-租户关系列表
|
||||
*/
|
||||
List<UserTenantDO> getUserTenantList(UserTenantPageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 获得用户 simpleList 列表 !!!注意,此方法查询的用户列表,包含了租户下属租户的所有用户
|
||||
* @return 用户列表(只含 id、nickname)
|
||||
*/
|
||||
List<UserSimpleRespVO> getUserBelongSimpleList();
|
||||
|
||||
/**
|
||||
* 获得租户 simpleList 列表
|
||||
* @return 租户列表(只含 id、name)
|
||||
*/
|
||||
List<TenantRespVO> getTenantBelongSimpleList();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package cn.iocoder.yudao.module.system.service.usertenant;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSimpleRespVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.usertenant.vo.UserTenantSaveReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.usertenant.UserTenantDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.usertenant.UserTenantMapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 用户-租户关系 Service 实现类
|
||||
*
|
||||
* @author 管理员
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class UserTenantServiceImpl implements UserTenantService {
|
||||
|
||||
@Resource
|
||||
private UserTenantMapper userTenantMapper;
|
||||
@Resource
|
||||
private AdminUserMapper adminUserMapper;
|
||||
@Resource
|
||||
private TenantMapper tenantMapper;
|
||||
@Resource
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
@Override
|
||||
public Long createUserTenant(UserTenantSaveReqVO createReqVO) {
|
||||
// 插入
|
||||
UserTenantDO userTenant = BeanUtils.toBean(createReqVO, UserTenantDO.class);
|
||||
userTenantMapper.insert(userTenant);
|
||||
// 返回
|
||||
return userTenant.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUserTenant(UserTenantSaveReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
validateUserTenantExists(updateReqVO.getId());
|
||||
// 更新
|
||||
UserTenantDO updateObj = BeanUtils.toBean(updateReqVO, UserTenantDO.class);
|
||||
userTenantMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteUserTenant(Long id) {
|
||||
// 校验存在
|
||||
validateUserTenantExists(id);
|
||||
// 删除
|
||||
userTenantMapper.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteUserTenantListByIds(List<Long> ids) {
|
||||
// 校验存在
|
||||
validateUserTenantExists(ids);
|
||||
// 删除
|
||||
userTenantMapper.deleteByIds(ids);
|
||||
}
|
||||
|
||||
private void validateUserTenantExists(List<Long> ids) {
|
||||
List<UserTenantDO> list = userTenantMapper.selectByIds(ids);
|
||||
if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
|
||||
throw exception(USER_TENANT_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateUserTenantExists(Long id) {
|
||||
if (userTenantMapper.selectById(id) == null) {
|
||||
throw exception(USER_TENANT_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserTenantDO getUserTenant(Long id) {
|
||||
return userTenantMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<UserTenantDO> getUserTenantPage(UserTenantPageReqVO pageReqVO) {
|
||||
return userTenantMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserTenantDO> getUserTenantList(UserTenantPageReqVO pageReqVO) {
|
||||
return userTenantMapper.selectList(new LambdaQueryWrapper<UserTenantDO>()
|
||||
.eq(pageReqVO.getUserId() != null, UserTenantDO::getUserId, pageReqVO.getUserId())
|
||||
.eq(pageReqVO.getTenantId() != null, UserTenantDO::getTenantId, pageReqVO.getTenantId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据当前租户ID获取租户部门
|
||||
*/
|
||||
private DeptDO getTenantDeptOrThrow() {
|
||||
LoginUser loginUser = getLoginUser();
|
||||
if (loginUser == null) {
|
||||
throw exception(USER_NOT_EXISTS);
|
||||
}
|
||||
Long tenantId = loginUser.getTenantId();
|
||||
if (tenantId == null) {
|
||||
throw exception(TENANT_NOT_EXISTS);
|
||||
}
|
||||
DeptDO deptDO = deptMapper.selectByTenantIdAndIsTenant(tenantId);
|
||||
if (deptDO == null) {
|
||||
throw exception(TENANT_NOT_EXISTS);
|
||||
}
|
||||
return deptDO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前租户组织及其所有下属组织ID(包含自身)
|
||||
*/
|
||||
private List<Long> getTenantAndSubDeptIds(Long tenantDeptId) {
|
||||
List<Long> deptIds = new ArrayList<>();
|
||||
deptIds.add(tenantDeptId);
|
||||
List<DeptDO> children = getAllSubDepts(tenantDeptId);
|
||||
for (DeptDO child : children) {
|
||||
deptIds.add(child.getId());
|
||||
}
|
||||
return deptIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前租户组织机构及其所有下属组织下的所有启用用户
|
||||
*/
|
||||
@Override
|
||||
public List<UserSimpleRespVO> getUserBelongSimpleList() {
|
||||
DeptDO tenantDept = getTenantDeptOrThrow();
|
||||
List<Long> deptIds = getTenantAndSubDeptIds(tenantDept.getId());
|
||||
// 查询这些组织下的所有用户
|
||||
List<AdminUserDO> userList = adminUserMapper.selectListByDeptIds(deptIds);
|
||||
List<UserSimpleRespVO> result = new ArrayList<>();
|
||||
for (AdminUserDO user : userList) {
|
||||
if (CommonStatusEnum.ENABLE.getStatus().equals(user.getStatus())) {
|
||||
UserSimpleRespVO vo = new UserSimpleRespVO();
|
||||
vo.setId(user.getId());
|
||||
vo.setNickname(user.getNickname());
|
||||
// vo.setUsername(user.getUsername());
|
||||
result.add(vo);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有启用状态的组织机构(不限制 isTenant),支持多层嵌套(中间可有非租户组织)。
|
||||
* 返回所有启用状态的组织及其 parentId,便于前端递归组装树状结构。
|
||||
*/
|
||||
@Override
|
||||
public List<TenantRespVO> getTenantBelongSimpleList() {
|
||||
DeptDO tenantDept = getTenantDeptOrThrow();
|
||||
List<DeptDO> children = getAllSubDepts(tenantDept.getId());
|
||||
Set<Long> tenantIdsSets = convertSet(children, DeptDO::getTenantId, DeptDO::getIsTenant);
|
||||
// 添加自身的租户ID
|
||||
tenantIdsSets.add(tenantDept.getTenantId());
|
||||
List<TenantDO> tenants = tenantMapper.selectByIds(tenantIdsSets);
|
||||
return BeanUtils.toBean(tenants, TenantRespVO.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归获取所有下属部门(不含自身)
|
||||
*/
|
||||
private List<DeptDO> getAllSubDepts(Long parentId) {
|
||||
List<DeptDO> result = new ArrayList<>();
|
||||
Queue<Long> queue = new LinkedList<>();
|
||||
queue.add(parentId);
|
||||
while (!queue.isEmpty()) {
|
||||
Long pid = queue.poll();
|
||||
List<DeptDO> children = deptMapper.selectListByParentId(Collections.singleton(pid));
|
||||
if (CollUtil.isNotEmpty(children)) {
|
||||
result.addAll(children);
|
||||
for (DeptDO child : children) {
|
||||
queue.add(child.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ spring:
|
||||
|
||||
# rocketmq 配置项,对应 RocketMQProperties 配置类
|
||||
rocketmq:
|
||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv
|
||||
name-server: 172.16.46.63:30876 # RocketMQ Namesrv
|
||||
|
||||
spring:
|
||||
# RabbitMQ 配置项,对应 RabbitProperties 配置类
|
||||
|
||||
Reference in New Issue
Block a user