321 lines
9.2 KiB
Go
321 lines
9.2 KiB
Go
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.CategoryIds, 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.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
{Key: FN.VideoInformation.CategoryIds, Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Video.FileId, Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{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.Sort, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
// /api/app/video/list 按 status 过滤,并按 addedTime/_id 倒序游标分页。
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Status, 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.MediaType, 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.CategoryIds, Value: 1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.CreatorId, Value: 1},
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "nodes.nodeId", Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
{Key: FN.VideoInformation.Real.WatchTimes, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
{Key: FN.VideoInformation.Sort, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.CreatorId, Value: 1},
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
{Key: FN.VideoInformation.Real.WatchTimes, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
{Key: FN.VideoInformation.Tags, Value: 1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
{Key: FN.VideoInformation.Real.WatchTimes, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Status, Value: 1},
|
|
{Key: FN.VideoInformation.Tags, 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.AddedTime, Value: -1},
|
|
{Key: FN.VideoInformation.IsTop, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.IsTop, Value: -1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Sort, Value: -1},
|
|
{Key: FN.VideoInformation.AddedTime, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.HevcStatus, Value: 1},
|
|
{Key: FN.VideoInformation.HevcFailCount, Value: 1},
|
|
{Key: FN.VideoInformation.Real.WatchTimes, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.HevcStatus, Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoInformation.Slug, 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) (
|
|
*VideoInformation, *srvdef.SrvErr) {
|
|
var VideoInformation VideoInformation
|
|
if err := db.FindOneEx(ctx, &VideoInformation, filter); 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
|
|
}
|
|
|
|
// FindOneVideoInformationWithOptions 按条件查询单条视频信息,支持projection等查询选项。
|
|
func FindOneVideoInformationWithOptions(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 {
|
|
log.Warn("VideoInformationmod:FindOneVideoInformationWithOptions db.FindOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
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, opts ...*options.CountOptions) (
|
|
int64, *srvdef.SrvErr) {
|
|
count, err := db.Count(ctx, filter, opts...)
|
|
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
|
|
}
|
|
|
|
// AggregateVideoInformations 聚合查询视频信息
|
|
func AggregateVideoInformations(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
|
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
|
log.Warn("videoinformationmod:AggregateVideoInformations failed", log.E(err))
|
|
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
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
|
|
}
|
|
|
|
// BulkWriteVideoInformation 批量写入或更新视频信息
|
|
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("视频信息批量写入或更新失败", log.E(err))
|
|
return nil, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
}
|
|
return result, nil
|
|
}
|