feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/chathub"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -19,15 +20,18 @@ func (r *Repository) Messages(ctx context.Context, principal Principal, conversa
|
||||
if _, err := r.findParticipant(db, principal, conversationID, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// 管理员需要验证会话存在
|
||||
var count int64
|
||||
if err := db.Model(&model.ChatConversation{}).Where("id = ?", conversationID).Count(&count).Error; err != nil {
|
||||
query := tenantConversationScope(ctx, db.Table("chat_conversations AS c")).Where("c.id = ?", conversationID)
|
||||
if err := query.Count(&count).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
return nil, ErrConversationNotFound
|
||||
}
|
||||
} else {
|
||||
if err := ensureAdminConversationAccess(ctx, db, principal.ID, conversationID, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// 构建查询
|
||||
@@ -59,7 +63,20 @@ func (r *Repository) SendMessage(ctx context.Context, principal Principal, conve
|
||||
var messageID uint64
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var conversation model.ChatConversation
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&conversation, conversationID).Error; err != nil {
|
||||
query := tx.Table("chat_conversations AS c").Select("c.*")
|
||||
if principal.Type == "admin" {
|
||||
var err error
|
||||
query, err = adminConversationScope(ctx, query, principal.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
query = tenantConversationScope(ctx, query)
|
||||
}
|
||||
if err := query.Clauses(clause.Locking{Strength: "UPDATE"}).Where("c.id = ?", conversationID).First(&conversation).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConversationNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
if conversation.Status != "active" {
|
||||
@@ -168,6 +185,7 @@ func (r *Repository) SendMessage(ctx context.Context, principal Principal, conve
|
||||
CreatedAt: msg.CreatedAt.Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
tenantScopedEvent(r.db.WithContext(ctx), event)
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
// 同时广播给所有在线客服,使「未分配 / 全部」视图实时刷新;
|
||||
// 参与者客服会重复收到,由前端按消息 id 去重。
|
||||
@@ -180,6 +198,9 @@ func (r *Repository) MarkRead(ctx context.Context, principal Principal, conversa
|
||||
updated := false
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if principal.Type == "admin" {
|
||||
if err := ensureAdminConversationAccess(ctx, tx, principal.ID, conversationID, true); err != nil {
|
||||
return err
|
||||
}
|
||||
var conversation model.ChatConversation
|
||||
if err := tx.Select("id", "last_message_id").First(&conversation, conversationID).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -224,6 +245,15 @@ func (r *Repository) MarkRead(ctx context.Context, principal Principal, conversa
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var count int64
|
||||
if err := tenantConversationScope(ctx, tx.Table("chat_conversations AS c")).
|
||||
Where("c.id = ?", conversationID).
|
||||
Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return ErrConversationNotFound
|
||||
}
|
||||
updated = true
|
||||
return tx.Model(participant).Update("last_read_at", now).Error
|
||||
})
|
||||
@@ -239,6 +269,7 @@ func (r *Repository) MarkRead(ctx context.Context, principal Principal, conversa
|
||||
ReaderID: principal.ID,
|
||||
ReadAt: now.Format(time.RFC3339),
|
||||
}
|
||||
tenantScopedEvent(r.db.WithContext(ctx), event)
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
// 同时广播给所有在线客服,覆盖未分配会话中非参与者客服的「已读」刷新。
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
@@ -250,6 +281,19 @@ func (r *Repository) MarkRead(ctx context.Context, principal Principal, conversa
|
||||
func (r *Repository) MarkAllAdminConversationsRead(ctx context.Context, adminID uint64) error {
|
||||
now := time.Now()
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
platform, err := adminHasPlatformScope(tx, adminID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
accessSQL := ""
|
||||
accessArgs := make([]any, 0)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 && !platform {
|
||||
condition, args := conversationTenantAccessCondition("c")
|
||||
accessSQL = " AND " + condition
|
||||
for range args {
|
||||
accessArgs = append(accessArgs, tenantID)
|
||||
}
|
||||
}
|
||||
// 先补齐状态行,再更新游标。拆成两步可兼容本地 MySQL/MariaDB 对
|
||||
// INSERT ... SELECT 同表读取并 ON DUPLICATE KEY UPDATE 的限制。
|
||||
if err := tx.Exec(`
|
||||
@@ -259,7 +303,9 @@ func (r *Repository) MarkAllAdminConversationsRead(ctx context.Context, adminID
|
||||
FROM chat_conversations AS c
|
||||
LEFT JOIN chat_admin_conversation_states AS existing
|
||||
ON existing.conversation_id = c.id AND existing.admin_user_id = ?
|
||||
WHERE existing.id IS NULL`, adminID, now, adminID).Error; err != nil {
|
||||
WHERE existing.id IS NULL`+accessSQL,
|
||||
append([]any{adminID, now, adminID}, accessArgs...)...,
|
||||
).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Exec(`
|
||||
@@ -270,6 +316,8 @@ func (r *Repository) MarkAllAdminConversationsRead(ctx context.Context, adminID
|
||||
cas.updated_at = CURRENT_TIMESTAMP
|
||||
WHERE cas.admin_user_id = ?
|
||||
AND c.last_message_id IS NOT NULL
|
||||
AND cas.last_read_message_id < c.last_message_id`, now, adminID).Error
|
||||
AND cas.last_read_message_id < c.last_message_id`+accessSQL,
|
||||
append([]any{now, adminID}, accessArgs...)...,
|
||||
).Error
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user