feat(tenant): 隔离客服通知文件与后台任务

This commit is contained in:
yml
2026-11-15 16:23:28 +08:00
parent c7dacfa025
commit e994fd9166
52 changed files with 1670 additions and 254 deletions
+26 -3
View File
@@ -8,6 +8,7 @@ import (
"time"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -137,7 +138,11 @@ func (r *Repository) FindConversation(ctx context.Context, principal Principal,
// 管理员可以查看任意会话,无需是 participant
if principal.Type == "admin" {
var conversation model.ChatConversation
if err := db.First(&conversation, id).Error; err != nil {
query, err := adminConversationScope(ctx, db, principal.ID)
if err != nil {
return nil, err
}
if err := query.Table("chat_conversations AS c").Select("c.*").Where("c.id = ?", id).First(&conversation).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrConversationNotFound
}
@@ -251,8 +256,16 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
if err != nil {
return nil, err
}
tenantID := tenancy.IDFromContext(ctx)
scene, title, groupCode, welcome := resolveSupportScene(scene)
if tenantID > 0 && site == nil {
if scope, ok := tenancy.FromContext(ctx); ok && strings.TrimSpace(scope.TenantName) != "" {
title = fmt.Sprintf("【%s】%s", scope.TenantName, title)
welcome = fmt.Sprintf("您好,欢迎咨询【%s】在线客服!请直接描述您遇到的问题,客服会尽快回复。", scope.TenantName)
}
}
if site != nil {
title = fmt.Sprintf("【%s】%s", site.SiteName, title)
welcome = fmt.Sprintf("您好,欢迎咨询【%s】在线客服!请直接描述您遇到的问题,客服会尽快回复。", site.SiteName)
@@ -274,6 +287,9 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
} else {
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)
}
err := existingQuery.Order("c.id ASC").Limit(1).Find(&existing).Error
if err != nil {
@@ -295,6 +311,9 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
if err != nil {
return err
}
if existing.ID > 0 && tenantID > 0 && existing.TenantID != tenantID {
existing = model.ChatConversation{}
}
if existing.ID > 0 && existing.SupportScene == "" {
_ = tx.Model(&existing).Update("support_scene", SupportSceneGeneral).Error
}
@@ -314,6 +333,9 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
if err != nil {
return err
}
if existing.ID > 0 && tenantID > 0 && existing.TenantID != tenantID {
existing = model.ChatConversation{}
}
}
if existing.ID > 0 {
conversationID = existing.ID
@@ -322,6 +344,7 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
now := time.Now()
conversation := model.ChatConversation{
TenantID: tenantID,
Type: ConversationTypeGeneralSupport,
SupportScene: scene,
Title: title,
@@ -347,12 +370,12 @@ func (r *Repository) EnsureSupportConversation(ctx context.Context, userID uint6
var supportID uint64
if site != nil && site.DefaultSupportGroupID != nil && *site.DefaultSupportGroupID > 0 {
var member model.ChatSupportGroupMember
if err := tx.Where("group_id = ?", *site.DefaultSupportGroupID).Order("id ASC").First(&member).Error; err == nil && member.AdminID > 0 {
if err := tx.Where("group_id = ?", *site.DefaultSupportGroupID).Order("id ASC").First(&member).Error; err == nil && member.AdminID > 0 && adminIsSupport(tx, member.AdminID, tenantID) {
supportID = member.AdminID
}
}
if supportID == 0 {
supportID = pickSupportAdminForScene(tx, groupCode)
supportID = pickSupportAdminForScene(tx, groupCode, tenantID)
}
if supportID > 0 {