Files
tyapi-server/internal/domains/user/entities/user.go

60 lines
1.2 KiB
Go

package entities
import (
"time"
"gorm.io/gorm"
)
// User 用户实体
type User struct {
ID string `gorm:"primaryKey;type:varchar(36)" json:"id"`
Phone string `gorm:"uniqueIndex;type:varchar(20);not null" json:"phone"`
Password string `gorm:"type:varchar(255);not null" json:"-"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
// 实现 Entity 接口
func (u *User) GetID() string {
return u.ID
}
func (u *User) GetCreatedAt() time.Time {
return u.CreatedAt
}
func (u *User) GetUpdatedAt() time.Time {
return u.UpdatedAt
}
// 验证方法
func (u *User) Validate() error {
if u.Phone == "" {
return NewValidationError("手机号不能为空")
}
if u.Password == "" {
return NewValidationError("密码不能为空")
}
return nil
}
// TableName 指定表名
func (User) TableName() string {
return "users"
}
// ValidationError 验证错误
type ValidationError struct {
Message string
}
func (e *ValidationError) Error() string {
return e.Message
}
func NewValidationError(message string) *ValidationError {
return &ValidationError{Message: message}
}