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 statusermod
import (
"91porn-server/common/pageopt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Matcher = pageopt.Matcher
// ItemMatch
type ItemMatch struct {
Item Itemtype
}
func (t *ItemMatch) New() Matcher {
return pageopt.NewAssignMatch("item", t.Item)
}
func List(sort bson.D, skip, limit int64, matchers ...Matcher) ([]UserStat, error) {
filter := pageopt.MergeM(matchers)
opt := &options.FindOptions{}
if len(sort) != 0 {
opt.SetSort(sort)
}
opt.SetSkip(skip)
opt.SetLimit(limit)
list := make([]UserStat, 0, limit)
return list, coll(nil).Find(&list, filter, opt)
}
+47
View File
@@ -0,0 +1,47 @@
package statusermod
import (
"time"
"91porn-server/common/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type (
M = bson.M
D = bson.D
Itemtype string
)
const (
VidIncome Itemtype = "vidIncome" //视频收益
UploadCount Itemtype = "uploadCount" //上传数量
)
type UserStat struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
UID uint64 `bson:"uid"`
Item Itemtype `bson:"item"`
Count int64 `bson:"count"`
RecordAt time.Time `bson:"recordAt"` //数据记录时间
UpdatedAt time.Time `bson:"updatedAt"` //文档更新时间
CreatedAt time.Time `bson:"createdAt"` //文档创建时间
}
type SetDoc struct {
UID uint64 `bson:"uid"`
Item Itemtype `bson:"item"`
Count int64 `bson:"count"`
RecordAt time.Time `bson:"recordAt"` //数据记录时间
}
type IncDoc SetDoc
func Init() {
mdb = db.Init(table)
initIndex()
}
+141
View File
@@ -0,0 +1,141 @@
package statusermod
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/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mdb *db.MongoDB
const table = models.UserStat
func coll(t *db.MongoTool) *db.MongoTool {
if t == nil {
return mdb.Coll(table)
}
return t.Coll(table)
}
// initIndex 初始化索引
func initIndex() {
many := []mongo.IndexModel{
{
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "item", Value: 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{Key: "uid", Value: -1}},
},
{
Keys: bson.D{{Key: "item", Value: -1}},
},
{
Keys: bson.D{{Key: "count", Value: -1}},
},
{
Keys: bson.D{{Key: "recordAt", Value: -1}},
},
{
Keys: bson.D{{Key: "updatedAt", Value: -1}},
},
{
Keys: bson.D{{Key: "createdAt", Value: -1}},
},
}
if _, err := coll(nil).CreateIndex(many); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
}
}
func ChangeIncStatTrans(trans *db.MongoTool, incDocList []IncDoc) error {
// create the slice of write models
writes := make([]mongo.WriteModel, len(incDocList))
for i, incDoc := range incDocList {
filter := M{
"uid": incDoc.UID,
"item": incDoc.Item,
}
update := M{
"$setOnInsert": M{
"uid": incDoc.UID,
"item": incDoc.Item,
"createdAt": time.Now(),
},
"$inc": M{
"count": incDoc.Count, //设计为统计表keywordStat聚合word得到
},
"$set": M{
"updatedAt": time.Now(),
"recordAt": incDoc.RecordAt,
},
}
writes[i] = mongo.NewUpdateOneModel().
SetFilter(filter).
SetUpdate(update).
SetUpsert(true)
log.Debug(fmt.Sprintf("table:%s [ filter:%+v update:%+v ]\n", table, filter, update))
}
if len(writes) == 0 {
return nil
}
//bulkWrite 不是原子操作 不具备事务性
opt := (&options.BulkWriteOptions{}).SetOrdered(false) //设为无序,触发并行写,提升写效率
_, err := coll(trans).Bulk(writes, opt)
return err
}
func ChangeSetStatTrans(trans *db.MongoTool, setDocList []SetDoc) error {
// create the slice of write models
writes := make([]mongo.WriteModel, len(setDocList))
for i, setDoc := range setDocList {
filter := M{
"uid": setDoc.UID,
"item": setDoc.Item,
}
update := M{
"$setOnInsert": M{
"uid": setDoc.UID,
"item": setDoc.Item,
"createdAt": time.Now(),
},
"$set": M{
"count": setDoc.Count,
"updatedAt": time.Now(),
"recordAt": setDoc.RecordAt,
},
}
writes[i] = mongo.NewUpdateOneModel().
SetFilter(filter).
SetUpdate(update).
SetUpsert(true)
log.Debug(fmt.Sprintf("table:%s [ filter:%+v update:%+v ]\n", table, filter, update))
}
if len(writes) == 0 {
return nil
}
//bulkWrite 不是原子操作 不具备事务性
opt := (&options.BulkWriteOptions{}).SetOrdered(false) //设为无序,触发并行写,提升写效率
_, err := coll(trans).Bulk(writes, opt)
return err
}
func ListByVidIncome(limit int64) ([]UserStat, error) {
sort := bson.D{{Key: "count", Value: -1}}
itemMatch := ItemMatch{VidIncome}
return List(sort, 0, limit, itemMatch.New())
}
func ListByUploadCount(limit int64) ([]UserStat, error) {
sort := bson.D{{Key: "count", Value: -1}}
itemMatch := ItemMatch{UploadCount}
return List(sort, 0, limit, itemMatch.New())
}