user_model.go 6.3 KB

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