common/structs/storage/provider.go

64 lines
1.5 KiB
Go
Raw Normal View History

2024-08-03 17:07:10 +08:00
package storage
import (
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type ProviderApi interface {
}
type providerApi struct {
db *gorm.DB
}
// ProviderArticleApi 管理供应商商品的接口
type ProviderArticleApi interface {
Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err error)
2024-08-05 17:21:58 +08:00
Upsert(article v2.ProviderArticle) error
2024-08-03 17:07:10 +08:00
}
type providerArticleApi struct {
db *gorm.DB
}
type GetProviderArticleQuery struct {
ID uint `query:"id"`
Brand string `query:"brand"`
Pid string `query:"pid"`
ProviderId string `query:"providerId"`
SkuId string `query:"skuId"`
}
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
}
func (p *providerArticleApi) Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err error) {
err = p.db.Scopes(query.Scope).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
}