-
Notifications
You must be signed in to change notification settings - Fork 14
/
robot.go
99 lines (76 loc) · 1.99 KB
/
robot.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
96
97
98
99
package dao
import (
"context"
"github.com/iimeta/iim-client/internal/model"
"github.com/iimeta/iim-client/internal/model/do"
"github.com/iimeta/iim-client/internal/model/entity"
"github.com/iimeta/iim-client/utility/db"
"go.mongodb.org/mongo-driver/bson"
)
var Robot = NewRobotDao()
type RobotDao struct {
*MongoDB[entity.Robot]
}
func NewRobotDao(database ...string) *RobotDao {
if len(database) == 0 {
database = append(database, db.DefaultDatabase)
}
return &RobotDao{
MongoDB: NewMongoDB[entity.Robot](database[0], do.ROBOT_COLLECTION),
}
}
// 获取登录机器的信息
func (d *RobotDao) GetLoginRobot(ctx context.Context) (*entity.Robot, error) {
robot, err := d.FindOne(ctx, bson.M{"user_id": 1, "status": model.RootStatusNormal})
if err != nil {
return nil, err
}
return robot, nil
}
// 根据绑定userId获取机器人的信息
func (d *RobotDao) GetRobotByUserId(ctx context.Context, userId int) (*entity.Robot, error) {
robot, err := d.FindOne(ctx, bson.M{"user_id": userId, "status": model.RootStatusNormal})
if err != nil {
return nil, err
}
return robot, nil
}
// 获取机器人列表
func (d *RobotDao) GetRobotList(ctx context.Context, userIds ...int) ([]*entity.Robot, error) {
filter := bson.M{
"is_talk": 1,
"status": model.RootStatusNormal,
}
if len(userIds) > 0 {
filter["user_id"] = bson.M{
"$in": userIds,
}
}
robotList, err := d.Find(ctx, filter)
if err != nil {
return nil, err
}
return robotList, nil
}
// 获取机器人用户列表
func (d *RobotDao) GetRobotUserList(ctx context.Context) ([]*entity.User, error) {
robotList, err := d.GetRobotList(ctx)
if err != nil {
return nil, err
}
if len(robotList) == 0 {
return nil, err
}
robotUserIds := make([]int, 0)
for _, robot := range robotList {
if robot.UserId == 1 {
continue
}
robotUserIds = append(robotUserIds, robot.UserId)
}
userList, err := User.FindUserListByUserIds(ctx, robotUserIds)
if err != nil {
return nil, err
}
return userList, nil
}