common/structs/storage/provider-article.go

101 lines
2.7 KiB
Go
Raw Normal View History

2024-08-26 15:15:04 +08:00
package storage
import (
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// ProviderArticleApi 管理供应商商品的接口
type ProviderArticleApi interface {
2024-08-31 12:46:18 +08:00
Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error)
2024-08-26 15:15:04 +08:00
Upsert(article v2.ProviderArticle) error
2024-08-30 13:29:39 +08:00
AutoMigrate() error
2024-08-31 18:10:06 +08:00
FindInBatches(query *GetProviderArticleQuery, results *[]v2.ProviderArticle, f func(tx *gorm.DB, batch int) error) error
2024-08-26 15:15:04 +08:00
}
type providerArticleApi struct {
db *gorm.DB
}
func NewProviderArticleApi(db *gorm.DB) ProviderArticleApi {
return &providerArticleApi{db: db}
}
2024-08-31 12:39:40 +08:00
// ******************GET
2024-08-26 15:15:04 +08:00
type GetProviderArticleQuery struct {
2024-08-31 12:39:40 +08:00
ID uint `query:"id"`
Brand v2.Brand `query:"brand"`
Pid string `query:"pid"`
ProviderId string `query:"providerId"`
SkuId string `query:"skuId"`
}
func NewGetProviderArticleQuery() *GetProviderArticleQuery {
return &GetProviderArticleQuery{}
}
func (g *GetProviderArticleQuery) SetID(id uint) *GetProviderArticleQuery {
g.ID = id
return g
}
func (g *GetProviderArticleQuery) SetBrand(brand v2.Brand) *GetProviderArticleQuery {
g.Brand = brand
return g
}
func (g *GetProviderArticleQuery) SetPid(pid string) *GetProviderArticleQuery {
g.Pid = pid
return g
}
func (g *GetProviderArticleQuery) SetProviderId(providerId string) *GetProviderArticleQuery {
g.ProviderId = providerId
return g
}
func (g *GetProviderArticleQuery) SetSkuId(skuId string) *GetProviderArticleQuery {
g.SkuId = skuId
return g
2024-08-26 15:15:04 +08:00
}
func (g *GetProviderArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if g.ID > 0 {
db = db.Where("id=?", g.ID)
}
if g.Brand != "" {
db = db.Where("brand=?", g.Brand)
}
if g.Pid != "" {
db = db.Where("pid=?", g.Pid)
}
if g.ProviderId != "" {
db = db.Where("provider_id=?", g.ProviderId)
}
if g.SkuId != "" {
db = db.Where("sku_id=?", g.SkuId)
}
return db
}
2024-08-31 12:46:18 +08:00
func (p *providerArticleApi) Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error) {
2024-08-26 15:15:04 +08:00
err = p.db.Scopes(query.Scope).Preload("CalculateProcess").First(&article).Error
return
}
func (p *providerArticleApi) Upsert(article v2.ProviderArticle) error {
return p.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "provider_id"}, {Name: "sku_id"}},
DoUpdates: clause.AssignmentColumns([]string{"cost"}),
}).Create(&article).Error
}
2024-08-30 13:29:39 +08:00
2024-08-31 18:10:06 +08:00
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
}
2024-08-30 13:29:39 +08:00
func (p *providerArticleApi) AutoMigrate() error {
return p.db.AutoMigrate(&v2.ProviderArticle{})
}