init: import web_huangguo2 server project
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package videocollectmod
|
||||
|
||||
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_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
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package videocollectmod
|
||||
|
||||
// TagVideoCollect 保存 video_collect 集合字段名
|
||||
type TagVideoCollect struct {
|
||||
Id string
|
||||
UserId string
|
||||
VideoId string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
VideoCollect TagVideoCollect
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
VideoCollect string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.VideoCollect.Id = "_id"
|
||||
FN.VideoCollect.UserId = "userId"
|
||||
FN.VideoCollect.VideoId = "videoId"
|
||||
FN.VideoCollect.CreatedAt = "createdAt"
|
||||
FN.VideoCollect.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.VideoCollect = "video_collect"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package videocollectmod
|
||||
|
||||
import "time"
|
||||
|
||||
// VideoCollect @table: video_collect
|
||||
//
|
||||
// VideoCollect 视频收藏关系模型
|
||||
type VideoCollect struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"` // 收藏记录ID
|
||||
UserId int64 `json:"userId" bson:"userId"` // 用户ID
|
||||
VideoId int64 `json:"videoId" bson:"videoId"` // 视频ID
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
// VideoCollectList 视频收藏关系列表
|
||||
type VideoCollectList []*VideoCollect
|
||||
Reference in New Issue
Block a user