123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package usercenter
- import (
- "encoding/json"
- "fmt"
- mysqlmgr "server/db/mysql"
- "time"
- )
- type User struct {
- ID int `json:"id" db:"id"`
- CreateTime time.Time `json:"create_time" db:"create_time"`
- UserID string `json:"user_id" db:"user_id"`
- FriendIDs json.RawMessage `json:"friend_ids" db:"friend_ids"` // 使用json.RawMessage处理JSON数据
- Head string `json:"head" db:"head"`
- Coin int `json:"coin" db:"coin"`
- Name string `json:"name" db:"name"`
- Account string `json:"account" db:"account"`
- Password string `json:"-" db:"password"` // json:"-" 表示不序列化密码
- }
- func GetUserByID(userID string) (*User, error) {
- var user User
- var friendData []byte // 接收数据库中的JSON原始数据
- // SQL查询(实际执行数据库操作)
- err := mysqlmgr.QueryRow(`
- SELECT id, user_id, friend_ids, name
- FROM users WHERE user_id = ?`,
- userID,
- ).Scan(
- &user.ID,
- &user.UserID,
- &friendData, // 扫描JSON数据到[]byte
- &user.Name,
- &user.Head,
- )
- if err != nil {
- return nil, err
- }
- // 将原始JSON数据存入结构体
- user.FriendIDs = json.RawMessage(friendData)
- return &user, nil
- }
- func GetUserByAccountAndPassword(Account string, Password string) (*User, error) {
- var user User
- var friendData []byte // 接收数据库中的JSON原始数据
- // SQL查询(实际执行数据库操作)
- err := mysqlmgr.QueryRow(`
- SELECT id, user_id, friend_ids, name
- FROM users WHERE account = ? AND password = ? LIMIT 1`,
- Account, Password,
- ).Scan(
- &user.ID,
- &user.UserID,
- &friendData, // 扫描JSON数据到[]byte
- &user.Name,
- &user.Head,
- )
- if err != nil {
- return nil, err
- }
- // 将原始JSON数据存入结构体
- user.FriendIDs = json.RawMessage(friendData)
- return &user, nil
- }
- func CreateUser(user *User) (int64, error) {
- result, err := mysqlmgr.Insert(`
- INSERT INTO users
- (create_time, user_id, friend_ids, head, coin, name, account, password)
- VALUES (NOW(), ?, ?, ?, ?, ?, ?, ?)`,
- user.UserID,
- user.FriendIDs, // 直接传入json.RawMessage
- user.Head,
- user.Coin,
- user.Name,
- user.Account,
- user.Password,
- )
- if err != nil {
- return 0, fmt.Errorf("创建用户失败: %v", err)
- }
- return result, err
- }
- func (u *User) SetFriendIDs(friendIDs []string) error {
- jsonData, err := json.Marshal(friendIDs)
- if err != nil {
- return err
- }
- u.FriendIDs = jsonData
- return nil
- }
- // 获取好友ID列表
- func (u *User) GetFriendIDs() ([]string, error) {
- var friends []string
- if len(u.FriendIDs) == 0 {
- return friends, nil
- }
- err := json.Unmarshal(u.FriendIDs, &friends)
- return friends, err
- }
- // 添加好友
- func (u *User) AddFriend(friendID string) error {
- friends, err := u.GetFriendIDs()
- if err != nil {
- return fmt.Errorf("获取好友列表失败: %v", err)
- }
- // 检查是否已是好友
- for _, id := range friends {
- if id == friendID {
- return fmt.Errorf("该用户已是好友")
- }
- }
- // 添加新好友
- friends = append(friends, friendID)
- u.SetFriendIDs(friends)
- return u.UpdateFriends(friends)
- }
- // 删除好友
- func (u *User) RemoveFriend(friendID string) error {
- friends, err := u.GetFriendIDs()
- if err != nil {
- return fmt.Errorf("获取好友列表失败: %v", err)
- }
- // 查找并删除
- newFriends := make([]string, 0, len(friends))
- found := false
- for _, id := range friends {
- if id == friendID {
- found = true
- continue
- }
- newFriends = append(newFriends, id)
- }
- if !found {
- return fmt.Errorf("该用户不是好友")
- }
- u.SetFriendIDs(newFriends)
- return u.UpdateFriends(newFriends)
- }
- // 更新好友列表(内部方法)
- func (u *User) UpdateFriends(friends []string) error {
- // 1. 转换为JSON
- jsonData, err := json.Marshal(friends)
- if err != nil {
- return fmt.Errorf("JSON编码失败: %v", err)
- }
- // 2. 更新数据库
- _, err = mysqlmgr.Update(`
- UPDATE users
- SET friend_ids = ?
- WHERE user_id = ?`,
- jsonData,
- u.UserID,
- )
- if err != nil {
- return fmt.Errorf("数据库更新失败: %v", err)
- }
- // 3. 更新内存数据
- u.FriendIDs = jsonData
- return nil
- }
- func (u *User) UpdateFriendsToDB() error {
- _, err := mysqlmgr.Update(`
- UPDATE users
- SET friend_ids = ?
- WHERE user_id = ?`,
- u.FriendIDs, // 使用已转换的JSON数据
- u.UserID,
- )
- if err != nil {
- return fmt.Errorf("数据库更新失败: %v", err)
- }
- return nil
- }
|