package repository import ( "context" "errors" "gorm.io/gorm" "pay-bridge/internal/model" ) // ChannelConfigRepository 渠道配置数据访问 type ChannelConfigRepository struct { db *gorm.DB } func NewChannelConfigRepository(db *gorm.DB) *ChannelConfigRepository { return &ChannelConfigRepository{db: db} } // GetByAppChannel 按 app_id + channel_code 查询 func (r *ChannelConfigRepository) GetByAppChannel(ctx context.Context, appID, channelCode string) (*model.ChannelConfig, error) { var cfg model.ChannelConfig err := r.db.WithContext(ctx).Where("app_id = ? AND channel_code = ? AND status = 1", appID, channelCode).First(&cfg).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } return &cfg, err } // Create 创建渠道配置 func (r *ChannelConfigRepository) Create(ctx context.Context, cfg *model.ChannelConfig) error { return r.db.WithContext(ctx).Create(cfg).Error } // Update 更新渠道配置 func (r *ChannelConfigRepository) Update(ctx context.Context, id uint64, updates map[string]any) error { return r.db.WithContext(ctx).Model(&model.ChannelConfig{}).Where("id = ?", id).Updates(updates).Error } // ListByApp 查询应用下所有启用的渠道配置 func (r *ChannelConfigRepository) ListByApp(ctx context.Context, appID string) ([]*model.ChannelConfig, error) { var cfgs []*model.ChannelConfig err := r.db.WithContext(ctx).Where("app_id = ? AND status = 1", appID).Find(&cfgs).Error return cfgs, err }