dataease-dm/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java

426 lines
19 KiB
Java
Raw Normal View History

2021-02-23 18:23:56 +08:00
package io.dataease.service.dataset;
import com.google.gson.Gson;
2021-03-18 17:59:52 +08:00
import io.dataease.base.domain.*;
import io.dataease.base.mapper.DatasetTableIncrementalConfigMapper;
2021-02-23 18:23:56 +08:00
import io.dataease.base.mapper.DatasetTableMapper;
import io.dataease.base.mapper.DatasourceMapper;
2021-04-06 12:32:39 +08:00
import io.dataease.commons.utils.AuthUtils;
import io.dataease.commons.utils.BeanUtils;
2021-02-23 18:23:56 +08:00
import io.dataease.controller.request.dataset.DataSetTableRequest;
import io.dataease.datasource.constants.DatasourceTypes;
import io.dataease.datasource.dto.TableFiled;
import io.dataease.datasource.provider.DatasourceProvider;
import io.dataease.datasource.provider.ProviderFactory;
import io.dataease.datasource.request.DatasourceRequest;
import io.dataease.dto.dataset.DataTableInfoDTO;
import org.apache.commons.collections4.CollectionUtils;
2021-02-23 18:23:56 +08:00
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
2021-02-23 18:23:56 +08:00
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
2021-02-23 18:23:56 +08:00
/**
* @Author gin
* @Date 2021/2/23 2:54 下午
*/
@Service
public class DataSetTableService {
@Resource
private DatasetTableMapper datasetTableMapper;
@Resource
private DatasourceMapper datasourceMapper;
@Resource
private DataSetTableFieldsService dataSetTableFieldsService;
2021-03-11 10:37:58 +08:00
@Resource
private DataSetTableTaskService dataSetTableTaskService;
2021-03-18 17:59:52 +08:00
@Resource
private DatasetTableIncrementalConfigMapper datasetTableIncrementalConfigMapper;
2021-02-23 18:23:56 +08:00
public void batchInsert(List<DatasetTable> datasetTable) throws Exception {
2021-02-23 18:23:56 +08:00
for (DatasetTable table : datasetTable) {
save(table);
}
}
public DatasetTable save(DatasetTable datasetTable) throws Exception {
2021-04-01 15:02:57 +08:00
checkName(datasetTable);
2021-02-23 18:23:56 +08:00
if (StringUtils.isEmpty(datasetTable.getId())) {
datasetTable.setId(UUID.randomUUID().toString());
2021-04-06 12:32:39 +08:00
datasetTable.setCreateBy(AuthUtils.getUser().getUsername());
2021-02-23 18:23:56 +08:00
datasetTable.setCreateTime(System.currentTimeMillis());
DataTableInfoDTO dataTableInfoDTO = new DataTableInfoDTO();
if (StringUtils.equalsIgnoreCase("db", datasetTable.getType())) {
dataTableInfoDTO.setTable(datasetTable.getName());
datasetTable.setInfo(new Gson().toJson(dataTableInfoDTO));
}
int insert = datasetTableMapper.insert(datasetTable);
// 添加表成功后获取当前表字段和类型抽象到dataease数据库
if (insert == 1) {
saveTableField(datasetTable);
}
2021-02-23 18:23:56 +08:00
} else {
2021-03-25 18:18:44 +08:00
datasetTableMapper.updateByPrimaryKeySelective(datasetTable);
2021-02-23 18:23:56 +08:00
}
return datasetTable;
}
public void delete(String id) {
datasetTableMapper.deleteByPrimaryKey(id);
dataSetTableFieldsService.deleteByTableId(id);
2021-03-11 10:37:58 +08:00
// 删除同步任务
dataSetTableTaskService.deleteByTableId(id);
2021-02-23 18:23:56 +08:00
}
public List<DatasetTable> list(DataSetTableRequest dataSetTableRequest) {
DatasetTableExample datasetTableExample = new DatasetTableExample();
2021-04-06 12:32:39 +08:00
DatasetTableExample.Criteria criteria = datasetTableExample.createCriteria();
criteria.andCreateByEqualTo(AuthUtils.getUser().getUsername());
2021-03-11 10:37:58 +08:00
if (StringUtils.isNotEmpty(dataSetTableRequest.getSceneId())) {
2021-04-06 12:32:39 +08:00
criteria.andSceneIdEqualTo(dataSetTableRequest.getSceneId());
2021-03-10 18:13:27 +08:00
}
2021-02-23 18:23:56 +08:00
if (StringUtils.isNotEmpty(dataSetTableRequest.getSort())) {
datasetTableExample.setOrderByClause(dataSetTableRequest.getSort());
}
return datasetTableMapper.selectByExampleWithBLOBs(datasetTableExample);
2021-02-23 18:23:56 +08:00
}
public DatasetTable get(String id) {
return datasetTableMapper.selectByPrimaryKey(id);
}
public List<TableFiled> getFields(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
datasourceRequest.setTable(new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getTable());
return datasourceProvider.getTableFileds(datasourceRequest);
}
public Map<String, List<DatasetTableField>> getFieldsFromDE(DataSetTableRequest dataSetTableRequest) throws Exception {
2021-03-10 18:13:27 +08:00
DatasetTableField datasetTableField = DatasetTableField.builder().build();
datasetTableField.setTableId(dataSetTableRequest.getId());
datasetTableField.setChecked(Boolean.TRUE);
List<DatasetTableField> fields = dataSetTableFieldsService.list(datasetTableField);
List<DatasetTableField> dimension = new ArrayList<>();
List<DatasetTableField> quota = new ArrayList<>();
fields.forEach(field -> {
if (field.getDeType() == 2 || field.getDeType() == 3) {
quota.add(field);
} else {
dimension.add(field);
}
});
Map<String, List<DatasetTableField>> map = new HashMap<>();
map.put("dimension", dimension);
map.put("quota", quota);
return map;
}
public List<String[]> getData(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
String table = new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getTable();
2021-02-25 10:42:48 +08:00
2021-03-10 18:13:27 +08:00
DatasetTableField datasetTableField = DatasetTableField.builder().build();
2021-02-25 10:42:48 +08:00
datasetTableField.setTableId(dataSetTableRequest.getId());
datasetTableField.setChecked(Boolean.TRUE);
List<DatasetTableField> fields = dataSetTableFieldsService.list(datasetTableField);
String[] fieldArray = fields.stream().map(DatasetTableField::getOriginName).toArray(String[]::new);
datasourceRequest.setQuery(createQuerySQL(ds.getType(), table, fieldArray));
return datasourceProvider.getData(datasourceRequest);
}
public Map<String, Object> getPreviewData(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
2021-03-10 18:13:27 +08:00
DatasetTableField datasetTableField = DatasetTableField.builder().build();
datasetTableField.setTableId(dataSetTableRequest.getId());
datasetTableField.setChecked(Boolean.TRUE);
List<DatasetTableField> fields = dataSetTableFieldsService.list(datasetTableField);
String[] fieldArray = fields.stream().map(DatasetTableField::getOriginName).toArray(String[]::new);
DataTableInfoDTO dataTableInfoDTO = new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class);
DatasetTable datasetTable = datasetTableMapper.selectByPrimaryKey(dataSetTableRequest.getId());
if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "db")) {
String table = dataTableInfoDTO.getTable();
datasourceRequest.setQuery(createQuerySQL(ds.getType(), table, fieldArray) + " LIMIT 0," + dataSetTableRequest.getRow());
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "sql")) {
String sql = dataTableInfoDTO.getSql();
datasourceRequest.setQuery(createQuerySQL(ds.getType(), " (" + sql + ") AS tmp ", fieldArray) + " LIMIT 0," + dataSetTableRequest.getRow());
}
List<String[]> data = new ArrayList<>();
try {
data.addAll(datasourceProvider.getData(datasourceRequest));
} catch (Exception e) {
}
List<Map<String, Object>> jsonArray = new ArrayList<>();
if (CollectionUtils.isNotEmpty(data)) {
jsonArray = data.stream().map(ele -> {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < ele.length; i++) {
map.put(fieldArray[i], ele[i]);
}
return map;
}).collect(Collectors.toList());
}
Map<String, Object> map = new HashMap<>();
map.put("fields", fields);
map.put("data", jsonArray);
return map;
}
public Map<String, Object> getSQLPreview(DataSetTableRequest dataSetTableRequest) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(dataSetTableRequest.getDataSourceId());
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
String sql = new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getSql();
datasourceRequest.setQuery(sql);
ResultSet dataResultSet = datasourceProvider.getDataResultSet(datasourceRequest);
List<String[]> data = datasourceProvider.fetchResult(dataResultSet);
List<TableFiled> fields = datasourceProvider.fetchResultField(dataResultSet);
String[] fieldArray = fields.stream().map(TableFiled::getFieldName).toArray(String[]::new);
List<Map<String, Object>> jsonArray = new ArrayList<>();
if (CollectionUtils.isNotEmpty(data)) {
jsonArray = data.stream().map(ele -> {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < ele.length; i++) {
map.put(fieldArray[i], ele[i]);
}
return map;
}).collect(Collectors.toList());
}
Map<String, Object> map = new HashMap<>();
map.put("fields", fields);
map.put("data", jsonArray);
return map;
}
2021-03-11 10:37:58 +08:00
public List<String[]> getDataSetData(String datasourceId, String table, List<DatasetTableField> fields) {
2021-03-10 18:13:27 +08:00
List<String[]> data = new ArrayList<>();
Datasource ds = datasourceMapper.selectByPrimaryKey(datasourceId);
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
String[] fieldArray = fields.stream().map(DatasetTableField::getOriginName).toArray(String[]::new);
datasourceRequest.setQuery(createQuerySQL(ds.getType(), table, fieldArray) + " LIMIT 0, 10");
try {
data.addAll(datasourceProvider.getData(datasourceRequest));
} catch (Exception e) {
}
return data;
}
2021-03-11 10:37:58 +08:00
public Long getDataSetTotalData(String datasourceId, String table) {
2021-03-10 18:13:27 +08:00
List<String[]> data = new ArrayList<>();
Datasource ds = datasourceMapper.selectByPrimaryKey(datasourceId);
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
datasourceRequest.setQuery("select count(*) from " + table);
try {
return datasourceProvider.count(datasourceRequest);
} catch (Exception e) {
}
return 0l;
}
2021-03-11 10:37:58 +08:00
public List<String[]> getDataSetPageData(String datasourceId, String table, List<DatasetTableField> fields, Long startPage, Long pageSize) {
2021-03-10 18:13:27 +08:00
List<String[]> data = new ArrayList<>();
Datasource ds = datasourceMapper.selectByPrimaryKey(datasourceId);
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
String[] fieldArray = fields.stream().map(DatasetTableField::getOriginName).toArray(String[]::new);
datasourceRequest.setPageSize(pageSize);
datasourceRequest.setStartPage(startPage);
datasourceRequest.setQuery(createQuerySQL(ds.getType(), table, fieldArray));
try {
return datasourceProvider.getData(datasourceRequest);
} catch (Exception e) {
}
return data;
}
2021-03-18 17:59:52 +08:00
public List<String[]> getDataSetDataBySql(String datasourceId, String table, String sql) {
List<String[]> data = new ArrayList<>();
Datasource ds = datasourceMapper.selectByPrimaryKey(datasourceId);
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
datasourceRequest.setQuery(sql);
try {
return datasourceProvider.getData(datasourceRequest);
} catch (Exception e) {
}
return data;
}
public void saveTableField(DatasetTable datasetTable) throws Exception {
Datasource ds = datasourceMapper.selectByPrimaryKey(datasetTable.getDataSourceId());
DataSetTableRequest dataSetTableRequest = new DataSetTableRequest();
BeanUtils.copyBean(dataSetTableRequest, datasetTable);
List<TableFiled> fields = new ArrayList<>();
long syncTime = System.currentTimeMillis();
if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "db")) {
fields = getFields(dataSetTableRequest);
} else if (StringUtils.equalsIgnoreCase(datasetTable.getType(), "sql")) {
DatasourceProvider datasourceProvider = ProviderFactory.getProvider(ds.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDatasource(ds);
datasourceRequest.setQuery(new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getSql());
ResultSet dataResultSet = datasourceProvider.getDataResultSet(datasourceRequest);
fields = datasourceProvider.fetchResultField(dataResultSet);
}
if (CollectionUtils.isNotEmpty(fields)) {
for (int i = 0; i < fields.size(); i++) {
TableFiled filed = fields.get(i);
2021-03-10 18:13:27 +08:00
DatasetTableField datasetTableField = DatasetTableField.builder().build();
datasetTableField.setTableId(datasetTable.getId());
datasetTableField.setOriginName(filed.getFieldName());
datasetTableField.setName(filed.getRemarks());
datasetTableField.setType(filed.getFieldType());
datasetTableField.setDeType(transFieldType(ds.getType(), filed.getFieldType()));
datasetTableField.setChecked(true);
datasetTableField.setColumnIndex(i);
datasetTableField.setLastSyncTime(syncTime);
dataSetTableFieldsService.save(datasetTableField);
}
}
}
public String createQuerySQL(String type, String table, String[] fields) {
DatasourceTypes datasourceType = DatasourceTypes.valueOf(type);
switch (datasourceType) {
case mysql:
return MessageFormat.format("SELECT {0} FROM {1}", StringUtils.join(fields, ","), table);
case sqlServer:
return MessageFormat.format("SELECT {0} FROM {1}", StringUtils.join(fields, ","), table);
default:
return MessageFormat.format("SELECT {0} FROM {1}", StringUtils.join(fields, ","), table);
}
}
public Integer transFieldType(String type, String field) {
DatasourceTypes datasourceType = DatasourceTypes.valueOf(type);
switch (datasourceType) {
case mysql:
return transMysqlField(field);
case sqlServer:
default:
return 0;
}
}
public Integer transMysqlField(String field) {
switch (field) {
case "CHAR":
case "VARCHAR":
case "TEXT":
case "TINYTEXT":
case "MEDIUMTEXT":
case "LONGTEXT":
case "ENUM":
return 0;// 文本
case "DATE":
case "TIME":
case "YEAR":
case "DATETIME":
case "TIMESTAMP":
return 1;// 时间
case "INT":
case "BIT":
case "TINYINT":
case "SMALLINT":
case "MEDIUMINT":
case "INTEGER":
case "BIGINT":
return 2;// 整型
case "FLOAT":
case "DOUBLE":
case "DECIMAL":
return 3;// 浮点
default:
return 0;
}
}
2021-03-18 17:59:52 +08:00
2021-04-01 15:02:57 +08:00
public DatasetTableIncrementalConfig incrementalConfig(DatasetTableIncrementalConfig datasetTableIncrementalConfig) {
if (StringUtils.isEmpty(datasetTableIncrementalConfig.getTableId())) {
return new DatasetTableIncrementalConfig();
}
2021-03-18 17:59:52 +08:00
DatasetTableIncrementalConfigExample example = new DatasetTableIncrementalConfigExample();
example.createCriteria().andTableIdEqualTo(datasetTableIncrementalConfig.getTableId());
List<DatasetTableIncrementalConfig> configs = datasetTableIncrementalConfigMapper.selectByExample(example);
2021-04-01 15:02:57 +08:00
if (CollectionUtils.isNotEmpty(configs)) {
2021-03-18 17:59:52 +08:00
return configs.get(0);
2021-04-01 15:02:57 +08:00
} else {
2021-03-18 17:59:52 +08:00
return new DatasetTableIncrementalConfig();
}
}
2021-04-01 15:02:57 +08:00
public DatasetTableIncrementalConfig incrementalConfig(String datasetTableId) {
2021-03-18 17:59:52 +08:00
DatasetTableIncrementalConfig datasetTableIncrementalConfig = new DatasetTableIncrementalConfig();
datasetTableIncrementalConfig.setTableId(datasetTableId);
return incrementalConfig(datasetTableIncrementalConfig);
}
2021-04-01 15:02:57 +08:00
public void saveIncrementalConfig(DatasetTableIncrementalConfig datasetTableIncrementalConfig) {
if (StringUtils.isEmpty(datasetTableIncrementalConfig.getId())) {
2021-03-18 17:59:52 +08:00
datasetTableIncrementalConfig.setId(UUID.randomUUID().toString());
datasetTableIncrementalConfigMapper.insertSelective(datasetTableIncrementalConfig);
2021-04-01 15:02:57 +08:00
} else {
2021-03-18 17:59:52 +08:00
DatasetTableIncrementalConfigExample example = new DatasetTableIncrementalConfigExample();
example.createCriteria().andTableIdEqualTo(datasetTableIncrementalConfig.getTableId());
datasetTableIncrementalConfigMapper.updateByExample(datasetTableIncrementalConfig, example);
}
}
2021-04-01 15:02:57 +08:00
private void checkName(DatasetTable datasetTable) {
if (StringUtils.isEmpty(datasetTable.getId()) && StringUtils.equalsIgnoreCase("db", datasetTable.getType())) {
return;
}
DatasetTableExample datasetTableExample = new DatasetTableExample();
DatasetTableExample.Criteria criteria = datasetTableExample.createCriteria();
if (StringUtils.isNotEmpty(datasetTable.getId())) {
criteria.andIdNotEqualTo(datasetTable.getId());
}
if (StringUtils.isNotEmpty(datasetTable.getSceneId())) {
criteria.andSceneIdEqualTo(datasetTable.getSceneId());
}
if (StringUtils.isNotEmpty(datasetTable.getName())) {
criteria.andNameEqualTo(datasetTable.getName());
}
List<DatasetTable> list = datasetTableMapper.selectByExample(datasetTableExample);
if (list.size() > 0) {
throw new RuntimeException("Name can't repeat in same group.");
}
}
2021-02-23 18:23:56 +08:00
}