user_model.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package model
  2. import (
  3. "context"
  4. "time"
  5. "gorm.io/gorm"
  6. )
  7. // User 用户模型
  8. type User struct {
  9. *Model
  10. Username string `gorm:"column:username;type:varchar(32);not null"`
  11. Phone string `gorm:"column:phone;type:varchar(11);not null"`
  12. Password string `gorm:"column:password;type:varchar(128);not null"`
  13. Avatar string `gorm:"column:avatar;type:varchar(255)"`
  14. Gender int32 `gorm:"column:gender;type:tinyint(1);default:0"` // 0:未知 1:男 2:女
  15. Status int32 `gorm:"column:status;type:tinyint(1);default:1"` // 1:正常 2:禁用
  16. LastLoginIp string `gorm:"column:last_login_ip;type:varchar(15)"`
  17. CreateTime time.Time `gorm:"column:create_time;autoCreateTime"`
  18. UpdateTime time.Time `gorm:"column:update_time;autoUpdateTime"`
  19. Nickname string `gorm:"column:nickname;NOT NULL"` // 昵称
  20. Salt string `gorm:"column:salt;NOT NULL"` // 盐值
  21. Sex int `gorm:"column:sex;default:0;NOT NULL"` // 性别 0 女、1 男
  22. LikeCount int `gorm:"column:like_count;default:0;NOT NULL"` // 收获的点赞数量
  23. TweetCount int `gorm:"column:tweet_count;default:0;NOT NULL"` // 帖子数量
  24. CollectionCount int `gorm:"column:collection_count;default:0;NOT NULL"` // 收获的收藏数量
  25. FollowCount int `gorm:"column:follow_count;default:0;NOT NULL"` // 关注数量
  26. FansCount int `gorm:"column:fans_count;default:0;NOT NULL"` // 粉丝数量
  27. }
  28. // TableName 表名
  29. func (User) TableName() string {
  30. return "user"
  31. }
  32. // FindByIds 通过ID列表批量查询用户
  33. func (m *UserModel) FindByIds(ctx context.Context, ids []int64) ([]*User, error) {
  34. var users []*User
  35. err := m.conn.WithContext(ctx).Where("id IN ?", ids).Find(&users).Error
  36. if err != nil {
  37. return nil, err
  38. }
  39. return users, nil
  40. }
  41. // FindOneByPhone 通过手机号查询用户
  42. func (m *UserModel) FindOneByPhone(ctx context.Context, phone string) (*User, error) {
  43. var user User
  44. err := m.conn.WithContext(ctx).Where("phone = ?", phone).First(&user).Error
  45. if err != nil {
  46. if err == gorm.ErrRecordNotFound {
  47. return nil, ErrNotFound
  48. }
  49. return nil, err
  50. }
  51. return &user, nil
  52. }
  53. // FindOneByUsername 通过用户名查询用户
  54. func (m *UserModel) FindOneByUsername(ctx context.Context, username string) (*User, error) {
  55. var user User
  56. err := m.conn.WithContext(ctx).Where("username = ?", username).First(&user).Error
  57. if err != nil {
  58. if err == gorm.ErrRecordNotFound {
  59. return nil, ErrNotFound
  60. }
  61. return nil, err
  62. }
  63. return &user, nil
  64. }
  65. // FindOneById 通过ID查询用户
  66. func (m *UserModel) FindOneById(ctx context.Context, id int64) (*User, error) {
  67. var user User
  68. err := m.conn.WithContext(ctx).Where("id = ?", id).First(&user).Error
  69. if err != nil {
  70. if err == gorm.ErrRecordNotFound {
  71. return nil, ErrNotFound
  72. }
  73. return nil, err
  74. }
  75. return &user, nil
  76. }
  77. // UpdateLoginIP 更新用户最后登录IP
  78. func (m *UserModel) UpdateLoginIP(ctx context.Context, userId int64, ip string) error {
  79. return m.conn.WithContext(ctx).Model(&User{}).
  80. Where("id = ?", userId).
  81. Update("last_login_ip", ip).Error
  82. }
  83. // Create 创建用户
  84. func (m *UserModel) Create(ctx context.Context, user *User) error {
  85. return m.conn.WithContext(ctx).Create(user).Error
  86. }
  87. // SearchByNickname 通过昵称搜索用户
  88. func (m *UserModel) SearchByNickname(ctx context.Context, nickname string, page, pageSize int) ([]*User, error) {
  89. var users []*User
  90. offset := (page - 1) * pageSize
  91. err := m.conn.WithContext(ctx).
  92. Where("nickname LIKE ? AND status = 1", "%"+nickname+"%").
  93. Offset(offset).
  94. Limit(pageSize).
  95. Order("id desc").
  96. Find(&users).Error
  97. if err != nil {
  98. return nil, err
  99. }
  100. return users, nil
  101. }
  102. // DecrementFansCount 减少粉丝数
  103. func (m *UserModel) DecrementFansCount(ctx context.Context, userId int64) error {
  104. return m.conn.WithContext(ctx).
  105. Model(&User{}).
  106. Where("id = ?", userId).
  107. UpdateColumn("fans_count", gorm.Expr("fans_count - ?", 1)).Error
  108. }
  109. // DecrementFollowCount 减少关注数
  110. func (m *UserModel) DecrementFollowCount(ctx context.Context, userId int64) error {
  111. return m.conn.WithContext(ctx).
  112. Model(&User{}).
  113. Where("id = ?", userId).
  114. UpdateColumn("follow_count", gorm.Expr("follow_count - ?", 1)).Error
  115. }
  116. // IncrementFansCount 增加粉丝数
  117. func (m *UserModel) IncrementFansCount(ctx context.Context, userId int64) error {
  118. return m.conn.WithContext(ctx).
  119. Model(&User{}).
  120. Where("id = ?", userId).
  121. UpdateColumn("fans_count", gorm.Expr("fans_count + ?", 1)).Error
  122. }
  123. // IncrementFollowCount 增加关注数
  124. func (m *UserModel) IncrementFollowCount(ctx context.Context, userId int64) error {
  125. return m.conn.WithContext(ctx).
  126. Model(&User{}).
  127. Where("id = ?", userId).
  128. UpdateColumn("follow_count", gorm.Expr("follow_count + ?", 1)).Error
  129. }
  130. // Transaction 执行数据库事务
  131. func (m *UserModel) Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error {
  132. return m.conn.WithContext(ctx).Transaction(fn)
  133. }
  134. type UserModel struct {
  135. conn *gorm.DB
  136. }
  137. var ErrNotFound = gorm.ErrRecordNotFound
  138. func NewUserModel(conn *gorm.DB) UserModel {
  139. return UserModel{
  140. conn: conn,
  141. }
  142. }