| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package model
- import (
- "context"
- "fmt"
- "time"
- "github.com/go-redis/redis/v8"
- "gorm.io/gorm"
- )
- // CommentReply 评论回复
- type CommentReply struct {
- *Model
- PostId int64 `json:"post_id" gorm:"post_id"` // POST ID
- UserId int64 `json:"user_id" gorm:"user_id"` // 用户ID
- CommentId int64 `json:"comment_id" gorm:"comment_id"` // 评论ID
- Ip string `json:"ip" gorm:"ip"` // IP地址
- IpLoc string `json:"ip_loc" gorm:"ip_loc"` // IP城市地址
- Content string `json:"content" gorm:"content"` // 评论内容
- Image string `json:"image" gorm:"image"` // 评论图片
- WithUserIds string `json:"with_user_ids" gorm:"with_user_ids"` // 被艾特的用户
- IsEssence int8 `json:"is_essence" gorm:"is_essence"` // 是否精选
- ReplyCount int64 `json:"reply_count" gorm:"reply_count"` // 回复数
- ThumbsUpCount int64 `json:"thumbs_up_count" gorm:"thumbs_up_count"` // 点赞数
- ReplyId int64 `json:"reply_id" gorm:"reply_id"` // 回复的id
- }
- // TableName 表名称
- func (*CommentReply) TableName() string {
- return "p_comment_reply"
- }
- type ReplyModel struct {
- conn *gorm.DB
- redis *redis.Client
- }
- func NewReplyModel(conn *gorm.DB, rdb *redis.Client) ReplyModel {
- return ReplyModel{
- conn: conn,
- redis: rdb,
- }
- }
- // Create 创建回复
- func (m *ReplyModel) Create(ctx context.Context, reply *CommentReply) error {
- err := m.conn.WithContext(ctx).Create(reply).Error
- if err != nil {
- return err
- }
- // 删除回复列表缓存
- m.ClearReplyListCache(ctx, reply.CommentId)
- return nil
- }
- // ClearReplyListCache 清除回复列表缓存
- func (m *ReplyModel) ClearReplyListCache(ctx context.Context, commentId int64) {
- pattern := fmt.Sprintf("reply:list:%d*", commentId)
- keys, err := m.redis.Keys(ctx, pattern).Result()
- if err != nil {
- return
- }
- if len(keys) > 0 {
- m.redis.Del(ctx, keys...)
- }
- }
- // GetReply 获取回复详情
- func (m *ReplyModel) GetReply(ctx context.Context, replyId int64) (*CommentReply, error) {
- var reply CommentReply
- err := m.conn.WithContext(ctx).Where("id = ?", replyId).First(&reply).Error
- if err != nil {
- return nil, err
- }
- return &reply, nil
- }
- // Delete 删除回复
- func (m *ReplyModel) Delete(ctx context.Context, replyId int64) error {
- return m.conn.WithContext(ctx).Model(&CommentReply{}).
- Where("id = ?", replyId).
- Updates(map[string]interface{}{
- "is_del": 1,
- "deleted_on": time.Now().Unix(),
- "modified_on": time.Now().Unix(),
- }).Error
- }
|