|
|
@ -1,11 +1,7 @@ |
|
|
package com.xujie.modules.order.support; |
|
|
package com.xujie.modules.order.support; |
|
|
|
|
|
|
|
|
import com.xujie.modules.order.dao.PoFieldMetaMapper; |
|
|
|
|
|
import com.xujie.modules.order.dao.PoImportFieldAliasMapper; |
|
|
|
|
|
import com.xujie.modules.order.entity.PoFieldMeta; |
|
|
|
|
|
import com.xujie.modules.order.entity.PoImportFieldAlias; |
|
|
|
|
|
import com.xujie.modules.order.vo.PoFieldSchemaFieldVo; |
|
|
import com.xujie.modules.order.vo.PoFieldSchemaFieldVo; |
|
|
import com.xujie.modules.order.vo.PoImportPreviewVo; |
|
|
|
|
|
|
|
|
import com.xujie.modules.order.vo.PoFieldSchemaVo; |
|
|
import com.xujie.modules.order.vo.PoImportResultVo; |
|
|
import com.xujie.modules.order.vo.PoImportResultVo; |
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
import org.apache.poi.ss.usermodel.DataFormatter; |
|
|
import org.apache.poi.ss.usermodel.DataFormatter; |
|
|
@ -20,136 +16,269 @@ import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
import java.util.Collections; |
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
|
|
|
import java.util.HashSet; |
|
|
import java.util.LinkedHashMap; |
|
|
import java.util.LinkedHashMap; |
|
|
|
|
|
import java.util.LinkedHashSet; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* PO 新增导入解析支持。 |
|
|
|
|
|
* |
|
|
|
|
|
* 设计目标: |
|
|
|
|
|
* 1) 模板字段严格来源于 customer 的可见字段权限(add 场景); |
|
|
|
|
|
* 2) 导入时先校验第一行表头是否匹配模板(允许调换顺序,不允许未知栏位); |
|
|
|
|
|
* 3) 保留行号,便于上层逐行保存失败时返回精确错误定位。 |
|
|
|
|
|
*/ |
|
|
@Component |
|
|
@Component |
|
|
public class PoImportParseSupport { |
|
|
public class PoImportParseSupport { |
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private PoImportFieldAliasMapper importFieldAliasMapper; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private PoFieldMetaMapper fieldMetaMapper; |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 系统自动生成字段:模板不展示,导入也不要求用户提供。 |
|
|
|
|
|
* - order_no: 后端按原有单号逻辑生成; |
|
|
|
|
|
* - item_no: 导入固定为 1; |
|
|
|
|
|
* - order_ref2: 后端按 order_ref1 自增生成。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private static final Set<String> AUTO_GENERATED_FIELD_CODES = new HashSet<String>( |
|
|
|
|
|
Arrays.asList("order_no", "item_no", "order_ref2")); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 业务强制必填列:即使权限配置未勾必填,也要求导入表头必须存在。 |
|
|
|
|
|
* 备注:供应商为二选一(supplier_name / supplier_no),在校验中单独处理。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private static final Set<String> STRONG_REQUIRED_FIELD_CODES = new HashSet<String>( |
|
|
|
|
|
Arrays.asList("po_no", "part_no", "price", "qty")); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 模板可展示但导入值不强制的字段。 |
|
|
|
|
|
* 说明:这些字段通常由上下文或后端回填(如 customer/status/planner), |
|
|
|
|
|
* 若按 requiredFlag 强制校验会与页面“可见但不可编辑”行为冲突。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private static final Set<String> REQUIRED_CHECK_SKIP_FIELD_CODES = new HashSet<String>( |
|
|
|
|
|
Arrays.asList( |
|
|
|
|
|
"customer", "status", "planner", |
|
|
|
|
|
"short_office", "short_npc", "item", "category_name", "sku", "value", |
|
|
|
|
|
"create_date", "order_no", "item_no", "order_ref1", "order_ref2" |
|
|
|
|
|
)); |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private PoFieldAuthSupport fieldAuthSupport; |
|
|
private PoFieldAuthSupport fieldAuthSupport; |
|
|
|
|
|
|
|
|
public PoImportPreviewVo parsePreview(MultipartFile file, String site, String customer) throws IOException { |
|
|
|
|
|
fieldAuthSupport.requireSite(site); |
|
|
|
|
|
fieldAuthSupport.requireCustomer(customer); |
|
|
|
|
|
fieldAuthSupport.validateCustomerScope(site, customer); |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 获取“新增 PO 导入模板”字段(按 sort_no 顺序)。 |
|
|
|
|
|
* 输入:site + customer |
|
|
|
|
|
* 输出:可见字段列表(已剔除系统自动生成字段) |
|
|
|
|
|
*/ |
|
|
|
|
|
public List<PoFieldSchemaFieldVo> listCreatePoOrderTemplateFields(String site, String customer) { |
|
|
|
|
|
String resolvedSite = fieldAuthSupport.requireSite(site); |
|
|
|
|
|
String resolvedCustomer = fieldAuthSupport.requireCustomer(customer); |
|
|
|
|
|
fieldAuthSupport.validateCustomerScope(resolvedSite, resolvedCustomer); |
|
|
|
|
|
|
|
|
ParsedSheet parsed = readSheet(file); |
|
|
|
|
|
Map<String, PoFieldSchemaFieldVo> writable = fieldAuthSupport.buildEditableFieldMap(site, customer); |
|
|
|
|
|
ColumnMapping mapping = buildColumnMapping(site, customer, parsed.headers, writable); |
|
|
|
|
|
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>(); |
|
|
|
|
|
for (List<String> row : parsed.dataRows) { |
|
|
|
|
|
Map<String, Object> mapped = mapRow(row, mapping); |
|
|
|
|
|
if (!mapped.isEmpty()) { |
|
|
|
|
|
rows.add(mapped); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
PoFieldSchemaVo schema = fieldAuthSupport.buildFieldSchema(resolvedSite, resolvedCustomer, "add"); |
|
|
|
|
|
if (schema == null || schema.getFields() == null) { |
|
|
|
|
|
return Collections.emptyList(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
PoImportPreviewVo preview = new PoImportPreviewVo(); |
|
|
|
|
|
preview.setRows(rows); |
|
|
|
|
|
preview.setSkippedColumns(mapping.skippedColumns); |
|
|
|
|
|
preview.setColumnMapping(mapping.columnMapping); |
|
|
|
|
|
return preview; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> parseRows(MultipartFile file, String site, String customer) throws IOException { |
|
|
|
|
|
return parsePreview(file, site, customer).getRows(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void validateImportFields(String site, String customer, ColumnMapping mapping) { |
|
|
|
|
|
Map<String, PoFieldSchemaFieldVo> writable = fieldAuthSupport.buildEditableFieldMap(site, customer); |
|
|
|
|
|
for (String fieldCode : mapping.mappedFieldCodes()) { |
|
|
|
|
|
PoFieldSchemaFieldVo field = writable.get(fieldCode); |
|
|
|
|
|
|
|
|
List<PoFieldSchemaFieldVo> fields = new ArrayList<PoFieldSchemaFieldVo>(); |
|
|
|
|
|
Set<String> seenCodes = new LinkedHashSet<String>(); |
|
|
|
|
|
for (PoFieldSchemaFieldVo field : schema.getFields()) { |
|
|
if (field == null || !"Y".equalsIgnoreCase(field.getVisibleFlag())) { |
|
|
if (field == null || !"Y".equalsIgnoreCase(field.getVisibleFlag())) { |
|
|
throw new IllegalArgumentException("导入字段无权限: " + fieldCode); |
|
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
String fieldCode = resolveFieldCode(field); |
|
|
|
|
|
if (!StringUtils.hasText(fieldCode) || AUTO_GENERATED_FIELD_CODES.contains(fieldCode)) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
if (!seenCodes.add(fieldCode)) { |
|
|
|
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
fields.add(field); |
|
|
} |
|
|
} |
|
|
|
|
|
return fields; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private Map<String, Object> mapRow(List<String> row, ColumnMapping mapping) { |
|
|
|
|
|
Map<String, Object> mapped = new LinkedHashMap<String, Object>(); |
|
|
|
|
|
for (Map.Entry<Integer, String> entry : mapping.indexToFieldCode.entrySet()) { |
|
|
|
|
|
int idx = entry.getKey(); |
|
|
|
|
|
if (idx >= row.size()) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
String value = row.get(idx); |
|
|
|
|
|
if (!StringUtils.hasText(value)) { |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 解析导入文件并完成首行栏位校验。 |
|
|
|
|
|
* 失败处理: |
|
|
|
|
|
* - 表头不匹配(未知/重复/缺必填)直接抛异常并终止; |
|
|
|
|
|
* - 行数据不在此处抛业务异常,由上层逐行保存时返回 rowErrors。 |
|
|
|
|
|
*/ |
|
|
|
|
|
public PoCreateOrderImportData parseCreatePoOrderImport(MultipartFile file, String site, String customer) throws IOException { |
|
|
|
|
|
if (file == null || file.isEmpty()) { |
|
|
|
|
|
throw new IllegalArgumentException("请先选择导入文件"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
List<PoFieldSchemaFieldVo> templateFields = listCreatePoOrderTemplateFields(site, customer); |
|
|
|
|
|
if (templateFields.isEmpty()) { |
|
|
|
|
|
throw new IllegalArgumentException("当前customer未配置可导入字段,请先维护字段权限"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TemplateDescriptor descriptor = buildTemplateDescriptor(templateFields); |
|
|
|
|
|
ParsedSheet parsedSheet = readSheet(file); |
|
|
|
|
|
HeaderMapping headerMapping = buildHeaderMapping(parsedSheet.headers, descriptor); |
|
|
|
|
|
|
|
|
|
|
|
List<PoCreateOrderImportRow> rows = new ArrayList<PoCreateOrderImportRow>(); |
|
|
|
|
|
for (ParsedRow parsedRow : parsedSheet.dataRows) { |
|
|
|
|
|
Map<String, Object> rowData = mapRow(parsedRow.values, headerMapping); |
|
|
|
|
|
if (rowData.isEmpty()) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
mapped.put(entry.getValue(), value.trim()); |
|
|
|
|
|
|
|
|
rows.add(new PoCreateOrderImportRow(parsedRow.rowNo, rowData)); |
|
|
} |
|
|
} |
|
|
return mapped; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new PoCreateOrderImportData(rows, descriptor.requiredFieldLabelMap); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private ColumnMapping buildColumnMapping(String site, String customer, List<String> headers, |
|
|
|
|
|
Map<String, PoFieldSchemaFieldVo> writable) { |
|
|
|
|
|
List<PoImportFieldAlias> aliases = new ArrayList<PoImportFieldAlias>(); |
|
|
|
|
|
aliases.addAll(importFieldAliasMapper.listBySiteAndCustomer(site, customer)); |
|
|
|
|
|
aliases.addAll(importFieldAliasMapper.listGlobalBySite(site)); |
|
|
|
|
|
List<PoFieldMeta> metaList = fieldMetaMapper.listBySite(site); |
|
|
|
|
|
|
|
|
public PoImportResultVo buildResult(int successCount, int failCount, |
|
|
|
|
|
List<String> skippedColumns, List<Map<String, Object>> rowErrors) { |
|
|
|
|
|
PoImportResultVo result = new PoImportResultVo(); |
|
|
|
|
|
result.setSuccessCount(successCount); |
|
|
|
|
|
result.setFailCount(failCount); |
|
|
|
|
|
result.setSkippedColumns(skippedColumns); |
|
|
|
|
|
result.setRowErrors(rowErrors); |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Map<String, String> aliasMap = new HashMap<String, String>(); |
|
|
|
|
|
for (PoImportFieldAlias alias : aliases) { |
|
|
|
|
|
if (alias == null || !"Y".equalsIgnoreCase(alias.getActive())) { |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 基于模板字段生成表头匹配描述: |
|
|
|
|
|
* 1) 支持按字段标签、field_code、field_prop 三种写法识别; |
|
|
|
|
|
* 2) 计算导入必填列,用于首行校验与行级必填校验。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private TemplateDescriptor buildTemplateDescriptor(List<PoFieldSchemaFieldVo> templateFields) { |
|
|
|
|
|
TemplateDescriptor descriptor = new TemplateDescriptor(); |
|
|
|
|
|
for (PoFieldSchemaFieldVo field : templateFields) { |
|
|
|
|
|
String fieldCode = resolveFieldCode(field); |
|
|
|
|
|
if (!StringUtils.hasText(fieldCode)) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
aliasMap.putIfAbsent(alias.getAliasNorm(), alias.getFieldCode()); |
|
|
|
|
|
|
|
|
String fieldLabel = resolveFieldLabel(field, fieldCode); |
|
|
|
|
|
descriptor.fieldCodeToLabel.put(fieldCode, fieldLabel); |
|
|
|
|
|
registerHeaderKey(descriptor.headerKeyToFieldCode, fieldLabel, fieldCode); |
|
|
|
|
|
registerHeaderKey(descriptor.headerKeyToFieldCode, fieldCode, fieldCode); |
|
|
|
|
|
registerHeaderKey(descriptor.headerKeyToFieldCode, PoFieldCodeSupport.toProp(fieldCode), fieldCode); |
|
|
|
|
|
if (isRequiredField(field, fieldCode)) { |
|
|
|
|
|
descriptor.requiredFieldLabelMap.put(fieldCode, fieldLabel); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Map<String, String> metaLabelMap = new HashMap<String, String>(); |
|
|
|
|
|
Map<String, String> metaCodeMap = new HashMap<String, String>(); |
|
|
|
|
|
for (PoFieldMeta meta : metaList) { |
|
|
|
|
|
if (meta == null || !"Y".equalsIgnoreCase(meta.getActive())) { |
|
|
|
|
|
|
|
|
for (String fieldCode : STRONG_REQUIRED_FIELD_CODES) { |
|
|
|
|
|
if (!descriptor.fieldCodeToLabel.containsKey(fieldCode)) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
metaCodeMap.put(PoFieldCodeSupport.normalizeAlias(meta.getFieldCode()), meta.getFieldCode()); |
|
|
|
|
|
metaLabelMap.put(PoFieldCodeSupport.normalizeAlias(meta.getFieldLabel()), meta.getFieldCode()); |
|
|
|
|
|
|
|
|
descriptor.requiredFieldLabelMap.putIfAbsent(fieldCode, descriptor.fieldCodeToLabel.get(fieldCode)); |
|
|
} |
|
|
} |
|
|
|
|
|
// 供应商允许“名称/编码”二选一,因此不强制某一个具体栏位必填。 |
|
|
|
|
|
descriptor.requiredFieldLabelMap.remove("supplier_name"); |
|
|
|
|
|
descriptor.requiredFieldLabelMap.remove("supplier_no"); |
|
|
|
|
|
return descriptor; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
ColumnMapping mapping = new ColumnMapping(); |
|
|
|
|
|
for (int i = 0; i < headers.size(); i++) { |
|
|
|
|
|
String header = headers.get(i); |
|
|
|
|
|
if (!StringUtils.hasText(header)) { |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 表头校验: |
|
|
|
|
|
* 1) 不允许未知栏位; |
|
|
|
|
|
* 2) 不允许重复栏位; |
|
|
|
|
|
* 3) 必填栏位必须出现; |
|
|
|
|
|
* 4) 供应商列必须至少存在 supplier_name / supplier_no 其一。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private HeaderMapping buildHeaderMapping(List<String> headers, TemplateDescriptor descriptor) { |
|
|
|
|
|
HeaderMapping mapping = new HeaderMapping(); |
|
|
|
|
|
if (headers == null || headers.isEmpty()) { |
|
|
|
|
|
throw new IllegalArgumentException("导入文件缺少表头,请使用模板第一行作为栏位名称"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Set<String> usedFieldCodes = new LinkedHashSet<String>(); |
|
|
|
|
|
Set<String> duplicatedHeaders = new LinkedHashSet<String>(); |
|
|
|
|
|
List<String> unknownHeaders = new ArrayList<String>(); |
|
|
|
|
|
Map<String, String> normalizedHeaderToRaw = new HashMap<String, String>(); |
|
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index < headers.size(); index++) { |
|
|
|
|
|
String rawHeader = headers.get(index); |
|
|
|
|
|
if (!StringUtils.hasText(rawHeader)) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
String trimmedHeader = rawHeader.trim(); |
|
|
|
|
|
String normalizedHeader = normalizeHeader(trimmedHeader); |
|
|
|
|
|
if (!StringUtils.hasText(normalizedHeader)) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
if (normalizedHeaderToRaw.containsKey(normalizedHeader)) { |
|
|
|
|
|
duplicatedHeaders.add(trimmedHeader); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
String fieldCode = resolveFieldCode(header.trim(), aliasMap, metaLabelMap, metaCodeMap); |
|
|
|
|
|
|
|
|
normalizedHeaderToRaw.put(normalizedHeader, trimmedHeader); |
|
|
|
|
|
|
|
|
|
|
|
String fieldCode = descriptor.headerKeyToFieldCode.get(normalizedHeader); |
|
|
if (!StringUtils.hasText(fieldCode)) { |
|
|
if (!StringUtils.hasText(fieldCode)) { |
|
|
mapping.skippedColumns.add(header.trim()); |
|
|
|
|
|
|
|
|
unknownHeaders.add(trimmedHeader); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
PoFieldSchemaFieldVo field = writable.get(fieldCode); |
|
|
|
|
|
if (field == null || !"Y".equalsIgnoreCase(field.getVisibleFlag())) { |
|
|
|
|
|
mapping.skippedColumns.add(header.trim() + "(无权限)"); |
|
|
|
|
|
|
|
|
if (!usedFieldCodes.add(fieldCode)) { |
|
|
|
|
|
duplicatedHeaders.add(trimmedHeader); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
mapping.indexToFieldCode.put(i, fieldCode); |
|
|
|
|
|
mapping.columnMapping.put(header.trim(), fieldCode); |
|
|
|
|
|
|
|
|
mapping.indexToFieldCode.put(index, fieldCode); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!duplicatedHeaders.isEmpty()) { |
|
|
|
|
|
throw new IllegalArgumentException("导入表头存在重复栏位: " + String.join("、", duplicatedHeaders)); |
|
|
|
|
|
} |
|
|
|
|
|
if (!unknownHeaders.isEmpty()) { |
|
|
|
|
|
throw new IllegalArgumentException("导入表头与模板不匹配,存在未知栏位: " + String.join("、", unknownHeaders)); |
|
|
|
|
|
} |
|
|
|
|
|
if (mapping.indexToFieldCode.isEmpty()) { |
|
|
|
|
|
throw new IllegalArgumentException("导入表头未匹配到可用栏位,请使用最新模板重试"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
List<String> missingRequiredLabels = new ArrayList<String>(); |
|
|
|
|
|
for (Map.Entry<String, String> entry : descriptor.requiredFieldLabelMap.entrySet()) { |
|
|
|
|
|
if (!usedFieldCodes.contains(entry.getKey())) { |
|
|
|
|
|
missingRequiredLabels.add(entry.getValue()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (!missingRequiredLabels.isEmpty()) { |
|
|
|
|
|
throw new IllegalArgumentException("导入表头缺少必填栏位: " + String.join("、", missingRequiredLabels)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
boolean supplierColumnExists = usedFieldCodes.contains("supplier_name") || usedFieldCodes.contains("supplier_no"); |
|
|
|
|
|
if (!supplierColumnExists) { |
|
|
|
|
|
throw new IllegalArgumentException("导入表头缺少供应商栏位,请至少提供 Vendor Name 或 supplier_no"); |
|
|
} |
|
|
} |
|
|
return mapping; |
|
|
return mapping; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private String resolveFieldCode(String header, Map<String, String> aliasMap, |
|
|
|
|
|
Map<String, String> metaLabelMap, Map<String, String> metaCodeMap) { |
|
|
|
|
|
String norm = PoFieldCodeSupport.normalizeAlias(header); |
|
|
|
|
|
if (aliasMap.containsKey(norm)) { |
|
|
|
|
|
return aliasMap.get(norm); |
|
|
|
|
|
} |
|
|
|
|
|
if (metaCodeMap.containsKey(norm)) { |
|
|
|
|
|
return metaCodeMap.get(norm); |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 将单行 Excel 数据按“列索引->字段编码”映射为业务行对象。 |
|
|
|
|
|
* 规则:空白单元格不写入 map,避免把空串覆盖后端默认值。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private Map<String, Object> mapRow(List<String> rowValues, HeaderMapping mapping) { |
|
|
|
|
|
Map<String, Object> mapped = new LinkedHashMap<String, Object>(); |
|
|
|
|
|
if (rowValues == null || rowValues.isEmpty()) { |
|
|
|
|
|
return mapped; |
|
|
} |
|
|
} |
|
|
if (metaLabelMap.containsKey(norm)) { |
|
|
|
|
|
return metaLabelMap.get(norm); |
|
|
|
|
|
|
|
|
for (Map.Entry<Integer, String> entry : mapping.indexToFieldCode.entrySet()) { |
|
|
|
|
|
int cellIndex = entry.getKey(); |
|
|
|
|
|
if (cellIndex >= rowValues.size()) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
String value = rowValues.get(cellIndex); |
|
|
|
|
|
if (!StringUtils.hasText(value)) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
mapped.put(entry.getValue(), value.trim()); |
|
|
} |
|
|
} |
|
|
return null; |
|
|
|
|
|
|
|
|
return mapped; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 读取首个工作表: |
|
|
|
|
|
* - 第一行读取为表头; |
|
|
|
|
|
* - 第二行开始读取数据; |
|
|
|
|
|
* - 仅保留存在有效值的行,并记录 Excel 原始行号(1-based)。 |
|
|
|
|
|
*/ |
|
|
private ParsedSheet readSheet(MultipartFile file) throws IOException { |
|
|
private ParsedSheet readSheet(MultipartFile file) throws IOException { |
|
|
ParsedSheet parsed = new ParsedSheet(); |
|
|
ParsedSheet parsed = new ParsedSheet(); |
|
|
DataFormatter formatter = new DataFormatter(); |
|
|
DataFormatter formatter = new DataFormatter(); |
|
|
@ -162,20 +291,21 @@ public class PoImportParseSupport { |
|
|
if (headerRow == null) { |
|
|
if (headerRow == null) { |
|
|
return parsed; |
|
|
return parsed; |
|
|
} |
|
|
} |
|
|
int lastCell = headerRow.getLastCellNum(); |
|
|
|
|
|
for (int c = 0; c < lastCell; c++) { |
|
|
|
|
|
Cell cell = headerRow.getCell(c); |
|
|
|
|
|
parsed.headers.add(formatter.formatCellValue(cell).trim()); |
|
|
|
|
|
|
|
|
int headerCellCount = Math.max(headerRow.getLastCellNum(), 0); |
|
|
|
|
|
for (int cellIndex = 0; cellIndex < headerCellCount; cellIndex++) { |
|
|
|
|
|
Cell cell = headerRow.getCell(cellIndex); |
|
|
|
|
|
parsed.headers.add(cell == null ? "" : formatter.formatCellValue(cell).trim()); |
|
|
} |
|
|
} |
|
|
for (int r = sheet.getFirstRowNum() + 1; r <= sheet.getLastRowNum(); r++) { |
|
|
|
|
|
Row row = sheet.getRow(r); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int rowIndex = sheet.getFirstRowNum() + 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { |
|
|
|
|
|
Row row = sheet.getRow(rowIndex); |
|
|
if (row == null) { |
|
|
if (row == null) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
List<String> values = new ArrayList<String>(); |
|
|
List<String> values = new ArrayList<String>(); |
|
|
boolean hasValue = false; |
|
|
boolean hasValue = false; |
|
|
for (int c = 0; c < lastCell; c++) { |
|
|
|
|
|
Cell cell = row.getCell(c); |
|
|
|
|
|
|
|
|
for (int cellIndex = 0; cellIndex < headerCellCount; cellIndex++) { |
|
|
|
|
|
Cell cell = row.getCell(cellIndex); |
|
|
String text = cell == null ? "" : formatter.formatCellValue(cell).trim(); |
|
|
String text = cell == null ? "" : formatter.formatCellValue(cell).trim(); |
|
|
if (StringUtils.hasText(text)) { |
|
|
if (StringUtils.hasText(text)) { |
|
|
hasValue = true; |
|
|
hasValue = true; |
|
|
@ -183,43 +313,159 @@ public class PoImportParseSupport { |
|
|
values.add(text); |
|
|
values.add(text); |
|
|
} |
|
|
} |
|
|
if (hasValue) { |
|
|
if (hasValue) { |
|
|
parsed.dataRows.add(values); |
|
|
|
|
|
|
|
|
parsed.dataRows.add(new ParsedRow(rowIndex + 1, values)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return parsed; |
|
|
return parsed; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public PoImportResultVo buildResult(int successCount, int failCount, |
|
|
|
|
|
List<String> skippedColumns, List<Map<String, Object>> rowErrors) { |
|
|
|
|
|
PoImportResultVo result = new PoImportResultVo(); |
|
|
|
|
|
result.setSuccessCount(successCount); |
|
|
|
|
|
result.setFailCount(failCount); |
|
|
|
|
|
result.setSkippedColumns(skippedColumns); |
|
|
|
|
|
result.setRowErrors(rowErrors); |
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 判断字段是否应纳入“导入必填列”。 |
|
|
|
|
|
* 约束: |
|
|
|
|
|
* 1) 自动生成字段不要求用户提供; |
|
|
|
|
|
* 2) 可见但系统回填字段不强制; |
|
|
|
|
|
* 3) 只对 requiredFlag=Y 且可输入字段生效。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private boolean isRequiredField(PoFieldSchemaFieldVo field, String fieldCode) { |
|
|
|
|
|
if (AUTO_GENERATED_FIELD_CODES.contains(fieldCode)) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
if (REQUIRED_CHECK_SKIP_FIELD_CODES.contains(fieldCode)) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
if (isReadOnlyField(field)) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
return "Y".equalsIgnoreCase(field.getRequiredFlag()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 判断字段是否只读。 |
|
|
|
|
|
* 说明:只读字段即使模板展示,也不作为导入必填值校验项。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private boolean isReadOnlyField(PoFieldSchemaFieldVo field) { |
|
|
|
|
|
if (field == null) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
String editorType = String.valueOf(field.getEditorType() == null ? "" : field.getEditorType()).trim().toLowerCase(); |
|
|
|
|
|
return "readonly".equals(editorType); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 解析 schema 字段编码,优先 fieldCode,其次 fieldProp。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private String resolveFieldCode(PoFieldSchemaFieldVo field) { |
|
|
|
|
|
if (field == null) { |
|
|
|
|
|
return ""; |
|
|
|
|
|
} |
|
|
|
|
|
if (StringUtils.hasText(field.getFieldCode())) { |
|
|
|
|
|
return PoFieldCodeSupport.toFieldCode(field.getFieldCode().trim()); |
|
|
|
|
|
} |
|
|
|
|
|
if (StringUtils.hasText(field.getFieldProp())) { |
|
|
|
|
|
return PoFieldCodeSupport.toFieldCode(field.getFieldProp().trim()); |
|
|
|
|
|
} |
|
|
|
|
|
return ""; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 解析模板表头显示名,标签为空时回退到字段编码。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private String resolveFieldLabel(PoFieldSchemaFieldVo field, String fallbackCode) { |
|
|
|
|
|
if (field != null && StringUtils.hasText(field.getFieldLabel())) { |
|
|
|
|
|
return field.getFieldLabel().trim(); |
|
|
|
|
|
} |
|
|
|
|
|
return fallbackCode; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 注册可识别表头键。 |
|
|
|
|
|
* 作用:允许用户用模板标签、field_code 或 field_prop 作为首行栏位名。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private void registerHeaderKey(Map<String, String> headerKeyToFieldCode, String source, String fieldCode) { |
|
|
|
|
|
if (!StringUtils.hasText(source) || !StringUtils.hasText(fieldCode)) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
String normalized = normalizeHeader(source); |
|
|
|
|
|
if (!StringUtils.hasText(normalized)) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
headerKeyToFieldCode.putIfAbsent(normalized, fieldCode); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static class ParsedSheet { |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 规范化表头文本: |
|
|
|
|
|
* - 去除“必填”后缀提示; |
|
|
|
|
|
* - 忽略空格/下划线/大小写差异,便于做顺序无关匹配。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private String normalizeHeader(String rawHeader) { |
|
|
|
|
|
if (!StringUtils.hasText(rawHeader)) { |
|
|
|
|
|
return ""; |
|
|
|
|
|
} |
|
|
|
|
|
String normalized = rawHeader.trim().replace("(必填)", "").replace("(必填)", ""); |
|
|
|
|
|
return PoFieldCodeSupport.normalizeAlias(normalized); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static class ParsedSheet { |
|
|
private final List<String> headers = new ArrayList<String>(); |
|
|
private final List<String> headers = new ArrayList<String>(); |
|
|
private final List<List<String>> dataRows = new ArrayList<List<String>>(); |
|
|
|
|
|
|
|
|
private final List<ParsedRow> dataRows = new ArrayList<ParsedRow>(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static class ParsedRow { |
|
|
|
|
|
private final int rowNo; |
|
|
|
|
|
private final List<String> values; |
|
|
|
|
|
|
|
|
|
|
|
private ParsedRow(int rowNo, List<String> values) { |
|
|
|
|
|
this.rowNo = rowNo; |
|
|
|
|
|
this.values = values; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static class ColumnMapping { |
|
|
|
|
|
|
|
|
private static class HeaderMapping { |
|
|
private final Map<Integer, String> indexToFieldCode = new LinkedHashMap<Integer, String>(); |
|
|
private final Map<Integer, String> indexToFieldCode = new LinkedHashMap<Integer, String>(); |
|
|
private final Map<String, String> columnMapping = new LinkedHashMap<String, String>(); |
|
|
|
|
|
private final List<String> skippedColumns = new ArrayList<String>(); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static class TemplateDescriptor { |
|
|
|
|
|
private final Map<String, String> headerKeyToFieldCode = new HashMap<String, String>(); |
|
|
|
|
|
private final Map<String, String> fieldCodeToLabel = new LinkedHashMap<String, String>(); |
|
|
|
|
|
private final Map<String, String> requiredFieldLabelMap = new LinkedHashMap<String, String>(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static class PoCreateOrderImportData { |
|
|
|
|
|
private final List<PoCreateOrderImportRow> rows; |
|
|
|
|
|
private final Map<String, String> requiredFieldLabelMap; |
|
|
|
|
|
|
|
|
|
|
|
public PoCreateOrderImportData(List<PoCreateOrderImportRow> rows, Map<String, String> requiredFieldLabelMap) { |
|
|
|
|
|
this.rows = rows == null ? Collections.<PoCreateOrderImportRow>emptyList() : rows; |
|
|
|
|
|
this.requiredFieldLabelMap = requiredFieldLabelMap == null |
|
|
|
|
|
? Collections.<String, String>emptyMap() |
|
|
|
|
|
: requiredFieldLabelMap; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<PoCreateOrderImportRow> getRows() { |
|
|
|
|
|
return rows; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Map<String, String> getRequiredFieldLabelMap() { |
|
|
|
|
|
return requiredFieldLabelMap; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static class PoCreateOrderImportRow { |
|
|
|
|
|
private final int rowNo; |
|
|
|
|
|
private final Map<String, Object> rowData; |
|
|
|
|
|
|
|
|
public List<String> mappedFieldCodes() { |
|
|
|
|
|
return new ArrayList<String>(indexToFieldCode.values()); |
|
|
|
|
|
|
|
|
public PoCreateOrderImportRow(int rowNo, Map<String, Object> rowData) { |
|
|
|
|
|
this.rowNo = rowNo; |
|
|
|
|
|
this.rowData = rowData == null ? Collections.<String, Object>emptyMap() : rowData; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Map<String, String> getColumnMapping() { |
|
|
|
|
|
return columnMapping; |
|
|
|
|
|
|
|
|
public int getRowNo() { |
|
|
|
|
|
return rowNo; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public List<String> getSkippedColumns() { |
|
|
|
|
|
return skippedColumns; |
|
|
|
|
|
|
|
|
public Map<String, Object> getRowData() { |
|
|
|
|
|
return rowData; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |