feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"hfb_sys/backend/internal/modules/qualityowner"
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
smsnode "hfb_sys/backend/internal/modules/sms"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -21,7 +22,7 @@ import (
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
initMu sync.RWMutex
|
||||
initialized bool
|
||||
initialized map[uint64]bool
|
||||
}
|
||||
|
||||
type AuditMeta = auditlog.Meta
|
||||
@@ -99,14 +100,15 @@ var adminVisibleConfigKeys = []string{
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
return &Repository{db: db, initialized: make(map[uint64]bool)}
|
||||
}
|
||||
|
||||
// EnsureDefaults 仅在服务启动或首次访问时初始化默认系统配置。
|
||||
// 使用读写锁保证进程生命周期内只真正落库初始化一次,避免每个读请求重复开事务写锁争抢。
|
||||
func (r *Repository) EnsureDefaults(ctx context.Context) error {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
r.initMu.RLock()
|
||||
if r.initialized {
|
||||
if r.initialized[tenantID] {
|
||||
r.initMu.RUnlock()
|
||||
return nil
|
||||
}
|
||||
@@ -114,13 +116,13 @@ func (r *Repository) EnsureDefaults(ctx context.Context) error {
|
||||
|
||||
r.initMu.Lock()
|
||||
defer r.initMu.Unlock()
|
||||
if r.initialized {
|
||||
if r.initialized[tenantID] {
|
||||
return nil
|
||||
}
|
||||
if err := r.ensureDefaults(ctx); err != nil {
|
||||
if err := r.ensureDefaults(ctx, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
r.initialized = true
|
||||
r.initialized[tenantID] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -129,7 +131,8 @@ func (r *Repository) List(ctx context.Context) ([]ConfigDTO, error) {
|
||||
return nil, err
|
||||
}
|
||||
var rows []model.SystemConfig
|
||||
if err := r.db.WithContext(ctx).Where("`key` IN ?", adminVisibleConfigKeys).Order("`key` ASC").Find(&rows).Error; err != nil {
|
||||
query := applyTenantScope(ctx, r.db.WithContext(ctx))
|
||||
if err := query.Where("`key` IN ?", adminVisibleConfigKeys).Order("`key` ASC").Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]ConfigDTO, 0, len(rows))
|
||||
@@ -150,7 +153,7 @@ func (r *Repository) FindValue(ctx context.Context, key string) (string, error)
|
||||
return "", err
|
||||
}
|
||||
var row model.SystemConfig
|
||||
if err := r.db.WithContext(ctx).Where("`key` = ?", key).First(&row).Error; err != nil {
|
||||
if err := applyTenantScope(ctx, r.db.WithContext(ctx)).Where("`key` = ?", key).First(&row).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return row.Value, nil
|
||||
@@ -161,10 +164,16 @@ func (r *Repository) Update(ctx context.Context, actorID uint64, key string, req
|
||||
return nil, err
|
||||
}
|
||||
var row model.SystemConfig
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("`key` = ?", key).First(&row).Error; err != nil {
|
||||
query := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("`key` = ?", key)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&row).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
row = model.SystemConfig{
|
||||
TenantID: tenantID,
|
||||
Key: key,
|
||||
Value: req.Value,
|
||||
Description: req.Description,
|
||||
@@ -186,7 +195,7 @@ func (r *Repository) Update(ctx context.Context, actorID uint64, key string, req
|
||||
if err := tx.Save(&row).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := syncRenterGrowthLevels(tx, key, req.Value); err != nil {
|
||||
if err := syncRenterGrowthLevels(tx, key, req.Value, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := appendAuditLog(tx, actorID, "system_config.update", row.ID, meta, map[string]any{
|
||||
@@ -198,7 +207,7 @@ func (r *Repository) Update(ctx context.Context, actorID uint64, key string, req
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := syncRenterGrowthLevels(tx, key, req.Value); err != nil {
|
||||
if err := syncRenterGrowthLevels(tx, key, req.Value, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
return appendAuditLog(tx, actorID, "system_config.create", row.ID, meta, map[string]any{
|
||||
@@ -213,7 +222,7 @@ func (r *Repository) Update(ctx context.Context, actorID uint64, key string, req
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
func syncRenterGrowthLevels(tx *gorm.DB, key string, value string) error {
|
||||
func syncRenterGrowthLevels(tx *gorm.DB, key string, value string, tenantID uint64) error {
|
||||
if key != rentergrowth.ConfigKey {
|
||||
return nil
|
||||
}
|
||||
@@ -221,13 +230,14 @@ func syncRenterGrowthLevels(tx *gorm.DB, key string, value string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rentergrowth.SyncAllUserLevels(tx, cfg)
|
||||
return rentergrowth.SyncAllUserLevelsForTenant(tx, cfg, tenantID)
|
||||
}
|
||||
|
||||
func (r *Repository) ensureDefaults(ctx context.Context) error {
|
||||
func (r *Repository) ensureDefaults(ctx context.Context, tenantID uint64) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for _, item := range defaultConfigs {
|
||||
row := model.SystemConfig{
|
||||
TenantID: tenantID,
|
||||
Key: item.Key,
|
||||
Value: item.Value,
|
||||
Description: item.Description,
|
||||
@@ -236,17 +246,17 @@ func (r *Repository) ensureDefaults(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
if item.Key == "chat.default_support_admin_id" {
|
||||
if err := updateDefaultSupportConfig(tx, item); err != nil {
|
||||
if err := updateDefaultSupportConfig(tx, item, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if item.Key == "chat.listing_group_welcome" {
|
||||
if err := updateLegacyListingGroupWelcome(tx, item); err != nil {
|
||||
if err := updateLegacyListingGroupWelcome(tx, item, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if item.Key == publishOptionsConfigKey {
|
||||
if err := updateLegacyPublishOptions(tx, item); err != nil {
|
||||
if err := updateLegacyPublishOptions(tx, item, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -255,9 +265,13 @@ func (r *Repository) ensureDefaults(ctx context.Context) error {
|
||||
})
|
||||
}
|
||||
|
||||
func updateLegacyListingGroupWelcome(tx *gorm.DB, item defaultConfig) error {
|
||||
func updateLegacyListingGroupWelcome(tx *gorm.DB, item defaultConfig, tenantID uint64) error {
|
||||
var row model.SystemConfig
|
||||
if err := tx.Where("`key` = ?", item.Key).First(&row).Error; err != nil {
|
||||
query := tx.Where("`key` = ?", item.Key)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&row).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
changed := false
|
||||
@@ -275,9 +289,13 @@ func updateLegacyListingGroupWelcome(tx *gorm.DB, item defaultConfig) error {
|
||||
return tx.Save(&row).Error
|
||||
}
|
||||
|
||||
func updateDefaultSupportConfig(tx *gorm.DB, item defaultConfig) error {
|
||||
func updateDefaultSupportConfig(tx *gorm.DB, item defaultConfig, tenantID uint64) error {
|
||||
var row model.SystemConfig
|
||||
if err := tx.Where("`key` = ?", item.Key).First(&row).Error; err != nil {
|
||||
query := tx.Where("`key` = ?", item.Key)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&row).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
changed := false
|
||||
@@ -295,9 +313,13 @@ func updateDefaultSupportConfig(tx *gorm.DB, item defaultConfig) error {
|
||||
return tx.Save(&row).Error
|
||||
}
|
||||
|
||||
func updateLegacyPublishOptions(tx *gorm.DB, item defaultConfig) error {
|
||||
func updateLegacyPublishOptions(tx *gorm.DB, item defaultConfig, tenantID uint64) error {
|
||||
var row model.SystemConfig
|
||||
if err := tx.Where("`key` = ?", item.Key).First(&row).Error; err != nil {
|
||||
query := tx.Where("`key` = ?", item.Key)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&row).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
normalized, err := normalizePublishOptionsValue(row.Value)
|
||||
@@ -538,3 +560,10 @@ func toDTO(row model.SystemConfig) ConfigDTO {
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func applyTenantScope(ctx context.Context, db *gorm.DB) *gorm.DB {
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
return db.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user