init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
package playletcollectmod
|
||||
|
||||
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_collect 集合和常用查询索引
|
||||
func Init(ctx context.Context) {
|
||||
coll = mongos.Shop.Coll(TN.PlayletCollect)
|
||||
initIndex(ctx)
|
||||
}
|
||||
|
||||
func initIndex(ctx context.Context) {
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.PlayletCollect.UserId, Value: 1},
|
||||
{Key: FN.PlayletCollect.PlayletId, Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.PlayletCollect.PlayletId, Value: 1},
|
||||
{Key: FN.PlayletCollect.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.PlayletCollect.UserId, Value: 1},
|
||||
{Key: FN.PlayletCollect.CreatedAt, Value: -1},
|
||||
{Key: FN.PlayletCollect.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.PlayletCollect.CreatedAt, Value: 1},
|
||||
{Key: FN.PlayletCollect.PlayletId, Value: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(playletcollectmod:initIndex) 创建索引失败 err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOnePlayletCollect 插入一条短剧收藏关系
|
||||
func InsertOnePlayletCollect(ctx context.Context, doc *PlayletCollect) (bool, *srvdef.SrvErr) {
|
||||
if doc.Id == 0 {
|
||||
doc.Id = mongos.Lid.PlayletCollect.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
|
||||
}
|
||||
|
||||
// ExistsPlayletCollect 查询短剧收藏关系是否存在
|
||||
func ExistsPlayletCollect(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
||||
var doc PlayletCollect
|
||||
if err := coll.FindOneEx(ctx, &doc, filter, options.FindOne().SetProjection(bson.D{
|
||||
{Key: FN.PlayletCollect.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
|
||||
}
|
||||
|
||||
// DeleteOnePlayletCollect 删除一条短剧收藏关系
|
||||
func DeleteOnePlayletCollect(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
|
||||
}
|
||||
|
||||
// FindManyPlayletCollect 按条件查询短剧收藏关系列表
|
||||
func FindManyPlayletCollect(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletCollectList, *srvdef.SrvErr) {
|
||||
data := make(PlayletCollectList, 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
|
||||
}
|
||||
|
||||
// CountPlayletCollect 统计满足条件的短剧收藏关系数量
|
||||
func CountPlayletCollect(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
|
||||
}
|
||||
|
||||
// AggregatePlayletCollect 聚合查询短剧收藏关系
|
||||
func AggregatePlayletCollect(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
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package playletcollectmod
|
||||
|
||||
// TagPlayletCollect 保存 playlet_collect 集合字段名
|
||||
type TagPlayletCollect struct {
|
||||
Id string
|
||||
UserId string
|
||||
PlayletId string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
PlayletCollect TagPlayletCollect
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
PlayletCollect string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.PlayletCollect.Id = "_id"
|
||||
FN.PlayletCollect.UserId = "userId"
|
||||
FN.PlayletCollect.PlayletId = "playletId"
|
||||
FN.PlayletCollect.CreatedAt = "createdAt"
|
||||
FN.PlayletCollect.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.PlayletCollect = "playlet_collect"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package playletcollectmod
|
||||
|
||||
import "time"
|
||||
|
||||
// PlayletCollect @table: playlet_collect
|
||||
//
|
||||
// PlayletCollect 短剧收藏关系模型
|
||||
type PlayletCollect struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"` // 收藏记录ID
|
||||
UserId int64 `json:"userId" bson:"userId"` // 用户ID
|
||||
PlayletId int64 `json:"playletId" bson:"playletId"` // 短剧ID
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
// PlayletCollectList 短剧收藏关系列表
|
||||
type PlayletCollectList []*PlayletCollect
|
||||
Reference in New Issue
Block a user