package tonerecomod 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" ) type ThemeType = string const ( MostLikes ThemeType = "mostLikes" //最多点赞 PlayAtMost ThemeType = "playAtMost" //最多播放 MostComments ThemeType = "mostComments" //最多评论 OfficialRecom ThemeType = "officialRecom" //官方推荐 GoldCoinArea ThemeType = "goldCoinArea" //金币专区 LatestUpload ThemeType = "latestUpload" //最新上传 FreeArea ThemeType = "freeArea" //免费专区 ) var mdb *db.MongoDB const table = models.ToneRecom func coll(t *db.MongoTool) *db.MongoTool { if t == nil { return mdb.Coll(table) } return t.Coll(table) } // InitIndex 设置index func initIndex() { many := []mongo.IndexModel{ { Keys: bson.D{{Key: "vid", Value: 1}}, Options: options.Index().SetUnique(true), }, { Keys: bson.D{{Key: "sortKey", 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]", models.ToneRecom, err)) } } // Insert ()插入消息记录 func InsertMany(offiRecoms []OffiRecomDoc) error { now := time.Now() docs := make([]interface{}, len(offiRecoms)) for i, offiRecom := range offiRecoms { offiRecom.CreatedAt = &now m, err := common.ToBsonM(offiRecom) if err != nil { return fmt.Errorf("tone OffiRecom InsertMany ToBsonM fail, error:%+v ", err) } docs[i] = m } if _, err := coll(nil).InsertMany(docs); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertMany", table, "InsertMany", err), log.Any("offiRecoms", offiRecoms), ) return err } return nil } // Delete Delete func DeleteMany(idArray []ObjectID) (err error) { if _, err = coll(nil).DeleteMany(M{"_id": M{"$in": idArray}}); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteMany", table, "DeleteMany", err), log.Any("idArray", idArray), ) return } return nil } func GetOffiRecomList(skip int64, limit int64, enable bool) ([]ObjectID, error) { opt := (&options.FindOptions{}). SetSort(D{{Key: "sortKey", Value: -1}}). SetSkip(skip). SetLimit(limit) filter := M{ "enable": enable, } offiRecomList := make([]OffiRecom, limit) if err := coll(nil).Find(&offiRecomList, filter, opt); err != nil { return nil, err } vidList := make([]ObjectID, len(offiRecomList)) for i, v := range offiRecomList { vidList[i] = v.VID } return vidList, nil }