feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -13,10 +14,11 @@ import (
|
||||
|
||||
// ExportBackup 导出所有支付配置备份,包含解密后的密钥。
|
||||
func (r *Repository) ExportBackup(ctx context.Context, actorID uint64, meta AuditMeta) (*ExportBackup, error) {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var backup *ExportBackup
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var items []model.PaymentMerchantConfig
|
||||
if err := tx.Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(tx, ctx).Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -42,6 +44,7 @@ func (r *Repository) ExportBackup(ctx context.Context, actorID uint64, meta Audi
|
||||
return appendAuditLog(tx, actorID, "payment_config.export", 0, meta, map[string]any{
|
||||
"total": len(configs),
|
||||
"exported_at": exportedAt,
|
||||
"tenant_id": tenantID,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
@@ -61,10 +64,11 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
|
||||
activeKeys := backupActiveKeys(backup.Configs)
|
||||
result := &ImportBackupResult{Total: len(backup.Configs)}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for payWay := range activeKeys {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -104,7 +108,7 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
}
|
||||
|
||||
var existing model.PaymentMerchantConfig
|
||||
err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
err = applyOwnerTenantScope(tx.Clauses(clause.Locking{Strength: "UPDATE"}), ctx).
|
||||
Where("provider = ? AND merchant_id = ? AND pay_way = ?", req.Provider, req.MerchantID, payWay).
|
||||
First(&existing).Error
|
||||
if err == nil {
|
||||
@@ -120,24 +124,25 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
|
||||
createdBy := actorID
|
||||
item := model.PaymentMerchantConfig{
|
||||
Name: req.Name,
|
||||
Provider: req.Provider,
|
||||
MerchantID: req.MerchantID,
|
||||
GatewayURL: req.GatewayURL,
|
||||
SignKey: encryptedSignKey,
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: model.JSONMap(req.ExtraConfig),
|
||||
IsDefault: req.IsDefault,
|
||||
Status: firstNonEmpty(req.Status, "active"),
|
||||
Environment: firstNonEmpty(req.Environment, "production"),
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &createdBy,
|
||||
UpdatedBy: &actorID,
|
||||
OwnerTenantID: tenantID,
|
||||
Name: req.Name,
|
||||
Provider: req.Provider,
|
||||
MerchantID: req.MerchantID,
|
||||
GatewayURL: req.GatewayURL,
|
||||
SignKey: encryptedSignKey,
|
||||
NotifyKey: encryptedNotifyKey,
|
||||
NotifyURL: req.NotifyURL,
|
||||
JumpURL: req.JumpURL,
|
||||
PayWay: payWay,
|
||||
JSPayFlag: firstNonEmpty(req.JSPayFlag, "2"),
|
||||
SignType: firstNonEmpty(req.SignType, defaultSignType(req.Provider)),
|
||||
ExtraConfig: model.JSONMap(req.ExtraConfig),
|
||||
IsDefault: req.IsDefault,
|
||||
Status: firstNonEmpty(req.Status, "active"),
|
||||
Environment: firstNonEmpty(req.Environment, "production"),
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &createdBy,
|
||||
UpdatedBy: &actorID,
|
||||
}
|
||||
if err := tx.Create(&item).Error; err != nil {
|
||||
return err
|
||||
@@ -146,9 +151,10 @@ func (r *Repository) ImportBackup(ctx context.Context, backup ExportBackup, acto
|
||||
}
|
||||
|
||||
return appendAuditLog(tx, actorID, "payment_config.import", 0, meta, map[string]any{
|
||||
"total": result.Total,
|
||||
"created": result.Created,
|
||||
"updated": result.Updated,
|
||||
"total": result.Total,
|
||||
"created": result.Created,
|
||||
"updated": result.Updated,
|
||||
"tenant_id": tenantID,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user