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