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
+37 -19
View File
@@ -46,6 +46,7 @@ func EnsureListingConversation(tx *gorm.DB, listing model.RentalListing, preferr
// 2. 建会话
now := time.Now()
conversation := model.ChatConversation{
TenantID: listing.SourceTenantID,
ListingID: &listing.ID,
Type: ConversationTypeListingGroup,
Title: listingConversationTitle(listing),
@@ -70,16 +71,19 @@ func EnsureListingConversation(tx *gorm.DB, listing model.RentalListing, preferr
}
// 获取收号组客服。分组未配置时回退到开放上传管理员或默认客服。
supportID, err := supportgroup.PickSupportAdmin(tx, supportgroup.GroupCodeOwnerOnboarding)
supportID, err := supportgroup.PickSupportAdminForTenant(tx, supportgroup.GroupCodeOwnerOnboarding, conversation.TenantID)
if err != nil {
return nil, err
}
if supportID <= 0 {
supportID = preferredSupportAdminID
if supportID <= 0 {
supportID = defaultSupportAdminID(tx)
supportID = defaultSupportAdminID(tx, conversation.TenantID)
}
}
if supportID > 0 && conversation.TenantID > 0 && !adminIsSupport(tx, supportID, conversation.TenantID) {
supportID = defaultSupportAdminID(tx, conversation.TenantID)
}
if supportID > 0 {
participants = append(participants, model.ChatParticipant{
ConversationID: conversation.ID,
@@ -97,7 +101,7 @@ func EnsureListingConversation(tx *gorm.DB, listing model.RentalListing, preferr
}
// 4. 取二维码:主站只用主站池,分站只用自己的池。池空则稍后补发,不跨池借用。
qrcode, qrcodeImageURL, err := resolveListingQrCode(tx, siteID)
qrcode, qrcodeImageURL, err := resolveListingQrCode(tx, siteID, conversation.TenantID)
if err != nil {
return nil, err
}
@@ -108,7 +112,7 @@ func EnsureListingConversation(tx *gorm.DB, listing model.RentalListing, preferr
}
// 5. 发欢迎语
welcomeMessage := getListingGroupWelcomeMessage(tx)
welcomeMessage := getListingGroupWelcomeMessage(tx, conversation.TenantID)
if err := sendSystemMessage(tx, conversation.ID, welcomeMessage); err != nil {
return nil, err
}
@@ -124,7 +128,7 @@ func EnsureListingConversation(tx *gorm.DB, listing model.RentalListing, preferr
if err := sendSystemMessage(tx, conversation.ID, noQrTip); err != nil {
return nil, err
}
if err := createQrCodeDeliveryTask(tx, conversation.ID); err != nil {
if err := createQrCodeDeliveryTask(tx, conversation.ID, conversation.TenantID); err != nil {
return nil, err
}
}
@@ -176,7 +180,7 @@ func AddRenterToListingConversation(tx *gorm.DB, listingID uint64, renterID uint
}
// 3. 支付后由卖号组客服接入跟进交接和售后。
handoffSupportID, err := supportgroup.PickSupportAdmin(tx, supportgroup.GroupCodeRenterHandoff)
handoffSupportID, err := supportgroup.PickSupportAdminForTenant(tx, supportgroup.GroupCodeRenterHandoff, conv.TenantID)
if err != nil {
return err
}
@@ -241,9 +245,13 @@ func listingConversationTitle(listing model.RentalListing) string {
return "账号群 " + listing.ListingNo
}
func getListingGroupWelcomeMessage(tx *gorm.DB) string {
func getListingGroupWelcomeMessage(tx *gorm.DB, tenantID uint64) string {
var cfg model.SystemConfig
if err := tx.Where("`key` = ?", listingGroupWelcomeConfigKey).First(&cfg).Error; err == nil && cfg.Value != "" {
query := tx.Where("`key` = ?", listingGroupWelcomeConfigKey)
if tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
if err := query.First(&cfg).Error; err == nil && cfg.Value != "" {
return cfg.Value
}
@@ -309,7 +317,7 @@ func updateConversationLastMessage(tx *gorm.DB, conversationID uint64, message *
}
func fetchUnusedQrCode(tx *gorm.DB) (*model.ChatQrCode, error) {
return fetchUnusedQrCodeForSite(tx, nil)
return fetchUnusedQrCodeForSite(tx, nil, 0)
}
func resolveListingDistributionSiteID(tx *gorm.DB, listing model.RentalListing) (uint64, error) {
@@ -337,19 +345,19 @@ func resolveListingDistributionSiteID(tx *gorm.DB, listing model.RentalListing)
return 0, nil
}
func resolveListingQrCode(tx *gorm.DB, siteID uint64) (*model.ChatQrCode, string, error) {
func resolveListingQrCode(tx *gorm.DB, siteID, tenantID uint64) (*model.ChatQrCode, string, error) {
var scope *uint64
if siteID > 0 {
scope = &siteID
}
qrcode, err := fetchUnusedQrCodeForSite(tx, scope)
qrcode, err := fetchUnusedQrCodeForSite(tx, scope, tenantID)
if err != nil || qrcode == nil {
return nil, "", err
}
return qrcode, qrcode.ImageURL, nil
}
func fetchUnusedQrCodeForSite(tx *gorm.DB, siteID *uint64) (*model.ChatQrCode, error) {
func fetchUnusedQrCodeForSite(tx *gorm.DB, siteID *uint64, tenantID uint64) (*model.ChatQrCode, error) {
var qrcode model.ChatQrCode
now := time.Now()
query := tx.Where("status = ?", QrCodeStatusUnused).
@@ -360,6 +368,9 @@ func fetchUnusedQrCodeForSite(tx *gorm.DB, siteID *uint64) (*model.ChatQrCode, e
} else {
query = query.Where("distribution_site_id = ?", *siteID)
}
if tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
err := query.
Limit(1).
Clauses(clause.Locking{Strength: "UPDATE"}).
@@ -377,14 +388,14 @@ func fetchUnusedQrCodeForSite(tx *gorm.DB, siteID *uint64) (*model.ChatQrCode, e
func fetchUnusedQrCodeForConversation(tx *gorm.DB, conversationID uint64) (*model.ChatQrCode, error) {
var conversation model.ChatConversation
if err := tx.Select("id", "distribution_site_id").First(&conversation, conversationID).Error; err != nil {
if err := tx.Select("id", "tenant_id", "distribution_site_id").First(&conversation, conversationID).Error; err != nil {
return nil, err
}
var siteID *uint64
if conversation.DistributionSiteID != nil && *conversation.DistributionSiteID > 0 {
siteID = conversation.DistributionSiteID
}
return fetchUnusedQrCodeForSite(tx, siteID)
return fetchUnusedQrCodeForSite(tx, siteID, conversation.TenantID)
}
func markQrCodeAsUsed(tx *gorm.DB, qrcodeID uint64, conversationID uint64) error {
@@ -398,8 +409,9 @@ func markQrCodeAsUsed(tx *gorm.DB, qrcodeID uint64, conversationID uint64) error
}).Error
}
func createQrCodeDeliveryTask(tx *gorm.DB, conversationID uint64) error {
func createQrCodeDeliveryTask(tx *gorm.DB, conversationID, tenantID uint64) error {
task := model.ChatQrCodeDeliveryTask{
TenantID: tenantID,
ConversationID: conversationID,
Status: "pending",
}
@@ -448,6 +460,9 @@ func checkQrCodeStockAndAlert(tx *gorm.DB, conversation *model.ChatConversation)
} else {
countQuery = countQuery.Where("distribution_site_id IS NULL")
}
if conversation != nil && conversation.TenantID > 0 {
countQuery = countQuery.Where("tenant_id = ?", conversation.TenantID)
}
var count int64
if err := countQuery.Count(&count).Error; err != nil {
return nil, err
@@ -459,12 +474,14 @@ func checkQrCodeStockAndAlert(tx *gorm.DB, conversation *model.ChatConversation)
// 低于阈值:写入站内信(事务内)
var csAdmins []model.AdminUser
if err := tx.Table("admin_users").
adminQuery := tx.Table("admin_users").
Joins("JOIN admin_user_roles ON admin_users.id = admin_user_roles.admin_user_id").
Joins("JOIN roles ON admin_user_roles.role_id = roles.id").
Where("roles.code = ? AND admin_users.status = ?", "cs", "active").
Select("admin_users.id").
Find(&csAdmins).Error; err != nil {
Where("roles.code = ? AND admin_users.status = ?", "cs", "active")
if conversation != nil && conversation.TenantID > 0 {
adminQuery = adminQuery.Where("admin_user_roles.tenant_id = ?", conversation.TenantID)
}
if err := adminQuery.Select("admin_users.id").Find(&csAdmins).Error; err != nil {
return nil, err
}
@@ -477,6 +494,7 @@ func checkQrCodeStockAndAlert(tx *gorm.DB, conversation *model.ChatConversation)
now := time.Now()
for _, admin := range csAdmins {
entries = append(entries, map[string]interface{}{
"tenant_id": conversation.TenantID,
"admin_user_id": admin.ID,
"type": "system",
"title": "二维码库存预警",