商品名称/编号/系列/规格/单位列索引从固定 0-4 改为从表头动态识别, 支持更多列名变体(品名/名称/货品名称/编码等)。 全部跳过时在 errors 中返回识别到的表头,便于诊断列格式问题。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -563,21 +563,32 @@ func (h *ImportHandler) ImportInventory(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dynamic column detection from header row
|
// 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
|
colQty, colPrice, colProductionDate, colBatchNo, colWarehouse, colSupplier, colRemark := 5, 6, 8, 9, 11, 13, 15
|
||||||
if len(rows) > 0 {
|
if len(rows) > 0 {
|
||||||
for j, h := range rows[0] {
|
for j, h := range rows[0] {
|
||||||
switch strings.TrimSpace(h) {
|
switch strings.TrimSpace(h) {
|
||||||
case "库存数量", "数量":
|
case "商品编号", "商品编码", "编号", "编码", "商品条码":
|
||||||
|
colProductCode = j
|
||||||
|
case "商品名称", "品名", "名称", "货品名称":
|
||||||
|
colProductName = j
|
||||||
|
case "系列", "品牌系列":
|
||||||
|
colSeries = j
|
||||||
|
case "规格", "规格型号":
|
||||||
|
colSpec = j
|
||||||
|
case "单位":
|
||||||
|
colUnit = j
|
||||||
|
case "库存数量", "数量", "库存":
|
||||||
colQty = j
|
colQty = j
|
||||||
case "单价":
|
case "单价", "进价", "采购单价":
|
||||||
colPrice = j
|
colPrice = j
|
||||||
case "生产日期":
|
case "生产日期", "生产年月":
|
||||||
colProductionDate = j
|
colProductionDate = j
|
||||||
case "批次", "批次号":
|
case "批次", "批次号":
|
||||||
colBatchNo = j
|
colBatchNo = j
|
||||||
case "所在仓库", "仓库":
|
case "所在仓库", "仓库", "库位":
|
||||||
colWarehouse = j
|
colWarehouse = j
|
||||||
case "供应商":
|
case "供应商", "供应商名称":
|
||||||
colSupplier = j
|
colSupplier = j
|
||||||
case "备注":
|
case "备注":
|
||||||
colRemark = j
|
colRemark = j
|
||||||
@@ -585,19 +596,22 @@ func (h *ImportHandler) ImportInventory(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果没有匹配到任何列头,返回诊断信息
|
||||||
|
detectedHeader := strings.Join(rows[0], " | ")
|
||||||
|
|
||||||
var logsToCreate []model.InventoryLog
|
var logsToCreate []model.InventoryLog
|
||||||
|
|
||||||
for i, row := range rows[1:] {
|
for i, row := range rows[1:] {
|
||||||
productName := cell(row, 1)
|
productName := cell(row, colProductName)
|
||||||
if productName == "" {
|
if productName == "" {
|
||||||
res.skipped++
|
res.skipped++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
productCode := cell(row, 0)
|
productCode := cell(row, colProductCode)
|
||||||
series := cell(row, 2)
|
series := cell(row, colSeries)
|
||||||
spec := cell(row, 3)
|
spec := cell(row, colSpec)
|
||||||
unit := cell(row, 4)
|
unit := cell(row, colUnit)
|
||||||
qtyStr := cell(row, colQty)
|
qtyStr := cell(row, colQty)
|
||||||
priceStr := cell(row, colPrice)
|
priceStr := cell(row, colPrice)
|
||||||
productionDateStr := cell(row, colProductionDate)
|
productionDateStr := cell(row, colProductionDate)
|
||||||
@@ -730,6 +744,12 @@ func (h *ImportHandler) ImportInventory(c *gin.Context) {
|
|||||||
h.db.CreateInBatches(&logsToCreate, 100)
|
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{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"imported": res.imported,
|
"imported": res.imported,
|
||||||
"updated": res.updated,
|
"updated": res.updated,
|
||||||
|
|||||||
Reference in New Issue
Block a user