2025-06-30 19:21:56 +08:00
|
|
|
package entities
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// User 用户实体
|
|
|
|
|
type User struct {
|
2025-07-02 16:17:59 +08:00
|
|
|
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:"-"`
|
2025-06-30 19:21:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现 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 {
|
2025-07-02 16:17:59 +08:00
|
|
|
if u.Phone == "" {
|
|
|
|
|
return NewValidationError("手机号不能为空")
|
2025-06-30 19:21:56 +08:00
|
|
|
}
|
|
|
|
|
if u.Password == "" {
|
2025-07-02 16:17:59 +08:00
|
|
|
return NewValidationError("密码不能为空")
|
2025-06-30 19:21:56 +08:00
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
|
}
|