53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// VoteOption 投票选项
|
||
|
|
type VoteOption struct {
|
||
|
|
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||
|
|
PostID string `json:"post_id" gorm:"type:varchar(36);index:idx_vote_option_post_sort,priority:1;not null"`
|
||
|
|
Content string `json:"content" gorm:"type:varchar(200);not null"`
|
||
|
|
SortOrder int `json:"sort_order" gorm:"default:0;index:idx_vote_option_post_sort,priority:2"`
|
||
|
|
VotesCount int `json:"votes_count" gorm:"default:0"`
|
||
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// BeforeCreate 创建前生成UUID
|
||
|
|
func (vo *VoteOption) BeforeCreate(tx *gorm.DB) error {
|
||
|
|
if vo.ID == "" {
|
||
|
|
vo.ID = uuid.New().String()
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (VoteOption) TableName() string {
|
||
|
|
return "vote_options"
|
||
|
|
}
|
||
|
|
|
||
|
|
// UserVote 用户投票记录
|
||
|
|
type UserVote struct {
|
||
|
|
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||
|
|
PostID string `json:"post_id" gorm:"type:varchar(36);index;uniqueIndex:idx_user_vote_post_user,priority:1;not null"`
|
||
|
|
UserID string `json:"user_id" gorm:"type:varchar(36);index;uniqueIndex:idx_user_vote_post_user,priority:2;not null"`
|
||
|
|
OptionID string `json:"option_id" gorm:"type:varchar(36);index;not null"`
|
||
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// BeforeCreate 创建前生成UUID
|
||
|
|
func (uv *UserVote) BeforeCreate(tx *gorm.DB) error {
|
||
|
|
if uv.ID == "" {
|
||
|
|
uv.ID = uuid.New().String()
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (UserVote) TableName() string {
|
||
|
|
return "user_votes"
|
||
|
|
}
|