136 lines
4.2 KiB
Go
136 lines
4.2 KiB
Go
package useractmod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"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.UserAct
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// initIndex 索引设置
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "vid", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "dailyDate", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "vid", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "addPoint", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "consumeKey", Value: 1}},
|
|
Options: options.Index().
|
|
SetName("uniq_daily_free_watch_consume").
|
|
SetUnique(true).
|
|
SetPartialFilterExpression(bson.M{"consumeKey": bson.M{"$gt": ""}}),
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("user model set index err ==>[%+v]", err))
|
|
}
|
|
}
|
|
|
|
// UserActUpsert 更新用户观看次数
|
|
func UserActUpsert(uid uint64, duration time.Time, watchCount int64) error {
|
|
filter := bson.M{"uid": uid, "dailyDate": duration}
|
|
update := bson.M{"$inc": bson.M{"addWatchCount": watchCount}, "$set": bson.M{"addUpdatedAt": time.Now()}}
|
|
if _, err := coll(nil).UpsertOne(filter, update); err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UserActUpsert", table, "UpsertOne", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UserActInsert 生成用户观看记录
|
|
func UserActInsert(act UserAct) error {
|
|
return UserActInsertTrans(nil, act)
|
|
}
|
|
|
|
// UserActInsertTrans 生成用户观看记录,支持复用Mongo事务。
|
|
func UserActInsertTrans(t *db.MongoTool, act UserAct) error {
|
|
if _, err := coll(t).InsertOne(act); err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UserActInsert", table, "InsertOne", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ActCount 查看用户观看次数 只统计通过非vip观看的次数
|
|
func ActCount(uid uint64, duration time.Time) int64 {
|
|
filter := bson.M{"uid": uid, "dailyDate": duration, "playWay": IsNoVip}
|
|
cnt, err := coll(nil).Count(filter)
|
|
if err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ActCount", table, "Count", err))
|
|
return 0
|
|
}
|
|
return cnt
|
|
}
|
|
|
|
// IsViewTodayByNoVip
|
|
func IsViewTodayByNoVip(uid uint64, vid primitive.ObjectID, duration time.Time) bool {
|
|
result, err := IsViewTodayByNoVipTrans(nil, uid, vid, duration)
|
|
if err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IsViewTodayByNoVip", table, "Exists", err))
|
|
return false
|
|
}
|
|
return result
|
|
}
|
|
|
|
// IsViewTodayByNoVipTrans 查询当天是否已通过非VIP次数观看,支持复用Mongo事务。
|
|
func IsViewTodayByNoVipTrans(t *db.MongoTool, uid uint64, vid primitive.ObjectID, duration time.Time) (bool, error) {
|
|
filter := bson.M{"uid": uid, "dailyDate": duration, "vid": vid, "playWay": IsNoVip}
|
|
return coll(t).Exists(filter)
|
|
}
|
|
|
|
// UserActFindByUid
|
|
func UserActFindByUid(uid uint64, duration time.Time) (u *UserAct, err error) {
|
|
filter := bson.M{"uid": uid, "dailyDate": duration}
|
|
if err = coll(nil).FindOne(&u, filter); err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UserActUpsert", table, "UpsertOne", err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// UserActFindByUidDesc
|
|
func UserActFindByUidDesc(uid uint64, duration time.Time) (u []*UserAct, err error) {
|
|
filter := bson.M{"uid": uid, "dailyDate": duration, "addPoint": Begin}
|
|
opt := options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetLimit(1)
|
|
if err = coll(nil).Find(&u, filter, opt); err != nil {
|
|
log.ZapLog.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UserActFindByUidDesc", table, "FindOne", err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func DeleteBeforeDailyDate(t *db.MongoTool, tm time.Time) error {
|
|
_, err := coll(t).DeleteMany(bson.M{"dailyDate": bson.M{"$lt": tm}})
|
|
return err
|
|
}
|