Initial commit

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-15 13:57:10 +08:00
co-authored by Claude Opus 5
commit 8679200f41
1897 changed files with 257900 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
package useractmod
import (
"time"
"91porn-server/common/db"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const (
IsNoVip uint64 = 0
Begin string = "BEGIN"
)
// 用户行为记录
type UserAct struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
VID primitive.ObjectID `json:"vid" bson:"vid"` //视频ID
PlayWay uint64 `json:"playWay" bson:"playWay"` //观看方式 0:非vip方式观看 1:vip观看
UID uint64 `json:"uid" bson:"uid"` //用户id
AddPoint string `json:"addPoint" bson:"addPoint"` //开始计算恢复观看次数的标志 标识:Begin
DailyDate time.Time `json:"dailyDate" bson:"dailyDate"` //日期 整点日期
CreatedAt time.Time `json:"createAt" bson:"createdAt"`
ConsumeKey string `json:"-" bson:"consumeKey,omitempty"` // 每日免费观看视频消费幂等键
}
func Init() {
mdb = db.Init(table)
initIndex()
}
+135
View File
@@ -0,0 +1,135 @@
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
}