/
chathistorylistlogic.go
77 lines (61 loc) · 1.8 KB
/
chathistorylistlogic.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
package logic
import (
"context"
"errors"
"github.com/acger/chat-svc/chat"
"github.com/acger/chat-svc/internal/svc"
"github.com/acger/user-svc/user"
"github.com/jinzhu/copier"
"gorm.io/gorm"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatHistoryListLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewChatHistoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatHistoryListLogic {
return &ChatHistoryListLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ChatHistoryListLogic) ChatHistoryList(in *chat.ChatHistoryReq) (*chat.ChatHistoryRsp, error) {
type ChatHistoryMessage struct {
Uid uint64
Status bool
}
var chatHistory []*ChatHistoryMessage
db := l.svcCtx.DB
query := db.Table("chat_histories a")
query = query.Joins("left join chats as b on b.uid = a.to_uid and b.to_uid = ?", in.Id)
query = query.Where("a.uid = ?", in.Id)
query = query.Group("a.to_uid")
query = query.Order("status, max(b.created_at) desc")
query = query.Select("a.to_uid as uid, IFNULL(MIN(b.status), true) as status")
result := query.Find(&chatHistory)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &chat.ChatHistoryRsp{}, nil
}
var uidList []uint64
for _, e := range chatHistory {
uidList = append(uidList, e.Uid)
}
//获取用户信息
userListRsp, _ := l.svcCtx.UserSvc.UserList(l.ctx, &user.UserListReq{Id: uidList})
userMap := make(map[uint64]*user.UserInfo)
for _, u := range userListRsp.User {
userMap[u.Id] = u
}
var chatUserList []*chat.ChatUser
for _, c := range chatHistory {
tmp := &chat.ChatUser{
Id: c.Uid,
Status: c.Status,
}
copier.Copy(tmp, userMap[c.Uid])
chatUserList = append(chatUserList, tmp)
}
return &chat.ChatHistoryRsp{Code: 0, User: chatUserList}, nil
}