提交 | 用户 | 时间
|
71e81e
|
1 |
package cn.stylefeng.guns.tenant.service; |
懒 |
2 |
|
|
3 |
import cn.stylefeng.guns.base.db.context.DataSourceContext; |
|
4 |
import cn.stylefeng.guns.base.db.entity.DatabaseInfo; |
|
5 |
import cn.stylefeng.guns.base.db.factory.DataBaseInfoFactory; |
|
6 |
import cn.stylefeng.guns.base.db.model.params.DatabaseInfoParam; |
|
7 |
import cn.stylefeng.guns.base.db.service.DatabaseInfoService; |
|
8 |
import cn.stylefeng.guns.base.db.util.DbUtil; |
|
9 |
import cn.stylefeng.guns.base.db.util.SqlRunUtil; |
|
10 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory; |
|
11 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo; |
|
12 |
import cn.stylefeng.guns.base.tenant.entity.TenantInfo; |
|
13 |
import cn.stylefeng.guns.base.tenant.model.params.TenantInfoParam; |
|
14 |
import cn.stylefeng.guns.base.tenant.model.result.TenantInfoResult; |
|
15 |
import cn.stylefeng.guns.base.tenant.service.TenantInfoService; |
|
16 |
import cn.stylefeng.guns.sys.core.util.SaltUtil; |
|
17 |
import cn.stylefeng.guns.tenant.mapper.TenantInfoMapper; |
|
18 |
import cn.stylefeng.roses.core.config.properties.DruidProperties; |
|
19 |
import cn.stylefeng.roses.core.data.SqlExe; |
|
20 |
import cn.stylefeng.roses.core.util.SpringContextHolder; |
|
21 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
22 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
23 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
24 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
25 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
26 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
27 |
import org.springframework.stereotype.Service; |
|
28 |
import org.springframework.transaction.annotation.Transactional; |
|
29 |
|
|
30 |
import java.io.Serializable; |
|
31 |
import java.util.List; |
|
32 |
|
|
33 |
import static cn.stylefeng.guns.base.tenant.consts.TenantConstants.TENANT_DB_PREFIX; |
|
34 |
|
|
35 |
/** |
|
36 |
* <p> |
|
37 |
* 租户表 服务实现类 |
|
38 |
* </p> |
|
39 |
* |
|
40 |
* @author stylefeng |
|
41 |
* @since 2019-06-16 |
|
42 |
*/ |
|
43 |
@Service |
|
44 |
public class TenantInfoServiceImpl extends ServiceImpl<TenantInfoMapper, TenantInfo> implements TenantInfoService { |
|
45 |
|
|
46 |
@Override |
|
47 |
@Transactional(rollbackFor = Exception.class) |
|
48 |
public void add(TenantInfoParam param) { |
|
49 |
|
|
50 |
//创建租户数据库 |
|
51 |
DruidProperties druidProperties = SpringContextHolder.getBean(DruidProperties.class); |
|
52 |
String databaseName = TENANT_DB_PREFIX + param.getCode(); |
|
53 |
DbUtil.createDatabase(druidProperties, databaseName); |
|
54 |
|
|
55 |
//创建租户的数据源记录 |
|
56 |
DatabaseInfoService databaseInfoService = null; |
|
57 |
try { |
|
58 |
databaseInfoService = SpringContextHolder.getBean(DatabaseInfoService.class); |
|
59 |
} catch (Exception e) { |
|
60 |
throw new ServiceException(500, "请先开启数据源容器模块!"); |
|
61 |
} |
|
62 |
DatabaseInfoParam dataBaseInfo = DataBaseInfoFactory.createDataBaseInfo(druidProperties, databaseName); |
|
63 |
databaseInfoService.add(dataBaseInfo); |
|
64 |
|
|
65 |
//初始化租户的数据库 |
|
66 |
SqlRunUtil.runClassPathSql("tenant_init.sql", databaseName); |
|
67 |
|
|
68 |
//插入租户记录 |
|
69 |
TenantInfo entity = getEntity(param); |
|
70 |
entity.setDbName(databaseName); |
|
71 |
this.save(entity); |
|
72 |
|
|
73 |
//切换数据源到新的租户,初始化新租户的用户名和密码 |
|
74 |
String randomSalt = SaltUtil.getRandomSalt(); |
|
75 |
String md5 = SaltUtil.md5Encrypt(param.getAdminPassword(), randomSalt); |
|
76 |
|
|
77 |
SqlExe.update(DataSourceContext.getDataSources().get(databaseName), "update sys_user set password = ?, salt = ? where account = 'admin'", md5, randomSalt); |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
@Transactional(rollbackFor = Exception.class) |
|
82 |
public void delete(TenantInfoParam param) { |
|
83 |
|
|
84 |
//获取租户信息 |
|
85 |
TenantInfo tenantInfo = this.getById(param.getTenantId()); |
|
86 |
|
|
87 |
//删除租户信息 |
|
88 |
this.removeById(getKey(param)); |
|
89 |
|
|
90 |
//删除对应的数据源 |
|
91 |
DatabaseInfoService databaseInfoService = null; |
|
92 |
try { |
|
93 |
databaseInfoService = SpringContextHolder.getBean(DatabaseInfoService.class); |
|
94 |
} catch (Exception e) { |
|
95 |
throw new ServiceException(500, "请先开启数据源容器模块!"); |
|
96 |
} |
|
97 |
databaseInfoService.remove(new QueryWrapper<DatabaseInfo>().eq("db_name", tenantInfo.getDbName())); |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public void update(TenantInfoParam param) { |
|
102 |
TenantInfo oldEntity = getOldEntity(param); |
|
103 |
TenantInfo newEntity = getEntity(param); |
|
104 |
ToolUtil.copyProperties(newEntity, oldEntity); |
|
105 |
this.updateById(newEntity); |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public TenantInfoResult findBySpec(TenantInfoParam param) { |
|
110 |
return null; |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public List<TenantInfoResult> findListBySpec(TenantInfoParam param) { |
|
115 |
return null; |
|
116 |
} |
|
117 |
|
|
118 |
@Override |
|
119 |
public LayuiPageInfo findPageBySpec(TenantInfoParam param) { |
|
120 |
Page pageContext = getPageContext(); |
|
121 |
IPage page = this.baseMapper.customPageList(pageContext, param); |
|
122 |
return LayuiPageFactory.createPageInfo(page); |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public TenantInfo getByCode(String code) { |
|
127 |
return this.getOne(new QueryWrapper<TenantInfo>().eq("code", code)); |
|
128 |
} |
|
129 |
|
|
130 |
private Serializable getKey(TenantInfoParam param) { |
|
131 |
return param.getTenantId(); |
|
132 |
} |
|
133 |
|
|
134 |
private Page getPageContext() { |
|
135 |
return LayuiPageFactory.defaultPage(); |
|
136 |
} |
|
137 |
|
|
138 |
private TenantInfo getOldEntity(TenantInfoParam param) { |
|
139 |
return this.getById(getKey(param)); |
|
140 |
} |
|
141 |
|
|
142 |
private TenantInfo getEntity(TenantInfoParam param) { |
|
143 |
TenantInfo entity = new TenantInfo(); |
|
144 |
ToolUtil.copyProperties(param, entity); |
|
145 |
return entity; |
|
146 |
} |
|
147 |
|
|
148 |
} |