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") @PostMapping("excel/upload")
public Map<String, Object> excelUpload(@RequestParam("file") MultipartFile file) throws Exception { public Map<String, Object> excelUpload(@RequestParam("file") MultipartFile file, @RequestParam("tableId") String tableId) throws Exception {
return dataSetTableService.excelSaveAndParse(file); return dataSetTableService.excelSaveAndParse(file, tableId);
} }
@PostMapping("checkDorisTableIsExists/{id}") @PostMapping("checkDorisTableIsExists/{id}")

View File

@ -878,10 +878,28 @@ public class DataSetTableService {
return map; 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(); String filename = file.getOriginalFilename();
// parse file // parse file
Map<String, Object> fileMap = parseExcel(filename, file.getInputStream(), true); 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 // save file
String filePath = saveFile(file); String filePath = saveFile(file);
Map<String, Object> map = new HashMap<>(fileMap); Map<String, Object> map = new HashMap<>(fileMap);

View File

@ -272,4 +272,5 @@ i18n_msg_type_panel_share_cacnel=Dashboard unshared
i18n_msg_type_dataset_sync=Data set synchronization i18n_msg_type_dataset_sync=Data set synchronization
i18n_msg_type_dataset_sync_success=Dataset synchronization successful i18n_msg_type_dataset_sync_success=Dataset synchronization successful
i18n_msg_type_dataset_sync_faild=Dataset synchronization failed i18n_msg_type_dataset_sync_faild=Dataset synchronization failed
i18n_data_not_sync=Please sync data first 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_success=数据集同步成功
i18n_msg_type_dataset_sync_faild=数据集同步失败 i18n_msg_type_dataset_sync_faild=数据集同步失败
i18n_data_not_sync=请先完成数据同步 i18n_data_not_sync=请先完成数据同步
i18n_excel_colume_change=Excel的列名与原数据集不一致

View File

@ -274,4 +274,5 @@ i18n_msg_type_panel_share_cacnel=儀表板取消分享
i18n_msg_type_dataset_sync=數據集同步 i18n_msg_type_dataset_sync=數據集同步
i18n_msg_type_dataset_sync_success=數據集同步成功 i18n_msg_type_dataset_sync_success=數據集同步成功
i18n_msg_type_dataset_sync_faild=數據集同步失敗 i18n_msg_type_dataset_sync_faild=數據集同步失敗
i18n_data_not_sync=請先完成數據同步 i18n_data_not_sync=請先完成數據同步
i18n_excel_colume_change=Excel的列名與原數據集不一致

View File

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