Skip to content

Commit 2b6df2b

Browse files
committed
feat: add tanant API
1 parent fa1b0f1 commit 2b6df2b

File tree

22 files changed

+838
-110
lines changed

22 files changed

+838
-110
lines changed

app/src/main/resources/sql/h2/create_permission_table_ddl_2025_1029.sql

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,9 @@ create table `t_permission_role`
1313
unique index `u_idx_permission_role` (`name`) using btree
1414
) engine = innodb comment = '';
1515

16-
drop table if exists `r_tenant_user`;
17-
CREATE TABLE `r_tenant_user`
18-
(
19-
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
20-
`tenant_id` int NOT NULL COMMENT '租户id',
21-
`user_id` int NOT NULL COMMENT '用户id',
22-
PRIMARY KEY (`id`) USING BTREE,
23-
UNIQUE KEY `u_idx_tenant_user` (`tenant_id`,`user_id`) USING BTREE
24-
) engine = innodb comment = '租户和用户关系表';
25-
26-
drop table if exists `t_auth_users_units_roles`;
16+
drop table if exists `r_auth_users_units_roles`;
2717

28-
create table `t_auth_users_units_roles`
18+
create table `r_auth_users_units_roles`
2919
(
3020
`id` int not null auto_increment comment '主键id',
3121
`user_id` int not null comment '用户',

app/src/main/resources/sql/mysql/create_permission_table_ddl_2025_1029.sql

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,14 @@ create table `t_permission_role`
1313
unique index `u_idx_permission_role` (`name`) using btree
1414
) engine = innodb comment = '';
1515

16-
drop table if exists `r_tenant_user`;
17-
CREATE TABLE `r_tenant_user`
18-
(
19-
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id',
20-
`tenant_id` int NOT NULL COMMENT '租户id',
21-
`user_id` int NOT NULL COMMENT '用户id',
22-
PRIMARY KEY (`id`) USING BTREE,
23-
UNIQUE KEY `u_idx_tenant_user` (`tenant_id`,`user_id`) USING BTREE
24-
) engine = innodb comment = '租户和用户关系表';
25-
26-
drop table if exists `t_auth_users_units_roles`;
16+
drop table if exists `r_auth_users_units_roles`;
2717

28-
create table `t_auth_users_units_roles`
18+
create table `r_auth_users_units_roles`
2919
(
3020
`id` int not null auto_increment comment '主键id',
3121
`user_id` int not null comment '用户',
3222
`unit_id` int not null comment '业务单元',
33-
`unit_type` int not null comment '业务单元类型',
23+
`unit_type` varchar(60) not null comment '业务单元类型',
3424
`tenant_id` int not null comment '组织id',
3525
`role_id` int not null comment '角色id',
3626
`expired_time` timestamp comment '过期时间',

base/src/main/java/com/tinyengine/it/common/context/LoginUserContext.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
package com.tinyengine.it.common.context;
1313

14+
import com.tinyengine.it.model.entity.Tenant;
15+
16+
import java.util.List;
17+
1418
/**
1519
* 保存用户信息的上下文
1620
* 由集成方自行实现接口
@@ -20,29 +24,24 @@ public interface LoginUserContext {
2024
* 返回当前用户所诉的业务租户信息
2125
* @return 租户ID
2226
*/
23-
String getTenantId();
27+
List<Tenant> getTenants();
2428

2529
/**
2630
* 返回当前用户信息
31+
*
2732
* @return 用户ID
2833
*/
2934
String getLoginUserId();
3035

31-
/**
32-
* 返回当前用户所属业务租户信息
33-
* @return 业务租户ID
34-
*/
35-
String getRenterId();
36-
3736
/**
3837
* 返回当前设计器信息
3938
* @return 设计器ID
4039
*/
4140
int getPlatformId();
4241

4342
/**
44-
* getSiteId
45-
* @return Strinig
43+
* 设置当前组织信息
44+
* @praam tenants
4645
*/
47-
String getSiteId();
46+
void setTenants(List<Tenant> tenants);
4847
}

base/src/main/java/com/tinyengine/it/common/handler/MyMetaObjectHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void insertFill(MetaObject metaObject) {
4141
this.setFieldValByName("createdBy", loginUserContext.getLoginUserId(), metaObject);
4242
this.setFieldValByName("lastUpdatedBy", loginUserContext.getLoginUserId(), metaObject);
4343
this.setFieldValByName("platformId", loginUserContext.getPlatformId(), metaObject);
44-
fillStrategy(metaObject, "tenantId", loginUserContext.getTenantId());
44+
this.setFieldValByName("tenantId", loginUserContext.getTenants().get(0).getId(), metaObject);
4545
}
4646

