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

167 lines
7.3 KiB
Java
Raw Normal View History

2021-02-23 18:23:56 +08:00
package io.dataease.service.dataset;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
2021-02-23 18:23:56 +08:00
import com.alibaba.nacos.common.util.UuidUtils;
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;
import io.dataease.base.domain.DatasetTableField;
import io.dataease.base.domain.Datasource;
2021-02-23 18:23:56 +08:00
import io.dataease.base.mapper.DatasetTableMapper;
import io.dataease.base.mapper.DatasourceMapper;
import io.dataease.commons.utils.BeanUtils;
import io.dataease.commons.utils.SessionUtils;
2021-02-23 18:23:56 +08:00
import io.dataease.controller.request.dataset.DataSetTableRequest;
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 jnr.ffi.Struct;
import org.apache.commons.collections.CollectionUtils;
2021-02-23 18:23:56 +08:00
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
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-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-02-23 18:23:56 +08:00
if (StringUtils.isEmpty(datasetTable.getId())) {
datasetTable.setId(UuidUtils.generateUuid());
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 {
datasetTableMapper.updateByPrimaryKeyWithBLOBs(datasetTable);
2021-02-23 18:23:56 +08:00
}
return datasetTable;
}
public void delete(String id) {
datasetTableMapper.deleteByPrimaryKey(id);
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());
}
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 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();
datasourceRequest.setQuery("SELECT * FROM " + table + ";");
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();
// datasourceRequest.setTable(table);
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("SELECT " + StringUtils.join(fieldArray, ",") + " FROM " + table + " LIMIT 0,10;");
List<String[]> data = new ArrayList<>();
try {
data.addAll(datasourceProvider.getData(datasourceRequest));
} catch (Exception e) {
}
JSONArray jsonArray = new JSONArray();
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);
});
}
Map<String, Object> map = new HashMap<>();
map.put("fields", fields);
map.put("data", jsonArray);
return map;
}
public void saveTableField(DatasetTable datasetTable) throws Exception {
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());
datasetTableField.setChecked(true);
datasetTableField.setColumnIndex(i);
datasetTableField.setLastSyncTime(syncTime);
dataSetTableFieldsService.save(datasetTableField);
}
}
}
2021-02-23 18:23:56 +08:00
}