Initial commit
This commit is contained in:
112
server/internal/model/comment.go
Normal file
112
server/internal/model/comment.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Comment 商品评论
|
||||
type Comment struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
UserID uint `json:"user_id" gorm:"not null;index"`
|
||||
ProductID uint `json:"product_id" gorm:"not null;index"`
|
||||
OrderID uint `json:"order_id" gorm:"not null;index"`
|
||||
OrderItemID uint `json:"order_item_id" gorm:"not null;index"`
|
||||
Rating int `json:"rating" gorm:"not null;default:5"` // 评分 1-5星
|
||||
Content string `json:"content" gorm:"type:text"` // 评论内容
|
||||
Images string `json:"images" gorm:"type:text"` // 评论图片,JSON格式存储
|
||||
IsAnonymous bool `json:"is_anonymous" gorm:"default:false"` // 是否匿名评论
|
||||
Status int `json:"status" gorm:"default:1"` // 状态:1-正常,2-隐藏,3-删除
|
||||
ReplyCount int `json:"reply_count" gorm:"default:0"` // 回复数量
|
||||
LikeCount int `json:"like_count" gorm:"default:0"` // 点赞数量
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
// 关联数据
|
||||
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
||||
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
|
||||
Order Order `json:"order,omitempty" gorm:"foreignKey:OrderID"`
|
||||
OrderItem OrderItem `json:"order_item,omitempty" gorm:"foreignKey:OrderItemID"`
|
||||
Replies []CommentReply `json:"replies,omitempty" gorm:"foreignKey:CommentID"`
|
||||
}
|
||||
|
||||
// MarshalJSON 自定义JSON序列化,将Images字段从JSON字符串转换为数组
|
||||
func (c Comment) MarshalJSON() ([]byte, error) {
|
||||
type Alias Comment
|
||||
|
||||
// 解析Images字段
|
||||
var images []string
|
||||
if c.Images != "" {
|
||||
if err := json.Unmarshal([]byte(c.Images), &images); err != nil {
|
||||
// 如果解析失败,返回空数组
|
||||
images = []string{}
|
||||
}
|
||||
} else {
|
||||
images = []string{}
|
||||
}
|
||||
|
||||
// 创建临时结构体用于序列化
|
||||
return json.Marshal(&struct {
|
||||
*Alias
|
||||
Images []string `json:"images"`
|
||||
}{
|
||||
Alias: (*Alias)(&c),
|
||||
Images: images,
|
||||
})
|
||||
}
|
||||
|
||||
// CommentReply 评论回复
|
||||
type CommentReply struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
CommentID uint `json:"comment_id" gorm:"not null;index"`
|
||||
UserID uint `json:"user_id" gorm:"not null;index"`
|
||||
Content string `json:"content" gorm:"type:text;not null"`
|
||||
IsAdmin bool `json:"is_admin" gorm:"default:false"` // 是否管理员回复
|
||||
Status int `json:"status" gorm:"default:1"` // 状态:1-正常,2-隐藏,3-删除
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
// 关联数据
|
||||
Comment Comment `json:"comment,omitempty" gorm:"foreignKey:CommentID"`
|
||||
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
// CommentLike 评论点赞
|
||||
type CommentLike struct {
|
||||
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
CommentID uint `json:"comment_id" gorm:"not null;index"`
|
||||
UserID uint `json:"user_id" gorm:"not null;index"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
|
||||
// 关联数据
|
||||
Comment Comment `json:"comment,omitempty" gorm:"foreignKey:CommentID"`
|
||||
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (Comment) TableName() string {
|
||||
return "ai_comments"
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (CommentReply) TableName() string {
|
||||
return "ai_comment_replies"
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (CommentLike) TableName() string {
|
||||
return "ai_comment_likes"
|
||||
}
|
||||
|
||||
// CommentStats 评论统计
|
||||
type CommentStats struct {
|
||||
ProductID uint `json:"product_id"`
|
||||
TotalCount int `json:"total_count"` // 总评论数
|
||||
AverageRating float64 `json:"average_rating"` // 平均评分
|
||||
Rating1Count int `json:"rating_1_count"` // 1星评论数
|
||||
Rating2Count int `json:"rating_2_count"` // 2星评论数
|
||||
Rating3Count int `json:"rating_3_count"` // 3星评论数
|
||||
Rating4Count int `json:"rating_4_count"` // 4星评论数
|
||||
Rating5Count int `json:"rating_5_count"` // 5星评论数
|
||||
HasImagesCount int `json:"has_images_count"` // 带图评论数
|
||||
}
|
||||
Reference in New Issue
Block a user