fix(backend): 库存导入按商品编号匹配不再按名称合并 + 新增 fix-inventory-products 修复工具

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YZ4DskSRKsSiheQonFtQvx
This commit is contained in:
wangjia
2026-06-21 21:24:53 +08:00
parent dcb1d7c44c
commit 6faadf8670
3 changed files with 313 additions and 10 deletions
+15 -10
View File
@@ -633,12 +633,13 @@ func (h *ImportHandler) ImportInventory(c *gin.Context) {
res.total++ // 有商品名称的行才计入总数
// 从缓存查商品,找不到才创建
// 从缓存查商品,找不到才创建
// 有商品编号时:只按编号匹配,绝不回退「名称|系列|规格」合并——同名不同编号
// 是不同的特有产品/序列号,回退名称会把它们错并到同一 product(历史 bug 根因)。
var prod *model.Product
if productCode != "" {
prod = productByCode[productCode]
}
if prod == nil {
} else {
prod = productByNSS[productName+"|"+series+"|"+spec]
}
if prod == nil {
@@ -912,14 +913,18 @@ func parseUploadedExcel(c *gin.Context) ([][]string, error) {
func findOrCreateProductFn(db *gorm.DB, shopID uint64, code, name, series, spec string) (model.Product, error) {
var p model.Product
if db.Where("shop_id = ? AND name = ? AND series = ? AND spec = ? AND deleted_at IS NULL",
shopID, name, series, spec).First(&p).Error == nil {
// 若现有商品没有编码,补填
if p.Code == "" && code != "" {
db.Model(&p).Update("code", code)
p.Code = code
if code != "" {
// 有编号:按「编号」唯一匹配/创建(编号即特有产品序列号),
// 绝不按「名称|系列|规格」合并——同名不同编号是不同商品。
if db.Where("shop_id = ? AND code = ? AND deleted_at IS NULL", shopID, code).First(&p).Error == nil {
return p, nil
}
} else {
// 无编号:退回「名称|系列|规格」匹配(供 ImportStockIn/Out 等无编号场景)。
if db.Where("shop_id = ? AND name = ? AND series = ? AND spec = ? AND deleted_at IS NULL",
shopID, name, series, spec).First(&p).Error == nil {
return p, nil
}
return p, nil
}
p = model.Product{
TenantBase: model.TenantBase{ShopID: shopID},