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 fieldTypeReference = syncDataConfigService.getFieldTypeReference(dbe.getDbType()); DataTableEntity dt = new DataTableEntity(); if (DatabaseType.Oracle.equals(dbe.getDbType())) { Map>> 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>> 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); } }