ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
package jnpf.base.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.mapper.DbLinkMapper;
import jnpf.base.model.dblink.PaginationDbLink;
import jnpf.base.service.DbLinkService;
import jnpf.base.service.SuperServiceImpl;
import jnpf.constant.MsgCode;
import jnpf.database.model.dto.PrepSqlDTO;
import jnpf.database.model.entity.DbLinkEntity;
import jnpf.database.source.DbBase;
import jnpf.database.util.*;
import jnpf.exception.DataException;
import jnpf.util.TenantHolder;
import lombok.Cleanup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.Connection;
import java.util.List;
 
/**
 * 数据连接
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Service
public class DbLinkServiceImpl extends SuperServiceImpl<DbLinkMapper, DbLinkEntity> implements DbLinkService, InitializingBean {
 
    @Autowired
    private DataSourceUtil dataSourceUtils;
 
    @Override
    public List<DbLinkEntity> getList() {
        return this.baseMapper.getList();
    }
 
    @Override
    public List<DbLinkEntity> getList(PaginationDbLink pagination) {
        return this.baseMapper.getList(pagination);
    }
 
    @Override
    public DbLinkEntity getInfo(String id) {
        return this.baseMapper.getInfo(id);
    }
 
    @Override
    public boolean isExistByFullName(String fullName, String id) {
        return this.baseMapper.isExistByFullName(fullName, id);
    }
 
    @Override
    public DbLinkEntity getInfoByFullName(String fullName) {
        return this.baseMapper.getInfoByFullName(fullName);
    }
 
    @Override
    public void create(DbLinkEntity entity) {
        this.baseMapper.create(entity);
    }
 
    @Override
    public boolean update(String id, DbLinkEntity entity) {
        return this.baseMapper.update(id, entity);
    }
 
    @Override
    public void delete(DbLinkEntity entity) {
        this.baseMapper.deleteById(entity);
    }
 
    @Override
    public boolean testDbConnection(DbLinkEntity entity) {
        //判断字典数据类型编码是否错误,大小写不敏感
        DbBase db = DbTypeUtil.getDb(entity);
        if (db == null) {
            throw new DataException(MsgCode.DB001.get());
        }
        try {
            @Cleanup Connection conn = ConnUtil.getConn(entity.getUserName(), entity.getPassword(), ConnUtil.getUrl(entity));
            return conn != null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 设置数据源
     *
     * @param dbLinkId 数据连接id
     * @throws DataException ignore
     */
    @Override
    public DbLinkEntity getResource(String dbLinkId) throws Exception {
        DbLinkEntity dbLinkEntity = new DbLinkEntity();
        //多租户是否开启
        if ("0".equals(dbLinkId)) {
            if (TenantDataSourceUtil.isTenantAssignDataSource()) {
                // 默认数据库, 租户管理指定租户数据源
                dbLinkEntity = TenantDataSourceUtil.getTenantAssignDataSource(TenantHolder.getDatasourceId()).toDbLink(new DbLinkEntity());
                dbLinkEntity.setId("0");
            } else {
                // 默认数据库查询,从配置获取数据源信息
                BeanUtils.copyProperties(dataSourceUtils, dbLinkEntity);
                dbLinkEntity.setId("0");
                // 是系统默认的多租户
                TenantDataSourceUtil.initDataSourceTenantDbName(dbLinkEntity);
            }
        } else {
            try {
                DynamicDataSourceUtil.switchToDataSource(null);
                dbLinkEntity = this.getInfo(dbLinkId);
            } finally {
                DynamicDataSourceUtil.clearSwitchDataSource();
            }
        }
        // 添加并且切换数据源
        return dbLinkEntity;
    }
 
    @Override
    public void afterPropertiesSet() {
        PrepSqlDTO.DB_LINK_FUN = (dbLinkId) -> {
            try {
                return (DbLinkEntity) getResource(dbLinkId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        };
    }
 
}