提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.db;
H 2
3 import com.iailab.framework.common.util.object.BeanUtils;
4 import com.iailab.framework.mybatis.core.util.JdbcUtils;
5 import com.iailab.module.infra.controller.admin.db.vo.DataSourceConfigSaveReqVO;
6 import com.iailab.module.infra.dal.dataobject.db.DataSourceConfigDO;
7 import com.iailab.module.infra.dal.mysql.db.DataSourceConfigMapper;
8 import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
9 import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
10 import org.springframework.stereotype.Service;
11 import org.springframework.validation.annotation.Validated;
12
13 import javax.annotation.Resource;
14 import java.util.List;
15 import java.util.Objects;
16
17 import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
18 import static com.iailab.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS;
19 import static com.iailab.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_OK;
20
21 /**
22  * 数据源配置 Service 实现类
23  *
24  * @author iailab
25  */
26 @Service
27 @Validated
28 public class DataSourceConfigServiceImpl implements DataSourceConfigService {
29
30     @Resource
31     private DataSourceConfigMapper dataSourceConfigMapper;
32
33     @Resource
34     private DynamicDataSourceProperties dynamicDataSourceProperties;
35
36     @Override
37     public Long createDataSourceConfig(DataSourceConfigSaveReqVO createReqVO) {
38         DataSourceConfigDO config = BeanUtils.toBean(createReqVO, DataSourceConfigDO.class);
39         validateConnectionOK(config);
40
41         // 插入
42         dataSourceConfigMapper.insert(config);
43         // 返回
44         return config.getId();
45     }
46
47     @Override
48     public void updateDataSourceConfig(DataSourceConfigSaveReqVO updateReqVO) {
49         // 校验存在
50         validateDataSourceConfigExists(updateReqVO.getId());
51         DataSourceConfigDO updateObj = BeanUtils.toBean(updateReqVO, DataSourceConfigDO.class);
52         validateConnectionOK(updateObj);
53
54         // 更新
55         dataSourceConfigMapper.updateById(updateObj);
56     }
57
58     @Override
59     public void deleteDataSourceConfig(Long id) {
60         // 校验存在
61         validateDataSourceConfigExists(id);
62         // 删除
63         dataSourceConfigMapper.deleteById(id);
64     }
65
66     private void validateDataSourceConfigExists(Long id) {
67         if (dataSourceConfigMapper.selectById(id) == null) {
68             throw exception(DATA_SOURCE_CONFIG_NOT_EXISTS);
69         }
70     }
71
72     @Override
73     public DataSourceConfigDO getDataSourceConfig(Long id) {
74         // 如果 id 为 0,默认为 master 的数据源
75         if (Objects.equals(id, DataSourceConfigDO.ID_MASTER)) {
76             return buildMasterDataSourceConfig();
77         }
78         // 从 DB 中读取
79         return dataSourceConfigMapper.selectById(id);
80     }
81
82     @Override
83     public List<DataSourceConfigDO> getDataSourceConfigList() {
84         List<DataSourceConfigDO> result = dataSourceConfigMapper.selectList();
85         // 补充 master 数据源
86         result.add(0, buildMasterDataSourceConfig());
87         return result;
88     }
89
90     private void validateConnectionOK(DataSourceConfigDO config) {
91         boolean success = JdbcUtils.isConnectionOK(config.getUrl(), config.getUsername(), config.getPassword());
92         if (!success) {
93             throw exception(DATA_SOURCE_CONFIG_NOT_OK);
94         }
95     }
96
97     private DataSourceConfigDO buildMasterDataSourceConfig() {
98         String primary = dynamicDataSourceProperties.getPrimary();
99         DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource().get(primary);
100         return new DataSourceConfigDO().setId(DataSourceConfigDO.ID_MASTER).setName(primary)
101                 .setUrl(dataSourceProperty.getUrl())
102                 .setUsername(dataSourceProperty.getUsername())
103                 .setPassword(dataSourceProperty.getPassword());
104     }
105
106 }