feat article 添加筛选条件

This commit is contained in:
timerzz 2024-09-12 17:36:06 +08:00
parent 93c7391125
commit 5adbffc9a1
3 changed files with 18 additions and 14 deletions

View File

@ -67,7 +67,8 @@ type FindArticleQuery struct {
Keyword string `query:"keyword"` Keyword string `query:"keyword"`
Brand v2.Brand `query:"brand"` Brand v2.Brand `query:"brand"`
Pid string `query:"pid"` Pid string `query:"pid"`
Available *bool `query:"available"` Available *bool `query:"available"` //可购买
RateSort string `query:"rate_sort,omitempty"` // 收益率排序
} }
func NewFindArticleQuery() *FindArticleQuery { func NewFindArticleQuery() *FindArticleQuery {
@ -94,21 +95,24 @@ func (f *FindArticleQuery) SetAvailable(available bool) *FindArticleQuery {
return f return f
} }
func (l *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB { func (f *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if l.ID != 0 { if f.ID != 0 {
db = db.Where("id=?", l.ID) db = db.Where("id=?", f.ID)
} }
if l.Keyword != "" { if f.Keyword != "" {
db = db.Where("(name ilike ? OR english_name ilike ? OR remark ilike ? )", "%"+l.Keyword+"%", "%"+l.Keyword+"%", "%"+l.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 != "" { if f.Brand != "" {
db = db.Where("brand=?", l.Brand) db = db.Where("brand=?", f.Brand)
} }
if l.Pid != "" { if f.Pid != "" {
db = db.Where("pid=?", l.Pid) db = db.Where("pid=?", f.Pid)
} }
if l.Available != nil { if f.Available != nil {
db = db.Where("available=?", *l.Available) db = db.Where("available=?", *f.Available)
}
if f.RateSort != "" {
db = db.Order("rate " + f.RateSort)
} }
return db return db
} }

View File

@ -89,7 +89,7 @@ func (p *providerArticleApi) Get(query *GetProviderArticleQuery) (article v2.Pro
func (p *providerArticleApi) Upsert(article v2.ProviderArticle) error { func (p *providerArticleApi) Upsert(article v2.ProviderArticle) error {
if err := p.db.Clauses(clause.OnConflict{ if err := p.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "provider_id"}, {Name: "sku_id"}}, 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 { }).Create(&article).Error; err != nil {
return err return err
} }

View File

@ -85,7 +85,7 @@ func (p *sellerArticleApi) Get(query *GetSellerArticleQuery) (article v2.SellerA
func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error { func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error {
if err := p.db.Clauses(clause.OnConflict{ if err := p.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}}, Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{"sell", "exclude"}), DoUpdates: clause.AssignmentColumns([]string{"sell", "exclude", "updated_at"}),
}).Create(&article).Error; err != nil { }).Create(&article).Error; err != nil {
return err return err
} }