package repository import ( "context" "errors" "gorm.io/gorm" "pay-bridge/internal/model" ) // ServiceFeeRepository 服务费数据访问 type ServiceFeeRepository struct { db *gorm.DB } func NewServiceFeeRepository(db *gorm.DB) *ServiceFeeRepository { return &ServiceFeeRepository{db: db} } // GetConfig 按 app_id + 支付方式分组查询配置 func (r *ServiceFeeRepository) GetConfig(ctx context.Context, appID string, group model.PayMethodGroup) (*model.ServiceFeeConfig, error) { var cfg model.ServiceFeeConfig err := r.db.WithContext(ctx). Where("app_id = ? AND pay_method_group = ? AND status = 1", appID, group). First(&cfg).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } return &cfg, err } // ListConfigs 查询应用所有服务费配置 func (r *ServiceFeeRepository) ListConfigs(ctx context.Context, appID string) ([]*model.ServiceFeeConfig, error) { var cfgs []*model.ServiceFeeConfig err := r.db.WithContext(ctx).Where("app_id = ? AND status = 1", appID).Find(&cfgs).Error return cfgs, err } // SaveConfig 保存配置(创建或更新) func (r *ServiceFeeRepository) SaveConfig(ctx context.Context, cfg *model.ServiceFeeConfig) error { return r.db.WithContext(ctx).Save(cfg).Error } // CreateLog 创建服务费流水 func (r *ServiceFeeRepository) CreateLog(ctx context.Context, log *model.ServiceFeeLog) error { return r.db.WithContext(ctx).Create(log).Error } // GetLog 按 trade_no + action 查询流水 func (r *ServiceFeeRepository) GetLog(ctx context.Context, tradeNo, action string) (*model.ServiceFeeLog, error) { var log model.ServiceFeeLog err := r.db.WithContext(ctx).Where("trade_no = ? AND action = ?", tradeNo, action).First(&log).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } return &log, err } // UpdateLogStatus 更新流水状态 func (r *ServiceFeeRepository) UpdateLogStatus(ctx context.Context, id uint64, status, channelSharingNo string) error { updates := map[string]any{"status": status} if channelSharingNo != "" { updates["channel_sharing_no"] = channelSharingNo } return r.db.WithContext(ctx).Model(&model.ServiceFeeLog{}).Where("id = ?", id).Updates(updates).Error }