This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
95 lines (85 loc) · 1.78 KB
/
global.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
User: cr-mao
Date: 2023/8/30 11:43
Email: crmao@qq.com
Desc: 全局变量都在这个包里面
*/
package manager
import (
"errors"
"sync"
)
type LianmengManager struct {
LimengInfos map[int64]map[int64]*User
locker sync.RWMutex
}
type User struct {
UserId int64
UserName string
LianmengId int64
}
var Manager *LianmengManager
func init() {
Manager = &LianmengManager{
LimengInfos: make(map[int64]map[int64]*User),
}
}
func GetManager() *LianmengManager {
return Manager
}
func (m *LianmengManager) GetUserByUserIdAndLianMengId(userId int64, lianmengId int64) (*User, error) {
m.locker.RLock()
defer m.locker.RUnlock()
limengInfo, ok := m.LimengInfos[lianmengId]
if !ok {
return nil, errors.New("lianmengId not exists")
}
user, ok := limengInfo[userId]
if !ok {
return nil, errors.New("lianmeng userId not exists")
}
return user, nil
}
func (m *LianmengManager) AddUser(user *User) {
if user == nil {
return
}
m.locker.Lock()
defer m.locker.Unlock()
limengInfo, ok := m.LimengInfos[user.LianmengId]
if !ok {
m.LimengInfos[user.LianmengId] = make(map[int64]*User)
limengInfo = m.LimengInfos[user.LianmengId]
}
limengInfo[user.UserId] = user
}
func (m *LianmengManager) DelUser(user *User) {
if user == nil {
return
}
m.locker.Lock()
defer m.locker.Unlock()
limengInfo, ok := m.LimengInfos[user.LianmengId]
if !ok {
return
}
if limengInfo != nil {
delete(limengInfo, user.UserId)
}
}
func (m *LianmengManager) GetUserIdListByLianmengId(lianmengId int64, userId int64) []int64 {
m.locker.RLock()
defer m.locker.RUnlock()
limengInfo, ok := m.LimengInfos[lianmengId]
if !ok {
return nil
}
var res = make([]int64, 0, len(limengInfo))
for id := range limengInfo {
if userId == id {
continue
}
res = append(res, id)
}
return res
}