From baeca78c5f2655be131a98178c15ca7a9ad0fdf9 Mon Sep 17 00:00:00 2001 From: timerzz Date: Mon, 26 Aug 2024 15:15:04 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0providerArticleApi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/provider-article.go | 60 +++++++++++++++++++++++++++++ structs/storage/storage.go | 9 ++++- 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 structs/storage/provider-article.go diff --git a/structs/storage/provider-article.go b/structs/storage/provider-article.go new file mode 100644 index 0000000..4b52629 --- /dev/null +++ b/structs/storage/provider-article.go @@ -0,0 +1,60 @@ +package storage + +import ( + v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +// ProviderArticleApi 管理供应商商品的接口 +type ProviderArticleApi interface { + Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err error) + Upsert(article v2.ProviderArticle) error +} + +type providerArticleApi struct { + db *gorm.DB +} + +func NewProviderArticleApi(db *gorm.DB) ProviderArticleApi { + return &providerArticleApi{db: 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).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 +} diff --git a/structs/storage/storage.go b/structs/storage/storage.go index f0ed645..ef8e46e 100644 --- a/structs/storage/storage.go +++ b/structs/storage/storage.go @@ -3,8 +3,9 @@ package storage import "gorm.io/gorm" type Storage struct { - articleApi ArticleApi - providerApi ProviderApi + articleApi ArticleApi + providerApi ProviderApi + providerArticleApi ProviderArticleApi } func NewStorage(db *gorm.DB) *Storage { @@ -23,3 +24,7 @@ func (s *Storage) Article() ArticleApi { func (s *Storage) Provider() ProviderApi { return s.providerApi } + +func (s *Storage) ProviderArticle() ProviderArticleApi { + return s.providerArticleApi +}