feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
// Create 创建配置
|
||||
func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
req = normalizeCreateRequest(req)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
// 验证必填字段
|
||||
if err := r.validateCreateRequest(req); err != nil {
|
||||
return nil, err
|
||||
@@ -42,7 +44,7 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
payWay := firstNonEmpty(req.PayWay, "ZFBZF")
|
||||
isDefault := req.IsDefault
|
||||
if status == "active" {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, 0, actorID, payWay, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
isDefault = true
|
||||
@@ -51,24 +53,25 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
}
|
||||
|
||||
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: req.ExtraConfig,
|
||||
IsDefault: isDefault,
|
||||
Status: status,
|
||||
Environment: environment,
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &actorID,
|
||||
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: req.ExtraConfig,
|
||||
IsDefault: isDefault,
|
||||
Status: status,
|
||||
Environment: environment,
|
||||
BusinessTags: req.BusinessTags,
|
||||
CreatedBy: &actorID,
|
||||
UpdatedBy: &actorID,
|
||||
}
|
||||
|
||||
if err := tx.Create(&item).Error; err != nil {
|
||||
@@ -77,8 +80,9 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest, actorID uint
|
||||
|
||||
// 记录审计日志
|
||||
if err := appendAuditLog(tx, actorID, "payment_config.create", item.ID, meta, map[string]any{
|
||||
"name": item.Name,
|
||||
"provider": item.Provider,
|
||||
"name": item.Name,
|
||||
"provider": item.Provider,
|
||||
"owner_tenant_id": item.OwnerTenantID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -110,9 +114,10 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRequest, a
|
||||
}
|
||||
|
||||
var dto ConfigDTO
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConfigNotFound
|
||||
}
|
||||
@@ -187,7 +192,7 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateRequest, a
|
||||
finalPayWay = "ZFBZF"
|
||||
}
|
||||
if finalStatus == "active" {
|
||||
if err := deactivateOtherConfigs(tx, id, actorID, finalPayWay); err != nil {
|
||||
if err := deactivateOtherConfigs(tx, id, actorID, finalPayWay, tenantID); err != nil {
|
||||
return err
|
||||
}
|
||||
updates["status"] = "active"
|
||||
@@ -288,7 +293,7 @@ func trimExtraConfig(config map[string]any) map[string]any {
|
||||
func (r *Repository) Delete(ctx context.Context, id uint64, actorID uint64, meta AuditMeta) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := tx.Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(tx.Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrConfigNotFound
|
||||
}
|
||||
@@ -320,8 +325,9 @@ func (r *Repository) Delete(ctx context.Context, id uint64, actorID uint64, meta
|
||||
}
|
||||
|
||||
// deactivateOtherConfigs 保证同一个支付方式同一时间只有一个启用配置。
|
||||
func deactivateOtherConfigs(tx *gorm.DB, activeID uint64, actorID uint64, payWay string) error {
|
||||
func deactivateOtherConfigs(tx *gorm.DB, activeID uint64, actorID uint64, payWay string, tenantID uint64) error {
|
||||
db := tx.Model(&model.PaymentMerchantConfig{}).
|
||||
Where("owner_tenant_id = ?", tenantID).
|
||||
Where("pay_way = ? AND (status = ? OR is_default = ?)", payWay, "active", true)
|
||||
if activeID > 0 {
|
||||
db = db.Where("id != ?", activeID)
|
||||
|
||||
Reference in New Issue
Block a user