From 831dbc595927e85645a26714a57e2ec793592459 Mon Sep 17 00:00:00 2001 From: wangjia <809946525@qq.com> Date: Sun, 24 May 2026 15:45:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(import):=20=E5=BA=93=E5=AD=98=E5=88=97?= =?UTF-8?q?=E5=A4=B4=E5=8A=A8=E6=80=81=E8=AF=86=E5=88=AB=20+=20=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=B8=8D=E5=8C=B9=E9=85=8D=E8=AF=8A=E6=96=AD=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 商品名称/编号/系列/规格/单位列索引从固定 0-4 改为从表头动态识别, 支持更多列名变体(品名/名称/货品名称/编码等)。 全部跳过时在 errors 中返回识别到的表头,便于诊断列格式问题。 Co-Authored-By: Claude Sonnet 4.6 --- backend/internal/handler/import.go | 40 ++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/backend/internal/handler/import.go b/backend/internal/handler/import.go index 59f1cfe..d96ba77 100644 --- a/backend/internal/handler/import.go +++ b/backend/internal/handler/import.go @@ -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,