feat 添加FindInBatches

This commit is contained in:
timerzz 2024-08-31 18:10:06 +08:00
parent 35ca258311
commit 33e0cdb047
3 changed files with 26 additions and 0 deletions

View File

@ -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{})
}

View File

@ -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
})
}

View File

@ -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{})
}