许鹏程
2024-08-15 112f25b820aea7b0487197d1516974589d1d9979
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
package com.product.data.service;
 
import cn.hutool.core.lang.UUID;
import com.product.admin.service.PublicService;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.data.config.CmnConst;
import com.product.data.config.DatabaseType;
import com.product.data.connection.ConnectionManager;
import com.product.data.entity.DatabaseEntity;
import com.product.data.service.impl.IDataSourceConfigService;
import com.product.data.utli.QueryDataService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
 
/**
 * @Author cheng
 * @Date 2024/8/13 18:20
 * @Desc 数据源配置业务层
 */
@Service
public class DataSourceConfigService extends AbstractBaseService implements IDataSourceConfigService {
 
 
    @Resource
    private SyncDataConfigService syncDataConfigService;
    @Resource
    private PublicService publicService;
 
    @Override
    public void save(FieldSetEntity fse) throws BaseException {
        DatabaseEntity dbe = new DatabaseEntity(fse);
        String uuid = fse.getUUID();
        if (StringUtils.isEmpty(uuid)) {
            fse.setValue("~type~", "add");
            fse.setValue("uuid", UUID.randomUUID().toString());
            uuid = fse.getUUID();
        }
        String finalUuid = uuid;
        try (Connection connection = ConnectionManager.getConnection(dbe)) {
            if (connection != null) {
                QueryDataService queryDataService = new QueryDataService(connection);
                String[] tableName = syncDataConfigService.getTableName(queryDataService, dbe);
                if ((tableName != null && tableName.length > 0) || DatabaseType.PSQL.equals(dbe.getDbType()) || DatabaseType.Oracle.equals(dbe.getDbType())) {
                    Map<String, String> fieldTypeReference = syncDataConfigService.getFieldTypeReference(dbe.getDbType());
                    DataTableEntity dt = new DataTableEntity();
                    if (DatabaseType.Oracle.equals(dbe.getDbType())) {
                        Map<String, List<Map<String, Object>>> tableFieldMap = syncDataConfigService.getOracleFieldInfo(queryDataService);
                        tableFieldMap.forEach((k, v) -> {
                            syncDataConfigService.getFieldName(CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG_FIELD, queryDataService, k, dbe, finalUuid, dt, fieldTypeReference, v);
                        });
                    } else if (DatabaseType.PSQL.equals(dbe.getDbType())) {
                        Map<String, List<Map<String, Object>>> pSqlFieldInfo = syncDataConfigService.getPSqlFieldInfo(dbe.getDbInstance(), queryDataService);
                        pSqlFieldInfo.forEach((k, v) -> {
                            syncDataConfigService.getFieldName(CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG_FIELD, queryDataService, k, dbe, finalUuid, dt, fieldTypeReference, v);
                        });
                    } else {
                        for (String table : tableName) {
                            syncDataConfigService.getFieldName(CmnConst.TABLE_PRODUCT_SYS_DATASOURCE_CONFIG_FIELD, queryDataService, table, dbe, finalUuid, dt, fieldTypeReference, null);
                        }
                    }
                    if (!DataTableEntity.isEmpty(dt)) {
                        fse.addSubDataTable(dt);
                    }
                }
                getBaseDao().saveFieldSetEntity(fse);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException(e);
        }
    }
 
    @Override
    public void delete(FieldSetEntity fse) throws BaseException {
        publicService.delete(fse);
    }
}