package videoinformationmod import ( "context" "videoweb/pub/common/log" "videoweb/pub/common/srvdef" "videoweb/pub/mongos" "fmt" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) // 表名 var db *mongos.Collection func Init(ctx context.Context) { // 获取数据库表 db = mongos.Shop.Coll(TN.VideoInformation) // 获取数据库表 indexes := []mongo.IndexModel{ { Keys: bson.D{ {Key: FN.VideoInformation.AddedTime, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Tags, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.CreatorId, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.IndexNow, Value: 1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.CreatedAt, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.UpdatedAt, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Video.FileId, Value: 1}, }, }, { Keys: bson.D{ {Key: "nodes.nodeId", Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Id, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.HeatValue, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.IsRecommend, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, {Key: FN.VideoInformation.Real.WeekWatchs, Value: -1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.CategoryId, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.IsRecommend, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, {Key: FN.VideoInformation.Real.WeekWatchs, Value: -1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.CategoryId, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.UpdatedAt, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.CategoryId, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, {Key: FN.VideoInformation.Fake.WatchTimes, Value: -1}, {Key: FN.VideoInformation.UpdatedAt, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.MediaType, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.MediaType, Value: 1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Tags, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.IsRecommend, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, {Key: FN.VideoInformation.Real.WeekWatchs, Value: -1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Tags, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Tags, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.CreatorId, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.AddedTime, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.MonthWatchs, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.IsTop, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.ActressIds, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.ActressIds, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.ActressIds, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.ActressIds, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.AddedTime, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.ActressIds, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.VideoCount, Value: -1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.ActressIds, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.Likes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.ActressIds, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.Likes, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.CategoryId, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.Likes, Value: -1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, {Key: FN.VideoInformation.Fake.WatchTimes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.Likes, Value: -1}, {Key: FN.VideoInformation.Fake.Likes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Sort, Value: -1}, {Key: FN.VideoInformation.Real.WeekWatchs, Value: -1}, {Key: FN.VideoInformation.AddedTime, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.UpdatedAt, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.AddedTime, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: 1}, {Key: FN.VideoInformation.Fake.WatchTimes, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.Likes, Value: 1}, {Key: FN.VideoInformation.Fake.Likes, Value: 1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.HevcStatus, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.Real.WatchTimes, Value: -1}, {Key: FN.VideoInformation.Fake.WatchTimes, Value: -1}, {Key: FN.VideoInformation.UpdatedAt, Value: -1}, {Key: FN.VideoInformation.Id, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.VideoInformation.HevcStatus, Value: 1}, {Key: FN.VideoInformation.Status, Value: 1}, {Key: FN.VideoInformation.UpdatedAt, Value: -1}, }, }, } if _, err := db.CreateIndex(ctx, indexes); err != nil { panic(fmt.Sprintf("(channelmod:initVideoInformationIndex) Create VideoInformation Index failed! err:%+v:", err)) } } // InsertOneVideoInformation 插入一个渠道统计 func InsertOneVideoInformation(ctx context.Context, qa *VideoInformation) *srvdef.SrvErr { if _, err := db.InsertOne(ctx, qa); err != nil { // log.Warn("VideoInformationmod:InsertOneVideoInformation db.InsertOne failed", log.E(err)) return srvdef.New(srvdef.ErrDbInsertError, "", "") } return nil } func InsertManyVideoInformation(ctx context.Context, qa []*VideoInformation) *srvdef.SrvErr { if _, err := db.InsertMany(ctx, &qa); err != nil { return srvdef.New(srvdef.ErrDbInsertError, "", "") } return nil } // DelOneVideoInformation 删除一个渠道统计 func DelOneVideoInformation(ctx context.Context, filter bson.M) ( int64, *srvdef.SrvErr) { count, err := db.DeleteOne(ctx, filter) if err != nil { log.Warn("VideoInformationmod:DelOneVideoInformation db.DeleteOne failed", log.E(err), log.Any("filter", filter)) return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "") } return count, nil } // FindOneVideoInformation 查找一个渠道统计 func FindOneVideoInformation(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) ( *VideoInformation, *srvdef.SrvErr) { var VideoInformation VideoInformation if err := db.FindOneEx(ctx, &VideoInformation, filter, opts...); err != nil { if err != mongo.ErrNoDocuments { log.Warn("VideoInformationmod:FindOneVideoInformation db.FindOne failed", log.E(err), log.Any("filter", filter)) return nil, srvdef.New(srvdef.ErrDbQueryError, "", "") } return nil, srvdef.New(srvdef.ErrNoData, "", "") } return &VideoInformation, nil } func FindManyVideoInformations(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (VideoInformationList, *srvdef.SrvErr) { data := make(VideoInformationList, 0) if err := db.Find(ctx, &data, filter, opts...); nil != err { log.Warn("faq:FindManyVideoInformations", log.E(err), log.Any("filter", filter)) return nil, srvdef.New(srvdef.ErrDbQuery.Code, "", "") } return data, nil } // CountVideoInformations 统计渠道统计数量 func CountVideoInformations(ctx context.Context, filter bson.M) ( int64, *srvdef.SrvErr) { count, err := db.Count(ctx, filter) if err != nil { log.Warn("faqmod:CountVideoInformations db.Count failed", log.E(err), log.Any("filter", filter)) return 0, srvdef.New(srvdef.ErrDbQueryError, "", "") } return count, nil } // UpdateOneVideoInformation 更新一个渠道统计 func UpdateOneVideoInformation(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr { result, err := db.UpdateOne(ctx, filter, update) if err != nil { log.Warn("post:UpdateOneVideoInformation db.UpdateOne failed", log.E(err), log.Any("filter", filter)) return srvdef.New(srvdef.ErrDbUpdateError, "", "") } if result.MatchedCount != 1 { log.Warn("post:UpdateOneVideoInformation db.UpdateOne failed", log.E(err), log.Any("filter", filter)) return srvdef.New(srvdef.ErrNoData, "", "") } return nil } func UpdateManyVideoInformation(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr { result, err := db.UpdateMany(ctx, filter, update) if err != nil { log.Warn("post:UpdateManyVideoInformation db.UpdateOne failed", log.E(err), log.Any("filter", filter)) return srvdef.New(srvdef.ErrDbUpdateError, "", "") } if result.MatchedCount < 1 { log.Warn("post:UpdateManyVideoInfomation db.UpdateOne failed", log.E(err), log.Any("filter", filter)) return srvdef.New(srvdef.ErrNoData, "", "") } return nil } func BulkWriteVideoInformation(ctx context.Context, docs []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, *srvdef.SrvErr) { result, err := db.BulkWrite(ctx, docs, opts...) if err != nil { log.Warn("post:BulkWriteVideoInformation db.BulkWrite failed", log.E(err)) return nil, srvdef.New(srvdef.ErrDbUpdateError, "", "") } return result, nil } // AggregateVideoInformations 聚合查询视频信息 func AggregateVideoInformations(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr { if err := db.Aggregate(ctx, bind, pipeline); err != nil { log.Warn("VideoInformation:AggregateVideoInformations db.Aggregate failed", log.E(err), log.Any("pipeline", pipeline)) return srvdef.New(srvdef.ErrDbQueryError, "", "") } return nil }