Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package playletfavoritemod
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
const CollectionName = "playlet_favorite"
|
||||
|
||||
const (
|
||||
FieldID = "_id"
|
||||
FieldUserID = "userId"
|
||||
FieldPlayletID = "playletId"
|
||||
FieldCreatedAt = "createdAt"
|
||||
FieldUpdatedAt = "updatedAt"
|
||||
)
|
||||
|
||||
type PlayletFavorite struct {
|
||||
ID int64 `bson:"_id,omitempty"`
|
||||
UserID int64 `bson:"userId"`
|
||||
PlayletID int64 `bson:"playletId"`
|
||||
CreatedAt time.Time `bson:"createdAt"`
|
||||
UpdatedAt time.Time `bson:"updatedAt"`
|
||||
}
|
||||
|
||||
var coll *mongos.Collection
|
||||
|
||||
// Init 初始化短剧收藏集合及查询索引
|
||||
func Init(ctx context.Context) {
|
||||
coll = mongos.Shop.Coll(CollectionName)
|
||||
indexes := []mongo.IndexModel{
|
||||
{Keys: bson.D{{Key: FieldUserID, Value: 1}, {Key: FieldPlayletID, Value: 1}}, Options: options.Index().SetUnique(true)},
|
||||
{Keys: bson.D{{Key: FieldUserID, Value: 1}, {Key: FieldCreatedAt, Value: -1}, {Key: FieldID, Value: -1}}},
|
||||
{Keys: bson.D{{Key: FieldPlayletID, Value: 1}}},
|
||||
}
|
||||
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(playletfavoritemod:Init) Create Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Upsert 按用户和短剧写入收藏关系并返回是否新增
|
||||
func Upsert(ctx context.Context, userID, playletID int64, now time.Time) (bool, *srvdef.SrvErr) {
|
||||
filter := bson.M{FieldUserID: userID, FieldPlayletID: playletID}
|
||||
update := bson.M{"$set": bson.M{FieldUpdatedAt: now}, "$setOnInsert": bson.M{
|
||||
FieldID: mongos.Lid.PlayletFavorite.Get(), FieldUserID: userID, FieldPlayletID: playletID, FieldCreatedAt: now,
|
||||
}}
|
||||
result, err := coll.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
|
||||
if err != nil {
|
||||
log.Warn("playletfavoritemod:Upsert failed", log.Any("filter", filter), log.E(err))
|
||||
return false, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return result.UpsertedCount > 0, nil
|
||||
}
|
||||
|
||||
// Delete 删除收藏关系并返回是否实际删除
|
||||
func Delete(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
||||
deleted, err := coll.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("playletfavoritemod:Delete failed", log.Any("filter", filter), log.E(err))
|
||||
return false, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return deleted > 0, nil
|
||||
}
|
||||
|
||||
// Count 统计收藏关系数量
|
||||
func Count(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("playletfavoritemod:Count failed", log.Any("filter", filter), log.E(err))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindMany 查询收藏关系列表
|
||||
func FindMany(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]*PlayletFavorite, *srvdef.SrvErr) {
|
||||
list := make([]*PlayletFavorite, 0)
|
||||
if err := coll.Find(ctx, &list, filter, opts...); err != nil {
|
||||
log.Warn("playletfavoritemod:FindMany failed", log.Any("filter", filter), log.E(err))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// Aggregate 聚合收藏关系和短剧数据
|
||||
func Aggregate(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := coll.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("playletfavoritemod:Aggregate failed", log.Any("pipeline", pipeline), log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user