1、修复库存变动-增加库存和产品入库时,新增多个相同且之前没有的商品时重复插入erp_stock表报错的问题;
This commit is contained in:
parent
8c87a34739
commit
aae1718e7c
|
@ -8,6 +8,7 @@ import (
|
|||
"go-admin/logger"
|
||||
"go-admin/tools"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
@ -690,9 +691,14 @@ func handleInventoryAdd(gdb *gorm.DB, changeOrder ErpInventoryChangeOrder) error
|
|||
Count: v.Count,
|
||||
DispatchCount: 0,
|
||||
}
|
||||
err = gdb.Create(stock).Error
|
||||
// 使用带冲突处理的插入操作
|
||||
err = gdb.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "store_id"}, {Name: "erp_commodity_id"}}, // 唯一约束字段
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("count + ?", v.Count)}), // 冲突时累加 count
|
||||
}).Create(stock).Error
|
||||
|
||||
if err != nil {
|
||||
logger.Errorf("create stock err:", err)
|
||||
logger.Errorf("create or update stock err:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"go-admin/logger"
|
||||
"go-admin/tools"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
@ -648,9 +649,14 @@ func productAuditAndUpdateStock(gdb *gorm.DB, productOrder ErpInventoryProductOr
|
|||
Count: v.Count,
|
||||
DispatchCount: 0,
|
||||
}
|
||||
err = gdb.Create(stock).Error
|
||||
// 使用带冲突处理的插入操作
|
||||
err = gdb.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "store_id"}, {Name: "erp_commodity_id"}}, // 唯一约束字段
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("count + ?", v.Count)}), // 冲突时累加 count
|
||||
}).Create(stock).Error
|
||||
|
||||
if err != nil {
|
||||
logger.Errorf("create stock err:", err)
|
||||
logger.Errorf("create or update stock err:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user