From c4bc3e368aa70761910bc73d82713bac88b4273d Mon Sep 17 00:00:00 2001 From: timerzz Date: Tue, 3 Sep 2024 17:18:48 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BF=AE=E6=94=B9article=20upsert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/article.go | 19 +++++++++++++++---- structs/storage/article_test.go | 27 ++++++++++++++------------- structs/storage/provider-article.go | 2 +- structs/storage/seller-article.go | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/structs/storage/article.go b/structs/storage/article.go index 8c1b3e8..d441a2c 100644 --- a/structs/storage/article.go +++ b/structs/storage/article.go @@ -1,6 +1,8 @@ package storage import ( + "fmt" + v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -26,10 +28,19 @@ func NewArticleApi(db *gorm.DB) ArticleApi { // Upsert 插入或者更新商品 func (a *articleApi) Upsert(article v2.Article) error { - return a.db.Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "pid"}, {Name: "brand"}}, - DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at", "cost_price", "sell_price", "rate", "remark", "exclude"}), - }).Create(&article).Error + return a.db.Transaction(func(tx *gorm.DB) error { + if err := tx.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "pid"}, {Name: "brand"}}, + DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at", "cost_price", "sell_price", "rate", "remark", "exclude"}), + }).Create(&article).Error; err != nil { + return err + } + if err := tx.Save(&article.Providers).Error; err != nil { + return fmt.Errorf("failed to save providers: %v", err) + } + return tx.Save(&article.Sellers).Error + }) + } // ******************Find和List diff --git a/structs/storage/article_test.go b/structs/storage/article_test.go index 036ed6c..843af14 100644 --- a/structs/storage/article_test.go +++ b/structs/storage/article_test.go @@ -15,39 +15,40 @@ func GetDevDB() (*gorm.DB, error) { } var article = v2.Article{ - Name: "test1", - EnglishName: "test1", + Name: "test2", + EnglishName: "test2", Pid: "111", Brand: "col", Desc: "desc", Image: "http://123", Available: false, - CostPrice: 123, + CostPrice: 321, Providers: []v2.ProviderArticle{ { + ID: 1, Pid: "111", - Brand: "col1", + Brand: "col", Link: "http://123", ProviderId: "coach2", - SkuID: "sku1", + SkuID: "sku2", Cost: v2.ProviderPrice{ - OriginalPrice: 111, - FinalPrice: 111, - CalMark: "uuuuu", + OriginalPrice: 222, + FinalPrice: 222, + CalMark: "aaa", }, HistoryPrice: []v2.ProviderPrice{ { - OriginalPrice: 111, - FinalPrice: 111, + OriginalPrice: 222, + FinalPrice: 222, CalMark: "uuuuu", }, }, }, }, - SellPrice: 111, + SellPrice: 222, Sellers: []v2.SellerArticle{}, - Rate: 111, - Remark: "111", + Rate: 222, + Remark: "222", } func TestUpsetArticle(t *testing.T) { diff --git a/structs/storage/provider-article.go b/structs/storage/provider-article.go index 2292a96..2e93f51 100644 --- a/structs/storage/provider-article.go +++ b/structs/storage/provider-article.go @@ -102,5 +102,5 @@ func (p *providerArticleApi) BatchUpdate(articles []v2.ProviderArticle) error { } func (p *providerArticleApi) AutoMigrate() error { - return p.db.AutoMigrate(&v2.ProviderArticle{}) + return p.db.AutoMigrate(&v2.ProviderArticle{}, &v2.ProviderPrice{}) } diff --git a/structs/storage/seller-article.go b/structs/storage/seller-article.go index 540cd63..0b82592 100644 --- a/structs/storage/seller-article.go +++ b/structs/storage/seller-article.go @@ -98,5 +98,5 @@ func (p *sellerArticleApi) BatchUpdate(articles []v2.SellerArticle) error { } func (p *sellerArticleApi) AutoMigrate() error { - return p.db.AutoMigrate(&v2.SellerArticle{}) + return p.db.AutoMigrate(&v2.SellerArticle{}, &v2.SellerPrice{}) }