4747
@Override
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.tinyengine.it.controller;
2+
3+
import com.tinyengine.it.common.base.Result;
4+
import com.tinyengine.it.common.exception.ExceptionEnum;
5+
import com.tinyengine.it.common.log.SystemControllerLog;
6+
import com.tinyengine.it.model.entity.Tenant;
7+
import com.tinyengine.it.service.platform.TenantService;
8+
import io.swagger.v3.oas.annotations.Operation;
9+
import io.swagger.v3.oas.annotations.Parameter;
10+
import io.swagger.v3.oas.annotations.media.Content;
11+
import io.swagger.v3.oas.annotations.media.Schema;
12+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
13+
import io.swagger.v3.oas.annotations.tags.Tag;
14+
import jakarta.validation.Valid;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.validation.annotation.Validated;
17+
import org.springframework.web.bind.annotation.GetMapping;
18+
import org.springframework.web.bind.annotation.PostMapping;
19+
import org.springframework.web.bind.annotation.RequestBody;
20+
import org.springframework.web.bind.annotation.RequestMapping;
21+
import org.springframework.web.bind.annotation.RequestParam;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
@Validated
28+
@RestController
29+
@RequestMapping("/platform-center/api")
30+
@Tag(name = "tenant")
31+
public class TenantController {
32+
/**
33+
* The tenant service.
34+
*/
35+
@Autowired
36+
private TenantService tenantService;
37+
38+
/**
39+
* 获取组织列表
40+
*
41+
* @return 返回值 all tenant
42+
*/
43+
@Operation(summary = "获取组织列表", description = "获取组织列表", responses = {
44+
@ApiResponse(responseCode = "200", description = "返回信息",
45+
content = @Content(mediaType = "application/json",
46+
schema = @Schema(implementation = Tenant.class))),
47+
@ApiResponse(responseCode = "400", description = "请求失败")
48+
})
49+
@SystemControllerLog(description = "获取组织列表")
50+
@GetMapping("/tenant/list")
51+
public Result<List<Tenant>> getAllTenant() {
52+
53+
return Result.success(tenantService.findAllTenant());
54+
}
55+
56+
/**
57+
* 新建组织
58+
*
59+
* @param tenant the tenant
60+
* @return tenant
61+
*/
62+
@Operation(summary = "新建组织", description = "新建组织",
63+
parameters = {
64+
@Parameter(name = "tenant", description = "入参对象")
65+
}, responses = {
66+
@ApiResponse(responseCode = "200", description = "返回信息",
67+
content = @Content(mediaType = "application/json",
68+
schema = @Schema(implementation = Tenant.class))),
69+
@ApiResponse(responseCode = "400", description = "请求失败")
70+
})
71+
@SystemControllerLog(description = "新建组织")
72+
@PostMapping("/tenant/create")
73+
public Result<Tenant> createTenant(@Valid @RequestBody Tenant tenant) {
74+
int result = tenantService.createTenant(tenant);
75+
return Result.success(tenantService.findTenantById(result));
76+
}
77+
78+
/**
79+
* 修改组织
80+
*
81+
* @param tenant the tenant
82+
* @return Tenant
83+
*/
84+
@Operation(summary = "修改组织", description = "修改组织",
85+
parameters = {
86+
@Parameter(name = "Tenant", description = "入参对象")
87+
}, responses = {
88+
@ApiResponse(responseCode = "200", description = "返回信息",
89+
content = @Content(mediaType = "application/json",
90+
schema = @Schema(implementation = Tenant.class))),
91+
@ApiResponse(responseCode = "400", description = "请求失败")
92+
})
93+
@SystemControllerLog(description = "修改组织")
94+
@PostMapping("/tenant/update")
95+
public Result<Tenant> updateTenant(@RequestBody Tenant tenant) {
96+
if (tenant.getId() == null || tenant.getId().isEmpty()) {
97+
return Result.failed(ExceptionEnum.CM002);
98+
}
99+
int result = tenantService.updateTenantById(tenant);
100+
if (result != 1) {
101+
return Result.failed(ExceptionEnum.CM002);
102+
}
103+
return Result.success(tenantService.findTenantById(Integer.valueOf(tenant.getId())));
104+
}
105+
106+
/**
107+
* 删除单个组织
108+
*
109+
* @param id the id
110+
* @return result
111+
*/
112+
@Operation(summary = "删除单个组织", description = "删除单个组织",
113+
parameters = {
114+
@Parameter(name = "id", description = "TenantId")
115+
}, responses = {
116+
@ApiResponse(responseCode = "200", description = "返回信息",
117+
content = @Content(mediaType = "application/json",
118+
schema = @Schema(implementation = Tenant.class))),
119+
@ApiResponse(responseCode = "400", description = "请求失败")
120+
})
121+
@SystemControllerLog(description = "删除单个组织")
122+
@GetMapping("/tenant/delete")
123+
public Result<Tenant> deleteTenant(@RequestParam Integer id) {
124+
int result = tenantService.deleteTenantById(id);
125+
if (result != 1) {
126+
return Result.failed(ExceptionEnum.CM009);
127+
}
128+
return Result.success(tenantService.findTenantById(id));
129+
}
130+
}

base/src/main/java/com/tinyengine/it/controller/UserController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.tinyengine.it.common.base.Result;
1616
import com.tinyengine.it.common.context.LoginUserContext;
1717
import com.tinyengine.it.common.log.SystemControllerLog;
18+
import com.tinyengine.it.mapper.AuthUsersUnitsRolesMapper;
19+
import com.tinyengine.it.model.entity.Tenant;
1820
import com.tinyengine.it.model.entity.User;
1921
import com.tinyengine.it.service.app.UserService;
2022

@@ -31,6 +33,8 @@
3133
import org.springframework.web.bind.annotation.RequestMapping;
3234
import org.springframework.web.bind.annotation.RestController;
3335

36+
import java.util.List;
37+
3438
/**
3539
* 查询用户信息
3640
*
@@ -54,6 +58,9 @@ public class UserController {
5458
@Autowired
5559
private LoginUserContext loginUserContext;
5660

61+
@Autowired
62+
AuthUsersUnitsRolesMapper authUsersUnitsRolesMapper;
63+
5764
/**
5865
* Me result.
5966
*
@@ -69,6 +76,8 @@ public class UserController {
6976
public Result<User> me() {
7077
String loginUserId = loginUserContext.getLoginUserId();
7178
User user = userService.queryUserById(loginUserId);
79+
List<Tenant> tenants = authUsersUnitsRolesMapper.queryAllTenantByUserId(Integer.valueOf(loginUserId));
80+
user.setTenant(tenants);
7281
if (user == null) {
7382
user = new User();
7483
user.setId(loginUserContext.getLoginUserId());

0 commit comments

Comments
 (0)