feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/chathub"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
// QrCodeStatus 二维码状态常量
|
||||
@@ -97,10 +98,12 @@ type QrCodeOCRConfig struct {
|
||||
|
||||
// CreateQrCode 创建二维码
|
||||
func (r *Repository) CreateQrCode(ctx context.Context, adminID uint64, req CreateQrCodeRequest) (*model.ChatQrCode, error) {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var qrcode model.ChatQrCode
|
||||
var deliveredMessages []model.ChatMessage
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
qrcode = model.ChatQrCode{
|
||||
TenantID: tenantID,
|
||||
ImageURL: req.ImageURL,
|
||||
GroupName: req.GroupName,
|
||||
Status: QrCodeStatusUnused,
|
||||
@@ -119,7 +122,7 @@ func (r *Repository) CreateQrCode(ctx context.Context, adminID uint64, req Creat
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
deliveredMessages, err = deliverPendingQrCodes(tx)
|
||||
deliveredMessages, err = deliverPendingQrCodes(tx, tenantID)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -132,6 +135,7 @@ func (r *Repository) CreateQrCode(ctx context.Context, adminID uint64, req Creat
|
||||
|
||||
// BatchCreateQrCode 批量创建二维码
|
||||
func (r *Repository) BatchCreateQrCode(ctx context.Context, adminID uint64, req BatchCreateQrCodeRequest) ([]model.ChatQrCode, error) {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var qrcodes []model.ChatQrCode
|
||||
var deliveredMessages []model.ChatMessage
|
||||
defaultExpires := time.Now().Add(7 * 24 * time.Hour)
|
||||
@@ -139,6 +143,7 @@ func (r *Repository) BatchCreateQrCode(ctx context.Context, adminID uint64, req
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for _, item := range req.Items {
|
||||
qr := model.ChatQrCode{
|
||||
TenantID: tenantID,
|
||||
ImageURL: item.ImageURL,
|
||||
GroupName: item.GroupName,
|
||||
Status: QrCodeStatusUnused,
|
||||
@@ -156,7 +161,7 @@ func (r *Repository) BatchCreateQrCode(ctx context.Context, adminID uint64, req
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
deliveredMessages, err = deliverPendingQrCodes(tx)
|
||||
deliveredMessages, err = deliverPendingQrCodes(tx, tenantID)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -177,12 +182,17 @@ func (r *Repository) ListQrCodes(ctx context.Context, req QrCodeListRequest) ([]
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
baseQuery := func() *gorm.DB {
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("chat_qrcode_pool AS q").
|
||||
Joins("LEFT JOIN chat_conversations AS c ON c.id = q.conversation_id").
|
||||
Where("q.distribution_site_id IS NULL")
|
||||
if tenantID > 0 {
|
||||
query = query.Where("q.tenant_id = ?", tenantID)
|
||||
}
|
||||
return applyQrCodeListFilters(
|
||||
r.db.WithContext(ctx).
|
||||
Table("chat_qrcode_pool AS q").
|
||||
Joins("LEFT JOIN chat_conversations AS c ON c.id = q.conversation_id").
|
||||
Where("q.distribution_site_id IS NULL"),
|
||||
query,
|
||||
req,
|
||||
now,
|
||||
)
|
||||
@@ -295,25 +305,40 @@ func parseQrCodeFilterTime(value string) (time.Time, bool) {
|
||||
func (r *Repository) GetQrCodeStats(ctx context.Context) (*QrCodeStats, error) {
|
||||
stats := &QrCodeStats{}
|
||||
now := time.Now()
|
||||
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
mainPool := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).Where("distribution_site_id IS NULL")
|
||||
if tenantID > 0 {
|
||||
mainPool = mainPool.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := mainPool.Count(&stats.TotalCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).
|
||||
Where("distribution_site_id IS NULL").
|
||||
usedQuery := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).
|
||||
Where("distribution_site_id IS NULL")
|
||||
if tenantID > 0 {
|
||||
usedQuery = usedQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := usedQuery.
|
||||
Where("status = ?", QrCodeStatusUsed).
|
||||
Count(&stats.UsedCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).
|
||||
Where("distribution_site_id IS NULL").
|
||||
disabledQuery := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).
|
||||
Where("distribution_site_id IS NULL")
|
||||
if tenantID > 0 {
|
||||
disabledQuery = disabledQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := disabledQuery.
|
||||
Where("status = ?", QrCodeStatusDisabled).
|
||||
Count(&stats.DisabledCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).
|
||||
Where("distribution_site_id IS NULL").
|
||||
unusedQuery := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).
|
||||
Where("distribution_site_id IS NULL")
|
||||
if tenantID > 0 {
|
||||
unusedQuery = unusedQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := unusedQuery.
|
||||
Where("status = ?", QrCodeStatusUnused).
|
||||
Where("expires_at IS NULL OR expires_at > ?", now).
|
||||
Count(&stats.UnusedCount).Error; err != nil {
|
||||
@@ -331,7 +356,11 @@ func (r *Repository) GetQrCodeOCRConfig(ctx context.Context) (*QrCodeOCRConfig,
|
||||
}
|
||||
var rows []model.SystemConfig
|
||||
keys := []string{qrCodeOCRTokenConfigKey, qrCodeOCRJobURLConfigKey, qrCodeOCRModelConfigKey}
|
||||
if err := r.db.WithContext(ctx).Where("`key` IN ?", keys).Find(&rows).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("`key` IN ?", keys)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, row := range rows {
|
||||
@@ -354,7 +383,11 @@ func (r *Repository) GetQrCodeOCRConfig(ctx context.Context) (*QrCodeOCRConfig,
|
||||
// UpdateQrCode 更新二维码
|
||||
func (r *Repository) UpdateQrCode(ctx context.Context, id uint64, req UpdateQrCodeRequest) error {
|
||||
var qrcode model.ChatQrCode
|
||||
if err := r.db.WithContext(ctx).Where("distribution_site_id IS NULL").First(&qrcode, id).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("distribution_site_id IS NULL")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&qrcode, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrQrCodeNotFound
|
||||
}
|
||||
@@ -409,7 +442,11 @@ func qrcodeWasIssued(qrcode model.ChatQrCode) bool {
|
||||
// DeleteQrCode 删除二维码
|
||||
func (r *Repository) DeleteQrCode(ctx context.Context, id uint64) error {
|
||||
var qrcode model.ChatQrCode
|
||||
if err := r.db.WithContext(ctx).Where("distribution_site_id IS NULL").First(&qrcode, id).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("distribution_site_id IS NULL")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&qrcode, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrQrCodeNotFound
|
||||
}
|
||||
@@ -425,7 +462,11 @@ func (r *Repository) BatchDeleteQrCodes(ctx context.Context, ids []uint64) (int6
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
result := r.db.WithContext(ctx).Where("id IN ? AND distribution_site_id IS NULL", ids).Delete(&model.ChatQrCode{})
|
||||
query := r.db.WithContext(ctx).Where("id IN ? AND distribution_site_id IS NULL", ids)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
result := query.Delete(&model.ChatQrCode{})
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
@@ -466,13 +507,16 @@ func (r *Repository) markQrCodeAsUsed(tx *gorm.DB, qrcodeID uint64, conversation
|
||||
}).Error
|
||||
}
|
||||
|
||||
func deliverPendingQrCodes(tx *gorm.DB) ([]model.ChatMessage, error) {
|
||||
func deliverPendingQrCodes(tx *gorm.DB, tenantID uint64) ([]model.ChatMessage, error) {
|
||||
deliveredMessages := make([]model.ChatMessage, 0)
|
||||
skipped := make([]uint64, 0)
|
||||
for {
|
||||
var task model.ChatQrCodeDeliveryTask
|
||||
query := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("status = ?", "pending")
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if len(skipped) > 0 {
|
||||
query = query.Where("id NOT IN ?", skipped)
|
||||
}
|
||||
@@ -548,12 +592,17 @@ func (r *Repository) ListSiteQrCodes(ctx context.Context, siteID uint64, req QrC
|
||||
req.Limit = 20
|
||||
}
|
||||
now := time.Now()
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
baseQuery := func() *gorm.DB {
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("chat_qrcode_pool AS q").
|
||||
Joins("LEFT JOIN chat_conversations AS c ON c.id = q.conversation_id").
|
||||
Where("q.distribution_site_id = ?", siteID)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("q.tenant_id = ?", tenantID)
|
||||
}
|
||||
return applyQrCodeListFilters(
|
||||
r.db.WithContext(ctx).
|
||||
Table("chat_qrcode_pool AS q").
|
||||
Joins("LEFT JOIN chat_conversations AS c ON c.id = q.conversation_id").
|
||||
Where("q.distribution_site_id = ?", siteID),
|
||||
query,
|
||||
req,
|
||||
now,
|
||||
)
|
||||
@@ -580,8 +629,13 @@ func (r *Repository) GetSiteQrCodeStats(ctx context.Context, siteID uint64) (*Qr
|
||||
}
|
||||
stats := &QrCodeStats{}
|
||||
now := time.Now()
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
base := func() *gorm.DB {
|
||||
return r.db.WithContext(ctx).Model(&model.ChatQrCode{}).Where("distribution_site_id = ?", siteID)
|
||||
query := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).Where("distribution_site_id = ?", siteID)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query
|
||||
}
|
||||
if err := base().Count(&stats.TotalCount).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -608,9 +662,11 @@ func (r *Repository) BatchCreateSiteQrCode(ctx context.Context, adminID, siteID
|
||||
var qrcodes []model.ChatQrCode
|
||||
var deliveredMessages []model.ChatMessage
|
||||
defaultExpires := time.Now().Add(7 * 24 * time.Hour)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for _, item := range req.Items {
|
||||
qr := model.ChatQrCode{
|
||||
TenantID: tenantID,
|
||||
ImageURL: item.ImageURL,
|
||||
GroupName: item.GroupName,
|
||||
DistributionSiteID: &siteID,
|
||||
@@ -629,7 +685,7 @@ func (r *Repository) BatchCreateSiteQrCode(ctx context.Context, adminID, siteID
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
deliveredMessages, err = deliverPendingQrCodes(tx)
|
||||
deliveredMessages, err = deliverPendingQrCodes(tx, tenantID)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
@@ -644,7 +700,11 @@ func (r *Repository) UpdateSiteQrCode(ctx context.Context, siteID, id uint64, re
|
||||
return err
|
||||
}
|
||||
var qrcode model.ChatQrCode
|
||||
if err := r.db.WithContext(ctx).Where("distribution_site_id = ?", siteID).First(&qrcode, id).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("distribution_site_id = ?", siteID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&qrcode, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrQrCodeNotFound
|
||||
}
|
||||
@@ -691,7 +751,11 @@ func (r *Repository) DeleteSiteQrCode(ctx context.Context, siteID, id uint64) er
|
||||
if err := r.ensureDistributionSite(ctx, siteID); err != nil {
|
||||
return err
|
||||
}
|
||||
result := r.db.WithContext(ctx).Where("id = ? AND distribution_site_id = ?", id, siteID).Delete(&model.ChatQrCode{})
|
||||
query := r.db.WithContext(ctx).Where("id = ? AND distribution_site_id = ?", id, siteID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
result := query.Delete(&model.ChatQrCode{})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
@@ -708,7 +772,11 @@ func (r *Repository) BatchDeleteSiteQrCodes(ctx context.Context, siteID uint64,
|
||||
if len(ids) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
result := r.db.WithContext(ctx).Where("id IN ? AND distribution_site_id = ?", ids, siteID).Delete(&model.ChatQrCode{})
|
||||
query := r.db.WithContext(ctx).Where("id IN ? AND distribution_site_id = ?", ids, siteID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
result := query.Delete(&model.ChatQrCode{})
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
@@ -736,6 +804,7 @@ func (r *Repository) notifyDeliveredQrCodeMessages(messages []model.ChatMessage)
|
||||
CreatedAt: msg.CreatedAt.Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
tenantScopedEvent(r.db, event)
|
||||
r.hub.NotifyConversation(msg.ConversationID, event)
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user