perf(chat): 优化后台客服会话查询与状态

This commit is contained in:
yml2213
2026-08-25 15:53:10 +08:00
parent 87327fbd91
commit 417f7398e4
14 changed files with 461 additions and 141 deletions
+15 -1
View File
@@ -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,
}