111 lines
3.0 KiB
Go
111 lines
3.0 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},
|
|
{Key: FN.PlayletLike.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletLike.UserId, Value: 1},
|
|
{Key: FN.PlayletLike.CreatedAt, Value: -1},
|
|
{Key: FN.PlayletLike.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletLike.CreatedAt, Value: 1},
|
|
{Key: FN.PlayletLike.PlayletId, Value: 1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(playletlikemod:initIndex) 创建索引失败 err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// InsertOnePlayletLike 插入一条短剧点赞关系
|
|
func InsertOnePlayletLike(ctx context.Context, doc *PlayletLike) (bool, *srvdef.SrvErr) {
|
|
if doc.Id == 0 {
|
|
doc.Id = mongos.Lid.PlayletLike.Get()
|
|
}
|
|
now := time.Now()
|
|
if doc.CreatedAt.IsZero() {
|
|
doc.CreatedAt = now
|
|
}
|
|
doc.UpdatedAt = now
|
|
if _, err := coll.InsertOne(ctx, doc); err != nil {
|
|
if mongo.IsDuplicateKeyError(err) {
|
|
return false, nil
|
|
}
|
|
log.Warn("插入短剧点赞关系失败", log.Any("doc", doc), log.E(err))
|
|
return false, srvdef.New(srvdef.ErrDbInsertError, "", "")
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// ExistsPlayletLike 查询短剧点赞关系是否存在
|
|
func ExistsPlayletLike(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
|
var doc PlayletLike
|
|
if err := coll.FindOneEx(ctx, &doc, filter, options.FindOne().SetProjection(bson.D{
|
|
{Key: FN.PlayletLike.Id, Value: 1},
|
|
})); err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return false, nil
|
|
}
|
|
log.Warn("查询短剧点赞关系失败", log.E(err), log.Any("filter", filter))
|
|
return false, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// DeleteOnePlayletLike 删除一条短剧点赞关系
|
|
func DeleteOnePlayletLike(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
|
count, err := coll.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("删除短剧点赞关系失败", log.E(err), log.Any("filter", filter))
|
|
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
|
}
|
|
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("聚合短剧点赞关系失败", log.E(err), log.Any("pipeline", pipeline))
|
|
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return nil
|
|
}
|