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
+93
View File
@@ -0,0 +1,93 @@
package statvidmod
import (
"time"
"91porn-server/common/pageopt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Matcher = pageopt.Matcher
// SumDateMatch
type SumDateMatch struct {
SumDate *time.Time
}
func (s *SumDateMatch) New() Matcher {
return pageopt.NewAssignMatch("sumDate", s.SumDate)
}
// SumDateGTEAndLTMatch
type SumDateGTEAndLTMatch struct {
GTE *time.Time
LT *time.Time
}
func (s *SumDateGTEAndLTMatch) New() Matcher {
return pageopt.NewGTEAndLTMatch("sumDate", s.GTE, s.LT)
}
// PublisherIDMatch
type PublisherIDMatch struct {
UID *uint64
}
func (p *PublisherIDMatch) New() Matcher {
return pageopt.NewAssignMatch("publisherID", p.UID)
}
// PayCountGTMatch
type PayCountGTMatch struct {
GT *int64
}
func (p *PayCountGTMatch) New() Matcher {
return pageopt.NewGTMatch("payCount", p.GT)
}
// PlayCountGTMatch
type PlayCountGTMatch struct {
GT *int64
}
func (p *PlayCountGTMatch) New() Matcher {
return pageopt.NewGTMatch("playCount", p.GT)
}
// VidMatch
type VidMatch struct {
Vid *ObjectID
}
func (p *VidMatch) New() Matcher {
return pageopt.NewAssignMatch("vid", p.Vid)
}
// VidInMatch
type VidInMatch struct {
Vids []ObjectID
}
func (p *VidInMatch) New() Matcher {
return pageopt.NewInMatch("vid", p.Vids)
}
func List(sort bson.D, skip, limit int64, matchs ...Matcher) ([]Stat, error) {
opt := &options.FindOptions{}
if len(sort) != 0 {
opt.SetSort(sort)
}
opt.SetSkip(skip)
opt.SetLimit(limit)
filter := pageopt.MergeM(matchs)
list := make([]Stat, 0, limit)
return list, coll(nil).Find(&list, filter, opt)
}
func Count(matchs ...Matcher) (int64, error) {
filter := pageopt.MergeM(matchs)
return coll(nil).Count(filter)
}
+128
View File
@@ -0,0 +1,128 @@
package statvidmod
import (
"fmt"
"time"
"91porn-server/common/log"
"91porn-server/common/maths"
"91porn-server/models/v/usermod"
"91porn-server/models/v/vidmod"
"91porn-server/web/vidhelp"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Video struct {
ID *primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Title *string `json:"title" bson:"title,omitempty"` //视频标题
PublisherID *uint64 `json:"publisherID" bson:"publisherID,omitempty"` //上传者ID
PublisherName *string `json:"publisherName"` //上传者姓名
Day *float64 `json:"day"` //售卖天数
Coins *int64 `json:"coins" bson:"coins,omitempty"` //定价
}
type VideoStatRecord struct {
SumDate *time.Time `json:"sumDate" bson:"sumDate,omitempty"`
Video Video `json:"video" bson:"video"` //视屏
Income *int64 `json:"income" bson:"income,omitempty"` //当日收入
Tax *float64 `json:"tax" bson:"tax,omitempty"` //税
PayCount *int64 `json:"payCount" bson:"payCount,omitempty"` //购买数
CreatedAt *time.Time `json:"createdAt" bson:"createdAt,omitempty"` //日志创建时间
}
type VideoStatExport struct {
SumDate time.Time `json:"sumDate" bson:"sumDate,omitempty"`
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Title string `json:"title" bson:"title,omitempty"` //视频标题
PublisherID uint64 `json:"publisherID" bson:"publisherID,omitempty"` //上传者ID
Coins int64 `json:"coins" bson:"coins,omitempty"` //定价
Income int64 `json:"income" bson:"income,omitempty"` //当日收入
Tax float64 `json:"tax" bson:"tax,omitempty"` //税
PayCount int64 `json:"payCount" bson:"payCount,omitempty"` //购买数
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //日志创建时间
}
type VideoStatPage struct {
Total int64 `json:"total" bson:"total"`
List []VideoStatRecord `json:"list" bson:"list"`
}
var payCountMin int64 = 0
// VideoStatPages
func VideoStatPages(skip, limit int64,
sumDateGTEAndLTMatch SumDateGTEAndLTMatch,
vidMatch VidMatch,
vidInMatch VidInMatch,
) (VideoStatPage, error) {
matList := []Matcher{
(&PayCountGTMatch{&payCountMin}).New(),
sumDateGTEAndLTMatch.New(),
vidMatch.New(),
vidInMatch.New(),
}
sort := bson.D{{Key: "sumDate", Value: -1}}
statList, err := List(sort, skip, limit, matList...)
if err != nil {
return VideoStatPage{}, err
}
vidList := make([]ObjectID, len(statList))
for i, v := range statList {
vidList[i] = v.Vid
}
videoMap, err := vidmod.VideoMap(vidList)
if err != nil {
return VideoStatPage{}, err
}
//用户id
statListLen := len(statList)
userIds := make([]uint64, 0, statListLen)
recordList := make([]VideoStatRecord, statListLen)
for i, stat := range statList {
_stat := stat
vid := _stat.Vid
videoInfo := Video{
ID: &vid,
}
if video, ok := videoMap[vid]; ok {
videoInfo.Title = &video.Title
videoInfo.PublisherID = &video.PublisherID
videoInfo.Coins = &video.Coins
d := vidhelp.CalculateVideoSellDays(video.ReviewAt)
videoInfo.Day = &d
userIds = append(userIds, video.PublisherID)
}
tax := maths.ToFloat64_b2(_stat.Tax)
recordList[i] = VideoStatRecord{
SumDate: &_stat.SumDate,
Video: videoInfo,
Income: &_stat.Income,
Tax: &tax,
PayCount: &_stat.PayCount,
CreatedAt: &_stat.CreatedAt,
}
}
//查询用户信息
userMap, err := usermod.UserMap(userIds)
if err == nil {
for _, r := range recordList {
if r.Video.PublisherID == nil {
log.Warn(fmt.Sprintf("视频[%v]信息异常==>缺少上传者ID", r.Video.ID))
continue
}
if u, ok := userMap[*r.Video.PublisherID]; ok {
r.Video.PublisherName = &u.Name
}
}
}
count, err := Count(matList...)
if err != nil {
return VideoStatPage{}, err
}
return VideoStatPage{
Total: count,
List: recordList,
}, nil
}
+134
View File
@@ -0,0 +1,134 @@
package statvidmod
import (
"fmt"
"time"
"91porn-server/common"
"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.VideoStat
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: "vid", Value: 1}},
},
{
Keys: bson.D{{Key: "sumDate", Value: -1}, {Key: "vid", Value: 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{Key: "playCount", Value: -1}},
Options: options.Index().SetSparse(true),
},
{
Keys: bson.D{{Key: "createdAt", Value: -1}},
},
{
Keys: bson.D{{Key: "updatedAt", Value: -1}},
},
{
Keys: bson.D{{Key: "recordAt", Value: -1}},
},
{
Keys: bson.D{{Key: "sumDate", Value: -1}, {Key: "playCount", Value: -1}},
},
}
if _, err := coll(nil).CreateIndex(many); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
}
}
func ChangeStatTrans(trans *db.MongoTool, sumDate time.Time, recordTime time.Time, statDocMap VideoStatDocMap) error {
writes := make([]mongo.WriteModel, len(statDocMap))
i := 0
for vid, statDoc := range statDocMap {
filter := M{
"sumDate": sumDate,
"vid": vid,
}
setM, err := common.ToBsonM(statDoc.VideoStatSetDoc)
if err != nil {
return err
}
setM["recordAt"] = recordTime
setM["updatedAt"] = time.Now()
incM, err := common.ToBsonM(statDoc.VideoStatIncDoc)
if err != nil {
return err
}
update := M{
"$setOnInsert": M{
"sumDate": sumDate,
"vid": vid,
"createdAt": time.Now(),
},
}
if len(incM) != 0 {
update["$inc"] = incM
}
if len(setM) != 0 {
update["$set"] = setM
}
writes[i] = mongo.NewUpdateOneModel().
SetFilter(filter).
SetUpdate(update).
SetUpsert(true)
i++
log.Debug(fmt.Sprintf("sumDate:%s table:%s [ vid:%+v filter:%+v update:%+v ]\n", sumDate, table, vid, filter, update))
}
if len(writes) == 0 {
return nil
}
//bulkWrite 不是原子操作 不具备事务性
opt := (&options.BulkWriteOptions{}).SetOrdered(false) //设为无序,触发并行写,提升写效率
_, err := coll(trans).Bulk(writes, opt)
return err
}
// GetDailyPlayCountGroup
// @return vid List, vid->dailyPlayCount Map, error
func GetDailyPlayCountGroup(skip, limit int64) ([]ObjectID, map[ObjectID]int64, error) {
playCountMin := int64(0)
playCountGTMatch := PlayCountGTMatch{&playCountMin}
sort := D{{Key: "sumDate", Value: -1}, {Key: "playCount", Value: -1}}
list, err := List(sort, skip, limit, playCountGTMatch.New())
if err != nil {
return nil, nil, err
}
listLen := len(list)
vidList := make([]ObjectID, listLen)
countMap := make(map[ObjectID]int64, listLen)
for i, v := range list {
vidList[i] = v.Vid
countMap[v.Vid] = v.PlayCount
}
return vidList, countMap, nil
}
func SumByRecordAt(match RecordAtGTEAndLTMatch) ([]SumGroup, error) {
return Sum(match.New())
}
func DeleteBeforeCreatedAt(t *db.MongoTool, tm time.Time) error {
_, err := coll(t).DeleteMany(bson.M{"createdAt": bson.M{"$lt": tm}})
return err
}
+44
View File
@@ -0,0 +1,44 @@
package statvidmod
import (
"time"
"91porn-server/common/pageopt"
)
type SumGroup struct {
Vid ObjectID `bson:"_id"`
Income int64 `bson:"income"` //收入
PayCount int64 `bson:"payCount"` //购买次数
PlayCount int64 `bson:"playCount"` //播放次数
PayPlayCount int64 `bson:"payPlayCount"` //付费播放次数
Tax float64 `bson:"tax"` //[未完成]税
}
// PlayCountGTMatch
type RecordAtGTEAndLTMatch struct {
GTE *time.Time
LT *time.Time
}
func (r *RecordAtGTEAndLTMatch) New() Matcher {
return pageopt.NewGTEAndLTMatch("recordAt", r.GTE, r.LT)
}
func Sum(matchs ...Matcher) ([]SumGroup, error) {
filter := pageopt.MergeM(matchs)
pipeline := []M{}
pipeline = append(pipeline, M{"$match": filter})
pipeline = append(pipeline, M{
"$group": M{
"_id": "$vid",
"income": M{"$sum": "$income"},
"payCount": M{"$sum": "$payCount"},
"playCount": M{"$sum": "$playCount"},
"payPlayCount": M{"$sum": "$payPlayCount"},
"tax": M{"$sum": "$tax"},
},
})
list := []SumGroup{}
return list, coll(nil).Aggregate(&list, pipeline)
}
+58
View File
@@ -0,0 +1,58 @@
package statvidmod
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
ObjectID = primitive.ObjectID
Stat struct {
ID ObjectID `bson:"_id,omitempty"`
SumDate time.Time `bson:"sumDate"`
Vid ObjectID `bson:"vid"`
Income int64 `bson:"income"` //收入
PayCount int64 `bson:"payCount"` //购买次数
PlayCount int64 `bson:"playCount"` //播放次数
PayPlayCount int64 `bson:"payPlayCount"` //付费播放次数
Tax float64 `bson:"tax"` //税
RecordAt time.Time `bson:"recordAt"` //数据记录时间
CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
}
VideoStatIncDoc struct {
Income *int64 `bson:"income,omitempty"` //收入
PayCount *int64 `bson:"payCount,omitempty"` //购买次数
PlayCount *int64 `bson:"playCount,omitempty"` //播放次数
PayPlayCount *int64 `bson:"payPlayCount,omitempty"` //付费播放次数
Tax *float64 `bson:"tax,omitempty"` //税
}
VideoStatSetDoc struct {
//****
}
VideoStatDoc struct {
VideoStatIncDoc `bson:",inline"`
VideoStatSetDoc `bson:",inline"`
}
VideoStatDocMap = map[ObjectID]*VideoStatDoc
)
func Init() {
mdb = db.Init(table)
initIndex()
}