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,