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()) }