Browse Source

验货

master
常熟吴彦祖 3 months ago
parent
commit
a6ec958183
  1. 10
      build.gradle
  2. 184
      src/main/java/com/xujie/modules/order/controller/PODetailController.java
  3. 15
      src/main/java/com/xujie/modules/order/service/Iface/IPODetailService.java
  4. 92
      src/main/java/com/xujie/modules/order/service/Impl/PODetailServiceImpl.java
  5. 476
      src/main/java/com/xujie/modules/order/support/PoImportParseSupport.java
  6. 2
      src/main/resources/application.yml

10
build.gradle

@ -39,8 +39,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
//邮件的包
implementation 'com.alibaba:fastjson:2.0.57'
// 邮件相关链路里使用到 fastjson 兼容坐标,这里与 fastjson2 统一到安全版本,避免版本漂移。
implementation 'com.alibaba:fastjson:2.0.64'
implementation 'org.springframework.boot:spring-boot-starter-mail'
// MyBatis-Plus Spring Boot Starter
implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.8'
@ -69,9 +69,9 @@ dependencies {
//阿里duid
implementation 'com.alibaba:druid-spring-boot-3-starter:1.2.23'
//fastjson2
implementation 'com.alibaba.fastjson2:fastjson2:2.0.20'
implementation 'com.alibaba.fastjson2:fastjson2-extension:2.0.20'
// fastjson2 安全版本统一升级:修复云平台扫描命中的 <= 2.0.62 风险。
implementation 'com.alibaba.fastjson2:fastjson2:2.0.64'
implementation 'com.alibaba.fastjson2:fastjson2-extension:2.0.64'
implementation 'org.apache.commons:commons-lang3:3.7'
implementation 'com.google.guava:guava:29.0-jre'

184
src/main/java/com/xujie/modules/order/controller/PODetailController.java

@ -4,7 +4,7 @@ package com.xujie.modules.order.controller;
import com.xujie.common.utils.PageUtils;
import com.xujie.common.utils.R;
import com.xujie.modules.order.entity.PODetail;
import com.xujie.modules.order.vo.PoImportPreviewVo;
import com.xujie.modules.order.support.PoImportParseSupport;
import com.xujie.modules.order.vo.PoImportResultVo;
import com.xujie.modules.order.service.Iface.IPODetailService;
import com.xujie.modules.order.vo.PODetailInspectionVO;
@ -14,6 +14,7 @@ import com.xujie.modules.order.vo.PODetailVo;
import com.xujie.modules.order.vo.PoCodeOptionVo;
import com.xujie.modules.order.vo.PoDetailAdvancedQueryVo;
import com.xujie.modules.order.vo.PoCustomerScopeRequestVo;
import com.xujie.modules.order.vo.PoFieldSchemaFieldVo;
import com.xujie.modules.order.vo.PoFieldSchemaRequestVo;
import com.xujie.modules.order.vo.PoFieldSchemaVo;
import io.swagger.annotations.Api;
@ -31,7 +32,6 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
@ -52,6 +52,8 @@ public class PODetailController {
@Autowired
private IPODetailService baseService;
@Autowired
private PoImportParseSupport poImportParseSupport;
/**
* 保存数据
@ -274,96 +276,56 @@ public class PODetailController {
}
/**
* 下载PO订单导入模板(直接生成 Excel,避免文件找不到问题)
* 下载 PO 导入模板。
* 模板规则:
* 1) 第一行严格按 customer 当前可见字段顺序输出;
* 2) 字段来自 add 场景权限,且自动生成字段(order_no/item_no/order_ref2)已剔除;
* 3) 前端导入时以该第一行作为表头校验基准。
*/
@GetMapping("/downloadTemplate")
public ResponseEntity<Resource> downloadTemplate() {
public ResponseEntity<Resource> downloadTemplate(@RequestParam("site") String site,
@RequestParam("customer") String customer) {
try {
// 直接在内存中生成 Excel 模板
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("PO订单数据");
// 创建表头样式
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置边框
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
// 设置背景色
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 创建表头行
Row headerRow = sheet.createRow(0);
String[] headers = {
"客户名", "买手名", "供应商名", "订单号", "款号", "单价",
"订单数量", "出运数量", "始发港", "目的港", "运输方式",
"订单下单日期", "订单询单日期", "订单确认日期", "Original Arrival Date",
"是否需要预付款", "分管Planner", "备注"
};
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(headerStyle);
List<PoFieldSchemaFieldVo> templateFields =
poImportParseSupport.listCreatePoOrderTemplateFields(site, customer);
if (templateFields == null || templateFields.isEmpty()) {
return ResponseEntity.badRequest().build();
}
// 添加示例数据行
Row dataRow1 = sheet.createRow(1);
dataRow1.createCell(0).setCellValue("客户A");
dataRow1.createCell(1).setCellValue("张三");
dataRow1.createCell(2).setCellValue("供应商X");
dataRow1.createCell(3).setCellValue("PO20260428001");
dataRow1.createCell(4).setCellValue("ITEM001");
dataRow1.createCell(5).setCellValue(100.50);
dataRow1.createCell(6).setCellValue(1000);
dataRow1.createCell(7).setCellValue(1000);
dataRow1.createCell(8).setCellValue("SHA");
dataRow1.createCell(9).setCellValue("LAX");
dataRow1.createCell(10).setCellValue("海运");
dataRow1.createCell(11).setCellValue("2026-04-28");
dataRow1.createCell(12).setCellValue("2026-04-20");
dataRow1.createCell(13).setCellValue("2026-04-22");
dataRow1.createCell(14).setCellValue("2026-06-15");
// 第15列(确认完货日期)已移除,由系统自动计算
dataRow1.createCell(15).setCellValue("是");
dataRow1.createCell(16).setCellValue("李四");
dataRow1.createCell(17).setCellValue("示例备注");
// 设置列宽
sheet.setColumnWidth(0, 15 * 256); // 客户名
sheet.setColumnWidth(1, 12 * 256); // 买手名
sheet.setColumnWidth(2, 18 * 256); // 供应商名
sheet.setColumnWidth(3, 18 * 256); // 订单号
sheet.setColumnWidth(4, 12 * 256); // 款号
sheet.setColumnWidth(5, 10 * 256); // 单价
sheet.setColumnWidth(6, 12 * 256); // 订单数量
sheet.setColumnWidth(7, 12 * 256); // 出运数量
sheet.setColumnWidth(8, 12 * 256); // 始发港
sheet.setColumnWidth(9, 12 * 256); // 目的港
sheet.setColumnWidth(10, 12 * 256); // 运输方式
sheet.setColumnWidth(11, 18 * 256); // 订单下单日期
sheet.setColumnWidth(12, 18 * 256); // 订单询单日期
sheet.setColumnWidth(13, 18 * 256); // 订单确认日期
sheet.setColumnWidth(14, 22 * 256); // Original Arrival Date
sheet.setColumnWidth(15, 18 * 256); // 是否需要预付款
sheet.setColumnWidth(16, 15 * 256); // 分管Planner
sheet.setColumnWidth(17, 20 * 256); // 备注
// 写入到字节数组
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
workbook.close();
byte[] bytes = outputStream.toByteArray();
ByteArrayResource resource = new ByteArrayResource(bytes);
byte[] bytes;
try (Workbook workbook = new XSSFWorkbook();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Sheet sheet = workbook.createSheet("PO订单导入模板");
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Row headerRow = sheet.createRow(0);
for (int i = 0; i < templateFields.size(); i++) {
PoFieldSchemaFieldVo field = templateFields.get(i);
String headerLabel = resolveTemplateFieldLabel(field);
Cell cell = headerRow.createCell(i);
cell.setCellValue(headerLabel);
cell.setCellStyle(headerStyle);
int recommendedWidth = Math.max(12, Math.min(36, headerLabel.length() + 6));
sheet.setColumnWidth(i, recommendedWidth * 256);
}
workbook.write(outputStream);
bytes = outputStream.toByteArray();
}
String fileName = "PO订单导入模板.xlsx";
ByteArrayResource resource = new ByteArrayResource(bytes);
String fileName = "PO订单导入模板_" + customer + ".xlsx";
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString())
.replaceAll("\\+", "%20");
@ -373,35 +335,26 @@ public class PODetailController {
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(bytes.length))
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
} catch (IllegalArgumentException ex) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.internalServerError().build();
}
}
/**
* 预览上传的 Excel 数据
* 导入创建 PO(逐行创建 header + detail)。
* 返回值:
* - successCount / failCount:导入汇总;
* - rowErrors:失败行号与错误原因。
*/
@PostMapping("/previewUpload")
@ApiOperation("预览上传数据")
public R previewUpload(@RequestParam("file") MultipartFile file,
@RequestParam("site") String site,
@RequestParam("customer") String customer) throws IOException {
PoImportPreviewVo preview = baseService.previewUpload(file, site, customer);
return R.ok().put("data", preview);
}
/**
* 批量保存上传的数据
*/
@PostMapping("/batchSave")
@ApiOperation("批量保存")
public R batchSave(@RequestParam("file") MultipartFile file,
@RequestParam("site") String site,
@RequestParam("customer") String customer) {
@PostMapping("/importCreatePoOrder")
@ApiOperation("导入创建PO订单")
public R importCreatePoOrder(@RequestParam("file") MultipartFile file,
@RequestParam("site") String site,
@RequestParam("customer") String customer) {
try {
PoImportResultVo result = baseService.batchSave(file, site, customer);
PoImportResultVo result = baseService.importCreatePoOrder(file, site, customer);
return R.ok().put("data", result);
} catch (Exception e) {
return R.error(e.getMessage());
@ -415,4 +368,21 @@ public class PODetailController {
return R.ok().put("page", page);
}
/**
* 解析模板字段显示名称。
* 失败兜底:若字段标签为空,退回 fieldCode,避免导出空表头导致前端无法校验。
*/
private String resolveTemplateFieldLabel(PoFieldSchemaFieldVo field) {
if (field == null) {
return "";
}
if (field.getFieldLabel() != null && field.getFieldLabel().trim().length() > 0) {
return field.getFieldLabel().trim();
}
if (field.getFieldCode() != null) {
return field.getFieldCode().trim();
}
return "";
}
}

15
src/main/java/com/xujie/modules/order/service/Iface/IPODetailService.java

@ -5,8 +5,6 @@ import com.xujie.common.utils.PageUtils;
import com.xujie.modules.order.entity.PODetail;
import com.xujie.modules.order.vo.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -35,14 +33,13 @@ public interface IPODetailService extends IService<PODetail> {
void batchUpdatePoDetail(List<PODetailVo> poDetailVoList);
/**
* 预览上传的 Excel 数据(无模板,按表头 alias 动态映射)
*/
PoImportPreviewVo previewUpload(MultipartFile file, String site, String customer) throws IOException;
/**
* 批量保存上传的数据
* 导入创建 PO 订单。
* 业务语义:
* 1) 文件第一行按模板规则校验栏位(顺序可变);
* 2) 每一行独立创建 header + detail;
* 3) 返回成功/失败数量与行级失败明细。
*/
PoImportResultVo batchSave(MultipartFile file, String site, String customer);
PoImportResultVo importCreatePoOrder(MultipartFile file, String site, String customer);
/**
* 根据 PODetail 的 order_no + item_no 查询关联的验货申请明细(分页)

92
src/main/java/com/xujie/modules/order/service/Impl/PODetailServiceImpl.java

@ -26,7 +26,6 @@ import com.xujie.modules.order.vo.PoCodeOptionVo;
import com.xujie.modules.order.vo.PoDetailAdvancedQueryVo;
import com.xujie.modules.order.vo.PoFieldSchemaFieldVo;
import com.xujie.modules.order.vo.PoFieldSchemaVo;
import com.xujie.modules.order.vo.PoImportPreviewVo;
import com.xujie.modules.order.vo.PoImportResultVo;
import com.xujie.modules.order.vo.PoShipPreviewVo;
import com.xujie.modules.order.vo.PoShipSubmitVo;
@ -1189,49 +1188,74 @@ public class PODetailServiceImpl extends ServiceImpl<PODetailMapper, PODetail> i
poHeaderMapper.updatePoHeaderForEdit(header);
}
/**
* 导入创建 PO(逐行执行)。
* 关键规则:
* 1) 首行栏位由解析器统一校验(顺序可调整,未知栏位直接拒绝);
* 2) 行级保存失败不影响后续行,最终返回 success/fail 与 rowErrors;
* 3) 事务超时扩展到 10 分钟,兼容大批量(数千行)导入。
*/
@Override
public PoImportPreviewVo previewUpload(MultipartFile file, String site, String customer) throws IOException {
return poImportParseSupport.parsePreview(file, site, customer);
}
@Override
@Transactional
public PoImportResultVo batchSave(MultipartFile file, String site, String customer) {
@Transactional(timeout = 600)
public PoImportResultVo importCreatePoOrder(MultipartFile file, String site, String customer) {
try {
site = poFieldAuthSupport.requireSite(site);
customer = poFieldAuthSupport.requireCustomer(customer);
poFieldAuthSupport.validateWriteContext(site, customer);
PoImportPreviewVo preview = poImportParseSupport.parsePreview(file, site, customer);
PoImportParseSupport.PoCreateOrderImportData importData =
poImportParseSupport.parseCreatePoOrderImport(file, site, customer);
List<PoImportParseSupport.PoCreateOrderImportRow> importRows = importData.getRows();
if (importRows == null || importRows.isEmpty()) {
throw new RuntimeException("未读取到可导入数据,请检查模板第二行开始是否存在数据行");
}
List<Map<String, Object>> rowErrors = new ArrayList<Map<String, Object>>();
int successCount = 0;
int failCount = 0;
int rowNum = 1;
Map<String, Integer> ref2Counter = new HashMap<String, Integer>();
for (Map<String, Object> row : preview.getRows()) {
rowNum++;
for (PoImportParseSupport.PoCreateOrderImportRow importRow : importRows) {
int rowNum = importRow.getRowNo();
try {
saveImportRow(site, customer, row, rowNum, ref2Counter);
saveImportRow(
site,
customer,
importRow.getRowData(),
rowNum,
ref2Counter,
importData.getRequiredFieldLabelMap()
);
successCount++;
} catch (Exception ex) {
failCount++;
Map<String, Object> err = new LinkedHashMap<String, Object>();
err.put("rowNo", rowNum);
err.put("message", ex.getMessage());
err.put("message", StringUtils.hasText(ex.getMessage()) ? ex.getMessage() : "保存失败");
rowErrors.add(err);
}
}
return poImportParseSupport.buildResult(
successCount, failCount, preview.getSkippedColumns(), rowErrors);
successCount,
failCount,
Collections.<String>emptyList(),
rowErrors
);
} catch (IOException e) {
throw new RuntimeException("文件读取失败: " + e.getMessage());
}
}
/**
* 保存单行导入数据。
* 入参:
* - row:已按 field_code 映射的单行数据;
* - requiredFieldLabelMap:当前模板必填列定义(field_code -> label)。
* 异常:任何校验失败直接抛异常,由上层记录为 rowErrors。
*/
private void saveImportRow(String site, String customer, Map<String, Object> row,
int rowNum, Map<String, Integer> ref2Counter) {
int rowNum, Map<String, Integer> ref2Counter, Map<String, String> requiredFieldLabelMap) {
validateImportRequiredValues(row, requiredFieldLabelMap);
PODetailVo importVo = PoImportRowMapper.toDetailVo(row, site, customer);
importVo.setCustomer(customer);
if (!StringUtils.hasText(importVo.getPartNo())) {
@ -1315,6 +1339,42 @@ public class PODetailServiceImpl extends ServiceImpl<PODetailMapper, PODetail> i
saveModel(poDetailVo);
}
/**
* 行级必填值校验(基于模板 requiredFlag)。
* 边界处理:
* 1) 允许 requiredFieldLabelMap 为空(由后续业务校验兜底);
* 2) 仅校验模板必填字段的“是否有值”,具体业务合法性由 create/save 链路继续校验。
*/
private void validateImportRequiredValues(Map<String, Object> row, Map<String, String> requiredFieldLabelMap) {
if (requiredFieldLabelMap == null || requiredFieldLabelMap.isEmpty()) {
return;
}
Map<String, Object> safeRow = row == null ? Collections.<String, Object>emptyMap() : row;
for (Map.Entry<String, String> entry : requiredFieldLabelMap.entrySet()) {
String fieldCode = entry.getKey();
Object value = safeRow.get(fieldCode);
if (hasImportValue(value)) {
continue;
}
String label = StringUtils.hasText(entry.getValue()) ? entry.getValue() : fieldCode;
throw new RuntimeException(label + " 不能为空");
}
}
/**
* 判断导入单元格是否存在有效值。
* 规则:字符串去空白后为空视为“未填写”,其余类型按有值处理。
*/
private boolean hasImportValue(Object value) {
if (value == null) {
return false;
}
if (value instanceof String) {
return StringUtils.hasText((String) value);
}
return true;
}
@Override
@Transactional
public void createPoOrder(PODetailVo vo) {

476
src/main/java/com/xujie/modules/order/support/PoImportParseSupport.java

@ -1,11 +1,7 @@
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.PoImportPreviewVo;
import com.xujie.modules.order.vo.PoFieldSchemaVo;
import com.xujie.modules.order.vo.PoImportResultVo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
@ -20,136 +16,269 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* PO 新增导入解析支持。
*
* 设计目标:
* 1) 模板字段严格来源于 customer 的可见字段权限(add 场景);
* 2) 导入时先校验第一行表头是否匹配模板(允许调换顺序,不允许未知栏位);
* 3) 保留行号,便于上层逐行保存失败时返回精确错误定位。
*/
@Component
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
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())) {
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;
}
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;
}
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;
}
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;
}
String fieldCode = resolveFieldCode(header.trim(), aliasMap, metaLabelMap, metaCodeMap);
normalizedHeaderToRaw.put(normalizedHeader, trimmedHeader);
String fieldCode = descriptor.headerKeyToFieldCode.get(normalizedHeader);
if (!StringUtils.hasText(fieldCode)) {
mapping.skippedColumns.add(header.trim());
unknownHeaders.add(trimmedHeader);
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;
}
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;
}
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 {
ParsedSheet parsed = new ParsedSheet();
DataFormatter formatter = new DataFormatter();
@ -162,20 +291,21 @@ public class PoImportParseSupport {
if (headerRow == null) {
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) {
continue;
}
List<String> values = new ArrayList<String>();
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();
if (StringUtils.hasText(text)) {
hasValue = true;
@ -183,43 +313,159 @@ public class PoImportParseSupport {
values.add(text);
}
if (hasValue) {
parsed.dataRows.add(values);
parsed.dataRows.add(new ParsedRow(rowIndex + 1, values));
}
}
}
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<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<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;
}
}
}

2
src/main/resources/application.yml

@ -101,4 +101,4 @@ query:
part:
formal-approval:
enabled: true # 转正是否需要审批:true 走审批流,false 直接转正式物料
enabled: false # 无用
Loading…
Cancel
Save