184 lines
3.4 KiB
Go
184 lines
3.4 KiB
Go
package adsmod
|
|
|
|
import (
|
|
"time"
|
|
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/pageopt"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type AdsSlice []Ads
|
|
|
|
func (r AdsSlice) Len() int {
|
|
return len(r)
|
|
}
|
|
|
|
func (r AdsSlice) Swap(i, j int) {
|
|
r[i], r[j] = r[j], r[i]
|
|
}
|
|
|
|
func (r AdsSlice) Less(i, j int) bool {
|
|
return r[i].SortCode < r[j].SortCode
|
|
}
|
|
|
|
func (a AdsSlice) ToIDs() []ObjectID {
|
|
ids := make([]ObjectID, 0, len(a))
|
|
for _, v := range a {
|
|
ids = append(ids, v.ID)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func (a AdsSlice) ToPositions() []AdPosition {
|
|
ps := make([]AdPosition, 0, len(a))
|
|
for _, v := range a {
|
|
ps = append(ps, v.Position)
|
|
}
|
|
return ps
|
|
}
|
|
|
|
func (a AdsSlice) SliceByPosition(position AdPosition) AdsSlice {
|
|
ads := make(AdsSlice, 0, len(a))
|
|
for _, v := range a {
|
|
if v.Position == position {
|
|
ads = append(ads, v)
|
|
}
|
|
}
|
|
return ads
|
|
}
|
|
|
|
type Matcher = pageopt.Matcher
|
|
|
|
// AdsTypeMatch
|
|
type AdsTypeMatch struct {
|
|
AdsType *AdsType
|
|
}
|
|
|
|
func (a *AdsTypeMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("adsType", a.AdsType)
|
|
}
|
|
|
|
// AdPositionMatch
|
|
type AdPositionMatch struct {
|
|
Position *AdPosition
|
|
}
|
|
|
|
func (a *AdPositionMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("position", a.Position)
|
|
}
|
|
|
|
// AdPositionNinMatch
|
|
type AdPositionNinMatch struct {
|
|
PositionList []AdPosition
|
|
}
|
|
|
|
func (a *AdPositionNinMatch) New() Matcher {
|
|
return pageopt.NewNinMatch("position", a.PositionList)
|
|
}
|
|
|
|
// DistrictCodeMatch
|
|
type DistrictCodeMatch struct {
|
|
DistrictCode *string
|
|
}
|
|
|
|
func (a *DistrictCodeMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("districtCode", a.DistrictCode)
|
|
}
|
|
|
|
// ActiveMatch
|
|
type ActiveMatch struct {
|
|
Active *bool
|
|
}
|
|
|
|
type StartLTEMatch struct {
|
|
t time.Time
|
|
}
|
|
|
|
// IDMatch
|
|
type IDMatch = pageopt.IDMatch
|
|
|
|
// IDInMatch
|
|
type IDInMatch = pageopt.IDInMatch
|
|
|
|
type Sort = bson.D
|
|
|
|
type EndGTMatch struct {
|
|
t time.Time
|
|
}
|
|
|
|
func (a *ActiveMatch) New() Matcher {
|
|
return pageopt.NewAssignMatch("active", a.Active)
|
|
}
|
|
|
|
func (s *StartLTEMatch) New() Matcher {
|
|
return pageopt.NewLTEMatch("start", s.t)
|
|
}
|
|
|
|
func (e *EndGTMatch) New() Matcher {
|
|
return pageopt.NewGTMatch("end", e.t)
|
|
}
|
|
|
|
var (
|
|
Sort_createdAt_N1 = Sort{{Key: "createdAt", Value: -1}}
|
|
Sort_sortCode_N1 = Sort{{Key: "sortCode", Value: -1}}
|
|
)
|
|
|
|
func List(sort Sort, skip, limit *int64, matchs ...Matcher) (AdsSlice, error) {
|
|
filter := pageopt.MergeM(matchs)
|
|
opt := (&options.FindOptions{})
|
|
if len(sort) != 0 {
|
|
opt.SetSort(sort)
|
|
}
|
|
if skip != nil {
|
|
opt.SetSkip(*skip)
|
|
}
|
|
if limit != nil {
|
|
opt.SetLimit(*limit)
|
|
}
|
|
list := AdsSlice{}
|
|
err := coll(nil).Find(&list, filter, opt)
|
|
if err != nil {
|
|
log.Error("adsmod List error", log.E(err))
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func Count(matchs ...Matcher) (int64, error) {
|
|
filter := pageopt.MergeM(matchs)
|
|
count, err := coll(nil).Count(filter)
|
|
if err != nil {
|
|
log.Error("adsmod List error", log.E(err))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
func SumClickByID(matchs ...Matcher) (map[ObjectID]int64, error) {
|
|
filter := pageopt.MergeM(matchs)
|
|
var pipeline []bson.M
|
|
pipeline = append(pipeline, bson.M{"$match": filter})
|
|
pipeline = append(pipeline, bson.M{
|
|
"$group": bson.M{
|
|
"_id": "$_id",
|
|
"click": bson.M{"$sum": "$click"},
|
|
},
|
|
})
|
|
var list []struct {
|
|
ID ObjectID `bson:"_id"`
|
|
Click int64 `bson:"click"` //点击次数
|
|
}
|
|
err := coll(nil).Aggregate(&list, pipeline)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m := make(map[ObjectID]int64, len(list))
|
|
for _, v := range list {
|
|
m[v.ID] += v.Click
|
|
}
|
|
return m, err
|
|
}
|