first commit
This commit is contained in:
24
qiaoba-apis/qiaoba-api-tenant/pom.xml
Normal file
24
qiaoba-apis/qiaoba-api-tenant/pom.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>qiaoba-apis</artifactId>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>qiaoba-api-tenant</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<artifactId>qiaoba-common-datasource</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.qiaoba</groupId>
|
||||
<artifactId>qiaoba-common-doc</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,81 @@
|
||||
package com.qiaoba.api.tenant.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.qiaoba.common.base.entity.BaseEntity;
|
||||
import com.qiaoba.common.base.validate.AddGroup;
|
||||
import com.qiaoba.common.base.validate.EditGroup;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 租户 sys_tenant
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/30 10:20
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("sys_tenant")
|
||||
@NoArgsConstructor
|
||||
public class SysTenant extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId
|
||||
@NotNull(message = "id不能为空", groups = {EditGroup.class})
|
||||
private String tenantId;
|
||||
|
||||
@NotBlank(message = "企业名称不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "企业名称")
|
||||
private String companyName;
|
||||
|
||||
@Schema(description = "联系人")
|
||||
private String contactName;
|
||||
|
||||
@Schema(description = "联系电话")
|
||||
private String contactPhone;
|
||||
|
||||
@Schema(description = "地址")
|
||||
private String address;
|
||||
|
||||
@Schema(description = "企业简介")
|
||||
private String profile;
|
||||
|
||||
@Schema(description = "统一社会信用代码")
|
||||
private String licenseNumber;
|
||||
|
||||
@Schema(description = "域名")
|
||||
private String domain;
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
@NotNull(message = "过期时间不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "过期时间")
|
||||
private Date expireTime;
|
||||
|
||||
@NotNull(message = "用户数量不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "用户数量")
|
||||
private Long accountCount;
|
||||
|
||||
@NotNull(message = "状态不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "状态")
|
||||
private String status;
|
||||
|
||||
private String mode;
|
||||
|
||||
private String initialized;
|
||||
|
||||
public SysTenant(String tenantId, String status) {
|
||||
this.tenantId = tenantId;
|
||||
this.status = status;
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.qiaoba.api.tenant.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.qiaoba.common.base.entity.BaseEntity;
|
||||
import com.qiaoba.common.base.validate.AddGroup;
|
||||
import com.qiaoba.common.base.validate.EditGroup;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 租户数据源
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/7 14:38
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("sys_tenant_datasource")
|
||||
public class SysTenantDatasource extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId
|
||||
@NotNull(message = "id不能为空", groups = {EditGroup.class})
|
||||
private String datasourceId;
|
||||
|
||||
@NotBlank(message = "数据源类型不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "数据源类型")
|
||||
private String type;
|
||||
|
||||
@NotBlank(message = "数据库IP不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "数据库IP")
|
||||
private String ip;
|
||||
|
||||
@NotBlank(message = "数据库端口不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "数据库端口")
|
||||
private String port;
|
||||
|
||||
@NotBlank(message = "数据库名称不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "数据库名称")
|
||||
private String dbName;
|
||||
|
||||
@Schema(description = "模式名称")
|
||||
private String schemaName;
|
||||
|
||||
@NotBlank(message = "数据库账号不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "数据库账号")
|
||||
private String username;
|
||||
|
||||
@NotBlank(message = "数据库密码不能为空", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "数据库密码")
|
||||
private String password;
|
||||
|
||||
@Schema(description = "连接池-初始化大小-默认: 5")
|
||||
private Integer initCount;
|
||||
|
||||
@Schema(description = "连接池-最小空闲线程数-默认: 10")
|
||||
private Integer minCount;
|
||||
|
||||
@Schema(description = "连接池-最大连接池数量-默认: 20")
|
||||
private Integer maxCount;
|
||||
|
||||
private String isPrimary;
|
||||
|
||||
@NotBlank(message = "租户ID", groups = {AddGroup.class, EditGroup.class})
|
||||
@Schema(description = "租户ID")
|
||||
private String tenantId;
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.qiaoba.api.tenant.entity.param;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 租户数据源查询参数
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/9 13:18
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SysTenantDatasourceParam implements Serializable {
|
||||
|
||||
private String tenantId;
|
||||
|
||||
private String isPrimary;
|
||||
|
||||
private String ip;
|
||||
|
||||
public SysTenantDatasourceParam(String tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
public SysTenantDatasourceParam(String tenantId, String isPrimary) {
|
||||
this.tenantId = tenantId;
|
||||
this.isPrimary = isPrimary;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.qiaoba.api.tenant.entity.param;
|
||||
|
||||
import com.qiaoba.common.base.enums.BaseEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 租户查询条件
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/30 11:02
|
||||
*/
|
||||
@Data
|
||||
public class SysTenantParam implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 未过期
|
||||
*/
|
||||
public static final String TYPE_NOT_EXPIRED = "1";
|
||||
|
||||
/**
|
||||
* 已过期
|
||||
*/
|
||||
public static final String TYPE_EXPIRED = "2";
|
||||
|
||||
private String companyName;
|
||||
|
||||
private String contactName;
|
||||
|
||||
@Schema(description = "帐号状态(1正常 0停用)")
|
||||
private String status;
|
||||
|
||||
private Date time;
|
||||
|
||||
@Schema(description = "类型(1正常 2过期)")
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 是否是登陆接口
|
||||
*/
|
||||
private Boolean isLogin = false;
|
||||
|
||||
public static SysTenantParam buildNormalSelectParam() {
|
||||
SysTenantParam param = new SysTenantParam();
|
||||
param.setTime(new Date());
|
||||
param.setStatus(BaseEnum.NORMAL.getCode());
|
||||
param.setType(SysTenantParam.TYPE_NOT_EXPIRED);
|
||||
param.setIsLogin(true);
|
||||
return param;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.qiaoba.api.tenant.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 租户类型
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/5 16:33
|
||||
*/
|
||||
@Getter
|
||||
public enum TenantModeEnum {
|
||||
|
||||
/**
|
||||
* 字段模式-隔离级别最小
|
||||
*/
|
||||
COLUMN("1", "字段模式"),
|
||||
|
||||
/**
|
||||
* SCHEMA模式-隔离级别中等
|
||||
*/
|
||||
SCHEMA("2", "SCHEMA模式"),
|
||||
|
||||
/**
|
||||
* 数据源模式-隔离级别最大
|
||||
*/
|
||||
DATASOURCE("3", "数据源模式");
|
||||
|
||||
private final String mode;
|
||||
private final String info;
|
||||
|
||||
TenantModeEnum(String code, String info) {
|
||||
this.mode = code;
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public static Boolean isColumn(String mode) {
|
||||
return COLUMN.mode.equals(mode);
|
||||
}
|
||||
|
||||
public static Boolean isSchema(String mode) {
|
||||
return SCHEMA.mode.equals(mode);
|
||||
}
|
||||
|
||||
public static Boolean isDatasource(String mode) {
|
||||
return DATASOURCE.mode.equals(mode);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.qiaoba.api.tenant.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 租户状态
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/6/5 16:33
|
||||
*/
|
||||
@Getter
|
||||
public enum TenantStatusEnum {
|
||||
|
||||
/**
|
||||
* 禁用
|
||||
*/
|
||||
DISABLE("0", "禁用"),
|
||||
|
||||
/**
|
||||
* 过期
|
||||
*/
|
||||
EXPIRE("2", "过期");
|
||||
|
||||
|
||||
private final String status;
|
||||
private final String info;
|
||||
|
||||
TenantStatusEnum(String code, String info) {
|
||||
this.status = code;
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public static Boolean isDisable(String status) {
|
||||
return DISABLE.status.equals(status);
|
||||
}
|
||||
|
||||
public static Boolean isExpire(String status) {
|
||||
return EXPIRE.status.equals(status);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.qiaoba.api.tenant.service;
|
||||
|
||||
import com.qiaoba.api.tenant.entity.SysTenant;
|
||||
import com.qiaoba.api.tenant.entity.param.SysTenantParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 租户管理 对外接口暴露
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023-07-01 08:34:20
|
||||
*/
|
||||
public interface SysTenantApiService {
|
||||
|
||||
/**
|
||||
* 条件查询租户列表
|
||||
*
|
||||
* @param param 条件
|
||||
* @return list
|
||||
*/
|
||||
List<SysTenant> selectList(SysTenantParam param);
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.qiaoba.api.tenant.utils;
|
||||
|
||||
import com.qiaoba.api.tenant.entity.SysTenant;
|
||||
import com.qiaoba.api.tenant.enums.TenantModeEnum;
|
||||
import com.qiaoba.common.base.constants.TenantConstant;
|
||||
import com.qiaoba.common.base.context.BaseContext;
|
||||
import com.qiaoba.common.database.context.TenantDbTypeContext;
|
||||
|
||||
/**
|
||||
* TenantUtil
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023-07-01 08:34:20
|
||||
*/
|
||||
public class TenantUtil {
|
||||
|
||||
/**
|
||||
* 设置上下文信息
|
||||
*
|
||||
* @param sysTenant 租户
|
||||
*/
|
||||
public static void setContext(SysTenant sysTenant) {
|
||||
BaseContext.setTenantId(sysTenant.getTenantId());
|
||||
BaseContext.setSchema(sysTenant.getMode().equals(TenantModeEnum.SCHEMA.getMode()));
|
||||
// 数据源模式-设置第三方数据源
|
||||
if (TenantModeEnum.isDatasource(sysTenant.getMode())) {
|
||||
//设置当前租户对应的数据源
|
||||
BaseContext.setDataSource(sysTenant.getTenantId());
|
||||
BaseContext.setDatabaseType(TenantDbTypeContext.get(sysTenant.getTenantId()));
|
||||
}
|
||||
// 字段模式 or Schema模式-数据源选择默认数据源
|
||||
else {
|
||||
BaseContext.setDataSource(TenantConstant.DEFAULT_TENANT_ID);
|
||||
BaseContext.setDatabaseType(TenantDbTypeContext.getDefault());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user