From 015781e484f70d980b017d7bcb785323333cc72e Mon Sep 17 00:00:00 2001 From: zhanghaogang Date: Sat, 3 Aug 2024 17:07:10 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=93=8D=E4=BD=9C=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/article.go | 75 +++++++++++++++++++++++++++++++++++++ structs/storage/provider.go | 62 ++++++++++++++++++++++++++++++ structs/storage/scope.go | 35 +++++++++++++++++ structs/storage/seller.go | 62 ++++++++++++++++++++++++++++++ structs/v2/article.go | 8 +++- structs/v2/provider.go | 10 ++++- structs/v2/seller.go | 10 +++-- 7 files changed, 256 insertions(+), 6 deletions(-) create mode 100644 structs/storage/provider.go create mode 100644 structs/storage/scope.go create mode 100644 structs/storage/seller.go diff --git a/structs/storage/article.go b/structs/storage/article.go index 8364795..ae2c35b 100644 --- a/structs/storage/article.go +++ b/structs/storage/article.go @@ -7,9 +7,14 @@ import ( ) type Storage struct { + ArticleApi } type ArticleApi interface { + Upsert(article v2.Article) error + Find(query FindArticleQuery) (articles []v2.Article, err error) + List(query PageListQuery) (articles []v2.Article, total int64, err error) + Get(query GetArticleQuery) (article v2.Article, err error) } type articleApi struct { @@ -23,3 +28,73 @@ func (a *articleApi) Upsert(article v2.Article) error { DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at", "cost_price", "sell_price", "rate", "remark"}), }).Create(&article).Error } + +type FindArticleQuery struct { + ID uint `query:"id"` + Keyword string `query:"keyword"` + Brand string `query:"brand"` + Pid string `query:"pid"` + Available *bool `query:"available"` +} + +func (l *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB { + if l.ID != 0 { + db = db.Where("`id` = ?", l.ID) + } + if l.Keyword != "" { + db = db.Where("(`name` LIKE ? OR `english_name LIKE ? OR `remark` LIKE ? )", "%"+l.Keyword+"%", "%"+l.Keyword+"%", "%"+l.Keyword+"%") + } + if l.Brand != "" { + db = db.Where("`brand` = ?", l.Brand) + } + if l.Pid != "" { + db = db.Where("`pid` = ?", l.Pid) + } + if l.Available != nil { + db = db.Where("`available` = ?", *l.Available) + } + return db +} + +func (a *articleApi) Find(query FindArticleQuery) (articles []v2.Article, err error) { + err = a.db.Scopes(query.Scope).Find(&articles).Error + return +} + +func (a *articleApi) List(query PageListQuery) (articles []v2.Article, total int64, err error) { + err = a.db.Scopes(query.Scoper.Scope).Count(&total).Error + if err != nil { + return + } + err = a.db.Scopes(query.Scope).Find(&articles).Error + return +} + +type GetArticleQuery struct { + ID uint `query:"id"` + Brand string `query:"brand"` + Pid string `query:"pid"` + History bool `query:"history"` +} + +func (g *GetArticleQuery) Scope(db *gorm.DB) *gorm.DB { + db = db.Preload("Providers").Preload("Sellers") + 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.History { + db = db.Preload("Providers.HistoryPrice").Preload("Sellers.HistoryPrice") + } + return db +} + +func (a *articleApi) Get(query GetArticleQuery) (article v2.Article, err error) { + err = a.db.Scopes(query.Scope).First(&article).Error + return +} diff --git a/structs/storage/provider.go b/structs/storage/provider.go new file mode 100644 index 0000000..9f30182 --- /dev/null +++ b/structs/storage/provider.go @@ -0,0 +1,62 @@ +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) +} + +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 +} diff --git a/structs/storage/scope.go b/structs/storage/scope.go new file mode 100644 index 0000000..c9575cb --- /dev/null +++ b/structs/storage/scope.go @@ -0,0 +1,35 @@ +package storage + +import "gorm.io/gorm" + +type Scoper interface { + Scope(db *gorm.DB) *gorm.DB +} + +type PageQuery struct { + Page int `query:"page"` + Size int `query:"size"` +} + +func (p *PageQuery) Scope(db *gorm.DB) *gorm.DB { + if p.Size <= 0 { + p.Size = 10 + } + + if p.Page <= 0 { + p.Page = 1 + } + return db.Offset((p.Page - 1) * p.Size).Limit(p.Size) +} + +type PageListQuery struct { + Scoper + PageQuery +} + +func (p *PageListQuery) Scope(db *gorm.DB) *gorm.DB { + if p.Scoper != nil { + db = p.Scoper.Scope(db) + } + return p.PageQuery.Scope(db) +} diff --git a/structs/storage/seller.go b/structs/storage/seller.go new file mode 100644 index 0000000..d18288c --- /dev/null +++ b/structs/storage/seller.go @@ -0,0 +1,62 @@ +package storage + +import ( + v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +type SellerApi interface { +} + +type sellerApi struct { + db *gorm.DB +} + +// SellerArticleApi 管理供应商商品的接口 +type SellerArticleApi interface { + Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err 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 +} diff --git a/structs/v2/article.go b/structs/v2/article.go index 798ba49..0f482f3 100644 --- a/structs/v2/article.go +++ b/structs/v2/article.go @@ -6,6 +6,12 @@ import ( "gorm.io/gorm" ) +type Brand string + +const ( + Brand_Coach Brand = "coach" +) + // Article 商品 type Article struct { ID uint `gorm:"primary_key" json:"id"` @@ -19,7 +25,7 @@ type Article struct { // 货号 Pid string `gorm:"index:article_pid_brand,unique" json:"pid"` // 品牌 - Brand string `gorm:"index:article_pid_brand,unique" json:"brand"` + Brand Brand `gorm:"index:article_pid_brand,unique" json:"brand"` // 描述 Desc string `json:"desc"` // 图片 diff --git a/structs/v2/provider.go b/structs/v2/provider.go index 75507f0..0f206c5 100644 --- a/structs/v2/provider.go +++ b/structs/v2/provider.go @@ -4,6 +4,12 @@ import "time" type ProviderId string +const ( + ProviderId_CoachOutlet_US ProviderId = "coachOutlet_us" + ProviderId_CoachOutlet_CN ProviderId = "coachOutlet_cn" + ProviderId_Coach_CN ProviderId = "coach_cn" +) + // ProviderArticle 供应商商品信息 type ProviderArticle struct { ID uint `gorm:"primary_key" json:"-"` @@ -12,9 +18,9 @@ type ProviderArticle struct { // 对应的商品ID ArticleID uint // 货号 - Pid string `gorm:"index:provider_pid_brand,unique" json:"pid"` + Pid string `gorm:"index" json:"pid"` // 品牌 - Brand string `gorm:"index:provider_pid_brand,unique" json:"brand"` + Brand Brand `gorm:"index" json:"brand"` // 供应商商品链接链接 Link string `json:"link"` // 供应商id diff --git a/structs/v2/seller.go b/structs/v2/seller.go index 6cd8eb7..149fa64 100644 --- a/structs/v2/seller.go +++ b/structs/v2/seller.go @@ -4,6 +4,10 @@ import "time" type SellerId string +const ( + SellerId_DW SellerId = "dewu" +) + // SellerArticle 销售商商品信息 type SellerArticle struct { ID uint `gorm:"primary_key" json:"-"` @@ -11,10 +15,10 @@ type SellerArticle struct { // 商品的ID ArticleID uint // 货号 - Pid string `gorm:"index:seller_pid_brand,unique" json:"pid"` + Pid string `gorm:"index" json:"pid"` // 品牌 - Brand string `gorm:"index:seller_pid_brand,unique" json:"brand"` - // 销售商商品链接链接 + Brand Brand `gorm:"index" json:"brand"` + // 销售商商品链接 Link string `json:"link"` // 销售商id SellerId SellerId `gorm:"index:sellerId_sku,unique" json:"sellerId"`