fix(import): 库存列头动态识别 + 格式不匹配诊断信息
Deploy / deploy (push) Failing after 59s

商品名称/编号/系列/规格/单位列索引从固定 0-4 改为从表头动态识别,
支持更多列名变体(品名/名称/货品名称/编码等)。

全部跳过时在 errors 中返回识别到的表头,便于诊断列格式问题。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-05-24 15:45:39 +08:00
parent d5dc499c6b
commit 831dbc5959
+30 -10
View File
@@ -563,21 +563,32 @@ func (h *ImportHandler) ImportInventory(c *gin.Context) {
}
// Dynamic column detection from header row
colProductCode, colProductName, colSeries, colSpec, colUnit := 0, 1, 2, 3, 4
colQty, colPrice, colProductionDate, colBatchNo, colWarehouse, colSupplier, colRemark := 5, 6, 8, 9, 11, 13, 15
if len(rows) > 0 {
for j, h := range rows[0] {
switch strings.TrimSpace(h) {
case "库存数量", "数量":
case "商品编号", "商品编码", "编号", "编码", "商品条码":
colProductCode = j
case "商品名称", "品名", "名称", "货品名称":
colProductName = j
case "系列", "品牌系列":
colSeries = j
case "规格", "规格型号":
colSpec = j
case "单位":
colUnit = j
case "库存数量", "数量", "库存":
colQty = j
case "单价":
case "单价", "进价", "采购单价":
colPrice = j
case "生产日期":
case "生产日期", "生产年月":
colProductionDate = j
case "批次", "批次号":
colBatchNo = j
case "所在仓库", "仓库":
case "所在仓库", "仓库", "库位":
colWarehouse = j
case "供应商":
case "供应商", "供应商名称":
colSupplier = j
case "备注":
colRemark = j
@@ -585,19 +596,22 @@ func (h *ImportHandler) ImportInventory(c *gin.Context) {
}
}
// 如果没有匹配到任何列头,返回诊断信息
detectedHeader := strings.Join(rows[0], " | ")
var logsToCreate []model.InventoryLog
for i, row := range rows[1:] {
productName := cell(row, 1)
productName := cell(row, colProductName)
if productName == "" {
res.skipped++
continue
}
productCode := cell(row, 0)
series := cell(row, 2)
spec := cell(row, 3)
unit := cell(row, 4)
productCode := cell(row, colProductCode)
series := cell(row, colSeries)
spec := cell(row, colSpec)
unit := cell(row, colUnit)
qtyStr := cell(row, colQty)
priceStr := cell(row, colPrice)
productionDateStr := cell(row, colProductionDate)
@@ -730,6 +744,12 @@ func (h *ImportHandler) ImportInventory(c *gin.Context) {
h.db.CreateInBatches(&logsToCreate, 100)
}
// 全部跳过说明列格式不匹配,返回诊断信息
if res.imported == 0 && res.updated == 0 && res.skipped > 0 && len(res.errors) == 0 {
res.errors = append(res.errors,
fmt.Sprintf("所有行商品名称列为空,可能列格式不匹配。识别到的表头:%s", detectedHeader))
}
c.JSON(http.StatusOK, gin.H{
"imported": res.imported,
"updated": res.updated,