package model import ( "gorm.io/gorm" "hepa-calc-api/global" "time" ) // User 用户表 type User struct { UserId int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:用户id" json:"user_id"` AppIden string `gorm:"column:app_iden;type:varchar(50);comment:app唯一标识" json:"app_iden"` UserName string `gorm:"column:user_name;type:varchar(200);comment:用户名称" json:"user_name"` Mobile string `gorm:"column:mobile;type:varchar(20);comment:手机号;NOT NULL" json:"mobile"` UserStatus int `gorm:"column:user_status;type:tinyint(1);default:1;comment:状态(1:正常 2:禁用)" json:"user_status"` RegisterSource int `gorm:"column:register_source;type:tinyint(1);default:1;comment:注册来源(1:app注册 2:公众号注册)" json:"register_source"` OpenId string `gorm:"column:open_id;type:varchar(100);comment:用户微信标识" json:"open_id"` UnionId string `gorm:"column:union_id;type:varchar(100);comment:微信开放平台标识" json:"union_id"` Birthday *LocalTime `gorm:"column:birthday;type:datetime;comment:出生日期" json:"birthday"` Age *uint `gorm:"column:age;type:int(10) unsigned;comment:年龄" json:"age"` Sex *int `gorm:"column:sex;type:tinyint(1) unsigned;comment:性别(0:未知 1:男 2:女)" json:"sex"` Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"` IsMember int `gorm:"column:is_member;type:tinyint(1);default:0;comment:是否会员(0:否 1:是)" json:"is_member"` MemberExpireDate *LocalTime `gorm:"column:member_expire_date;type:datetime;comment:会员到期时间(非会员时为null)" json:"member_expire_date"` LoginAt *LocalTime `gorm:"column:login_at;type:datetime;comment:登陆时间" json:"login_at"` LoginIp string `gorm:"column:login_ip;type:varchar(255);comment:登陆ip" json:"login_ip"` Model } func (m *User) TableName() string { return "user" } func (m *User) BeforeCreate(tx *gorm.DB) error { if m.UserId == 0 { m.UserId = 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 }