package repository import ( "context" "errors" "gorm.io/gorm" "pay-bridge/internal/model" ) // ReconciliationRepository 对账数据访问 type ReconciliationRepository struct { db *gorm.DB } func NewReconciliationRepository(db *gorm.DB) *ReconciliationRepository { return &ReconciliationRepository{db: db} } // CreateReport 创建对账报告 func (r *ReconciliationRepository) CreateReport(ctx context.Context, report *model.ReconciliationReport) error { return r.db.WithContext(ctx).Create(report).Error } // GetReport 查询对账报告 func (r *ReconciliationRepository) GetReport(ctx context.Context, appID, billDate, channelCode string) (*model.ReconciliationReport, error) { var report model.ReconciliationReport err := r.db.WithContext(ctx). Where("app_id = ? AND bill_date = ? AND channel_code = ?", appID, billDate, channelCode). First(&report).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } return &report, err } // UpdateReport 更新对账报告 func (r *ReconciliationRepository) UpdateReport(ctx context.Context, id uint64, updates map[string]any) error { return r.db.WithContext(ctx).Model(&model.ReconciliationReport{}).Where("id = ?", id).Updates(updates).Error } // CreateException 创建对账异常记录 func (r *ReconciliationRepository) CreateException(ctx context.Context, ex *model.ReconciliationException) error { return r.db.WithContext(ctx).Create(ex).Error } // ListExceptions 查询报告下的异常明细 func (r *ReconciliationRepository) ListExceptions(ctx context.Context, reportID uint64) ([]*model.ReconciliationException, error) { var exs []*model.ReconciliationException err := r.db.WithContext(ctx).Where("report_id = ?", reportID).Find(&exs).Error return exs, err } // ListPaidOrdersByDate 查询指定日期的已支付订单(用于对账) func (r *ReconciliationRepository) ListPaidOrdersByDate(ctx context.Context, appID, date string) ([]*model.TradeOrder, error) { var orders []*model.TradeOrder err := r.db.WithContext(ctx). Where("app_id = ? AND status = ? AND DATE(pay_time) = ?", appID, model.TradeStatusPaid, date). Find(&orders).Error return orders, err }