forked from github/dataease
feat(X-Pack): 数据填报Excel批量上传校验优化
This commit is contained in:
parent
7e89b0472d
commit
a11fbebe20
@ -41,6 +41,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
@ -171,7 +172,15 @@ public class DataFillDataService {
|
||||
List<String[]> countData = datasourceProvider.getData(datasourceRequest);
|
||||
long count = NumberUtils.toLong(countData.get(0)[0]);
|
||||
|
||||
String searchSql = extDDLProvider.searchSql(dataFillForm.getTableName(), searchFields, whereSql, searchRequest.getPageSize(), (searchRequest.getCurrentPage() - 1) * searchRequest.getPageSize());
|
||||
long totalPage = new BigDecimal(count).divide(new BigDecimal(searchRequest.getPageSize()), 0, RoundingMode.CEILING).longValue();
|
||||
|
||||
long currentPage = totalPage < searchRequest.getCurrentPage() ? totalPage - 1 : searchRequest.getCurrentPage();
|
||||
|
||||
if (currentPage < 1) {
|
||||
currentPage = 1;
|
||||
}
|
||||
|
||||
String searchSql = extDDLProvider.searchSql(dataFillForm.getTableName(), searchFields, whereSql, searchRequest.getPageSize(), (currentPage - 1) * searchRequest.getPageSize());
|
||||
datasourceRequest.setQuery(searchSql);
|
||||
|
||||
List<String[]> data2 = datasourceProvider.getData(datasourceRequest);
|
||||
@ -241,7 +250,7 @@ public class DataFillDataService {
|
||||
.setFields(fields)
|
||||
.setTotal(count)
|
||||
.setPageSize(searchRequest.getPageSize())
|
||||
.setCurrentPage(searchRequest.getCurrentPage());
|
||||
.setCurrentPage(currentPage);
|
||||
}
|
||||
|
||||
|
||||
@ -378,7 +387,7 @@ public class DataFillDataService {
|
||||
uniqueMap.putIfAbsent(name, new ArrayList<>());
|
||||
if (uniqueMap.get(name).contains(data.get(name).toString())) {
|
||||
//提前判断录入的数据有没有unique字段重复的
|
||||
DataEaseException.throwException(extTableFields.get(name).getSettings().getName() + " 值不能重复");
|
||||
DataEaseException.throwException("[" + extTableFields.get(name).getSettings().getName() + "] 值: " + data.get(name) + " 不能重复");
|
||||
} else {
|
||||
uniqueMap.get(name).add(data.get(name).toString());
|
||||
}
|
||||
@ -416,7 +425,7 @@ public class DataFillDataService {
|
||||
long count = NumberUtils.toLong(countData.get(0)[0]);
|
||||
|
||||
if (count > 0) {
|
||||
DataEaseException.throwException(extTableFields.get(uniqueField.getFiledName()).getSettings().getName() + " 值不能重复");
|
||||
DataEaseException.throwException("[" + extTableFields.get(uniqueField.getFiledName()).getSettings().getName() + "] 值: " + data.get(name) + " 在数据库中已存在, 不能重复");
|
||||
}
|
||||
|
||||
}
|
||||
@ -684,14 +693,10 @@ public class DataFillDataService {
|
||||
if (i < excelDatum.size()) {
|
||||
excelRowData = excelDatum.get(i);
|
||||
}
|
||||
if (StringUtils.isBlank(excelRowData)) { //处理必填,这里如果是字符串格式的,强制改成空字符串防止报错,其他类型都直接报错
|
||||
if (StringUtils.isBlank(excelRowData)) { //处理必填
|
||||
excelRowData = null;
|
||||
if (field.getSettings().isRequired()) {
|
||||
if (field.getSettings().getMapping().getType().equals(ExtTableField.BaseType.nvarchar) ||
|
||||
field.getSettings().getMapping().getType().equals(ExtTableField.BaseType.text)) {
|
||||
excelRowData = StringUtils.EMPTY;
|
||||
} else {
|
||||
DataEaseException.throwException(field.getSettings().getName() + "不能为空");
|
||||
}
|
||||
DataEaseException.throwException("[" + field.getSettings().getName() + "] 不能为空");
|
||||
}
|
||||
}
|
||||
if (excelRowData == null) {
|
||||
@ -709,7 +714,11 @@ public class DataFillDataService {
|
||||
break;
|
||||
case datetime:
|
||||
Date date = getDate(field, excelRowData);
|
||||
rowData.put(field.getSettings().getMapping().getColumnName(), date.getTime());
|
||||
Long time = date == null ? null : date.getTime();
|
||||
if (time != null && time < 0) {
|
||||
throw new Exception("时间不能小于1970/01/01");
|
||||
}
|
||||
rowData.put(field.getSettings().getMapping().getColumnName(), time);
|
||||
break;
|
||||
default:
|
||||
if (StringUtils.equalsIgnoreCase(field.getType(), "checkbox") ||
|
||||
@ -723,11 +732,23 @@ public class DataFillDataService {
|
||||
}
|
||||
if (field.getSettings().isRequired()) {
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
DataEaseException.throwException(field.getSettings().getName() + "不能为空");
|
||||
DataEaseException.throwException("[" + field.getSettings().getName() + "] 不能为空");
|
||||
}
|
||||
}
|
||||
rowData.put(field.getSettings().getMapping().getColumnName(), gson.toJson(list));
|
||||
} else {
|
||||
//校验手机号,校验邮箱格式
|
||||
if (StringUtils.equalsAnyIgnoreCase(field.getSettings().getInputType(), "tel")) {
|
||||
if (!excelRowData.matches("^1[3|4|5|7|8][0-9]{9}$")) {
|
||||
throw new Exception(Translator.get("i18n_wrong_tel"));
|
||||
}
|
||||
}
|
||||
if (StringUtils.equalsAnyIgnoreCase(field.getSettings().getInputType(), "email")) {
|
||||
if (!excelRowData.matches("^[a-zA-Z0-9_._-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")) {
|
||||
throw new Exception(Translator.get("i18n_wrong_email"));
|
||||
}
|
||||
}
|
||||
|
||||
rowData.put(field.getSettings().getMapping().getColumnName(), excelRowData);
|
||||
}
|
||||
}
|
||||
@ -735,7 +756,7 @@ public class DataFillDataService {
|
||||
} catch (DataEaseException e) {
|
||||
DataEaseException.throwException(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
DataEaseException.throwException(field.getSettings().getName() + "格式错误");
|
||||
DataEaseException.throwException("[" + field.getSettings().getName() + "] 值: " + excelRowData + " 格式解析错误: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ -755,6 +776,9 @@ public class DataFillDataService {
|
||||
}
|
||||
|
||||
private static Date getDate(ExtTableField field, String excelRowData) throws ParseException {
|
||||
if (StringUtils.isBlank(excelRowData)) {
|
||||
return null;
|
||||
}
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //默认会拿到这种格式的
|
||||
if (field.getSettings().isEnableTime()) {
|
||||
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
@ -387,7 +387,7 @@ public class DataFillService {
|
||||
break;
|
||||
case text:
|
||||
case nvarchar:
|
||||
if (StringUtils.equalsIgnoreCase("select", formField.getType()) || StringUtils.equalsIgnoreCase("checkbox", formField.getType())) {
|
||||
if (StringUtils.equalsIgnoreCase("select", formField.getType()) && formField.getSettings().isMultiple() || StringUtils.equalsIgnoreCase("checkbox", formField.getType())) {
|
||||
example = "\n(多个值使用分号\";\"分割)";
|
||||
} else if (StringUtils.equalsIgnoreCase("email", formField.getSettings().getInputType())) {
|
||||
example = "\n(邮箱格式)";
|
||||
|
@ -77,7 +77,12 @@ export default {
|
||||
name: this.$t('data_fill.form.email'),
|
||||
rules: [{ pattern: EMAIL_REGEX, message: this.$t('user.email_format_is_incorrect'), trigger: ['blur', 'change'] }]
|
||||
}
|
||||
]
|
||||
],
|
||||
pickerOptions: {
|
||||
disabledDate: (time) => {
|
||||
return time.getTime() < new Date(0).getTime()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
watch: {},
|
||||
@ -343,6 +348,7 @@ export default {
|
||||
:placeholder="item.settings.placeholder"
|
||||
style="width: 100%"
|
||||
size="small"
|
||||
:picker-options="pickerOptions"
|
||||
/>
|
||||
<el-date-picker
|
||||
v-else-if="item.type === 'date' && item.settings.enableTime"
|
||||
@ -353,6 +359,7 @@ export default {
|
||||
:placeholder="item.settings.placeholder"
|
||||
style="width: 100%"
|
||||
size="small"
|
||||
:picker-options="pickerOptions"
|
||||
/>
|
||||
<el-date-picker
|
||||
v-else-if="item.type === 'dateRange' && !item.settings.enableTime"
|
||||
@ -365,6 +372,7 @@ export default {
|
||||
:end-placeholder="item.settings.endPlaceholder"
|
||||
style="width: 100%"
|
||||
size="small"
|
||||
:picker-options="pickerOptions"
|
||||
/>
|
||||
<el-date-picker
|
||||
v-else-if="item.type === 'dateRange' && item.settings.enableTime"
|
||||
@ -377,6 +385,7 @@ export default {
|
||||
:end-placeholder="item.settings.endPlaceholder"
|
||||
style="width: 100%"
|
||||
size="small"
|
||||
:picker-options="pickerOptions"
|
||||
/>
|
||||
|
||||
</el-form-item>
|
||||
|
@ -136,7 +136,8 @@
|
||||
</div>
|
||||
<div style="flex: 1">
|
||||
<grid-table
|
||||
v-if="columns.length > 0"
|
||||
v-if="columns.length > 0 && dataTableShow"
|
||||
ref="dataTable"
|
||||
v-loading="$store.getters.loadingMap[$store.getters.currentPath]"
|
||||
style="width: 100%; height: 100%"
|
||||
border
|
||||
@ -547,6 +548,7 @@ export default {
|
||||
Authorization: token,
|
||||
'Accept-Language': i18n.locale.replace('_', '-')
|
||||
},
|
||||
dataTableShow: true,
|
||||
fileList: [],
|
||||
uploading: false,
|
||||
operateName: '',
|
||||
@ -642,6 +644,10 @@ export default {
|
||||
this.data = []
|
||||
this.records = []
|
||||
this.tasks = []
|
||||
this.dataTableShow = false
|
||||
this.$nextTick(() => {
|
||||
this.dataTableShow = true
|
||||
})
|
||||
}
|
||||
this.initTable(this.param.id)
|
||||
},
|
||||
@ -731,6 +737,7 @@ export default {
|
||||
if (res.data) {
|
||||
this.paginationConfig.key = res.data.key
|
||||
this.paginationConfig.total = res.data.total
|
||||
this.paginationConfig.currentPage = res.data.currentPage
|
||||
const _data = []
|
||||
forEach(res.data.data, d => {
|
||||
const obj = {}
|
||||
@ -911,7 +918,7 @@ export default {
|
||||
const link = document.createElement('a')
|
||||
link.style.display = 'none'
|
||||
link.href = URL.createObjectURL(blob)
|
||||
link.download = 'test.xlsx' // 下载的文件名
|
||||
link.download = this.param.name + '.xlsx' // 下载的文件名
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
|
Loading…
Reference in New Issue
Block a user