@@ -0,0 +1,117 @@
|
||||
package statvidtotalmod
|
||||
|
||||
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.VideoTotalStat
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "vid", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "playCount", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "payCount", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "payPlayCount", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "recordAt", 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, recordAt time.Time, incDocMap IncDocMap) error {
|
||||
writes := make([]mongo.WriteModel, len(incDocMap))
|
||||
i := 0
|
||||
for vid, doc := range incDocMap {
|
||||
filter := M{
|
||||
"vid": vid,
|
||||
}
|
||||
incM, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
update := M{
|
||||
"$setOnInsert": M{
|
||||
"vid": vid,
|
||||
"createdAt": time.Now(),
|
||||
},
|
||||
"$set": M{
|
||||
"updatedAt": time.Now(),
|
||||
"recordAt": recordAt,
|
||||
},
|
||||
}
|
||||
if len(incM) != 0 {
|
||||
update["$inc"] = incM
|
||||
}
|
||||
writes[i] = mongo.NewUpdateOneModel().
|
||||
SetFilter(filter).
|
||||
SetUpdate(update).
|
||||
SetUpsert(true)
|
||||
i++
|
||||
log.Debug(fmt.Sprintf("table:%s [ vid:%+v filter:%+v update:%+v ]\n", table, vid, filter, update))
|
||||
}
|
||||
if len(writes) == 0 {
|
||||
return nil
|
||||
}
|
||||
//bulkWrite 不是原子操作 不具备事务性
|
||||
opt := (&options.BulkWriteOptions{}).SetOrdered(false) //设为无序,触发并行写,提升写效率
|
||||
_, err := coll(trans).Bulk(writes, opt)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetPayCountMap(vidList []ObjectID) (map[ObjectID]int64, error) {
|
||||
if len(vidList) == 0 {
|
||||
return make(map[ObjectID]int64), nil
|
||||
}
|
||||
filter := M{
|
||||
"vid": M{"$in": vidList},
|
||||
}
|
||||
list := []struct {
|
||||
Vid ObjectID `bson:"vid"`
|
||||
PayCount int64 `bson:"payCount"` //购买次数
|
||||
}{}
|
||||
if err := coll(nil).Find(&list, filter); err != nil {
|
||||
return make(map[ObjectID]int64), nil
|
||||
}
|
||||
m := make(map[ObjectID]int64, len(list))
|
||||
for _, v := range list {
|
||||
m[v.Vid] = v.PayCount
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
Reference in New Issue
Block a user