2024-08-27 16:39:21 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SellerArticleApi 管理供应商商品的接口
|
|
|
|
type SellerArticleApi interface {
|
2024-08-31 12:46:18 +08:00
|
|
|
Get(query *GetProviderArticleQuery) (article v2.ProviderArticle, err error)
|
2024-08-27 16:39:21 +08:00
|
|
|
Upsert(article v2.SellerArticle) error
|
2024-08-31 18:10:06 +08:00
|
|
|
FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error
|
2024-08-30 13:29:39 +08:00
|
|
|
AutoMigrate() error
|
2024-08-27 16:39:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type sellerArticleApi struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetSellerArticleQuery struct {
|
2024-08-31 12:39:40 +08:00
|
|
|
ID uint `query:"id"`
|
|
|
|
Brand v2.Brand `query:"brand"`
|
|
|
|
Pid string `query:"pid"`
|
|
|
|
SellerId string `query:"sellerId"`
|
|
|
|
SkuId string `query:"skuId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGetSellerArticleQuery() *GetSellerArticleQuery {
|
|
|
|
return &GetSellerArticleQuery{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GetSellerArticleQuery) SetID(id uint) *GetSellerArticleQuery {
|
|
|
|
g.ID = id
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GetSellerArticleQuery) SetBrand(brand v2.Brand) *GetSellerArticleQuery {
|
|
|
|
g.Brand = brand
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
func (g *GetSellerArticleQuery) SetPid(pid string) *GetSellerArticleQuery {
|
|
|
|
g.Pid = pid
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
func (g *GetSellerArticleQuery) SetSellerId(sellerId string) *GetSellerArticleQuery {
|
|
|
|
g.SellerId = sellerId
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
func (g *GetSellerArticleQuery) SetSkuId(skuId string) *GetSellerArticleQuery {
|
|
|
|
g.SkuId = skuId
|
|
|
|
return g
|
2024-08-27 16:39:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GetSellerArticleQuery) 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.SellerId != "" {
|
|
|
|
db = db.Where("seller_id=?", g.SellerId)
|
|
|
|
}
|
|
|
|
if g.SkuId != "" {
|
|
|
|
db = db.Where("sku_id=?", g.SkuId)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2024-08-31 12:46:18 +08:00
|
|
|
func (p *sellerArticleApi) Get(query *GetSellerArticleQuery) (article v2.SellerArticle, err error) {
|
2024-08-27 16:39:21 +08:00
|
|
|
err = p.db.Scopes(query.Scope).First(&article).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error {
|
|
|
|
return p.db.Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "seller_id"}, {Name: "sku_id"}},
|
2024-08-31 12:39:40 +08:00
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"sell", "exclude"}),
|
2024-08-27 16:39:21 +08:00
|
|
|
}).Create(&article).Error
|
|
|
|
}
|
2024-08-30 13:29:39 +08:00
|
|
|
|
2024-08-31 18:10:06 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-30 13:29:39 +08:00
|
|
|
func (p *sellerArticleApi) AutoMigrate() error {
|
|
|
|
return p.db.AutoMigrate(&v2.SellerArticle{})
|
|
|
|
}
|