优化结构
This commit is contained in:
@@ -27,10 +27,38 @@ func (ConfigPO) TableName() string { return "sys_integration_configs" }
|
||||
|
||||
type integrationConfigRepo struct{ data Provider }
|
||||
|
||||
type paymentConfigReader struct{ data Provider }
|
||||
|
||||
func NewIntegrationConfigRepo(data Provider) integrationbiz.IntegrationConfigRepo {
|
||||
return &integrationConfigRepo{data: data}
|
||||
}
|
||||
|
||||
// NewPaymentConfigReader exposes only the raw payment configuration needed by
|
||||
// the payment data module. The ConfigPO and its table name stay private here.
|
||||
func NewPaymentConfigReader(data Provider) integrationbiz.PaymentConfigReader {
|
||||
return &paymentConfigReader{data: data}
|
||||
}
|
||||
|
||||
func (r *paymentConfigReader) ReadPaymentConfig(ctx context.Context, provider string) (*integrationbiz.PaymentConfig, error) {
|
||||
if r == nil || r.data == nil || r.data.DB() == nil {
|
||||
return nil, errors.New("集成配置数据库未初始化")
|
||||
}
|
||||
provider = strings.ToLower(strings.TrimSpace(provider))
|
||||
if provider == "" {
|
||||
return nil, errors.New("支付渠道不能为空")
|
||||
}
|
||||
var row ConfigPO
|
||||
if err := r.data.DB().WithContext(ctx).
|
||||
Where("kind = ? AND provider = ?", integrationbiz.IntegrationKindPayment, provider).
|
||||
First(&row).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, integrationbiz.ErrPaymentConfigNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &integrationbiz.PaymentConfig{Enabled: row.Enabled, Values: append(json.RawMessage(nil), []byte(row.Config)...)}, nil
|
||||
}
|
||||
|
||||
func (r *integrationConfigRepo) ListIntegrationConfigs(ctx context.Context, kind string) ([]*integrationbiz.IntegrationConfig, error) {
|
||||
var rows []ConfigPO
|
||||
if err := r.data.DB().WithContext(ctx).Where("kind = ?", kind).Order("provider ASC").Find(&rows).Error; err != nil {
|
||||
|
||||
@@ -2,4 +2,4 @@ package integration
|
||||
|
||||
import "github.com/google/wire"
|
||||
|
||||
var ProviderSet = wire.NewSet(NewIntegrationConfigRepo)
|
||||
var ProviderSet = wire.NewSet(NewIntegrationConfigRepo, NewPaymentConfigReader)
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"fmt"
|
||||
integrationbiz "kra/internal/biz/integration"
|
||||
bizpayment "kra/internal/biz/payment"
|
||||
dataintegration "kra/internal/data/integration"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -18,67 +17,40 @@ import (
|
||||
datapayment "kra/internal/integration/payment"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type paymentRepo struct{ data Provider }
|
||||
|
||||
func NewPaymentRepo(data Provider) bizpayment.PaymentRepo { return &paymentRepo{data: data} }
|
||||
|
||||
func ensurePaymentIntegrationConfigs(db *gorm.DB) error {
|
||||
for _, provider := range bizpayment.SupportedPaymentProviders {
|
||||
var row dataintegration.ConfigPO
|
||||
err := db.Where("kind = ? AND provider = ?", integrationbiz.IntegrationKindPayment, provider).First(&row).Error
|
||||
defaults := integrationbiz.DefaultIntegrationConfig(integrationbiz.IntegrationKindPayment, provider)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
encoded, _ := json.Marshal(defaults)
|
||||
if err := db.Create(&dataintegration.ConfigPO{Kind: integrationbiz.IntegrationKindPayment, Provider: provider, Enabled: false, Config: string(encoded)}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
values := map[string]any{}
|
||||
_ = json.Unmarshal([]byte(row.Config), &values)
|
||||
changed := false
|
||||
for key, value := range defaults {
|
||||
if _, exists := values[key]; !exists {
|
||||
values[key] = value
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
encoded, _ := json.Marshal(values)
|
||||
if err := db.Model(&row).Update("config", string(encoded)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
type paymentRepo struct {
|
||||
data Provider
|
||||
config integrationbiz.PaymentConfigReader
|
||||
}
|
||||
|
||||
func (r *paymentRepo) row(ctx context.Context, provider string) (*dataintegration.ConfigPO, map[string]any, error) {
|
||||
var row dataintegration.ConfigPO
|
||||
if err := r.data.DB().WithContext(ctx).Where("kind = ? AND provider = ?", integrationbiz.IntegrationKindPayment, provider).First(&row).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil, bizpayment.ErrPaymentProviderNotFound
|
||||
}
|
||||
return nil, nil, err
|
||||
func NewPaymentRepo(data Provider, config integrationbiz.PaymentConfigReader) bizpayment.PaymentRepo {
|
||||
return &paymentRepo{data: data, config: config}
|
||||
}
|
||||
|
||||
func (r *paymentRepo) values(ctx context.Context, provider string) (map[string]any, error) {
|
||||
if r == nil || r.config == nil {
|
||||
return nil, errors.New("支付配置仓储未接入")
|
||||
}
|
||||
if !row.Enabled {
|
||||
return nil, nil, fmt.Errorf("支付渠道 %s 未启用", provider)
|
||||
config, err := r.config.ReadPaymentConfig(ctx, provider)
|
||||
if err != nil {
|
||||
if errors.Is(err, integrationbiz.ErrPaymentConfigNotFound) {
|
||||
return nil, bizpayment.ErrPaymentProviderNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if config == nil || !config.Enabled {
|
||||
return nil, fmt.Errorf("支付渠道 %s 未启用", provider)
|
||||
}
|
||||
values := map[string]any{}
|
||||
if err := json.Unmarshal([]byte(row.Config), &values); err != nil {
|
||||
return nil, nil, fmt.Errorf("支付配置格式错误: %w", err)
|
||||
if err := json.Unmarshal(config.Values, &values); err != nil {
|
||||
return nil, fmt.Errorf("支付配置格式错误: %w", err)
|
||||
}
|
||||
return &row, values, nil
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func (r *paymentRepo) adapter(ctx context.Context, provider string) (datapayment.Adapter, map[string]any, error) {
|
||||
_, values, err := r.row(ctx, provider)
|
||||
values, err := r.values(ctx, provider)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Provider is the narrow persistence seam required by payment repositories.
|
||||
// Keeping it here lets payment remain an independent data module.
|
||||
|
||||
Reference in New Issue
Block a user