93 lines
3.5 KiB
Go
93 lines
3.5 KiB
Go
package videofavoritemod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/mongos"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var coll *mongos.Collection
|
|
|
|
// Init 初始化 video_favorite 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.VideoFavorite)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{Keys: bson.D{{Key: FN.VideoFavorite.UserID, Value: 1}, {Key: FN.VideoFavorite.VideoID, Value: 1}}, Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: FN.VideoFavorite.UserID, Value: 1}, {Key: FN.VideoFavorite.CreatedAt, Value: -1}, {Key: FN.VideoFavorite.Id, Value: -1}}},
|
|
{Keys: bson.D{{Key: FN.VideoFavorite.VideoID, Value: 1}}},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(videofavoritemod:initIndex) Create Index failed! err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// UpsertOneVideoFavorite 按用户ID和视频ID写入收藏关系,返回是否新增收藏
|
|
func UpsertOneVideoFavorite(ctx context.Context, userId, videoId int64, now time.Time) (bool, *srvdef.SrvErr) {
|
|
filter := bson.M{FN.VideoFavorite.UserID: userId, FN.VideoFavorite.VideoID: videoId}
|
|
update := bson.M{
|
|
"$set": bson.M{FN.VideoFavorite.UpdatedAt: now},
|
|
"$setOnInsert": bson.M{
|
|
FN.VideoFavorite.Id: mongos.Lid.VideoFavorite.Get(),
|
|
FN.VideoFavorite.UserID: userId,
|
|
FN.VideoFavorite.VideoID: videoId,
|
|
FN.VideoFavorite.CreatedAt: now,
|
|
},
|
|
}
|
|
result, err := coll.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
|
|
if err != nil {
|
|
log.Warn("videofavoritemod:UpsertOneVideoFavorite db.UpdateOne failed", log.Any("filter", filter), log.E(err))
|
|
return false, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
}
|
|
return result.UpsertedCount > 0, nil
|
|
}
|
|
|
|
// DeleteOneVideoFavorite 删除一条视频收藏关系,返回是否实际删除
|
|
func DeleteOneVideoFavorite(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
|
result, err := coll.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("videofavoritemod:DeleteOneVideoFavorite db.DeleteOne failed", log.Any("filter", filter), log.E(err))
|
|
return false, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
|
}
|
|
return result > 0, nil
|
|
}
|
|
|
|
// FindManyVideoFavorite 分页查询视频收藏关系
|
|
func FindManyVideoFavorite(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (VideoFavoriteList, *srvdef.SrvErr) {
|
|
data := make(VideoFavoriteList, 0)
|
|
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("videofavoritemod:FindManyVideoFavorite db.Find failed", log.Any("filter", filter), log.E(err))
|
|
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// CountVideoFavorite 统计视频收藏关系数量
|
|
func CountVideoFavorite(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
|
count, err := coll.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("videofavoritemod:CountVideoFavorite db.Count failed", log.Any("filter", filter), log.E(err))
|
|
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// AggregateVideoFavorite 聚合视频收藏数据
|
|
func AggregateVideoFavorite(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
|
if err := coll.Aggregate(ctx, bind, pipeline); err != nil {
|
|
log.Warn("videofavoritemod:AggregateVideoFavorite db.Aggregate failed", log.E(err), log.Any("pipeline", pipeline))
|
|
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return nil
|
|
}
|