优化结构
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)
|
||||
|
||||
Reference in New Issue
Block a user