2021-02-23 18:23:56 +08:00
|
|
|
package io.dataease.service.dataset;
|
|
|
|
|
2021-02-23 23:34:52 +08:00
|
|
|
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;
|
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-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-23 18:23:56 +08:00
|
|
|
import io.dataease.controller.request.dataset.DataSetTableRequest;
|
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-23 18:23:56 +08:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
2021-02-24 11:21:37 +08:00
|
|
|
import java.util.HashMap;
|
2021-02-23 18:23:56 +08:00
|
|
|
import java.util.List;
|
2021-02-24 11:21:37 +08:00
|
|
|
import java.util.Map;
|
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-23 18:23:56 +08:00
|
|
|
|
|
|
|
public void batchInsert(List<DatasetTable> datasetTable) {
|
|
|
|
for (DatasetTable table : datasetTable) {
|
|
|
|
save(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public DatasetTable save(DatasetTable datasetTable) {
|
|
|
|
if (StringUtils.isEmpty(datasetTable.getId())) {
|
|
|
|
datasetTable.setId(UuidUtils.generateUuid());
|
|
|
|
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-23 18:23:56 +08:00
|
|
|
datasetTableMapper.insert(datasetTable);
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-24 11:21:37 +08:00
|
|
|
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);
|
|
|
|
datasourceRequest.setQuery("SELECT * FROM " + table + " LIMIT 0,10;");
|
2021-02-23 23:34:52 +08:00
|
|
|
|
2021-02-24 11:21:37 +08:00
|
|
|
List<TableFiled> fields = datasourceProvider.getTableFileds(datasourceRequest);
|
2021-02-23 23:34:52 +08:00
|
|
|
List<String[]> data = datasourceProvider.getData(datasourceRequest);
|
|
|
|
|
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
|
data.forEach(ele -> {
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
for (int i = 0; i < ele.length; i++) {
|
|
|
|
jsonObject.put(fields.get(i).getFieldName(), ele[i]);
|
|
|
|
}
|
|
|
|
jsonArray.add(jsonObject);
|
|
|
|
});
|
|
|
|
|
2021-02-24 11:21:37 +08:00
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
map.put("fields",fields);
|
|
|
|
map.put("data",jsonArray);
|
|
|
|
|
|
|
|
return map;
|
2021-02-23 23:34:52 +08:00
|
|
|
}
|
2021-02-23 18:23:56 +08:00
|
|
|
}
|