fix(chat): 分销站点客服会话按归属租户可见

This commit is contained in:
2026-11-21 14:30:06 +08:00
parent 9d542af51f
commit 2c59085de3
14 changed files with 474 additions and 16 deletions
+21 -1
View File
@@ -8,6 +8,7 @@ import (
"time"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/chatext"
"hfb_sys/backend/internal/tenancy"
"gorm.io/gorm"
@@ -257,6 +258,15 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
return nil, err
}
tenantID := tenancy.IDFromContext(ctx)
if site != nil {
mappedTenantID, mapErr := chatext.TenantIDForSite(r.db.WithContext(ctx), site.ID)
if mapErr != nil {
return nil, mapErr
}
if mappedTenantID > 0 {
tenantID = mappedTenantID
}
}
scene, title, groupCode, welcome := resolveSupportScene(scene)
if tenantID > 0 && site == nil {
@@ -288,7 +298,12 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
existingQuery = existingQuery.Where("c.distribution_site_id IS NULL OR c.distribution_site_id = 0")
}
if tenantID > 0 {
existingQuery = existingQuery.Where("c.tenant_id = ?", tenantID)
if site != nil {
// 历史分销客服会话可能仍为 tenant_id=0,按站点映射兼容并逐步归正。
existingQuery = existingQuery.Where("(c.tenant_id = ? OR c.tenant_id = 0)", tenantID)
} else {
existingQuery = existingQuery.Where("c.tenant_id = ?", tenantID)
}
}
err := existingQuery.Order("c.id ASC").Limit(1).Find(&existing).Error
@@ -338,6 +353,11 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
}
}
if existing.ID > 0 {
if tenantID > 0 && existing.TenantID == 0 {
if err := tx.Model(&existing).Update("tenant_id", tenantID).Error; err != nil {
return err
}
}
conversationID = existing.ID
return nil
}