@@ -0,0 +1,74 @@
|
||||
package adsclicklogmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.AdsClickLog
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
// InitIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "adsType", Value: 1}, {Key: "createdAt", 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))
|
||||
}
|
||||
}
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
type AdsClick struct {
|
||||
ID primitive.ObjectID `json:"-" bson:"_id,omitempty"`
|
||||
UID uint64 `json:"uid" bson:"uid"`
|
||||
ClickId primitive.ObjectID `json:"clickId" bson:"clickId"`
|
||||
ObjType string `json:"objType" bson:"objType"`
|
||||
AdsType int64 `json:"adsType" bson:"adsType"` // 0 默认; 1 金主楼凤广告
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
func AddClick(uid uint64, clickId primitive.ObjectID, objType string, adsType int64) error {
|
||||
now := time.Now()
|
||||
_, err := coll(nil).InsertOne(AdsClick{
|
||||
UID: uid,
|
||||
ClickId: clickId,
|
||||
ObjType: objType,
|
||||
AdsType: adsType,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func GetDailyClickCount(uid uint64) (uint64, error) {
|
||||
year, month, day := time.Now().Date()
|
||||
start := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
|
||||
end := time.Date(year, month, day+1, 0, 0, 0, 0, time.Local)
|
||||
res, err := coll(nil).Distinct("clickId", bson.M{"uid": uid, "adsType": 1, "createdAt": bson.M{"$gte": start, "$lt": end}})
|
||||
return uint64(len(res)), err
|
||||
}
|
||||
Reference in New Issue
Block a user