来源:hgdjnet-server 9bfbafe10d417deea959bd53117bc88a10e60a83 项目身份 HKDJ / JHA-216,独立官网仓库,Jenkins 守卫键 hkdjnet 测试目录 /server/hkdj_net,App/Web API 30331/30332 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
122 lines
3.6 KiB
Go
122 lines
3.6 KiB
Go
package videocollectmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"hkdj-web-server/pub/common/log"
|
|
"hkdj-web-server/pub/common/srvdef"
|
|
"hkdj-web-server/pub/mongos"
|
|
"time"
|
|
|
|
"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_collect 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.VideoCollect)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoCollect.UserId, Value: 1},
|
|
{Key: FN.VideoCollect.VideoId, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoCollect.VideoId, Value: 1},
|
|
{Key: FN.VideoCollect.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoCollect.UserId, Value: 1},
|
|
{Key: FN.VideoCollect.CreatedAt, Value: -1},
|
|
{Key: FN.VideoCollect.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.VideoCollect.CreatedAt, Value: 1},
|
|
{Key: FN.VideoCollect.VideoId, Value: 1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(videocollectmod:initIndex) 创建索引失败 err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// InsertOneVideoCollect 插入一条视频收藏关系
|
|
func InsertOneVideoCollect(ctx context.Context, doc *VideoCollect) (bool, *srvdef.SrvErr) {
|
|
if doc.Id == 0 {
|
|
doc.Id = mongos.Lid.VideoCollect.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
|
|
}
|
|
|
|
// ExistsVideoCollect 查询视频收藏关系是否存在
|
|
func ExistsVideoCollect(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
|
var doc VideoCollect
|
|
if err := coll.FindOneEx(ctx, &doc, filter, options.FindOne().SetProjection(bson.D{
|
|
{Key: FN.VideoCollect.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
|
|
}
|
|
|
|
// DeleteOneVideoCollect 删除一条视频收藏关系
|
|
func DeleteOneVideoCollect(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
|
|
}
|
|
|
|
// FindManyVideoCollect 按条件查询视频收藏关系列表
|
|
func FindManyVideoCollect(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (VideoCollectList, *srvdef.SrvErr) {
|
|
data := make(VideoCollectList, 0)
|
|
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("查询视频收藏关系列表失败", log.E(err), log.Any("filter", filter))
|
|
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// CountVideoCollect 统计满足条件的视频收藏关系数量
|
|
func CountVideoCollect(ctx context.Context, filter bson.M, opts ...*options.CountOptions) (int64, *srvdef.SrvErr) {
|
|
count, err := coll.Count(ctx, filter, opts...)
|
|
if err != nil {
|
|
log.Warn("统计视频收藏关系数量失败", log.E(err), log.Any("filter", filter))
|
|
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|