init: import web_huangguo2 server project
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
package commentmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"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 db *mongos.Collection
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
// 获取数据库表
|
||||
db = mongos.Shop.Coll(TN.Comment)
|
||||
// 获取数据库表
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.ObjectId, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.Id, Value: 1},
|
||||
{Key: FN.Comment.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.ObjectId, Value: 1},
|
||||
{Key: FN.Comment.ReviewStatus, Value: 1},
|
||||
{Key: FN.Comment.Level, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.ObjectId, Value: 1},
|
||||
{Key: FN.Comment.ObjectType, Value: 1},
|
||||
{Key: FN.Comment.ReviewStatus, Value: 1},
|
||||
{Key: FN.Comment.ParentsId, Value: 1},
|
||||
{Key: FN.Comment.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.UserId, Value: 1},
|
||||
{Key: FN.Comment.ObjectType, Value: 1},
|
||||
{Key: FN.Comment.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.ObjectId, Value: 1},
|
||||
{Key: FN.Comment.ObjectType, Value: 1},
|
||||
{Key: FN.Comment.ReviewStatus, Value: 1},
|
||||
{Key: FN.Comment.ParentsId, Value: 1},
|
||||
{Key: FN.Comment.CountLike, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.ParentsId, Value: 1},
|
||||
{Key: FN.Comment.Level, Value: 1},
|
||||
{Key: FN.Comment.ReviewStatus, Value: 1},
|
||||
{Key: FN.Comment.CreatedAt, Value: 1},
|
||||
{Key: FN.Comment.Id, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Comment.TopRank, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(comment:initCommentIndex) Create Comment Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneComment 插入一个评论
|
||||
func InsertOneComment(ctx context.Context, Comment *CommentModel) *srvdef.SrvErr {
|
||||
if _, err := db.InsertOne(ctx, Comment); err != nil {
|
||||
log.Warn("comment:InsertOneComment db.InsertOne failed", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelOneComment 删除一个评论
|
||||
func DelOneComment(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("comment:DelOneComment db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// DelManyComment 删除一个评论评论
|
||||
func DelManyComment(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteMany(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("comment:DelOneComment db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindOneComment 查找一个评论
|
||||
func FindOneComment(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) (
|
||||
*CommentModel, *srvdef.SrvErr) {
|
||||
var Comment CommentModel
|
||||
if err := db.FindOneEx(
|
||||
ctx, &Comment, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Info("comment:FindOneComment db.FindOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return &Comment, nil
|
||||
}
|
||||
|
||||
// CountComments 统计评论视频评论数量
|
||||
func CountComments(ctx context.Context, filter bson.M) (
|
||||
int64, *srvdef.SrvErr) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("comment:CountComments db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOneComment 更新一个评论
|
||||
func UpdateOneComment(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("comment:UpdateOneComment db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
log.Warn("comment:UpdateOneComment db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateManyComment 更新一批评论
|
||||
func UpdateManyComment(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := db.UpdateMMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("comment:UpdateManyComment db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount <= 0 {
|
||||
log.Warn("comment:UpdateManyComment db.UpdateMMany failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
//return srvdef.New(srvdef.ErrCommentNotExsit, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyComment 查找一批评论
|
||||
func FindManyComment(ctx context.Context, filter bson.M,
|
||||
opts ...*options.FindOptions) (CommentModelList, *srvdef.SrvErr) {
|
||||
data := make(CommentModelList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("comment:FindManyComment db.Find failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// InsertManyComment 插入一批用户
|
||||
func InsertManyComment(ctx context.Context, histrory *CommentModelList) *srvdef.SrvErr {
|
||||
if _, err := db.InsertMany(ctx, histrory); err != nil {
|
||||
log.Warn("comment:InsertManyComment db.InsertMany failed", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AggregateComment 聚合查找一批数据
|
||||
func AggregateComment(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := db.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("comment:AggregateComment db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ObjectAggregateComment 聚合查找一批数据
|
||||
func ObjectAggregateComment(ctx context.Context,
|
||||
bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := db.ObjectAggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("comment:AggregateComment db.Aggregate failed",
|
||||
log.E(err), log.Any("pipeline", pipeline))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Executable
+54
@@ -0,0 +1,54 @@
|
||||
package commentmod
|
||||
|
||||
type TagComment struct {
|
||||
CreatedAt string
|
||||
Id string
|
||||
Level string
|
||||
ObjectId string
|
||||
ParentsId string
|
||||
ReplyCommentId string
|
||||
ReplyUserName string
|
||||
Text string
|
||||
TopRank string
|
||||
UpdatedAt string
|
||||
UserId string
|
||||
UserName string
|
||||
UserAvatar string
|
||||
ReviewStatus string
|
||||
CommentCount string
|
||||
CountLike string
|
||||
Ip string
|
||||
ObjectName string
|
||||
ObjectType string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
Comment TagComment
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
Comment string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.Comment.CreatedAt = "createdAt"
|
||||
FN.Comment.Id = "_id"
|
||||
FN.Comment.Level = "level"
|
||||
FN.Comment.ObjectId = "objectId"
|
||||
FN.Comment.ParentsId = "parentsId"
|
||||
FN.Comment.ReplyCommentId = "replyCommentId"
|
||||
FN.Comment.ReplyUserName = "replyUserName"
|
||||
FN.Comment.Text = "text"
|
||||
FN.Comment.TopRank = "topRank"
|
||||
FN.Comment.UpdatedAt = "updatedAt"
|
||||
FN.Comment.UserId = "userId"
|
||||
FN.Comment.UserName = "userName"
|
||||
FN.Comment.UserAvatar = "userAvatar"
|
||||
FN.Comment.ReviewStatus = "reviewStatus"
|
||||
FN.Comment.Ip = "ip"
|
||||
FN.Comment.CommentCount = "commentCount"
|
||||
FN.Comment.CountLike = "countLike"
|
||||
FN.Comment.ObjectName = "objectName"
|
||||
FN.Comment.ObjectType = "objectType"
|
||||
TN.Comment = "_comment"
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package commentmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// 评论管理
|
||||
// @table:_comment
|
||||
type CommentModel struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"` // 评论ID
|
||||
Ip string `json:"ip" bson:"ip"` // 评论IP
|
||||
ObjectId int64 `json:"objectId" bson:"objectId"` // 对象ID
|
||||
ObjectType int64 `json:"objectType" bson:"objectType"` // 对象类型
|
||||
ObjectName string `json:"objectName" bson:"objectName"` // 对象名
|
||||
UserId int64 `json:"userId" bson:"userId"` // 评论用户ID
|
||||
UserName string `json:"userName" bson:"userName"` // 用户名称
|
||||
UserAvatar string `json:"userAvatar" bson:"userAvatar"` // 评论用户头像
|
||||
Level int64 `json:"level" bson:"level"` // 评论级别,1-一级评论 2-二级评论
|
||||
ParentsId int64 `json:"parentsId" bson:"parentsId"` // 上级评论ID,一级评论为0
|
||||
ReplyCommentId int64 `json:"replyCommentId" bson:"replyCommentId"` // 被回复评论ID,一级评论为0
|
||||
ReplyUserName string `json:"replyUserName" bson:"replyUserName"` // 被回复用户名称
|
||||
Text string `json:"text" bson:"text"` // 评论内容
|
||||
CommentCount int64 `json:"commentCount" bson:"commentCount"` // 该评论回复数量
|
||||
CountLike int64 `json:"countLike" bson:"countLike"` // 评论点赞数
|
||||
ReviewStatus int `json:"reviewStatus" bson:"reviewStatus"` // 审核状态 0 未审核 1审核成功 2 审核失败
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 更新时间
|
||||
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` // 创建时间
|
||||
TopRank int64 `json:"topRank" bson:"topRank"` // 置顶排序
|
||||
}
|
||||
|
||||
type CommentModelList []*CommentModel
|
||||
Reference in New Issue
Block a user