84 lines
3.1 KiB
Go
84 lines
3.1 KiB
Go
package playletlikemod
|
|
|
|
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 初始化 playlet_like 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.PlayletLike)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{Keys: bson.D{{Key: FN.PlayletLike.UserID, Value: 1}, {Key: FN.PlayletLike.PlayletID, Value: 1}}, Options: options.Index().SetUnique(true)},
|
|
{Keys: bson.D{{Key: FN.PlayletLike.PlayletID, Value: 1}}},
|
|
{Keys: bson.D{{Key: FN.PlayletLike.CreatedAt, Value: -1}, {Key: FN.PlayletLike.PlayletID, Value: 1}}},
|
|
{Keys: bson.D{{Key: FN.PlayletLike.UserID, Value: 1}, {Key: FN.PlayletLike.CreatedAt, Value: -1}, {Key: FN.PlayletLike.Id, Value: -1}}},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(playletlikemod:initIndex) Create Index failed! err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// UpsertOnePlayletLike 按用户ID和短剧ID写入点赞关系,返回是否新增点赞
|
|
func UpsertOnePlayletLike(ctx context.Context, userId, playletId int64, now time.Time) (bool, *srvdef.SrvErr) {
|
|
filter := bson.M{FN.PlayletLike.UserID: userId, FN.PlayletLike.PlayletID: playletId}
|
|
update := bson.M{
|
|
"$set": bson.M{FN.PlayletLike.UpdatedAt: now},
|
|
"$setOnInsert": bson.M{
|
|
FN.PlayletLike.Id: mongos.Lid.PlayletLike.Get(),
|
|
FN.PlayletLike.UserID: userId,
|
|
FN.PlayletLike.PlayletID: playletId,
|
|
FN.PlayletLike.CreatedAt: now,
|
|
},
|
|
}
|
|
result, err := coll.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
|
|
if err != nil {
|
|
log.Warn("playletlikemod:UpsertOnePlayletLike db.UpdateOne failed", log.Any("filter", filter), log.E(err))
|
|
return false, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
}
|
|
return result.UpsertedCount > 0, nil
|
|
}
|
|
|
|
// DeleteOnePlayletLike 删除一条短剧点赞关系,返回是否实际删除
|
|
func DeleteOnePlayletLike(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
|
result, err := coll.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("playletlikemod:DeleteOnePlayletLike db.DeleteOne failed", log.Any("filter", filter), log.E(err))
|
|
return false, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
|
}
|
|
return result > 0, nil
|
|
}
|
|
|
|
// CountPlayletLike 统计短剧点赞关系数量
|
|
func CountPlayletLike(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
|
count, err := coll.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("playletlikemod:CountPlayletLike db.Count failed", log.Any("filter", filter), log.E(err))
|
|
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// AggregatePlayletLike 聚合查询短剧点赞关系
|
|
func AggregatePlayletLike(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
|
if err := coll.Aggregate(ctx, bind, pipeline); err != nil {
|
|
log.Warn("playletlikemod:AggregatePlayletLike db.Aggregate failed", log.Any("pipeline", pipeline), log.E(err))
|
|
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return nil
|
|
}
|