package storage import ( v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" "gorm.io/gorm" "gorm.io/gorm/clause" ) // SellerArticleApi 管理供应商商品的接口 type SellerArticleApi interface { Get(query *GetSellerArticleQuery) (article v2.SellerArticle, err error) Upsert(article v2.SellerArticle) error Update(sellerArticle v2.SellerArticle, selects ...string) error FindInBatches(query *GetSellerArticleQuery, results *[]v2.SellerArticle, f func(tx *gorm.DB, batch int) error) error AutoMigrate() error SellerPrice(query *GetSellerArticleQuery) (history v2.SellerPrice, err error) } type sellerArticleApi struct { db *gorm.DB } func NewSellerArticleApi(db *gorm.DB) SellerArticleApi { return &sellerArticleApi{db: db} } type GetSellerArticleQuery struct { ID uint `query:"id"` Brand v2.Brand `query:"brand"` Pid string `query:"pid"` SellerId v2.SellerId `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 v2.SellerId) *GetSellerArticleQuery { g.SellerId = sellerId return g } func (g *GetSellerArticleQuery) SetSkuId(skuId string) *GetSellerArticleQuery { g.SkuId = skuId return g } 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 } func (p *sellerArticleApi) Get(query *GetSellerArticleQuery) (article v2.SellerArticle, err error) { err = p.db.Scopes(query.Scope).First(&article).Error return } func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error { if err := p.db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "id"}}, DoUpdates: clause.AssignmentColumns([]string{"sell", "exclude"}), }).Create(&article).Error; err != nil { return err } if len(article.HistoryPrice) > 0 { return p.db.Save(&article.HistoryPrice).Error } return nil } 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) BatchUpdate(articles []v2.SellerArticle) error { return p.db.Select("id", "sell").Save(&articles).Error } func (p *sellerArticleApi) AutoMigrate() error { return p.db.AutoMigrate(&v2.SellerArticle{}, &v2.SellerPrice{}) } func (p *sellerArticleApi) SellerPrice(query *GetSellerArticleQuery) (history v2.SellerPrice, err error) { err = p.db.Scopes(query.Scope).Find(&history).Error return } func (p *sellerArticleApi) Update(sellerArticle v2.SellerArticle, selects ...string) error { if len(selects) == 0 { selects = []string{"Exclude"} } return p.db.Model(&sellerArticle).Select(selects).Omit(clause.Associations).Updates(&sellerArticle).Error }