From eb8632ab443501d3ea3acbe35e13806d78aa53d2 Mon Sep 17 00:00:00 2001 From: timerzz Date: Sat, 31 Aug 2024 18:27:17 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/provider-article.go | 6 ++++++ structs/storage/provider-article_test.go | 14 +++++++++++--- structs/storage/seller-article.go | 11 ++++++++++- structs/storage/storage.go | 5 +++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/structs/storage/provider-article.go b/structs/storage/provider-article.go index 61b124a..fcd20eb 100644 --- a/structs/storage/provider-article.go +++ b/structs/storage/provider-article.go @@ -10,6 +10,7 @@ import ( type ProviderArticleApi interface { Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error) Upsert(article v2.ProviderArticle) error + BatchUpdate(articles []v2.ProviderArticle) error AutoMigrate() error FindInBatches(query *GetProviderArticleQuery, results *[]v2.ProviderArticle, f func(tx *gorm.DB, batch int) error) error } @@ -95,6 +96,11 @@ func (p *providerArticleApi) FindInBatches(query *GetProviderArticleQuery, resul return err } +// 批量更新,更新价格时用到 +func (p *providerArticleApi) BatchUpdate(articles []v2.ProviderArticle) error { + return p.db.Select("id", "cost").Save(&articles).Error +} + func (p *providerArticleApi) AutoMigrate() error { return p.db.AutoMigrate(&v2.ProviderArticle{}) } diff --git a/structs/storage/provider-article_test.go b/structs/storage/provider-article_test.go index 3efda68..9c1ff88 100644 --- a/structs/storage/provider-article_test.go +++ b/structs/storage/provider-article_test.go @@ -1,6 +1,7 @@ package storage import ( + "fmt" "testing" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" @@ -39,8 +40,15 @@ func TestProviderArticleApi_FindInBatches(t *testing.T) { } storage := NewStorage(db) var results = make([]v2.ProviderArticle, 0) - storage.ProviderArticle().FindInBatches(&GetProviderArticleQuery{ProviderId: "1"}, &results, func(tx *gorm.DB, batch int) error { - t.Log(results) - return nil + err = storage.ProviderArticle().FindInBatches(&GetProviderArticleQuery{ProviderId: "1"}, &results, func(tx *gorm.DB, batch int) error { + for idx := range results { + results[idx].Link = fmt.Sprintf("test_%d", idx) + results[idx].SkuID = fmt.Sprintf("sku_%d", idx) + } + + return db.Select("id", "link").Save(&results).Error }) + if err != nil { + t.Fatal(err) + } } diff --git a/structs/storage/seller-article.go b/structs/storage/seller-article.go index aacb950..b16f84f 100644 --- a/structs/storage/seller-article.go +++ b/structs/storage/seller-article.go @@ -8,7 +8,7 @@ import ( // SellerArticleApi 管理供应商商品的接口 type SellerArticleApi interface { - Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error) + Get(query *GetSellerArticleQuery) (article v2.SellerArticle, err error) Upsert(article v2.SellerArticle) error FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error AutoMigrate() error @@ -18,6 +18,10 @@ type sellerArticleApi struct { db *gorm.DB } +func NewSellerArticleApi(db *gorm.DB) SellerArticleApi { + return &sellerArticleApi{db: db} +} + type GetSellerArticleQuery struct { ID uint `query:"id"` Brand v2.Brand `query:"brand"` @@ -88,6 +92,11 @@ func (p *sellerArticleApi) FindInBatches(query *GetSellerArticleQuery, results * return err } +// 批量更新,更新价格时用到 +func (p *sellerArticleApi) BatchUpdate(articles []v2.SellerArticle) error { + return p.db.Select("id", "sell").Save(&articles).Error +} + func (p *sellerArticleApi) AutoMigrate() error { return p.db.AutoMigrate(&v2.SellerArticle{}) } diff --git a/structs/storage/storage.go b/structs/storage/storage.go index 53c31b6..5470b92 100644 --- a/structs/storage/storage.go +++ b/structs/storage/storage.go @@ -16,6 +16,7 @@ func NewStorage(db *gorm.DB) *Storage { providerApi: NewProviderApi(db), providerArticleApi: NewProviderArticleApi(db), sellerApi: NewSellerApi(db), + sellerArticleApi: NewSellerArticleApi(db), } } @@ -33,3 +34,7 @@ func (s *Storage) ProviderArticle() ProviderArticleApi { func (s *Storage) Seller() SellerApi { return s.sellerApi } + +func (s *Storage) SellerArticle() SellerArticleApi { + return s.sellerArticleApi +}