From 5adbffc9a1f472f5902f52f8dc881a426e386c5a Mon Sep 17 00:00:00 2001 From: timerzz Date: Thu, 12 Sep 2024 17:36:06 +0800 Subject: [PATCH] =?UTF-8?q?feat=20article=20=E6=B7=BB=E5=8A=A0=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/article.go | 28 ++++++++++++++++------------ structs/storage/provider-article.go | 2 +- structs/storage/seller-article.go | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/structs/storage/article.go b/structs/storage/article.go index a80742d..b2eeaee 100644 --- a/structs/storage/article.go +++ b/structs/storage/article.go @@ -67,7 +67,8 @@ type FindArticleQuery struct { Keyword string `query:"keyword"` Brand v2.Brand `query:"brand"` Pid string `query:"pid"` - Available *bool `query:"available"` + Available *bool `query:"available"` //可购买 + RateSort string `query:"rate_sort,omitempty"` // 收益率排序 } func NewFindArticleQuery() *FindArticleQuery { @@ -94,21 +95,24 @@ func (f *FindArticleQuery) SetAvailable(available bool) *FindArticleQuery { return f } -func (l *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB { - if l.ID != 0 { - db = db.Where("id=?", l.ID) +func (f *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB { + if f.ID != 0 { + db = db.Where("id=?", f.ID) } - if l.Keyword != "" { - db = db.Where("(name ilike ? OR english_name ilike ? OR remark ilike ? )", "%"+l.Keyword+"%", "%"+l.Keyword+"%", "%"+l.Keyword+"%") + if f.Keyword != "" { + db = db.Where("(name ilike ? OR english_name ilike ? OR remark ilike ? OR pid ilike ?)", "%"+f.Keyword+"%", "%"+f.Keyword+"%", "%"+f.Keyword+"%", "%"+f.Keyword+"%") } - if l.Brand != "" { - db = db.Where("brand=?", l.Brand) + if f.Brand != "" { + db = db.Where("brand=?", f.Brand) } - if l.Pid != "" { - db = db.Where("pid=?", l.Pid) + if f.Pid != "" { + db = db.Where("pid=?", f.Pid) } - if l.Available != nil { - db = db.Where("available=?", *l.Available) + if f.Available != nil { + db = db.Where("available=?", *f.Available) + } + if f.RateSort != "" { + db = db.Order("rate " + f.RateSort) } return db } diff --git a/structs/storage/provider-article.go b/structs/storage/provider-article.go index 3896408..b0d3a95 100644 --- a/structs/storage/provider-article.go +++ b/structs/storage/provider-article.go @@ -89,7 +89,7 @@ func (p *providerArticleApi) Get(query *GetProviderArticleQuery) (article v2.Pro func (p *providerArticleApi) Upsert(article v2.ProviderArticle) error { if err := p.db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "provider_id"}, {Name: "sku_id"}}, - DoUpdates: clause.AssignmentColumns([]string{"cost", "available"}), + DoUpdates: clause.AssignmentColumns([]string{"cost", "available", "updated_at"}), }).Create(&article).Error; err != nil { return err } diff --git a/structs/storage/seller-article.go b/structs/storage/seller-article.go index 63a4395..fd38020 100644 --- a/structs/storage/seller-article.go +++ b/structs/storage/seller-article.go @@ -85,7 +85,7 @@ func (p *sellerArticleApi) Get(query *GetSellerArticleQuery) (article v2.SellerA 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"}), + DoUpdates: clause.AssignmentColumns([]string{"sell", "exclude", "updated_at"}), }).Create(&article).Error; err != nil { return err }