vote-api/api/model/Article.go
2024-08-28 18:31:18 +08:00

38 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"gorm.io/gorm"
"time"
"vote-api/global"
)
// Article 文章表
type Article struct {
ArticleId int64 `gorm:"column:article_id;type:bigint(19);primary_key;comment:主键id" json:"article_id"`
ArticleTitle string `gorm:"column:article_title;type:varchar(200);comment:文章标题" json:"article_title"`
ArticleStatus int `gorm:"column:article_status;type:tinyint(1);default:1;comment:文章状态1:正常 2:禁用)" json:"article_status"`
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;default:0;comment:总票数" json:"vote_num"`
ArticleContent string `gorm:"column:article_content;type:text;comment:文章内容" json:"article_content"`
Model
ArticleAuthor []*ArticleAuthor `gorm:"foreignKey:ArticleId;references:article_id" json:"article_author"`
Rank *int `gorm:"column:rank;type:tinyint(1) unsigned;comment:排名" json:"rank"`
}
func (m *Article) TableName() string {
return "article"
}
func (m *Article) BeforeCreate(tx *gorm.DB) error {
if m.ArticleId == 0 {
m.ArticleId = global.Snowflake.Generate().Int64()
}
m.CreatedAt = LocalTime(time.Now())
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
m.UpdatedAt = LocalTime(time.Now())
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
return nil
}