| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package model
- import (
- "context"
- "time"
- "gorm.io/gorm"
- )
- // User 用户模型
- type User struct {
- *Model
- Username string `gorm:"column:username;type:varchar(32);not null"`
- Phone string `gorm:"column:phone;type:varchar(11);not null"`
- Password string `gorm:"column:password;type:varchar(128);not null"`
- Avatar string `gorm:"column:avatar;type:varchar(255)"`
- Gender int32 `gorm:"column:gender;type:tinyint(1);default:0"` // 0:未知 1:男 2:女
- Status int32 `gorm:"column:status;type:tinyint(1);default:1"` // 1:正常 2:禁用
- LastLoginIp string `gorm:"column:last_login_ip;type:varchar(15)"`
- CreateTime time.Time `gorm:"column:create_time;autoCreateTime"`
- UpdateTime time.Time `gorm:"column:update_time;autoUpdateTime"`
- Nickname string `gorm:"column:nickname;NOT NULL"` // 昵称
- Salt string `gorm:"column:salt;NOT NULL"` // 盐值
- Sex int `gorm:"column:sex;default:0;NOT NULL"` // 性别 0 女、1 男
- LikeCount int `gorm:"column:like_count;default:0;NOT NULL"` // 收获的点赞数量
- TweetCount int `gorm:"column:tweet_count;default:0;NOT NULL"` // 帖子数量
- CollectionCount int `gorm:"column:collection_count;default:0;NOT NULL"` // 收获的收藏数量
- FollowCount int `gorm:"column:follow_count;default:0;NOT NULL"` // 关注数量
- FansCount int `gorm:"column:fans_count;default:0;NOT NULL"` // 粉丝数量
- }
- // TableName 表名
- func (User) TableName() string {
- return "user"
- }
- // FindByIds 通过ID列表批量查询用户
- func (m *UserModel) FindByIds(ctx context.Context, ids []int64) ([]*User, error) {
- var users []*User
- err := m.conn.WithContext(ctx).Where("id IN ?", ids).Find(&users).Error
- if err != nil {
- return nil, err
- }
- return users, nil
- }
- // FindOneByPhone 通过手机号查询用户
- func (m *UserModel) FindOneByPhone(ctx context.Context, phone string) (*User, error) {
- var user User
- err := m.conn.WithContext(ctx).Where("phone = ?", phone).First(&user).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, ErrNotFound
- }
- return nil, err
- }
- return &user, nil
- }
- // FindOneByUsername 通过用户名查询用户
- func (m *UserModel) FindOneByUsername(ctx context.Context, username string) (*User, error) {
- var user User
- err := m.conn.WithContext(ctx).Where("username = ?", username).First(&user).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, ErrNotFound
- }
- return nil, err
- }
- return &user, nil
- }
- // FindOneById 通过ID查询用户
- func (m *UserModel) FindOneById(ctx context.Context, id int64) (*User, error) {
- var user User
- err := m.conn.WithContext(ctx).Where("id = ?", id).First(&user).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, ErrNotFound
- }
- return nil, err
- }
- return &user, nil
- }
- // UpdateLoginIP 更新用户最后登录IP
- func (m *UserModel) UpdateLoginIP(ctx context.Context, userId int64, ip string) error {
- return m.conn.WithContext(ctx).Model(&User{}).
- Where("id = ?", userId).
- Update("last_login_ip", ip).Error
- }
- // Create 创建用户
- func (m *UserModel) Create(ctx context.Context, user *User) error {
- return m.conn.WithContext(ctx).Create(user).Error
- }
- // SearchByNickname 通过昵称搜索用户
- func (m *UserModel) SearchByNickname(ctx context.Context, nickname string, page, pageSize int) ([]*User, error) {
- var users []*User
- offset := (page - 1) * pageSize
- err := m.conn.WithContext(ctx).
- Where("nickname LIKE ? AND status = 1", "%"+nickname+"%").
- Offset(offset).
- Limit(pageSize).
- Order("id desc").
- Find(&users).Error
- if err != nil {
- return nil, err
- }
- return users, nil
- }
- // DecrementFansCount 减少粉丝数
- func (m *UserModel) DecrementFansCount(ctx context.Context, userId int64) error {
- return m.conn.WithContext(ctx).
- Model(&User{}).
- Where("id = ?", userId).
- UpdateColumn("fans_count", gorm.Expr("fans_count - ?", 1)).Error
- }
- // DecrementFollowCount 减少关注数
- func (m *UserModel) DecrementFollowCount(ctx context.Context, userId int64) error {
- return m.conn.WithContext(ctx).
- Model(&User{}).
- Where("id = ?", userId).
- UpdateColumn("follow_count", gorm.Expr("follow_count - ?", 1)).Error
- }
- // IncrementFansCount 增加粉丝数
- func (m *UserModel) IncrementFansCount(ctx context.Context, userId int64) error {
- return m.conn.WithContext(ctx).
- Model(&User{}).
- Where("id = ?", userId).
- UpdateColumn("fans_count", gorm.Expr("fans_count + ?", 1)).Error
- }
- // IncrementFollowCount 增加关注数
- func (m *UserModel) IncrementFollowCount(ctx context.Context, userId int64) error {
- return m.conn.WithContext(ctx).
- Model(&User{}).
- Where("id = ?", userId).
- UpdateColumn("follow_count", gorm.Expr("follow_count + ?", 1)).Error
- }
- // Transaction 执行数据库事务
- func (m *UserModel) Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error {
- return m.conn.WithContext(ctx).Transaction(fn)
- }
- type UserModel struct {
- conn *gorm.DB
- }
- var ErrNotFound = gorm.ErrRecordNotFound
- func NewUserModel(conn *gorm.DB) UserModel {
- return UserModel{
- conn: conn,
- }
- }
|