feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -14,7 +15,7 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
var items []model.PaymentMerchantConfig
|
||||
var total int64
|
||||
|
||||
db := r.db.WithContext(ctx).Model(&model.PaymentMerchantConfig{})
|
||||
db := applyOwnerTenantScope(r.db.WithContext(ctx).Model(&model.PaymentMerchantConfig{}), ctx)
|
||||
|
||||
// 过滤条件
|
||||
if query.Provider != "" {
|
||||
@@ -72,7 +73,7 @@ func (r *Repository) List(ctx context.Context, query ListQuery) ([]ConfigDTO, in
|
||||
// FindByID 根据 ID 查询配置
|
||||
func (r *Repository) FindByID(ctx context.Context, id uint64, includeSecret bool, actorID uint64, meta AuditMeta) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx).Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrConfigNotFound
|
||||
}
|
||||
@@ -134,7 +135,7 @@ func (r *Repository) applyPaymentReferenceFlags(ctx context.Context, dtos []Conf
|
||||
// FindRuntimeByID 根据配置 ID 查询运行时配置,不写审计日志,供支付单回溯原配置使用。
|
||||
func (r *Repository) FindRuntimeByID(ctx context.Context, id uint64, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx).Where("id = ?", id), ctx).First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrConfigNotFound
|
||||
}
|
||||
@@ -150,7 +151,8 @@ func (r *Repository) FindRuntimeByID(ctx context.Context, id uint64, includeSecr
|
||||
// FindDefault 查询指定服务商的默认启用配置,兼容旧调用路径。
|
||||
func (r *Repository) FindDefault(ctx context.Context, provider string) (*model.PaymentMerchantConfig, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND is_default = ? AND status = ?", provider, true, "active").First(&item).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND is_default = ? AND status = ?", provider, true, "active").First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNoActiveConfigFound
|
||||
}
|
||||
@@ -162,7 +164,7 @@ func (r *Repository) FindDefault(ctx context.Context, provider string) (*model.P
|
||||
// FindDefaultByPayWay 查询指定支付方式的默认启用配置。
|
||||
func (r *Repository) FindDefaultByPayWay(ctx context.Context, payWay string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("pay_way = ? AND status = ?", payWay, "active").
|
||||
Order("is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
@@ -194,7 +196,7 @@ func (r *Repository) FindDefaultByProvider(ctx context.Context, provider string,
|
||||
// FindDefaultByProviderPayWay 查询指定服务商和支付方式的默认启用配置。
|
||||
func (r *Repository) FindDefaultByProviderPayWay(ctx context.Context, provider string, payWay string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND pay_way = ? AND status = ?", provider, payWay, "active").
|
||||
Order("is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
@@ -213,7 +215,8 @@ func (r *Repository) FindDefaultByProviderPayWay(ctx context.Context, provider s
|
||||
// FindByProviderMerchantPayWay 根据服务商、商户号和支付方式查配置,用于旧支付单回溯原商户密钥。
|
||||
func (r *Repository) FindByProviderMerchantPayWay(ctx context.Context, provider string, merchantID string, payWay string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND merchant_id = ? AND pay_way = ?", provider, merchantID, payWay).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND merchant_id = ? AND pay_way = ?", provider, merchantID, payWay).
|
||||
Order("status = 'active' DESC, is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -231,7 +234,8 @@ func (r *Repository) FindByProviderMerchantPayWay(ctx context.Context, provider
|
||||
// FindByProviderMerchant 根据服务商和商户号查配置,保留给历史数据没有支付方式时兜底使用。
|
||||
func (r *Repository) FindByProviderMerchant(ctx context.Context, provider string, merchantID string, includeSecret bool) (*ConfigDTO, error) {
|
||||
var item model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND merchant_id = ?", provider, merchantID).
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND merchant_id = ?", provider, merchantID).
|
||||
Order("status = 'active' DESC, is_default DESC, id DESC").
|
||||
First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -249,10 +253,18 @@ func (r *Repository) FindByProviderMerchant(ctx context.Context, provider string
|
||||
// FindActiveByProvider 查询提供商的所有激活配置
|
||||
func (r *Repository) FindActiveByProvider(ctx context.Context, provider string) ([]model.PaymentMerchantConfig, error) {
|
||||
var items []model.PaymentMerchantConfig
|
||||
if err := r.db.WithContext(ctx).Where("provider = ? AND status = ?", provider, "active").Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
if err := applyOwnerTenantScope(r.db.WithContext(ctx), ctx).
|
||||
Where("provider = ? AND status = ?", provider, "active").Order("is_default DESC, id DESC").Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func applyOwnerTenantScope(db *gorm.DB, ctx context.Context) *gorm.DB {
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
return db.Where("owner_tenant_id = ?", tenantID)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// Create 创建配置
|
||||
|
||||
Reference in New Issue
Block a user