提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.tenant; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
d66edb
|
4 |
import cn.hutool.core.util.StrUtil; |
H |
5 |
import com.google.common.annotations.VisibleForTesting; |
e7c126
|
6 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
H |
7 |
import com.iailab.framework.common.pojo.PageResult; |
|
8 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
9 |
import com.iailab.module.system.controller.admin.tenant.vo.packages.TenantPackagePageReqVO; |
|
10 |
import com.iailab.module.system.controller.admin.tenant.vo.packages.TenantPackageSaveReqVO; |
|
11 |
import com.iailab.module.system.dal.dataobject.tenant.TenantDO; |
|
12 |
import com.iailab.module.system.dal.dataobject.tenant.TenantPackageDO; |
|
13 |
import com.iailab.module.system.dal.mysql.tenant.TenantPackageMapper; |
|
14 |
import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
|
15 |
import org.springframework.context.annotation.Lazy; |
|
16 |
import org.springframework.stereotype.Service; |
|
17 |
import org.springframework.validation.annotation.Validated; |
|
18 |
|
|
19 |
import javax.annotation.Resource; |
|
20 |
import java.util.List; |
|
21 |
|
|
22 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
23 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
24 |
|
|
25 |
/** |
|
26 |
* 租户套餐 Service 实现类 |
|
27 |
* |
|
28 |
* @author iailab |
|
29 |
*/ |
|
30 |
@Service |
|
31 |
@Validated |
|
32 |
public class TenantPackageServiceImpl implements TenantPackageService { |
|
33 |
|
|
34 |
@Resource |
|
35 |
private TenantPackageMapper tenantPackageMapper; |
|
36 |
|
|
37 |
@Resource |
|
38 |
@Lazy // 避免循环依赖的报错 |
|
39 |
private TenantService tenantService; |
|
40 |
|
|
41 |
@Override |
|
42 |
public Long createTenantPackage(TenantPackageSaveReqVO createReqVO) { |
d66edb
|
43 |
// 校验套餐名是否重复 |
H |
44 |
validateTenantPackageNameUnique(null, createReqVO.getName()); |
e7c126
|
45 |
// 插入 |
H |
46 |
TenantPackageDO tenantPackage = BeanUtils.toBean(createReqVO, TenantPackageDO.class); |
|
47 |
tenantPackageMapper.insert(tenantPackage); |
|
48 |
// 返回 |
|
49 |
return tenantPackage.getId(); |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
@DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换 |
|
54 |
public void updateTenantPackage(TenantPackageSaveReqVO updateReqVO) { |
|
55 |
// 校验存在 |
|
56 |
TenantPackageDO tenantPackage = validateTenantPackageExists(updateReqVO.getId()); |
|
57 |
// 更新 |
|
58 |
TenantPackageDO updateObj = BeanUtils.toBean(updateReqVO, TenantPackageDO.class); |
|
59 |
tenantPackageMapper.updateById(updateObj); |
|
60 |
// 如果菜单发生变化,则修改每个租户的菜单 |
|
61 |
if (!CollUtil.isEqualList(tenantPackage.getMenuIds(), updateReqVO.getMenuIds())) { |
|
62 |
List<TenantDO> tenants = tenantService.getTenantListByPackageId(tenantPackage.getId()); |
|
63 |
tenants.forEach(tenant -> tenantService.updateTenantRoleMenu(tenant.getId(), updateReqVO.getMenuIds())); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public void deleteTenantPackage(Long id) { |
|
69 |
// 校验存在 |
|
70 |
validateTenantPackageExists(id); |
|
71 |
// 校验正在使用 |
|
72 |
validateTenantUsed(id); |
|
73 |
// 删除 |
|
74 |
tenantPackageMapper.deleteById(id); |
|
75 |
} |
|
76 |
|
|
77 |
private TenantPackageDO validateTenantPackageExists(Long id) { |
|
78 |
TenantPackageDO tenantPackage = tenantPackageMapper.selectById(id); |
|
79 |
if (tenantPackage == null) { |
|
80 |
throw exception(TENANT_PACKAGE_NOT_EXISTS); |
|
81 |
} |
|
82 |
return tenantPackage; |
|
83 |
} |
|
84 |
|
|
85 |
private void validateTenantUsed(Long id) { |
|
86 |
if (tenantService.getTenantCountByPackageId(id) > 0) { |
|
87 |
throw exception(TENANT_PACKAGE_USED); |
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
@Override |
|
92 |
public TenantPackageDO getTenantPackage(Long id) { |
|
93 |
return tenantPackageMapper.selectById(id); |
|
94 |
} |
|
95 |
|
|
96 |
@Override |
|
97 |
public PageResult<TenantPackageDO> getTenantPackagePage(TenantPackagePageReqVO pageReqVO) { |
|
98 |
return tenantPackageMapper.selectPage(pageReqVO); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public TenantPackageDO validTenantPackage(Long id) { |
|
103 |
TenantPackageDO tenantPackage = tenantPackageMapper.selectById(id); |
|
104 |
if (tenantPackage == null) { |
|
105 |
throw exception(TENANT_PACKAGE_NOT_EXISTS); |
|
106 |
} |
|
107 |
if (tenantPackage.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) { |
|
108 |
throw exception(TENANT_PACKAGE_DISABLE, tenantPackage.getName()); |
|
109 |
} |
|
110 |
return tenantPackage; |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public List<TenantPackageDO> getTenantPackageListByStatus(Integer status) { |
|
115 |
return tenantPackageMapper.selectListByStatus(status); |
|
116 |
} |
|
117 |
|
d66edb
|
118 |
@VisibleForTesting |
H |
119 |
void validateTenantPackageNameUnique(Long id, String name) { |
|
120 |
if (StrUtil.isBlank(name)) { |
|
121 |
return; |
|
122 |
} |
|
123 |
TenantPackageDO tenantPackage = tenantPackageMapper.selectByName(name); |
|
124 |
if (tenantPackage == null) { |
|
125 |
return; |
|
126 |
} |
|
127 |
// 如果 id 为空,说明不用比较是否为相同 id 的用户 |
|
128 |
if (id == null) { |
|
129 |
throw exception(TENANT_PACKAGE_NAME_DUPLICATE); |
|
130 |
} |
|
131 |
if (!tenantPackage.getId().equals(id)) { |
|
132 |
throw exception(TENANT_PACKAGE_NAME_DUPLICATE); |
|
133 |
} |
|
134 |
} |
|
135 |
|
e7c126
|
136 |
} |