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