user_model.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // FindOne 获取一条用户信息
  84. func (m *UserModel) FindOne(ctx context.Context, userId int64) (*User, error) {
  85. var userInfo *User
  86. err := m.conn.WithContext(ctx).Model(&User{}).
  87. Where("id = ?", userId).
  88. First(&userInfo).Error
  89. return userInfo, err
  90. }
  91. // Create 创建用户
  92. func (m *UserModel) Create(ctx context.Context, user *User) error {
  93. return m.conn.WithContext(ctx).Create(user).Error
  94. }
  95. // SearchByNickname 通过昵称搜索用户
  96. func (m *UserModel) SearchByNickname(ctx context.Context, nickname string, page, pageSize int) ([]*User, error) {
  97. var users []*User
  98. offset := (page - 1) * pageSize
  99. err := m.conn.WithContext(ctx).
  100. Where("nickname LIKE ? AND status = 1", "%"+nickname+"%").
  101. Offset(offset).
  102. Limit(pageSize).
  103. Order("id desc").
  104. Find(&users).Error
  105. if err != nil {
  106. return nil, err
  107. }
  108. return users, nil
  109. }
  110. // DecrementFansCount 减少粉丝数
  111. func (m *UserModel) DecrementFansCount(ctx context.Context, userId int64) error {
  112. return m.conn.WithContext(ctx).
  113. Model(&User{}).
  114. Where("id = ?", userId).
  115. UpdateColumn("fans_count", gorm.Expr("fans_count - ?", 1)).Error
  116. }
  117. // DecrementFollowCount 减少关注数
  118. func (m *UserModel) DecrementFollowCount(ctx context.Context, userId int64) error {
  119. return m.conn.WithContext(ctx).
  120. Model(&User{}).
  121. Where("id = ?", userId).
  122. UpdateColumn("follow_count", gorm.Expr("follow_count - ?", 1)).Error
  123. }
  124. // IncrementFansCount 增加粉丝数
  125. func (m *UserModel) IncrementFansCount(ctx context.Context, userId int64) error {
  126. return m.conn.WithContext(ctx).
  127. Model(&User{}).
  128. Where("id = ?", userId).
  129. UpdateColumn("fans_count", gorm.Expr("fans_count + ?", 1)).Error
  130. }
  131. // IncrementFollowCount 增加关注数
  132. func (m *UserModel) IncrementFollowCount(ctx context.Context, userId int64) error {
  133. return m.conn.WithContext(ctx).
  134. Model(&User{}).
  135. Where("id = ?", userId).
  136. UpdateColumn("follow_count", gorm.Expr("follow_count + ?", 1)).Error
  137. }
  138. // IncrementTweetCount 增加帖子数
  139. func (m *UserModel) IncrementTweetCount(ctx context.Context, userId int64) error {
  140. return m.conn.WithContext(ctx).
  141. Model(&User{}).
  142. Where("id = ?", userId).
  143. UpdateColumn("tweet_count", gorm.Expr("tweet_count + ?", 1)).Error
  144. }
  145. // DecrementTweetCount 减少帖子数
  146. func (m *UserModel) DecrementTweetCount(ctx context.Context, userId int64) error {
  147. return m.conn.WithContext(ctx).
  148. Model(&User{}).
  149. Where("id = ?", userId).
  150. UpdateColumn("tweet_count", gorm.Expr("tweet_count - ?", 1)).Error
  151. }
  152. // Transaction 执行数据库事务
  153. func (m *UserModel) Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error {
  154. return m.conn.WithContext(ctx).Transaction(fn)
  155. }
  156. // IsPostLikedByUser 检查用户是否点赞了帖子
  157. func (m *UserModel) IsPostLikedByUser(ctx context.Context, userId, postId int64) (bool, error) {
  158. var count int64
  159. err := m.conn.WithContext(ctx).
  160. Model(&PostAction{}).
  161. Where("user_id = ? AND post_id = ? and type = 0", userId, postId).
  162. Count(&count).Error
  163. return count > 0, err
  164. }
  165. // IsPostCollectedByUser 检查用户是否收藏了帖子
  166. func (m *UserModel) IsPostCollectedByUser(ctx context.Context, userId, postId int64) (bool, error) {
  167. var count int64
  168. err := m.conn.WithContext(ctx).
  169. Model(&PostAction{}).
  170. Where("user_id = ? AND post_id = ? and type = 1", userId, postId).
  171. Count(&count).Error
  172. return count > 0, err
  173. }
  174. type UserModel struct {
  175. conn *gorm.DB
  176. }
  177. var ErrNotFound = gorm.ErrRecordNotFound
  178. func NewUserModel(conn *gorm.DB) *UserModel {
  179. return &UserModel{
  180. conn: conn,
  181. }
  182. }