79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package kwrankmod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/pageopt"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type Matcher = pageopt.Matcher
|
|
|
|
// SumDateMatch
|
|
type SumDateMatch struct {
|
|
SumDate *time.Time
|
|
}
|
|
|
|
func (i *SumDateMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("sumDate", i.SumDate)
|
|
}
|
|
|
|
// EnableMatch
|
|
type EnableMatch struct {
|
|
Enable *bool
|
|
}
|
|
|
|
func (i *EnableMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("enable", i.Enable)
|
|
}
|
|
|
|
func List(sort bson.D, skip, limit int64, matchers ...Matcher) (WordArray, error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
opt := &options.FindOptions{}
|
|
if len(sort) != 0 {
|
|
opt.SetSort(sort)
|
|
}
|
|
opt.SetSkip(skip)
|
|
opt.SetLimit(limit)
|
|
wordArray := make(WordArray, 0, limit)
|
|
if err := coll(nil).Find(&wordArray, filter, opt); err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("coll:%s List fail error:%+v:", table, err))
|
|
return wordArray, err
|
|
}
|
|
return wordArray, nil
|
|
}
|
|
|
|
type Keyword struct {
|
|
KeyWord string `bson:"word" json:"keyWord"` //关键字
|
|
Count int64 `bson:"count" json:"count"` //关键字搜索次数
|
|
}
|
|
|
|
func KwsList(sort bson.D, skip, limit int64, matchers ...Matcher) ([]Keyword, error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
opt := &options.FindOptions{}
|
|
if len(sort) != 0 {
|
|
opt.SetSort(sort)
|
|
}
|
|
opt.SetSkip(skip)
|
|
opt.SetLimit(limit)
|
|
opt.SetProjection(M{
|
|
"word": 1,
|
|
"count": 1,
|
|
})
|
|
list := make([]Keyword, 0, limit)
|
|
if err := coll(nil).Find(&list, filter, opt); err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("coll:%s List fail error:%+v:", table, err))
|
|
return list, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func Count(matchers ...Matcher) (int64, error) {
|
|
filter := pageopt.MergeM(matchers)
|
|
return coll(nil).Count(filter)
|
|
}
|