2021-02-23 18:23:56 +08:00
|
|
|
|
package io.dataease.service.dataset;
|
|
|
|
|
|
2021-02-25 12:28:55 +08:00
|
|
|
|
|
2021-02-23 23:34:52 +08:00
|
|
|
|
import com.google.gson.Gson;
|
2021-02-23 18:23:56 +08:00
|
|
|
|
import io.dataease.base.domain.DatasetTable;
|
|
|
|
|
import io.dataease.base.domain.DatasetTableExample;
|
2021-02-24 17:14:37 +08:00
|
|
|
|
import io.dataease.base.domain.DatasetTableField;
|
2021-02-23 23:34:52 +08:00
|
|
|
|
import io.dataease.base.domain.Datasource;
|
2021-02-23 18:23:56 +08:00
|
|
|
|
import io.dataease.base.mapper.DatasetTableMapper;
|
2021-02-23 23:34:52 +08:00
|
|
|
|
import io.dataease.base.mapper.DatasourceMapper;
|
2021-02-24 17:14:37 +08:00
|
|
|
|
import io.dataease.commons.utils.BeanUtils;
|
2021-02-23 18:23:56 +08:00
|
|
|
|
import io.dataease.controller.request.dataset.DataSetTableRequest;
|
2021-02-24 18:06:21 +08:00
|
|
|
|
import io.dataease.datasource.constants.DatasourceTypes;
|
2021-02-23 23:34:52 +08:00
|
|
|
|
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;
|
2021-02-25 12:28:55 +08:00
|
|
|
|
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-25 18:15:10 +08:00
|
|
|
|
|
2021-02-23 18:23:56 +08:00
|
|
|
|
import javax.annotation.Resource;
|
2021-02-24 18:06:21 +08:00
|
|
|
|
import java.text.MessageFormat;
|
2021-02-24 17:14:37 +08:00
|
|
|
|
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;
|
2021-02-23 23:34:52 +08:00
|
|
|
|
@Resource
|
|
|
|
|
private DatasourceMapper datasourceMapper;
|
2021-02-24 17:14:37 +08:00
|
|
|
|
@Resource
|
|
|
|
|
private DataSetTableFieldsService dataSetTableFieldsService;
|
2021-02-23 18:23:56 +08:00
|
|
|
|
|
2021-02-24 17:14:37 +08:00
|
|
|
|
public void batchInsert(List<DatasetTable> datasetTable) throws Exception {
|
2021-02-23 18:23:56 +08:00
|
|
|
|
for (DatasetTable table : datasetTable) {
|
|
|
|
|
save(table);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 17:14:37 +08:00
|
|
|
|
public DatasetTable save(DatasetTable datasetTable) throws Exception {
|
2021-02-23 18:23:56 +08:00
|
|
|
|
if (StringUtils.isEmpty(datasetTable.getId())) {
|
2021-02-25 12:28:55 +08:00
|
|
|
|
datasetTable.setId(UUID.randomUUID().toString());
|
2021-02-23 18:23:56 +08:00
|
|
|
|
datasetTable.setCreateTime(System.currentTimeMillis());
|
2021-02-23 23:34:52 +08:00
|
|
|
|
DataTableInfoDTO dataTableInfoDTO = new DataTableInfoDTO();
|
|
|
|
|
if (StringUtils.equalsIgnoreCase("db", datasetTable.getType())) {
|
|
|
|
|
dataTableInfoDTO.setTable(datasetTable.getName());
|
|
|
|
|
}
|
|
|
|
|
datasetTable.setInfo(new Gson().toJson(dataTableInfoDTO));
|
2021-02-24 17:14:37 +08:00
|
|
|
|
int insert = datasetTableMapper.insert(datasetTable);
|
|
|
|
|
// 添加表成功后,获取当前表字段和类型,抽象到dataease数据库
|
|
|
|
|
if (insert == 1) {
|
|
|
|
|
saveTableField(datasetTable);
|
|
|
|
|
}
|
2021-02-23 18:23:56 +08:00
|
|
|
|
} else {
|
2021-02-23 23:34:52 +08:00
|
|
|
|
datasetTableMapper.updateByPrimaryKeyWithBLOBs(datasetTable);
|
2021-02-23 18:23:56 +08:00
|
|
|
|
}
|
|
|
|
|
return datasetTable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delete(String id) {
|
|
|
|
|
datasetTableMapper.deleteByPrimaryKey(id);
|
2021-02-24 17:14:37 +08:00
|
|
|
|
dataSetTableFieldsService.deleteByTableId(id);
|
2021-02-23 18:23:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DatasetTable> list(DataSetTableRequest dataSetTableRequest) {
|
|
|
|
|
DatasetTableExample datasetTableExample = new DatasetTableExample();
|
|
|
|
|
datasetTableExample.createCriteria().andSceneIdEqualTo(dataSetTableRequest.getSceneId());
|
|
|
|
|
if (StringUtils.isNotEmpty(dataSetTableRequest.getSort())) {
|
|
|
|
|
datasetTableExample.setOrderByClause(dataSetTableRequest.getSort());
|
|
|
|
|
}
|
2021-02-23 23:34:52 +08:00
|
|
|
|
return datasetTableMapper.selectByExampleWithBLOBs(datasetTableExample);
|
2021-02-23 18:23:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DatasetTable get(String id) {
|
|
|
|
|
return datasetTableMapper.selectByPrimaryKey(id);
|
|
|
|
|
}
|
2021-02-23 23:34:52 +08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 11:21:37 +08:00
|
|
|
|
public List<String[]> getData(DataSetTableRequest dataSetTableRequest) throws Exception {
|
2021-02-23 23:34:52 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
DatasetTableField datasetTableField = new DatasetTableField();
|
|
|
|
|
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));
|
|
|
|
|
|
2021-02-24 11:21:37 +08:00
|
|
|
|
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);
|
|
|
|
|
String table = new Gson().fromJson(dataSetTableRequest.getInfo(), DataTableInfoDTO.class).getTable();
|
2021-02-24 17:14:37 +08:00
|
|
|
|
// datasourceRequest.setTable(table);
|
|
|
|
|
|
|
|
|
|
DatasetTableField datasetTableField = new DatasetTableField();
|
|
|
|
|
datasetTableField.setTableId(dataSetTableRequest.getId());
|
|
|
|
|
datasetTableField.setChecked(Boolean.TRUE);
|
|
|
|
|
List<DatasetTableField> fields = dataSetTableFieldsService.list(datasetTableField);
|
2021-02-23 23:34:52 +08:00
|
|
|
|
|
2021-02-24 17:14:37 +08:00
|
|
|
|
String[] fieldArray = fields.stream().map(DatasetTableField::getOriginName).toArray(String[]::new);
|
2021-02-24 18:06:21 +08:00
|
|
|
|
// datasourceRequest.setQuery("SELECT " + StringUtils.join(fieldArray, ",") + " FROM " + table + " LIMIT 0,10;");
|
2021-02-25 09:59:18 +08:00
|
|
|
|
datasourceRequest.setQuery(createQuerySQL(ds.getType(), table, fieldArray) + " LIMIT 0,10");
|
2021-02-24 17:14:37 +08:00
|
|
|
|
|
|
|
|
|
List<String[]> data = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
data.addAll(datasourceProvider.getData(datasourceRequest));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
}
|
2021-02-23 23:34:52 +08:00
|
|
|
|
|
2021-02-25 12:28:55 +08:00
|
|
|
|
|
|
|
|
|
/*JSONArray jsonArray = new JSONArray();
|
2021-02-24 17:14:37 +08:00
|
|
|
|
if (CollectionUtils.isNotEmpty(data)) {
|
|
|
|
|
data.forEach(ele -> {
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
for (int i = 0; i < ele.length; i++) {
|
|
|
|
|
jsonObject.put(fieldArray[i], ele[i]);
|
|
|
|
|
}
|
|
|
|
|
jsonArray.add(jsonObject);
|
|
|
|
|
});
|
2021-02-25 12:28:55 +08:00
|
|
|
|
}*/
|
|
|
|
|
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());
|
2021-02-24 17:14:37 +08:00
|
|
|
|
}
|
2021-02-23 23:34:52 +08:00
|
|
|
|
|
2021-02-25 12:28:55 +08:00
|
|
|
|
|
2021-02-24 11:21:37 +08:00
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
2021-02-24 17:14:37 +08:00
|
|
|
|
map.put("fields", fields);
|
|
|
|
|
map.put("data", jsonArray);
|
2021-02-24 11:21:37 +08:00
|
|
|
|
|
|
|
|
|
return map;
|
2021-02-23 23:34:52 +08:00
|
|
|
|
}
|
2021-02-24 17:14:37 +08:00
|
|
|
|
|
|
|
|
|
public void saveTableField(DatasetTable datasetTable) throws Exception {
|
2021-02-25 18:15:10 +08:00
|
|
|
|
Datasource ds = datasourceMapper.selectByPrimaryKey(datasetTable.getDataSourceId());
|
2021-02-24 17:14:37 +08:00
|
|
|
|
DataSetTableRequest dataSetTableRequest = new DataSetTableRequest();
|
|
|
|
|
BeanUtils.copyBean(dataSetTableRequest, datasetTable);
|
|
|
|
|
List<TableFiled> fields = getFields(dataSetTableRequest);
|
|
|
|
|
long syncTime = System.currentTimeMillis();
|
|
|
|
|
if (CollectionUtils.isNotEmpty(fields)) {
|
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
|
TableFiled filed = fields.get(i);
|
|
|
|
|
DatasetTableField datasetTableField = new DatasetTableField();
|
|
|
|
|
datasetTableField.setTableId(datasetTable.getId());
|
|
|
|
|
datasetTableField.setOriginName(filed.getFieldName());
|
|
|
|
|
datasetTableField.setName(filed.getRemarks());
|
|
|
|
|
datasetTableField.setType(filed.getFieldType());
|
2021-02-25 18:15:10 +08:00
|
|
|
|
datasetTableField.setDeType(transFieldType(ds.getType(), filed.getFieldType()));
|
2021-02-24 17:14:37 +08:00
|
|
|
|
datasetTableField.setChecked(true);
|
|
|
|
|
datasetTableField.setColumnIndex(i);
|
|
|
|
|
datasetTableField.setLastSyncTime(syncTime);
|
|
|
|
|
dataSetTableFieldsService.save(datasetTableField);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-24 18:06:21 +08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-25 18:15:10 +08:00
|
|
|
|
|
|
|
|
|
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":
|
|
|
|
|
case "FLOAT":
|
|
|
|
|
case "DOUBLE":
|
|
|
|
|
case "DECIMAL":
|
|
|
|
|
return 2;// 数值
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-23 18:23:56 +08:00
|
|
|
|
}
|