feat 添加批量更新

This commit is contained in:
timerzz 2024-08-31 18:27:17 +08:00
parent 33e0cdb047
commit eb8632ab44
4 changed files with 32 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import (
type ProviderArticleApi interface { type ProviderArticleApi interface {
Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error) Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error)
Upsert(article v2.ProviderArticle) error Upsert(article v2.ProviderArticle) error
BatchUpdate(articles []v2.ProviderArticle) error
AutoMigrate() error AutoMigrate() error
FindInBatches(query *GetProviderArticleQuery, results *[]v2.ProviderArticle, f func(tx *gorm.DB, batch int) error) 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 return err
} }
// 批量更新,更新价格时用到
func (p *providerArticleApi) BatchUpdate(articles []v2.ProviderArticle) error {
return p.db.Select("id", "cost").Save(&articles).Error
}
func (p *providerArticleApi) AutoMigrate() error { func (p *providerArticleApi) AutoMigrate() error {
return p.db.AutoMigrate(&v2.ProviderArticle{}) return p.db.AutoMigrate(&v2.ProviderArticle{})
} }

View File

@ -1,6 +1,7 @@
package storage package storage
import ( import (
"fmt"
"testing" "testing"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
@ -39,8 +40,15 @@ func TestProviderArticleApi_FindInBatches(t *testing.T) {
} }
storage := NewStorage(db) storage := NewStorage(db)
var results = make([]v2.ProviderArticle, 0) var results = make([]v2.ProviderArticle, 0)
storage.ProviderArticle().FindInBatches(&GetProviderArticleQuery{ProviderId: "1"}, &results, func(tx *gorm.DB, batch int) error { err = storage.ProviderArticle().FindInBatches(&GetProviderArticleQuery{ProviderId: "1"}, &results, func(tx *gorm.DB, batch int) error {
t.Log(results) for idx := range results {
return nil 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)
}
} }

View File

@ -8,7 +8,7 @@ import (
// SellerArticleApi 管理供应商商品的接口 // SellerArticleApi 管理供应商商品的接口
type SellerArticleApi interface { type SellerArticleApi interface {
Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error) Get(query *GetSellerArticleQuery) (article v2.SellerArticle, err error)
Upsert(article v2.SellerArticle) error Upsert(article v2.SellerArticle) error
FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error
AutoMigrate() error AutoMigrate() error
@ -18,6 +18,10 @@ type sellerArticleApi struct {
db *gorm.DB db *gorm.DB
} }
func NewSellerArticleApi(db *gorm.DB) SellerArticleApi {
return &sellerArticleApi{db: db}
}
type GetSellerArticleQuery struct { type GetSellerArticleQuery struct {
ID uint `query:"id"` ID uint `query:"id"`
Brand v2.Brand `query:"brand"` Brand v2.Brand `query:"brand"`
@ -88,6 +92,11 @@ func (p *sellerArticleApi) FindInBatches(query *GetSellerArticleQuery, results *
return err return err
} }
// 批量更新,更新价格时用到
func (p *sellerArticleApi) BatchUpdate(articles []v2.SellerArticle) error {
return p.db.Select("id", "sell").Save(&articles).Error
}
func (p *sellerArticleApi) AutoMigrate() error { func (p *sellerArticleApi) AutoMigrate() error {
return p.db.AutoMigrate(&v2.SellerArticle{}) return p.db.AutoMigrate(&v2.SellerArticle{})
} }

View File

@ -16,6 +16,7 @@ func NewStorage(db *gorm.DB) *Storage {
providerApi: NewProviderApi(db), providerApi: NewProviderApi(db),
providerArticleApi: NewProviderArticleApi(db), providerArticleApi: NewProviderArticleApi(db),
sellerApi: NewSellerApi(db), sellerApi: NewSellerApi(db),
sellerArticleApi: NewSellerArticleApi(db),
} }
} }
@ -33,3 +34,7 @@ func (s *Storage) ProviderArticle() ProviderArticleApi {
func (s *Storage) Seller() SellerApi { func (s *Storage) Seller() SellerApi {
return s.sellerApi return s.sellerApi
} }
func (s *Storage) SellerArticle() SellerArticleApi {
return s.sellerArticleApi
}