From 4e3069f229439be3257f873ff59d36d2ba705c1c Mon Sep 17 00:00:00 2001 From: timerzz Date: Wed, 4 Sep 2024 14:30:15 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BF=AE=E6=94=B9=E5=AD=90article?= =?UTF-8?q?=E7=9A=84upsert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/provider-article.go | 10 ++++++++-- structs/storage/seller-article.go | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/structs/storage/provider-article.go b/structs/storage/provider-article.go index 2e93f51..b548b2a 100644 --- a/structs/storage/provider-article.go +++ b/structs/storage/provider-article.go @@ -85,10 +85,16 @@ func (p *providerArticleApi) Get(query *GetProviderArticleQuery) (article v2.Pro } func (p *providerArticleApi) Upsert(article v2.ProviderArticle) error { - return p.db.Clauses(clause.OnConflict{ + if err := p.db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "provider_id"}, {Name: "sku_id"}}, DoUpdates: clause.AssignmentColumns([]string{"cost"}), - }).Create(&article).Error + }).Create(&article).Error; err != nil { + return err + } + if len(article.HistoryPrice) > 0 { + return p.db.Save(&article.HistoryPrice).Error + } + return nil } func (p *providerArticleApi) FindInBatches(query *GetProviderArticleQuery, results *[]v2.ProviderArticle, f func(tx *gorm.DB, batch int) error) error { diff --git a/structs/storage/seller-article.go b/structs/storage/seller-article.go index 0b82592..cf0323d 100644 --- a/structs/storage/seller-article.go +++ b/structs/storage/seller-article.go @@ -81,10 +81,16 @@ func (p *sellerArticleApi) Get(query *GetSellerArticleQuery) (article v2.SellerA } func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error { - return p.db.Clauses(clause.OnConflict{ + if err := p.db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "id"}}, DoUpdates: clause.AssignmentColumns([]string{"sell", "exclude"}), - }).Create(&article).Error + }).Create(&article).Error; err != nil { + return err + } + if len(article.HistoryPrice) > 0 { + return p.db.Save(&article.HistoryPrice).Error + } + return nil } func (p *sellerArticleApi) FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error {