perf(chat): 优化后台客服会话查询与状态
This commit is contained in:
@@ -53,6 +53,19 @@ func (r *Repository) FindConversation(ctx context.Context, principal Principal,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var state model.ChatAdminConversationState
|
||||
if err := db.Where("conversation_id = ? AND admin_user_id = ?", conversation.ID, principal.ID).First(&state).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
var unreadCount int64
|
||||
if err := db.Table("chat_messages AS cm").
|
||||
Where("cm.conversation_id = ?", conversation.ID).
|
||||
Where("cm.sender_type <> ?", "system").
|
||||
Where("NOT (cm.sender_type = ? AND cm.sender_id = ?)", "admin", principal.ID).
|
||||
Where("cm.id > ?", state.LastReadMessageID).
|
||||
Count(&unreadCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := ConversationDTO{
|
||||
ID: conversation.ID,
|
||||
OrderID: conversation.OrderID,
|
||||
@@ -62,11 +75,12 @@ func (r *Repository) FindConversation(ctx context.Context, principal Principal,
|
||||
Title: conversation.Title,
|
||||
Status: conversation.Status,
|
||||
Role: "admin", // 管理员角色
|
||||
AdminRemark: state.Remark,
|
||||
Participants: participants,
|
||||
LastMessageID: conversation.LastMessageID,
|
||||
LastMessagePreview: conversation.LastMessagePreview,
|
||||
LastMessageAt: conversation.LastMessageAt,
|
||||
UnreadCount: 0, // 管理员不计未读
|
||||
UnreadCount: unreadCount,
|
||||
CreatedAt: conversation.CreatedAt,
|
||||
UpdatedAt: conversation.UpdatedAt,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user