init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
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
|
||||
}
|
||||
|
||||
// FindManyPlayletLike 按条件查询短剧点赞关系列表
|
||||
func FindManyPlayletLike(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletLikeList, *srvdef.SrvErr) {
|
||||
list := make(PlayletLikeList, 0)
|
||||
if err := coll.Find(ctx, &list, filter, opts...); err != nil {
|
||||
log.Warn("查询短剧点赞关系列表失败", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// CountPlayletLike 统计短剧点赞关系数量
|
||||
func CountPlayletLike(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("统计短剧点赞关系数量失败", log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, 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
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package playletlikemod
|
||||
|
||||
// TagPlayletLike 保存 playlet_like 集合字段名
|
||||
type TagPlayletLike struct {
|
||||
Id string
|
||||
UserId string
|
||||
PlayletId string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
PlayletLike TagPlayletLike
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
PlayletLike string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.PlayletLike.Id = "_id"
|
||||
FN.PlayletLike.UserId = "userId"
|
||||
FN.PlayletLike.PlayletId = "playletId"
|
||||
FN.PlayletLike.CreatedAt = "createdAt"
|
||||
FN.PlayletLike.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.PlayletLike = "playlet_like"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package playletlikemod
|
||||
|
||||
import "time"
|
||||
|
||||
// PlayletLike @table: playlet_like
|
||||
//
|
||||
// PlayletLike 短剧点赞关系模型
|
||||
type PlayletLike 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"` // 修改时间
|
||||
}
|
||||
|
||||
// PlayletLikeList 短剧点赞关系列表
|
||||
type PlayletLikeList []*PlayletLike
|
||||
Reference in New Issue
Block a user