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-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 ;
2021-02-23 23:34:52 +08:00
import io.dataease.base.mapper.DatasourceMapper ;
2021-05-18 16:07:19 +08:00
import io.dataease.base.mapper.ext.ExtDataSetTableMapper ;
2021-05-07 19:01:11 +08:00
import io.dataease.commons.utils.* ;
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 ;
2021-05-07 19:01:11 +08:00
import io.dataease.datasource.provider.JdbcProvider ;
2021-02-23 23:34:52 +08:00
import io.dataease.datasource.provider.ProviderFactory ;
import io.dataease.datasource.request.DatasourceRequest ;
2021-04-28 17:30:18 +08:00
import io.dataease.dto.dataset.DataSetPreviewPage ;
2021-05-18 16:07:19 +08:00
import io.dataease.dto.dataset.DataSetTableDTO ;
2021-05-10 15:22:36 +08:00
import io.dataease.dto.dataset.DataSetTableUnionDTO ;
import io.dataease.dto.dataset.DataTableInfoCustomUnion ;
2021-02-23 23:34:52 +08:00
import io.dataease.dto.dataset.DataTableInfoDTO ;
2021-05-17 16:43:02 +08:00
import io.dataease.provider.DDLProvider ;
import io.dataease.provider.QueryProvider ;
2021-02-25 12:28:55 +08:00
import org.apache.commons.collections4.CollectionUtils ;
2021-04-14 18:46:13 +08:00
import org.apache.commons.lang3.ObjectUtils ;
2021-02-23 18:23:56 +08:00
import org.apache.commons.lang3.StringUtils ;
2021-04-19 18:33:53 +08:00
import org.apache.poi.hssf.usermodel.HSSFRow ;
import org.apache.poi.hssf.usermodel.HSSFSheet ;
import org.apache.poi.hssf.usermodel.HSSFWorkbook ;
import org.apache.poi.ss.usermodel.Cell ;
import org.apache.poi.ss.usermodel.CellType ;
import org.apache.poi.xssf.usermodel.XSSFRow ;
import org.apache.poi.xssf.usermodel.XSSFSheet ;
import org.apache.poi.xssf.usermodel.XSSFWorkbook ;
import org.springframework.beans.factory.annotation.Value ;
2021-02-23 18:23:56 +08:00
import org.springframework.stereotype.Service ;
2021-04-19 18:33:53 +08:00
import org.springframework.web.multipart.MultipartFile ;
2021-02-25 18:15:10 +08:00
2021-02-23 18:23:56 +08:00
import javax.annotation.Resource ;
2021-04-19 18:33:53 +08:00
import java.io.* ;
import java.math.BigDecimal ;
import java.nio.charset.StandardCharsets ;
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-03-11 10:37:58 +08:00
@Resource
private DataSetTableTaskService dataSetTableTaskService ;
2021-03-18 17:59:52 +08:00
@Resource
2021-05-07 16:01:04 +08:00
private CommonThreadPool commonThreadPool ;
@Resource
private ExtractDataService extractDataService ;
@Resource
2021-05-18 16:07:19 +08:00
private ExtDataSetTableMapper extDataSetTableMapper ;
@Resource
2021-03-18 17:59:52 +08:00
private DatasetTableIncrementalConfigMapper datasetTableIncrementalConfigMapper ;
2021-05-10 15:22:36 +08:00
@Resource
private DataSetTableUnionService dataSetTableUnionService ;
2021-04-19 18:33:53 +08:00
@Value ( " ${upload.file.path} " )
private String path ;
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-04-01 15:02:57 +08:00
checkName ( datasetTable ) ;
2021-05-11 11:11:42 +08:00
if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " sql " ) ) {
DataSetTableRequest dataSetTableRequest = new DataSetTableRequest ( ) ;
BeanUtils . copyBean ( dataSetTableRequest , datasetTable ) ;
getSQLPreview ( dataSetTableRequest ) ;
}
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-04-06 12:32:39 +08:00
datasetTable . setCreateBy ( AuthUtils . getUser ( ) . getUsername ( ) ) ;
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 ( ) ) ;
2021-03-15 15:24:10 +08:00
datasetTable . setInfo ( new Gson ( ) . toJson ( dataTableInfoDTO ) ) ;
2021-02-23 23:34:52 +08:00
}
2021-02-24 17:14:37 +08:00
int insert = datasetTableMapper . insert ( datasetTable ) ;
// 添加表成功后, 获取当前表字段和类型, 抽象到dataease数据库
if ( insert = = 1 ) {
saveTableField ( datasetTable ) ;
2021-05-12 12:15:42 +08:00
if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " excel " ) ) {
commonThreadPool . addTask ( ( ) - > {
extractDataService . extractData ( datasetTable . getId ( ) , null , " all_scope " ) ;
} ) ;
}
2021-02-24 17:14:37 +08:00
}
2021-02-23 18:23:56 +08:00
} else {
2021-04-29 12:12:12 +08:00
int update = datasetTableMapper . updateByPrimaryKeySelective ( datasetTable ) ;
// sql 更新
if ( update = = 1 ) {
2021-05-12 15:53:53 +08:00
if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " sql " ) | | StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " custom " ) ) {
2021-04-29 12:12:12 +08:00
// 删除所有字段,重新抽象
dataSetTableFieldsService . deleteByTableId ( datasetTable . getId ( ) ) ;
saveTableField ( datasetTable ) ;
}
}
2021-02-23 18:23:56 +08:00
}
return datasetTable ;
}
2021-05-12 16:39:53 +08:00
public void delete ( String id ) throws Exception {
2021-02-23 18:23:56 +08:00
datasetTableMapper . deleteByPrimaryKey ( id ) ;
2021-02-24 17:14:37 +08:00
dataSetTableFieldsService . deleteByTableId ( id ) ;
2021-03-11 10:37:58 +08:00
// 删除同步任务
dataSetTableTaskService . deleteByTableId ( id ) ;
2021-05-12 15:17:51 +08:00
deleteDorisTable ( id ) ;
}
2021-05-12 16:39:53 +08:00
private void deleteDorisTable ( String datasetId ) throws Exception {
2021-05-12 15:17:51 +08:00
String dorisTableName = DorisTableUtils . dorisName ( datasetId ) ;
2021-05-12 16:39:53 +08:00
Datasource dorisDatasource = ( Datasource ) CommonBeanFactory . getBean ( " DorisDatasource " ) ;
JdbcProvider jdbcProvider = CommonBeanFactory . getBean ( JdbcProvider . class ) ;
2021-05-12 15:17:51 +08:00
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( dorisDatasource ) ;
2021-05-17 16:43:02 +08:00
DDLProvider ddlProvider = ProviderFactory . getDDLProvider ( dorisDatasource . getType ( ) ) ;
datasourceRequest . setQuery ( ddlProvider . dropTableOrView ( dorisTableName ) ) ;
2021-05-12 15:17:51 +08:00
jdbcProvider . exec ( datasourceRequest ) ;
2021-05-17 16:43:02 +08:00
datasourceRequest . setQuery ( ddlProvider . dropTableOrView ( DorisTableUtils . dorisTmpName ( dorisTableName ) ) ) ;
2021-05-12 15:17:51 +08:00
jdbcProvider . exec ( datasourceRequest ) ;
2021-02-23 18:23:56 +08:00
}
2021-05-18 16:07:19 +08:00
public List < DataSetTableDTO > list ( DataSetTableRequest dataSetTableRequest ) {
dataSetTableRequest . setUserId ( String . valueOf ( AuthUtils . getUser ( ) . getUserId ( ) ) ) ;
return extDataSetTableMapper . search ( dataSetTableRequest ) ;
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-03-01 18:03:11 +08:00
public Map < String , List < DatasetTableField > > getFieldsFromDE ( DataSetTableRequest dataSetTableRequest ) throws Exception {
2021-03-10 18:13:27 +08:00
DatasetTableField datasetTableField = DatasetTableField . builder ( ) . build ( ) ;
2021-03-01 18:03:11 +08:00
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 - > {
2021-04-09 22:42:38 +08:00
if ( field . getDeType ( ) = = 2 | | field . getDeType ( ) = = 3 ) {
2021-03-01 18:03:11 +08:00
quota . add ( field ) ;
} else {
dimension . add ( field ) ;
}
} ) ;
2021-04-25 15:10:49 +08:00
// quota add count
DatasetTableField count = DatasetTableField . builder ( )
. id ( " count " )
. tableId ( dataSetTableRequest . getId ( ) )
. originName ( " * " )
. name ( " 记录数* " )
2021-05-11 12:11:07 +08:00
. dataeaseName ( " * " )
2021-04-25 15:10:49 +08:00
. type ( " INT " )
. checked ( true )
. columnIndex ( 999 )
. deType ( 2 )
. build ( ) ;
quota . add ( count ) ;
2021-03-01 18:03:11 +08:00
Map < String , List < DatasetTableField > > map = new HashMap < > ( ) ;
map . put ( " dimension " , dimension ) ;
map . put ( " quota " , quota ) ;
return map ;
}
2021-04-28 17:30:18 +08:00
public Map < String , Object > getPreviewData ( DataSetTableRequest dataSetTableRequest , Integer page , Integer pageSize ) throws Exception {
2021-03-10 18:13:27 +08:00
DatasetTableField datasetTableField = DatasetTableField . builder ( ) . build ( ) ;
2021-02-24 17:14:37 +08:00
datasetTableField . setTableId ( dataSetTableRequest . getId ( ) ) ;
datasetTableField . setChecked ( Boolean . TRUE ) ;
List < DatasetTableField > fields = dataSetTableFieldsService . list ( datasetTableField ) ;
2021-05-07 19:01:11 +08:00
String [ ] fieldArray = fields . stream ( ) . map ( DatasetTableField : : getDataeaseName ) . toArray ( String [ ] : : new ) ;
2021-03-15 15:24:10 +08:00
DataTableInfoDTO dataTableInfoDTO = new Gson ( ) . fromJson ( dataSetTableRequest . getInfo ( ) , DataTableInfoDTO . class ) ;
DatasetTable datasetTable = datasetTableMapper . selectByPrimaryKey ( dataSetTableRequest . getId ( ) ) ;
2021-04-20 16:23:47 +08:00
List < String [ ] > data = new ArrayList < > ( ) ;
2021-04-28 17:30:18 +08:00
DataSetPreviewPage dataSetPreviewPage = new DataSetPreviewPage ( ) ;
dataSetPreviewPage . setShow ( Integer . valueOf ( dataSetTableRequest . getRow ( ) ) ) ;
dataSetPreviewPage . setPage ( page ) ;
dataSetPreviewPage . setPageSize ( pageSize ) ;
int realSize = Integer . parseInt ( dataSetTableRequest . getRow ( ) ) < pageSize ? Integer . parseInt ( dataSetTableRequest . getRow ( ) ) : pageSize ;
2021-04-28 18:39:01 +08:00
if ( page = = Integer . parseInt ( dataSetTableRequest . getRow ( ) ) / pageSize + 1 ) {
realSize = Integer . parseInt ( dataSetTableRequest . getRow ( ) ) % pageSize ;
}
2021-03-15 15:24:10 +08:00
if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " db " ) ) {
2021-04-20 16:23:47 +08:00
Datasource ds = datasourceMapper . selectByPrimaryKey ( dataSetTableRequest . getDataSourceId ( ) ) ;
DatasourceProvider datasourceProvider = ProviderFactory . getProvider ( ds . getType ( ) ) ;
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( ds ) ;
2021-03-15 15:24:10 +08:00
String table = dataTableInfoDTO . getTable ( ) ;
2021-05-17 16:43:02 +08:00
QueryProvider qp = ProviderFactory . getQueryProvider ( ds . getType ( ) ) ;
datasourceRequest . setQuery ( qp . createQuerySQLWithPage ( table , fields , page , pageSize , realSize ) ) ;
2021-04-20 16:23:47 +08:00
try {
data . addAll ( datasourceProvider . getData ( datasourceRequest ) ) ;
} catch ( Exception e ) {
2021-04-28 17:30:18 +08:00
e . printStackTrace ( ) ;
}
try {
2021-05-17 16:43:02 +08:00
datasourceRequest . setQuery ( qp . createQueryCountSQL ( table ) ) ;
2021-04-28 17:30:18 +08:00
dataSetPreviewPage . setTotal ( Integer . valueOf ( datasourceProvider . getData ( datasourceRequest ) . get ( 0 ) [ 0 ] ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2021-04-20 16:23:47 +08:00
}
2021-03-15 15:24:10 +08:00
} else if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " sql " ) ) {
2021-04-20 16:23:47 +08:00
Datasource ds = datasourceMapper . selectByPrimaryKey ( dataSetTableRequest . getDataSourceId ( ) ) ;
DatasourceProvider datasourceProvider = ProviderFactory . getProvider ( ds . getType ( ) ) ;
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( ds ) ;
2021-03-15 15:24:10 +08:00
String sql = dataTableInfoDTO . getSql ( ) ;
2021-05-17 16:43:02 +08:00
QueryProvider qp = ProviderFactory . getQueryProvider ( ds . getType ( ) ) ;
datasourceRequest . setQuery ( qp . createQuerySQLAsTmpWithPage ( sql , fields , page , pageSize , realSize ) ) ;
2021-04-20 16:23:47 +08:00
try {
data . addAll ( datasourceProvider . getData ( datasourceRequest ) ) ;
} catch ( Exception e ) {
2021-04-28 17:30:18 +08:00
e . printStackTrace ( ) ;
}
try {
2021-05-17 16:43:02 +08:00
datasourceRequest . setQuery ( qp . createQueryCountSQLAsTmp ( sql ) ) ;
2021-04-28 17:30:18 +08:00
dataSetPreviewPage . setTotal ( Integer . valueOf ( datasourceProvider . getData ( datasourceRequest ) . get ( 0 ) [ 0 ] ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2021-04-20 16:23:47 +08:00
}
} else if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " excel " ) ) {
2021-05-10 15:22:36 +08:00
Datasource ds = ( Datasource ) CommonBeanFactory . getBean ( " DorisDatasource " ) ;
JdbcProvider jdbcProvider = CommonBeanFactory . getBean ( JdbcProvider . class ) ;
2021-05-07 19:01:11 +08:00
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( ds ) ;
String table = DorisTableUtils . dorisName ( dataSetTableRequest . getId ( ) ) ;
2021-05-17 16:43:02 +08:00
QueryProvider qp = ProviderFactory . getQueryProvider ( ds . getType ( ) ) ;
datasourceRequest . setQuery ( qp . createQuerySQLWithPage ( table , fields , page , pageSize , realSize ) ) ;
2021-05-07 19:01:11 +08:00
try {
data . addAll ( jdbcProvider . getData ( datasourceRequest ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2021-04-20 16:23:47 +08:00
2021-05-07 19:01:11 +08:00
try {
2021-05-17 16:43:02 +08:00
datasourceRequest . setQuery ( qp . createQueryCountSQL ( table ) ) ;
2021-05-07 19:01:11 +08:00
dataSetPreviewPage . setTotal ( Integer . valueOf ( jdbcProvider . getData ( datasourceRequest ) . get ( 0 ) [ 0 ] ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2021-04-20 16:23:47 +08:00
} else if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " custom " ) ) {
2021-05-10 15:22:36 +08:00
Datasource ds = ( Datasource ) CommonBeanFactory . getBean ( " DorisDatasource " ) ;
JdbcProvider jdbcProvider = CommonBeanFactory . getBean ( JdbcProvider . class ) ;
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( ds ) ;
String table = DorisTableUtils . dorisName ( dataSetTableRequest . getId ( ) ) ;
2021-05-17 16:43:02 +08:00
QueryProvider qp = ProviderFactory . getQueryProvider ( ds . getType ( ) ) ;
datasourceRequest . setQuery ( qp . createQuerySQLWithPage ( table , fields , page , pageSize , realSize ) ) ;
2021-05-10 15:22:36 +08:00
try {
data . addAll ( jdbcProvider . getData ( datasourceRequest ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2021-04-20 16:23:47 +08:00
2021-05-10 15:22:36 +08:00
try {
2021-05-17 16:43:02 +08:00
datasourceRequest . setQuery ( qp . createQueryCountSQL ( table ) ) ;
2021-05-10 15:22:36 +08:00
dataSetPreviewPage . setTotal ( Integer . valueOf ( jdbcProvider . getData ( datasourceRequest ) . get ( 0 ) [ 0 ] ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2021-02-24 17:14:37 +08:00
}
2021-02-23 23:34:52 +08:00
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-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-04-28 17:30:18 +08:00
map . put ( " page " , dataSetPreviewPage ) ;
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
2021-03-15 12:25:11 +08:00
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 ( ) ;
2021-05-11 12:21:08 +08:00
// 使用输入的sql先预执行一次,并拿到所有字段
2021-05-11 11:11:42 +08:00
datasourceRequest . setQuery ( sql ) ;
2021-05-11 12:21:08 +08:00
List < TableFiled > previewFields = datasourceProvider . fetchResultField ( datasourceRequest ) ;
2021-05-11 11:11:42 +08:00
// 正式执行
2021-05-17 16:43:02 +08:00
QueryProvider qp = ProviderFactory . getQueryProvider ( ds . getType ( ) ) ;
datasourceRequest . setQuery ( qp . createSQLPreview ( sql , previewFields . get ( 0 ) . getFieldName ( ) ) ) ;
2021-04-27 17:32:26 +08:00
Map < String , List > result = datasourceProvider . fetchResultAndField ( datasourceRequest ) ;
List < String [ ] > data = result . get ( " dataList " ) ;
List < TableFiled > fields = result . get ( " fieldList " ) ;
2021-03-15 15:24:10 +08:00
String [ ] fieldArray = fields . stream ( ) . map ( TableFiled : : getFieldName ) . toArray ( String [ ] : : new ) ;
2021-03-15 12:25:11 +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 + + ) {
2021-03-15 15:24:10 +08:00
map . put ( fieldArray [ i ] , ele [ i ] ) ;
2021-03-15 12:25:11 +08:00
}
return map ;
} ) . collect ( Collectors . toList ( ) ) ;
}
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " fields " , fields ) ;
map . put ( " data " , jsonArray ) ;
return map ;
}
2021-05-10 15:22:36 +08:00
public Map < String , Object > getCustomPreview ( DataSetTableRequest dataSetTableRequest ) throws Exception {
Datasource ds = ( Datasource ) CommonBeanFactory . getBean ( " DorisDatasource " ) ;
JdbcProvider jdbcProvider = CommonBeanFactory . getBean ( JdbcProvider . class ) ;
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( ds ) ;
// String table = DorisTableUtils.dorisName(dataSetTableRequest.getId());
DataTableInfoDTO dataTableInfoDTO = new Gson ( ) . fromJson ( dataSetTableRequest . getInfo ( ) , DataTableInfoDTO . class ) ;
List < DataSetTableUnionDTO > list = dataSetTableUnionService . listByTableId ( dataTableInfoDTO . getList ( ) . get ( 0 ) . getTableId ( ) ) ;
2021-05-11 12:21:08 +08:00
String sql = getCustomSQL ( dataTableInfoDTO , list ) ;
// 使用输入的sql先预执行一次,并拿到所有字段
datasourceRequest . setQuery ( sql ) ;
List < TableFiled > previewFields = jdbcProvider . fetchResultField ( datasourceRequest ) ;
2021-05-10 15:22:36 +08:00
2021-05-17 16:43:02 +08:00
QueryProvider qp = ProviderFactory . getQueryProvider ( ds . getType ( ) ) ;
datasourceRequest . setQuery ( qp . createSQLPreview ( sql , previewFields . get ( 0 ) . getFieldName ( ) ) ) ;
2021-05-10 15:22:36 +08:00
Map < String , List > result = jdbcProvider . fetchResultAndField ( datasourceRequest ) ;
List < String [ ] > data = result . get ( " dataList " ) ;
List < TableFiled > fields = result . get ( " fieldList " ) ;
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 ( ) ) ;
}
2021-05-10 17:57:31 +08:00
// 获取每个字段在当前de数据库中的name, 作为sql查询后的remarks返回前端展示
List < DatasetTableField > checkedFieldList = new ArrayList < > ( ) ;
dataTableInfoDTO . getList ( ) . forEach ( ele - > {
checkedFieldList . addAll ( dataSetTableFieldsService . getListByIds ( ele . getCheckedFields ( ) ) ) ;
} ) ;
for ( DatasetTableField datasetTableField : checkedFieldList ) {
for ( TableFiled tableFiled : fields ) {
if ( StringUtils . equalsIgnoreCase ( tableFiled . getFieldName ( ) , DorisTableUtils . dorisFieldName ( datasetTableField . getTableId ( ) + " _ " + datasetTableField . getDataeaseName ( ) ) ) ) {
tableFiled . setRemarks ( datasetTableField . getName ( ) ) ;
break ;
}
}
}
2021-05-10 15:22:36 +08:00
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " fields " , fields ) ;
map . put ( " data " , jsonArray ) ;
return map ;
}
2021-05-17 16:43:02 +08:00
// 自助数据集从doris里预览数据
2021-05-10 15:22:36 +08:00
private String getCustomSQL ( DataTableInfoDTO dataTableInfoDTO , List < DataSetTableUnionDTO > list ) {
2021-05-10 17:57:31 +08:00
Map < String , String [ ] > customInfo = new TreeMap < > ( ) ;
2021-05-10 15:22:36 +08:00
dataTableInfoDTO . getList ( ) . forEach ( ele - > {
String table = DorisTableUtils . dorisName ( ele . getTableId ( ) ) ;
List < DatasetTableField > fields = dataSetTableFieldsService . getListByIds ( ele . getCheckedFields ( ) ) ;
2021-05-10 17:57:31 +08:00
String [ ] array = fields . stream ( ) . map ( f - > table + " . " + f . getDataeaseName ( ) + " AS " + DorisTableUtils . dorisFieldName ( ele . getTableId ( ) + " _ " + f . getDataeaseName ( ) ) ) . toArray ( String [ ] : : new ) ;
2021-05-10 15:22:36 +08:00
customInfo . put ( table , array ) ;
} ) ;
DataTableInfoCustomUnion first = dataTableInfoDTO . getList ( ) . get ( 0 ) ;
if ( CollectionUtils . isNotEmpty ( list ) ) {
StringBuilder field = new StringBuilder ( ) ;
Iterator < Map . Entry < String , String [ ] > > iterator = customInfo . entrySet ( ) . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
Map . Entry < String , String [ ] > next = iterator . next ( ) ;
field . append ( StringUtils . join ( next . getValue ( ) , " , " ) ) . append ( " , " ) ;
}
String f = field . substring ( 0 , field . length ( ) - 1 ) ;
StringBuilder join = new StringBuilder ( ) ;
for ( DataTableInfoCustomUnion dataTableInfoCustomUnion : dataTableInfoDTO . getList ( ) ) {
for ( DataSetTableUnionDTO dto : list ) {
// 被关联表和自助数据集的表相等
if ( StringUtils . equals ( dto . getTargetTableId ( ) , dataTableInfoCustomUnion . getTableId ( ) ) ) {
DatasetTableField sourceField = dataSetTableFieldsService . get ( dto . getSourceTableFieldId ( ) ) ;
DatasetTableField targetField = dataSetTableFieldsService . get ( dto . getTargetTableFieldId ( ) ) ;
join . append ( convertUnionTypeToSQL ( dto . getSourceUnionRelation ( ) ) )
. append ( DorisTableUtils . dorisName ( dto . getTargetTableId ( ) ) )
. append ( " ON " )
. append ( DorisTableUtils . dorisName ( dto . getSourceTableId ( ) ) ) . append ( " . " ) . append ( sourceField . getDataeaseName ( ) )
. append ( " = " )
. append ( DorisTableUtils . dorisName ( dto . getTargetTableId ( ) ) ) . append ( " . " ) . append ( targetField . getDataeaseName ( ) ) ;
}
}
}
return MessageFormat . format ( " SELECT {0} FROM {1} " , f , DorisTableUtils . dorisName ( first . getTableId ( ) ) ) + join . toString ( ) ;
} else {
return MessageFormat . format ( " SELECT {0} FROM {1} " , StringUtils . join ( customInfo . get ( DorisTableUtils . dorisName ( first . getTableId ( ) ) ) , " , " ) , DorisTableUtils . dorisName ( first . getTableId ( ) ) ) ;
}
}
private String convertUnionTypeToSQL ( String unionType ) {
switch ( unionType ) {
case " 1:1 " :
return " INNER JOIN " ;
case " 1:N " :
return " LEFT JOIN " ;
case " N:1 " :
return " RIGHT JOIN " ;
default :
return " INNER JOIN " ;
}
}
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 ) ;
2021-03-15 15:24:10 +08:00
List < TableFiled > fields = new ArrayList < > ( ) ;
2021-02-24 17:14:37 +08:00
long syncTime = System . currentTimeMillis ( ) ;
2021-03-15 15:24:10 +08:00
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 ( ) ) ;
2021-04-27 17:32:26 +08:00
fields = datasourceProvider . fetchResultField ( datasourceRequest ) ;
2021-04-20 16:23:47 +08:00
} else if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " excel " ) ) {
DataTableInfoDTO dataTableInfoDTO = new Gson ( ) . fromJson ( dataSetTableRequest . getInfo ( ) , DataTableInfoDTO . class ) ;
String path = dataTableInfoDTO . getData ( ) ;
File file = new File ( path ) ;
Map < String , Object > map = parseExcel ( path . substring ( path . lastIndexOf ( " / " ) + 1 ) , new FileInputStream ( file ) , false ) ;
fields = ( List < TableFiled > ) map . get ( " fields " ) ;
} else if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " custom " ) ) {
// save field
2021-05-10 15:22:36 +08:00
DataTableInfoDTO dataTableInfoDTO = new Gson ( ) . fromJson ( dataSetTableRequest . getInfo ( ) , DataTableInfoDTO . class ) ;
List < DataTableInfoCustomUnion > list = dataTableInfoDTO . getList ( ) ;
List < DatasetTableField > fieldList = new ArrayList < > ( ) ;
list . forEach ( ele - > {
List < DatasetTableField > listByIds = dataSetTableFieldsService . getListByIds ( ele . getCheckedFields ( ) ) ;
2021-05-10 18:41:49 +08:00
listByIds . forEach ( f - > {
f . setDataeaseName ( DorisTableUtils . dorisFieldName ( ele . getTableId ( ) + " _ " + f . getDataeaseName ( ) ) ) ;
} ) ;
2021-05-10 15:22:36 +08:00
fieldList . addAll ( listByIds ) ;
} ) ;
for ( int i = 0 ; i < fieldList . size ( ) ; i + + ) {
DatasetTableField datasetTableField = fieldList . get ( i ) ;
datasetTableField . setId ( null ) ;
datasetTableField . setTableId ( datasetTable . getId ( ) ) ;
datasetTableField . setColumnIndex ( i ) ;
}
dataSetTableFieldsService . batchEdit ( fieldList ) ;
// custom 创建doris视图
createDorisView ( DorisTableUtils . dorisName ( datasetTable . getId ( ) ) , getCustomSQL ( dataTableInfoDTO , dataSetTableUnionService . listByTableId ( dataTableInfoDTO . getList ( ) . get ( 0 ) . getTableId ( ) ) ) ) ;
return ;
2021-03-15 15:24:10 +08:00
}
2021-05-18 15:17:57 +08:00
QueryProvider qp = null ;
if ( ! ObjectUtils . isEmpty ( ds ) ) {
qp = ProviderFactory . getQueryProvider ( ds . getType ( ) ) ;
2021-03-15 15:24:10 +08:00
}
2021-02-24 17:14:37 +08:00
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 ( ) ;
2021-02-24 17:14:37 +08:00
datasetTableField . setTableId ( datasetTable . getId ( ) ) ;
datasetTableField . setOriginName ( filed . getFieldName ( ) ) ;
datasetTableField . setName ( filed . getRemarks ( ) ) ;
2021-05-10 15:22:36 +08:00
if ( StringUtils . equalsIgnoreCase ( datasetTable . getType ( ) , " excel " ) ) {
2021-05-12 15:17:51 +08:00
datasetTableField . setDataeaseName ( DorisTableUtils . excelColumnName ( filed . getFieldName ( ) ) ) ;
2021-05-10 15:22:36 +08:00
} else {
2021-05-07 16:01:04 +08:00
datasetTableField . setDataeaseName ( filed . getFieldName ( ) ) ;
}
2021-02-24 17:14:37 +08:00
datasetTableField . setType ( filed . getFieldType ( ) ) ;
2021-04-20 16:23:47 +08:00
if ( ObjectUtils . isEmpty ( ds ) ) {
datasetTableField . setDeType ( transFieldType ( filed . getFieldType ( ) ) ) ;
2021-05-13 14:45:01 +08:00
datasetTableField . setDeExtractType ( transFieldType ( filed . getFieldType ( ) ) ) ;
2021-04-20 16:23:47 +08:00
} else {
2021-05-17 16:43:02 +08:00
Integer fieldType = qp . transFieldType ( filed . getFieldType ( ) ) ;
2021-05-13 15:17:27 +08:00
datasetTableField . setDeType ( fieldType = = 4 ? 2 : fieldType ) ;
datasetTableField . setDeExtractType ( fieldType ) ;
2021-04-20 16:23:47 +08:00
}
2021-04-27 17:32:26 +08:00
datasetTableField . setSize ( filed . getFieldSize ( ) ) ;
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
2021-05-10 15:22:36 +08:00
private void createDorisView ( String dorisTableName , String customSql ) throws Exception {
Datasource dorisDatasource = ( Datasource ) CommonBeanFactory . getBean ( " DorisDatasource " ) ;
JdbcProvider jdbcProvider = CommonBeanFactory . getBean ( JdbcProvider . class ) ;
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( dorisDatasource ) ;
2021-05-17 16:43:02 +08:00
DDLProvider ddlProvider = ProviderFactory . getDDLProvider ( dorisDatasource . getType ( ) ) ;
2021-05-12 16:39:53 +08:00
// 先删除表
2021-05-17 16:43:02 +08:00
datasourceRequest . setQuery ( ddlProvider . dropTableOrView ( dorisTableName ) ) ;
2021-05-12 16:39:53 +08:00
jdbcProvider . exec ( datasourceRequest ) ;
2021-05-17 16:43:02 +08:00
datasourceRequest . setQuery ( ddlProvider . createView ( dorisTableName , customSql ) ) ;
2021-05-10 15:22:36 +08:00
jdbcProvider . exec ( datasourceRequest ) ;
2021-02-24 18:06:21 +08:00
}
2021-02-25 18:15:10 +08:00
2021-04-20 16:23:47 +08:00
public Integer transFieldType ( String field ) {
switch ( field ) {
case " TEXT " :
return 0 ;
case " TIME " :
return 1 ;
case " INT " :
return 2 ;
case " DOUBLE " :
return 3 ;
default :
return 0 ;
}
}
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 ) {
2021-05-10 15:22:36 +08:00
if ( datasetTableIncrementalConfig = = null | | StringUtils . isEmpty ( datasetTableIncrementalConfig . getTableId ( ) ) ) {
2021-05-10 10:13:00 +08:00
return ;
}
2021-04-01 15:02:57 +08:00
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-05-12 16:10:54 +08:00
datasetTableIncrementalConfigMapper . updateByPrimaryKey ( datasetTableIncrementalConfig ) ;
2021-03-18 17:59:52 +08:00
}
}
2021-04-01 15:02:57 +08:00
private void checkName ( DatasetTable datasetTable ) {
2021-05-20 18:11:32 +08:00
// if (StringUtils.isEmpty(datasetTable.getId()) && StringUtils.equalsIgnoreCase("db", datasetTable.getType())) {
// return;
// }
2021-04-01 15:02:57 +08:00
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-04-14 18:46:13 +08:00
public Map < String , Object > getDatasetDetail ( String id ) {
Map < String , Object > map = new HashMap < > ( ) ;
DatasetTable table = datasetTableMapper . selectByPrimaryKey ( id ) ;
map . put ( " table " , table ) ;
if ( ObjectUtils . isNotEmpty ( table ) ) {
Datasource datasource = datasourceMapper . selectByPrimaryKey ( table . getDataSourceId ( ) ) ;
map . put ( " datasource " , datasource ) ;
}
return map ;
}
2021-04-19 18:33:53 +08:00
public Map < String , Object > excelSaveAndParse ( MultipartFile file ) throws Exception {
String filename = file . getOriginalFilename ( ) ;
2021-04-20 16:23:47 +08:00
// parse file
Map < String , Object > fileMap = parseExcel ( filename , file . getInputStream ( ) , true ) ;
// save file
String filePath = saveFile ( file ) ;
Map < String , Object > map = new HashMap < > ( fileMap ) ;
map . put ( " path " , filePath ) ;
return map ;
}
2021-04-19 18:33:53 +08:00
2021-04-20 16:23:47 +08:00
private Map < String , Object > parseExcel ( String filename , InputStream inputStream , boolean isPreview ) throws Exception {
String suffix = filename . substring ( filename . lastIndexOf ( " . " ) + 1 ) ;
2021-04-19 18:33:53 +08:00
List < TableFiled > fields = new ArrayList < > ( ) ;
List < String [ ] > data = new ArrayList < > ( ) ;
List < Map < String , Object > > jsonArray = new ArrayList < > ( ) ;
2021-05-20 11:12:32 +08:00
List < String > sheets = new ArrayList < > ( ) ;
2021-04-19 18:33:53 +08:00
if ( StringUtils . equalsIgnoreCase ( suffix , " xls " ) ) {
HSSFWorkbook workbook = new HSSFWorkbook ( inputStream ) ;
HSSFSheet sheet0 = workbook . getSheetAt ( 0 ) ;
2021-05-20 11:12:32 +08:00
for ( int i = 0 ; i < workbook . getNumberOfSheets ( ) ; i + + ) {
sheets . add ( workbook . getSheetAt ( i ) . getSheetName ( ) ) ;
}
2021-04-19 18:33:53 +08:00
if ( sheet0 . getNumMergedRegions ( ) > 0 ) {
throw new RuntimeException ( " Sheet have merged regions. " ) ;
}
2021-04-20 16:23:47 +08:00
int rows ;
if ( isPreview ) {
rows = Math . min ( sheet0 . getPhysicalNumberOfRows ( ) , 100 ) ;
} else {
rows = sheet0 . getPhysicalNumberOfRows ( ) ;
}
2021-04-19 18:33:53 +08:00
for ( int i = 0 ; i < rows ; i + + ) {
HSSFRow row = sheet0 . getRow ( i ) ;
String [ ] r = new String [ row . getPhysicalNumberOfCells ( ) ] ;
for ( int j = 0 ; j < row . getPhysicalNumberOfCells ( ) ; j + + ) {
if ( i = = 0 ) {
TableFiled tableFiled = new TableFiled ( ) ;
2021-04-20 16:23:47 +08:00
tableFiled . setFieldType ( " TEXT " ) ;
2021-05-18 16:33:41 +08:00
tableFiled . setFieldSize ( 1024 ) ;
2021-05-18 17:28:12 +08:00
String columnName = readCell ( row . getCell ( j ) ) ;
if ( StringUtils . isEmpty ( columnName ) ) {
columnName = " NONE_ " + String . valueOf ( j ) ;
2021-05-18 15:17:57 +08:00
}
2021-05-18 17:28:12 +08:00
tableFiled . setFieldName ( columnName ) ;
tableFiled . setRemarks ( columnName ) ;
2021-04-19 18:33:53 +08:00
fields . add ( tableFiled ) ;
} else {
r [ j ] = readCell ( row . getCell ( j ) ) ;
}
}
if ( i > 0 ) {
data . add ( r ) ;
}
}
} else if ( StringUtils . equalsIgnoreCase ( suffix , " xlsx " ) ) {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook ( inputStream ) ;
XSSFSheet sheet0 = xssfWorkbook . getSheetAt ( 0 ) ;
2021-05-20 11:12:32 +08:00
for ( int i = 0 ; i < xssfWorkbook . getNumberOfSheets ( ) ; i + + ) {
sheets . add ( xssfWorkbook . getSheetAt ( i ) . getSheetName ( ) ) ;
}
2021-04-19 18:33:53 +08:00
if ( sheet0 . getNumMergedRegions ( ) > 0 ) {
throw new RuntimeException ( " Sheet have merged regions. " ) ;
}
2021-04-20 16:23:47 +08:00
int rows ;
if ( isPreview ) {
rows = Math . min ( sheet0 . getPhysicalNumberOfRows ( ) , 100 ) ;
} else {
rows = sheet0 . getPhysicalNumberOfRows ( ) ;
}
2021-04-19 18:33:53 +08:00
for ( int i = 0 ; i < rows ; i + + ) {
XSSFRow row = sheet0 . getRow ( i ) ;
String [ ] r = new String [ row . getPhysicalNumberOfCells ( ) ] ;
for ( int j = 0 ; j < row . getPhysicalNumberOfCells ( ) ; j + + ) {
if ( i = = 0 ) {
TableFiled tableFiled = new TableFiled ( ) ;
2021-04-20 16:23:47 +08:00
tableFiled . setFieldType ( " TEXT " ) ;
2021-05-07 16:01:04 +08:00
tableFiled . setFieldSize ( 1024 ) ;
2021-05-18 17:28:12 +08:00
String columnName = readCell ( row . getCell ( j ) ) ;
if ( StringUtils . isEmpty ( columnName ) ) {
columnName = " NONE_ " + String . valueOf ( j ) ;
2021-05-18 16:33:41 +08:00
}
2021-05-18 17:28:12 +08:00
tableFiled . setFieldName ( columnName ) ;
tableFiled . setRemarks ( columnName ) ;
2021-04-19 18:33:53 +08:00
fields . add ( tableFiled ) ;
} else {
r [ j ] = readCell ( row . getCell ( j ) ) ;
}
}
if ( i > 0 ) {
data . add ( r ) ;
}
}
} else if ( StringUtils . equalsIgnoreCase ( suffix , " csv " ) ) {
BufferedReader reader = new BufferedReader ( new InputStreamReader ( inputStream , StandardCharsets . UTF_8 ) ) ;
String s = reader . readLine ( ) ; // first line
String [ ] split = s . split ( " , " ) ;
for ( String s1 : split ) {
TableFiled tableFiled = new TableFiled ( ) ;
tableFiled . setFieldName ( s1 ) ;
tableFiled . setRemarks ( s1 ) ;
2021-04-20 16:23:47 +08:00
tableFiled . setFieldType ( " TEXT " ) ;
2021-04-19 18:33:53 +08:00
fields . add ( tableFiled ) ;
}
int num = 1 ;
2021-04-20 16:23:47 +08:00
String line ;
2021-04-19 18:33:53 +08:00
while ( ( line = reader . readLine ( ) ) ! = null ) {
2021-04-20 16:23:47 +08:00
if ( isPreview ) {
if ( num > 100 ) {
break ;
}
2021-04-19 18:33:53 +08:00
}
data . add ( line . split ( " , " ) ) ;
num + + ;
}
}
String [ ] fieldArray = fields . stream ( ) . map ( TableFiled : : getFieldName ) . toArray ( String [ ] : : new ) ;
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 ( ) ) ;
}
inputStream . close ( ) ;
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " fields " , fields ) ;
map . put ( " data " , jsonArray ) ;
2021-05-20 11:12:32 +08:00
map . put ( " sheets " , sheets ) ;
2021-04-19 18:33:53 +08:00
return map ;
}
private String readCell ( Cell cell ) {
CellType cellTypeEnum = cell . getCellTypeEnum ( ) ;
if ( cellTypeEnum . equals ( CellType . STRING ) ) {
return cell . getStringCellValue ( ) ;
} else if ( cellTypeEnum . equals ( CellType . NUMERIC ) ) {
double d = cell . getNumericCellValue ( ) ;
try {
return new Double ( d ) . longValue ( ) + " " ;
} catch ( Exception e ) {
BigDecimal b = new BigDecimal ( d ) ;
return b . setScale ( 2 , BigDecimal . ROUND_HALF_UP ) . doubleValue ( ) + " " ;
}
} else if ( cellTypeEnum . equals ( CellType . BOOLEAN ) ) {
return cell . getBooleanCellValue ( ) ? " 1 " : " 0 " ;
} else {
return " " ;
}
}
2021-04-20 16:23:47 +08:00
private String saveFile ( MultipartFile file ) throws Exception {
String filename = file . getOriginalFilename ( ) ;
2021-05-07 18:08:45 +08:00
String suffix = filename . substring ( filename . lastIndexOf ( " . " ) + 1 ) ;
filename = Md5Utils . md5 ( filename . substring ( 0 , filename . length ( ) - suffix . length ( ) ) ) ;
2021-04-23 15:09:43 +08:00
String dirPath = path + AuthUtils . getUser ( ) . getUsername ( ) + " / " ;
File p = new File ( dirPath ) ;
2021-04-20 16:23:47 +08:00
if ( ! p . exists ( ) ) {
p . mkdirs ( ) ;
}
2021-05-07 18:08:45 +08:00
String filePath = dirPath + filename + " . " + suffix ;
2021-04-20 16:23:47 +08:00
File f = new File ( filePath ) ;
FileOutputStream fileOutputStream = new FileOutputStream ( f ) ;
fileOutputStream . write ( file . getBytes ( ) ) ;
fileOutputStream . flush ( ) ;
fileOutputStream . close ( ) ;
return filePath ;
}
2021-05-18 11:24:52 +08:00
public Boolean checkDorisTableIsExists ( String id ) throws Exception {
Datasource dorisDatasource = ( Datasource ) CommonBeanFactory . getBean ( " DorisDatasource " ) ;
JdbcProvider jdbcProvider = CommonBeanFactory . getBean ( JdbcProvider . class ) ;
DatasourceRequest datasourceRequest = new DatasourceRequest ( ) ;
datasourceRequest . setDatasource ( dorisDatasource ) ;
QueryProvider qp = ProviderFactory . getQueryProvider ( dorisDatasource . getType ( ) ) ;
datasourceRequest . setQuery ( qp . searchTable ( DorisTableUtils . dorisName ( id ) ) ) ;
List < String [ ] > data = jdbcProvider . getData ( datasourceRequest ) ;
return CollectionUtils . isNotEmpty ( data ) ;
}
2021-02-23 18:23:56 +08:00
}