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"`
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
}

View File

@ -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
}

View File

@ -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
}