feat: 追加/更新 excel 数据集时,列名与原数据集保持一致

This commit is contained in:
taojinlong 2021-07-12 17:24:35 +08:00
parent fb186e9cff
commit 48ed621854
6 changed files with 43 additions and 9 deletions

View File

@ -100,8 +100,8 @@ public class DataSetTableController {
}
@PostMapping("excel/upload")
public Map<String, Object> excelUpload(@RequestParam("file") MultipartFile file) throws Exception {
return dataSetTableService.excelSaveAndParse(file);
public Map<String, Object> excelUpload(@RequestParam("file") MultipartFile file, @RequestParam("tableId") String tableId) throws Exception {
return dataSetTableService.excelSaveAndParse(file, tableId);
}
@PostMapping("checkDorisTableIsExists/{id}")

View File

@ -878,10 +878,28 @@ public class DataSetTableService {
return map;
}
public Map<String, Object> excelSaveAndParse(MultipartFile file) throws Exception {
public Map<String, Object> excelSaveAndParse(MultipartFile file, String tableId) throws Exception {
String filename = file.getOriginalFilename();
// parse file
Map<String, Object> fileMap = parseExcel(filename, file.getInputStream(), true);
if(StringUtils.isNotEmpty(tableId)){
List<DatasetTableField> datasetTableFields = dataSetTableFieldsService.getFieldsByTableId(tableId);
datasetTableFields.sort((o1, o2) -> {
if (o1.getColumnIndex() == null) {
return -1;
}
if (o2.getColumnIndex() == null) {
return 1;
}
return o1.getColumnIndex().compareTo(o2.getColumnIndex());
});
List<TableFiled> fields = (List<TableFiled>)fileMap.get("fields");
List<String> newFields = fields.stream().map(TableFiled::getRemarks).collect(Collectors.toList());
List<String> oldFields = datasetTableFields.stream().map(DatasetTableField::getOriginName).collect(Collectors.toList());
if (!oldFields.equals(newFields)) {
DataEaseException.throwException(Translator.get("i18n_excel_colume_change"));
}
}
// save file
String filePath = saveFile(file);
Map<String, Object> map = new HashMap<>(fileMap);

View File

@ -273,3 +273,4 @@ i18n_msg_type_dataset_sync=Data set synchronization
i18n_msg_type_dataset_sync_success=Dataset synchronization successful
i18n_msg_type_dataset_sync_faild=Dataset synchronization failed
i18n_data_not_sync=Please sync data first
i18n_excel_colume_change=The column name of Excel is inconsistent with the original data set

View File

@ -272,3 +272,4 @@ i18n_msg_type_dataset_sync=数据集同步
i18n_msg_type_dataset_sync_success=数据集同步成功
i18n_msg_type_dataset_sync_faild=数据集同步失败
i18n_data_not_sync=请先完成数据同步
i18n_excel_colume_change=Excel的列名与原数据集不一致

View File

@ -275,3 +275,4 @@ i18n_msg_type_dataset_sync=數據集同步
i18n_msg_type_dataset_sync_success=數據集同步成功
i18n_msg_type_dataset_sync_faild=數據集同步失敗
i18n_data_not_sync=請先完成數據同步
i18n_excel_colume_change=Excel的列名與原數據集不一致

View File

@ -28,6 +28,7 @@
:multiple="false"
:show-file-list="false"
:file-list="fileList"
:data="param"
accept=".xls,.xlsx,"
:before-upload="beforeUpload"
:on-success="uploadSuccess"
@ -80,6 +81,7 @@
<script>
import { post } from '@/api/dataset/dataset'
import { getToken } from '@/utils/auth'
import i18n from "@/lang";
const token = getToken()
@ -89,6 +91,10 @@ export default {
param: {
type: Object,
default: null
},
tableId: {
type: String,
default: null
}
},
data() {
@ -100,7 +106,7 @@ export default {
mode: '1',
height: 600,
fileList: [],
headers: { Authorization: token },
headers: { Authorization: token , 'Accept-Language': i18n.locale.replace('_', '-')},
baseUrl: process.env.VUE_APP_BASE_API,
path: '',
uploading: false
@ -115,6 +121,11 @@ export default {
}
this.calHeight()
},
created() {
if (!this.param.tableId) {
this.param.tableId = ""
}
},
methods: {
// initDataSource() {
// listDatasource().then(response => {
@ -132,6 +143,10 @@ export default {
this.uploading = true
},
uploadFail(response, file, fileList) {
let myError=response.toString();
myError=myError.replace("Error: ","")
const errorMessage = JSON.parse(myError).message + ", " + this.$t('dataset.parse_error');
this.path = ''
this.fields = []
this.sheets = []
@ -143,7 +158,7 @@ export default {
this.uploading = false
this.$message({
type: 'error',
message: this.$t('dataset.parse_error'),
message: errorMessage,
showClose: true
})
},
@ -166,8 +181,6 @@ export default {
},
save() {
// console.log(this.checkTableList);
// console.log(this.scene);
if (!this.name || this.name === '') {
this.$message({
showClose: true,