diff --git a/structs/storage/provider-article.go b/structs/storage/provider-article.go index 976ab4e..61b124a 100644 --- a/structs/storage/provider-article.go +++ b/structs/storage/provider-article.go @@ -11,6 +11,7 @@ type ProviderArticleApi interface { Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error) Upsert(article v2.ProviderArticle) error AutoMigrate() error + FindInBatches(query *GetProviderArticleQuery, results *[]v2.ProviderArticle, f func(tx *gorm.DB, batch int) error) error } type providerArticleApi struct { @@ -89,6 +90,11 @@ func (p *providerArticleApi) Upsert(article v2.ProviderArticle) error { }).Create(&article).Error } +func (p *providerArticleApi) FindInBatches(query *GetProviderArticleQuery, results *[]v2.ProviderArticle, f func(tx *gorm.DB, batch int) error) error { + err := p.db.Scopes(query.Scope).Preload("CalculateProcess").FindInBatches(results, 20, f).Error + return err +} + 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 36b164c..3efda68 100644 --- a/structs/storage/provider-article_test.go +++ b/structs/storage/provider-article_test.go @@ -4,6 +4,7 @@ import ( "testing" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" + "gorm.io/gorm" ) var providerArticle = v2.ProviderArticle{ @@ -30,3 +31,16 @@ func TestProviderArticleApi_Upsert(t *testing.T) { storage := NewStorage(db) storage.ProviderArticle().Upsert(providerArticle) } + +func TestProviderArticleApi_FindInBatches(t *testing.T) { + db, err := GetDevDB() + if err != nil { + t.Fatal(err) + } + 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 + }) +} diff --git a/structs/storage/seller-article.go b/structs/storage/seller-article.go index b0fb86e..aacb950 100644 --- a/structs/storage/seller-article.go +++ b/structs/storage/seller-article.go @@ -10,6 +10,7 @@ import ( type SellerArticleApi interface { Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error) Upsert(article v2.SellerArticle) error + FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error AutoMigrate() error } @@ -82,6 +83,11 @@ func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error { }).Create(&article).Error } +func (p *sellerArticleApi) FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error { + err := p.db.Scopes(query.Scope).Preload("CalculateProcess").FindInBatches(results, 20, f).Error + return err +} + func (p *sellerArticleApi) AutoMigrate() error { return p.db.AutoMigrate(&v2.SellerArticle{}) }