users.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package usercenter
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. mysqlmgr "server/db/mysql"
  6. "time"
  7. )
  8. type User struct {
  9. ID int `json:"id" db:"id"`
  10. CreateTime time.Time `json:"create_time" db:"create_time"`
  11. UserID string `json:"user_id" db:"user_id"`
  12. FriendIDs json.RawMessage `json:"friend_ids" db:"friend_ids"` // 使用json.RawMessage处理JSON数据
  13. Head string `json:"head" db:"head"`
  14. Coin int `json:"coin" db:"coin"`
  15. Name string `json:"name" db:"name"`
  16. Account string `json:"account" db:"account"`
  17. Password string `json:"-" db:"password"` // json:"-" 表示不序列化密码
  18. }
  19. func GetUserByID(userID string) (*User, error) {
  20. var user User
  21. var friendData []byte // 接收数据库中的JSON原始数据
  22. // SQL查询(实际执行数据库操作)
  23. err := mysqlmgr.QueryRow(`
  24. SELECT id, user_id, friend_ids, name
  25. FROM users WHERE user_id = ?`,
  26. userID,
  27. ).Scan(
  28. &user.ID,
  29. &user.UserID,
  30. &friendData, // 扫描JSON数据到[]byte
  31. &user.Name,
  32. &user.Head,
  33. )
  34. if err != nil {
  35. return nil, err
  36. }
  37. // 将原始JSON数据存入结构体
  38. user.FriendIDs = json.RawMessage(friendData)
  39. return &user, nil
  40. }
  41. func GetUserByAccountAndPassword(Account string, Password string) (*User, error) {
  42. var user User
  43. var friendData []byte // 接收数据库中的JSON原始数据
  44. // SQL查询(实际执行数据库操作)
  45. err := mysqlmgr.QueryRow(`
  46. SELECT id, user_id, friend_ids, name
  47. FROM users WHERE account = ? AND password = ? LIMIT 1`,
  48. Account, Password,
  49. ).Scan(
  50. &user.ID,
  51. &user.UserID,
  52. &friendData, // 扫描JSON数据到[]byte
  53. &user.Name,
  54. &user.Head,
  55. )
  56. if err != nil {
  57. return nil, err
  58. }
  59. // 将原始JSON数据存入结构体
  60. user.FriendIDs = json.RawMessage(friendData)
  61. return &user, nil
  62. }
  63. func CreateUser(user *User) (int64, error) {
  64. result, err := mysqlmgr.Insert(`
  65. INSERT INTO users
  66. (create_time, user_id, friend_ids, head, coin, name, account, password)
  67. VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?)`,
  68. user.UserID,
  69. user.FriendIDs, // 直接传入json.RawMessage
  70. user.Head,
  71. user.Coin,
  72. user.Name,
  73. user.Account,
  74. user.Password,
  75. )
  76. if err != nil {
  77. return 0, fmt.Errorf("创建用户失败: %v", err)
  78. }
  79. return result, err
  80. }
  81. func (u *User) SetFriendIDs(friendIDs []string) error {
  82. jsonData, err := json.Marshal(friendIDs)
  83. if err != nil {
  84. return err
  85. }
  86. u.FriendIDs = jsonData
  87. return nil
  88. }
  89. // 获取好友ID列表
  90. func (u *User) GetFriendIDs() ([]string, error) {
  91. var friends []string
  92. if len(u.FriendIDs) == 0 {
  93. return friends, nil
  94. }
  95. err := json.Unmarshal(u.FriendIDs, &friends)
  96. return friends, err
  97. }
  98. // 添加好友
  99. func (u *User) AddFriend(friendID string) error {
  100. friends, err := u.GetFriendIDs()
  101. if err != nil {
  102. return fmt.Errorf("获取好友列表失败: %v", err)
  103. }
  104. // 检查是否已是好友
  105. for _, id := range friends {
  106. if id == friendID {
  107. return fmt.Errorf("该用户已是好友")
  108. }
  109. }
  110. // 添加新好友
  111. friends = append(friends, friendID)
  112. u.SetFriendIDs(friends)
  113. return u.UpdateFriends(friends)
  114. }
  115. // 删除好友
  116. func (u *User) RemoveFriend(friendID string) error {
  117. friends, err := u.GetFriendIDs()
  118. if err != nil {
  119. return fmt.Errorf("获取好友列表失败: %v", err)
  120. }
  121. // 查找并删除
  122. newFriends := make([]string, 0, len(friends))
  123. found := false
  124. for _, id := range friends {
  125. if id == friendID {
  126. found = true
  127. continue
  128. }
  129. newFriends = append(newFriends, id)
  130. }
  131. if !found {
  132. return fmt.Errorf("该用户不是好友")
  133. }
  134. u.SetFriendIDs(newFriends)
  135. return u.UpdateFriends(newFriends)
  136. }
  137. // 更新好友列表(内部方法)
  138. func (u *User) UpdateFriends(friends []string) error {
  139. // 1. 转换为JSON
  140. jsonData, err := json.Marshal(friends)
  141. if err != nil {
  142. return fmt.Errorf("JSON编码失败: %v", err)
  143. }
  144. // 2. 更新数据库
  145. _, err = mysqlmgr.Update(`
  146. UPDATE users
  147. SET friend_ids = ?
  148. WHERE user_id = ?`,
  149. jsonData,
  150. u.UserID,
  151. )
  152. if err != nil {
  153. return fmt.Errorf("数据库更新失败: %v", err)
  154. }
  155. // 3. 更新内存数据
  156. u.FriendIDs = jsonData
  157. return nil
  158. }
  159. func (u *User) UpdateFriendsToDB() error {
  160. _, err := mysqlmgr.Update(`
  161. UPDATE users
  162. SET friend_ids = ?
  163. WHERE user_id = ?`,
  164. u.FriendIDs, // 使用已转换的JSON数据
  165. u.UserID,
  166. )
  167. if err != nil {
  168. return fmt.Errorf("数据库更新失败: %v", err)
  169. }
  170. return nil
  171. }