Set up project files and add .gitignore to exclude local build/runtime artifacts. Made-with: Cursor
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package model
|
||
|
||
import (
|
||
"strconv"
|
||
"time"
|
||
|
||
"carrot_bbs/internal/pkg/utils"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// JoinType 群组加入类型
|
||
type JoinType int
|
||
|
||
const (
|
||
JoinTypeAnyone JoinType = 0 // 允许任何人加入
|
||
JoinTypeApproval JoinType = 1 // 需要审批
|
||
JoinTypeForbidden JoinType = 2 // 不允许加入
|
||
)
|
||
|
||
// Group 群组模型
|
||
type Group struct {
|
||
ID string `gorm:"primaryKey;size:20" json:"id"`
|
||
Name string `gorm:"size:50;not null" json:"name"`
|
||
Avatar string `gorm:"size:512" json:"avatar"`
|
||
Description string `gorm:"size:500" json:"description"`
|
||
OwnerID string `gorm:"type:varchar(36);not null;index" json:"owner_id"`
|
||
MemberCount int `gorm:"default:0" json:"member_count"`
|
||
MaxMembers int `gorm:"default:500" json:"max_members"`
|
||
JoinType JoinType `gorm:"default:0" json:"join_type"` // 0:允许任何人加入 1:需要审批 2:不允许加入
|
||
MuteAll bool `gorm:"default:false" json:"mute_all"` // 全员禁言
|
||
CreatedAt time.Time `json:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
}
|
||
|
||
// BeforeCreate 创建前生成雪花算法ID
|
||
func (g *Group) BeforeCreate(tx *gorm.DB) error {
|
||
if g.ID == "" {
|
||
id, err := utils.GetSnowflake().GenerateID()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
g.ID = strconv.FormatInt(id, 10)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// GetIDInt 获取数字类型的ID(用于比较)
|
||
func (g *Group) GetIDInt() uint64 {
|
||
id, _ := strconv.ParseUint(g.ID, 10, 64)
|
||
return id
|
||
}
|
||
|
||
// TableName 指定表名
|
||
func (Group) TableName() string {
|
||
return "groups"
|
||
}
|