From 4dda7ac1fab41ae8f489224c9a38fb8cdf75b8bc Mon Sep 17 00:00:00 2001 From: timerzz Date: Tue, 27 Aug 2024 16:39:21 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0seller=20Api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/seller-article.go | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 structs/storage/seller-article.go diff --git a/structs/storage/seller-article.go b/structs/storage/seller-article.go new file mode 100644 index 0000000..b4ae2cf --- /dev/null +++ b/structs/storage/seller-article.go @@ -0,0 +1,56 @@ +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 GetProviderArticleQuery) (article v2.ProviderArticle, err error) + Upsert(article v2.SellerArticle) error +} + +type sellerArticleApi struct { + db *gorm.DB +} + +type GetSellerArticleQuery struct { + ID uint `query:"id"` + Brand string `query:"brand"` + Pid string `query:"pid"` + SellerId string `query:"sellerId"` + SkuId string `query:"skuId"` +} + +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 { + return p.db.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "seller_id"}, {Name: "sku_id"}}, + DoUpdates: clause.AssignmentColumns([]string{"sell"}), + }).Create(&article).Error +}