@@ -0,0 +1,43 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package sectionstatmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type SectionStat struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // id
|
||||
SectionID primitive.ObjectID `json:"sectionID" bson:"sectionID"` // 专题ID
|
||||
HitsPerday int64 `json:"hitsPerDay" bson:"hitsPerDay"` // 每日点击量
|
||||
Date string `json:"date" bson:"date"` // 统计日期
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` // 刷新时间
|
||||
}
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.SectionClick
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
// initIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "sectionID", Value: 1}, {Key: "date", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
type ListRequest struct {
|
||||
SectionID primitive.ObjectID `json:"sectionID" swaggertype:"string" binding:"required"` // 专题ID
|
||||
commod.Page
|
||||
}
|
||||
|
||||
type ListResponse struct {
|
||||
List []SectionStat // 统计列表
|
||||
HasNext bool // 下一页
|
||||
}
|
||||
Reference in New Issue
Block a user