44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package sectionstatmod
|
|
|
|
import (
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// IncSectionHits 增加专题每日点击量
|
|
func IncSectionHits(sectionID primitive.ObjectID, date string) error {
|
|
filter := bson.M{"sectionID": sectionID, "date": date}
|
|
_, err := coll(nil).UpdateOne(filter, bson.M{"$inc": bson.M{"hitsPerDay": 1}})
|
|
return err
|
|
}
|
|
|
|
// UpsertSectionHits upsert某一天专题点击量
|
|
func UpsertSectionHits(sectionID primitive.ObjectID, date string) error {
|
|
filter := bson.M{"sectionID": sectionID, "date": date}
|
|
now := time.Now()
|
|
update := bson.M{
|
|
"$set": bson.M{"sectionID": sectionID, "date": date, "updatedAt": now},
|
|
"$setOnInsert": bson.M{"createdAt": now},
|
|
"$inc": bson.M{"hitsPerDay": 1},
|
|
}
|
|
_, err := coll(nil).UpsertOne(filter, update)
|
|
return err
|
|
}
|
|
|
|
// ListSectionHits 按页检索专题统计数据
|
|
func ListSectionHits(req ListRequest) (resp ListResponse, err error) {
|
|
opts := options.Find().SetSkip(int64(req.PageSize * (req.PageNumber - 1))).SetLimit(int64(req.PageSize + 1)).
|
|
SetSort(bson.D{{Key: "date", Value: -1}})
|
|
if err = coll(nil).Find(&resp.List, bson.M{"sectionID": req.SectionID}, opts); err != nil {
|
|
return
|
|
}
|
|
if uint64(len(resp.List)) > req.PageSize {
|
|
resp.HasNext = true
|
|
resp.List = resp.List[:req.PageSize]
|
|
}
|
|
return
|
|
}
|