// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"
	"math"
	"wechat-api/ent/payrecharge"
	"wechat-api/ent/predicate"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// PayRechargeQuery is the builder for querying PayRecharge entities.
type PayRechargeQuery struct {
	config
	ctx        *QueryContext
	order      []payrecharge.OrderOption
	inters     []Interceptor
	predicates []predicate.PayRecharge
	// intermediate query (i.e. traversal path).
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}

// Where adds a new predicate for the PayRechargeQuery builder.
func (prq *PayRechargeQuery) Where(ps ...predicate.PayRecharge) *PayRechargeQuery {
	prq.predicates = append(prq.predicates, ps...)
	return prq
}

// Limit the number of records to be returned by this query.
func (prq *PayRechargeQuery) Limit(limit int) *PayRechargeQuery {
	prq.ctx.Limit = &limit
	return prq
}

// Offset to start from.
func (prq *PayRechargeQuery) Offset(offset int) *PayRechargeQuery {
	prq.ctx.Offset = &offset
	return prq
}

// Unique configures the query builder to filter duplicate records on query.
// By default, unique is set to true, and can be disabled using this method.
func (prq *PayRechargeQuery) Unique(unique bool) *PayRechargeQuery {
	prq.ctx.Unique = &unique
	return prq
}

// Order specifies how the records should be ordered.
func (prq *PayRechargeQuery) Order(o ...payrecharge.OrderOption) *PayRechargeQuery {
	prq.order = append(prq.order, o...)
	return prq
}

// First returns the first PayRecharge entity from the query.
// Returns a *NotFoundError when no PayRecharge was found.
func (prq *PayRechargeQuery) First(ctx context.Context) (*PayRecharge, error) {
	nodes, err := prq.Limit(1).All(setContextOp(ctx, prq.ctx, "First"))
	if err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nil, &NotFoundError{payrecharge.Label}
	}
	return nodes[0], nil
}

// FirstX is like First, but panics if an error occurs.
func (prq *PayRechargeQuery) FirstX(ctx context.Context) *PayRecharge {
	node, err := prq.First(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return node
}

// FirstID returns the first PayRecharge ID from the query.
// Returns a *NotFoundError when no PayRecharge ID was found.
func (prq *PayRechargeQuery) FirstID(ctx context.Context) (id uint64, err error) {
	var ids []uint64
	if ids, err = prq.Limit(1).IDs(setContextOp(ctx, prq.ctx, "FirstID")); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &NotFoundError{payrecharge.Label}
		return
	}
	return ids[0], nil
}

// FirstIDX is like FirstID, but panics if an error occurs.
func (prq *PayRechargeQuery) FirstIDX(ctx context.Context) uint64 {
	id, err := prq.FirstID(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return id
}

// Only returns a single PayRecharge entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one PayRecharge entity is found.
// Returns a *NotFoundError when no PayRecharge entities are found.
func (prq *PayRechargeQuery) Only(ctx context.Context) (*PayRecharge, error) {
	nodes, err := prq.Limit(2).All(setContextOp(ctx, prq.ctx, "Only"))
	if err != nil {
		return nil, err
	}
	switch len(nodes) {
	case 1:
		return nodes[0], nil
	case 0:
		return nil, &NotFoundError{payrecharge.Label}
	default:
		return nil, &NotSingularError{payrecharge.Label}
	}
}

// OnlyX is like Only, but panics if an error occurs.
func (prq *PayRechargeQuery) OnlyX(ctx context.Context) *PayRecharge {
	node, err := prq.Only(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// OnlyID is like Only, but returns the only PayRecharge ID in the query.
// Returns a *NotSingularError when more than one PayRecharge ID is found.
// Returns a *NotFoundError when no entities are found.
func (prq *PayRechargeQuery) OnlyID(ctx context.Context) (id uint64, err error) {
	var ids []uint64
	if ids, err = prq.Limit(2).IDs(setContextOp(ctx, prq.ctx, "OnlyID")); err != nil {
		return
	}
	switch len(ids) {
	case 1:
		id = ids[0]
	case 0:
		err = &NotFoundError{payrecharge.Label}
	default:
		err = &NotSingularError{payrecharge.Label}
	}
	return
}

// OnlyIDX is like OnlyID, but panics if an error occurs.
func (prq *PayRechargeQuery) OnlyIDX(ctx context.Context) uint64 {
	id, err := prq.OnlyID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// All executes the query and returns a list of PayRecharges.
func (prq *PayRechargeQuery) All(ctx context.Context) ([]*PayRecharge, error) {
	ctx = setContextOp(ctx, prq.ctx, "All")
	if err := prq.prepareQuery(ctx); err != nil {
		return nil, err
	}
	qr := querierAll[[]*PayRecharge, *PayRechargeQuery]()
	return withInterceptors[[]*PayRecharge](ctx, prq, qr, prq.inters)
}

// AllX is like All, but panics if an error occurs.
func (prq *PayRechargeQuery) AllX(ctx context.Context) []*PayRecharge {
	nodes, err := prq.All(ctx)
	if err != nil {
		panic(err)
	}
	return nodes
}

// IDs executes the query and returns a list of PayRecharge IDs.
func (prq *PayRechargeQuery) IDs(ctx context.Context) (ids []uint64, err error) {
	if prq.ctx.Unique == nil && prq.path != nil {
		prq.Unique(true)
	}
	ctx = setContextOp(ctx, prq.ctx, "IDs")
	if err = prq.Select(payrecharge.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

// IDsX is like IDs, but panics if an error occurs.
func (prq *PayRechargeQuery) IDsX(ctx context.Context) []uint64 {
	ids, err := prq.IDs(ctx)
	if err != nil {
		panic(err)
	}
	return ids
}

// Count returns the count of the given query.
func (prq *PayRechargeQuery) Count(ctx context.Context) (int, error) {
	ctx = setContextOp(ctx, prq.ctx, "Count")
	if err := prq.prepareQuery(ctx); err != nil {
		return 0, err
	}
	return withInterceptors[int](ctx, prq, querierCount[*PayRechargeQuery](), prq.inters)
}

// CountX is like Count, but panics if an error occurs.
func (prq *PayRechargeQuery) CountX(ctx context.Context) int {
	count, err := prq.Count(ctx)
	if err != nil {
		panic(err)
	}
	return count
}

// Exist returns true if the query has elements in the graph.
func (prq *PayRechargeQuery) Exist(ctx context.Context) (bool, error) {
	ctx = setContextOp(ctx, prq.ctx, "Exist")
	switch _, err := prq.FirstID(ctx); {
	case IsNotFound(err):
		return false, nil
	case err != nil:
		return false, fmt.Errorf("ent: check existence: %w", err)
	default:
		return true, nil
	}
}

// ExistX is like Exist, but panics if an error occurs.
func (prq *PayRechargeQuery) ExistX(ctx context.Context) bool {
	exist, err := prq.Exist(ctx)
	if err != nil {
		panic(err)
	}
	return exist
}

// Clone returns a duplicate of the PayRechargeQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (prq *PayRechargeQuery) Clone() *PayRechargeQuery {
	if prq == nil {
		return nil
	}
	return &PayRechargeQuery{
		config:     prq.config,
		ctx:        prq.ctx.Clone(),
		order:      append([]payrecharge.OrderOption{}, prq.order...),
		inters:     append([]Interceptor{}, prq.inters...),
		predicates: append([]predicate.PayRecharge{}, prq.predicates...),
		// clone intermediate query.
		sql:  prq.sql.Clone(),
		path: prq.path,
	}
}

// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
//	var v []struct {
//		CreatedAt time.Time `json:"created_at,omitempty"`
//		Count int `json:"count,omitempty"`
//	}
//
//	client.PayRecharge.Query().
//		GroupBy(payrecharge.FieldCreatedAt).
//		Aggregate(ent.Count()).
//		Scan(ctx, &v)
func (prq *PayRechargeQuery) GroupBy(field string, fields ...string) *PayRechargeGroupBy {
	prq.ctx.Fields = append([]string{field}, fields...)
	grbuild := &PayRechargeGroupBy{build: prq}
	grbuild.flds = &prq.ctx.Fields
	grbuild.label = payrecharge.Label
	grbuild.scan = grbuild.Scan
	return grbuild
}

// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
//	var v []struct {
//		CreatedAt time.Time `json:"created_at,omitempty"`
//	}
//
//	client.PayRecharge.Query().
//		Select(payrecharge.FieldCreatedAt).
//		Scan(ctx, &v)
func (prq *PayRechargeQuery) Select(fields ...string) *PayRechargeSelect {
	prq.ctx.Fields = append(prq.ctx.Fields, fields...)
	sbuild := &PayRechargeSelect{PayRechargeQuery: prq}
	sbuild.label = payrecharge.Label
	sbuild.flds, sbuild.scan = &prq.ctx.Fields, sbuild.Scan
	return sbuild
}

// Aggregate returns a PayRechargeSelect configured with the given aggregations.
func (prq *PayRechargeQuery) Aggregate(fns ...AggregateFunc) *PayRechargeSelect {
	return prq.Select().Aggregate(fns...)
}

func (prq *PayRechargeQuery) prepareQuery(ctx context.Context) error {
	for _, inter := range prq.inters {
		if inter == nil {
			return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
		}
		if trv, ok := inter.(Traverser); ok {
			if err := trv.Traverse(ctx, prq); err != nil {
				return err
			}
		}
	}
	for _, f := range prq.ctx.Fields {
		if !payrecharge.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
		}
	}
	if prq.path != nil {
		prev, err := prq.path(ctx)
		if err != nil {
			return err
		}
		prq.sql = prev
	}
	return nil
}

func (prq *PayRechargeQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*PayRecharge, error) {
	var (
		nodes = []*PayRecharge{}
		_spec = prq.querySpec()
	)
	_spec.ScanValues = func(columns []string) ([]any, error) {
		return (*PayRecharge).scanValues(nil, columns)
	}
	_spec.Assign = func(columns []string, values []any) error {
		node := &PayRecharge{config: prq.config}
		nodes = append(nodes, node)
		return node.assignValues(columns, values)
	}
	for i := range hooks {
		hooks[i](ctx, _spec)
	}
	if err := sqlgraph.QueryNodes(ctx, prq.driver, _spec); err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nodes, nil
	}
	return nodes, nil
}

func (prq *PayRechargeQuery) sqlCount(ctx context.Context) (int, error) {
	_spec := prq.querySpec()
	_spec.Node.Columns = prq.ctx.Fields
	if len(prq.ctx.Fields) > 0 {
		_spec.Unique = prq.ctx.Unique != nil && *prq.ctx.Unique
	}
	return sqlgraph.CountNodes(ctx, prq.driver, _spec)
}

func (prq *PayRechargeQuery) querySpec() *sqlgraph.QuerySpec {
	_spec := sqlgraph.NewQuerySpec(payrecharge.Table, payrecharge.Columns, sqlgraph.NewFieldSpec(payrecharge.FieldID, field.TypeUint64))
	_spec.From = prq.sql
	if unique := prq.ctx.Unique; unique != nil {
		_spec.Unique = *unique
	} else if prq.path != nil {
		_spec.Unique = true
	}
	if fields := prq.ctx.Fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, payrecharge.FieldID)
		for i := range fields {
			if fields[i] != payrecharge.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
			}
		}
	}
	if ps := prq.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if limit := prq.ctx.Limit; limit != nil {
		_spec.Limit = *limit
	}
	if offset := prq.ctx.Offset; offset != nil {
		_spec.Offset = *offset
	}
	if ps := prq.order; len(ps) > 0 {
		_spec.Order = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return _spec
}

func (prq *PayRechargeQuery) sqlQuery(ctx context.Context) *sql.Selector {
	builder := sql.Dialect(prq.driver.Dialect())
	t1 := builder.Table(payrecharge.Table)
	columns := prq.ctx.Fields
	if len(columns) == 0 {
		columns = payrecharge.Columns
	}
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
	if prq.sql != nil {
		selector = prq.sql
		selector.Select(selector.Columns(columns...)...)
	}
	if prq.ctx.Unique != nil && *prq.ctx.Unique {
		selector.Distinct()
	}
	for _, p := range prq.predicates {
		p(selector)
	}
	for _, p := range prq.order {
		p(selector)
	}
	if offset := prq.ctx.Offset; offset != nil {
		// limit is mandatory for offset clause. We start
		// with default value, and override it below if needed.
		selector.Offset(*offset).Limit(math.MaxInt32)
	}
	if limit := prq.ctx.Limit; limit != nil {
		selector.Limit(*limit)
	}
	return selector
}

// PayRechargeGroupBy is the group-by builder for PayRecharge entities.
type PayRechargeGroupBy struct {
	selector
	build *PayRechargeQuery
}

// Aggregate adds the given aggregation functions to the group-by query.
func (prgb *PayRechargeGroupBy) Aggregate(fns ...AggregateFunc) *PayRechargeGroupBy {
	prgb.fns = append(prgb.fns, fns...)
	return prgb
}

// Scan applies the selector query and scans the result into the given value.
func (prgb *PayRechargeGroupBy) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, prgb.build.ctx, "GroupBy")
	if err := prgb.build.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*PayRechargeQuery, *PayRechargeGroupBy](ctx, prgb.build, prgb, prgb.build.inters, v)
}

func (prgb *PayRechargeGroupBy) sqlScan(ctx context.Context, root *PayRechargeQuery, v any) error {
	selector := root.sqlQuery(ctx).Select()
	aggregation := make([]string, 0, len(prgb.fns))
	for _, fn := range prgb.fns {
		aggregation = append(aggregation, fn(selector))
	}
	if len(selector.SelectedColumns()) == 0 {
		columns := make([]string, 0, len(*prgb.flds)+len(prgb.fns))
		for _, f := range *prgb.flds {
			columns = append(columns, selector.C(f))
		}
		columns = append(columns, aggregation...)
		selector.Select(columns...)
	}
	selector.GroupBy(selector.Columns(*prgb.flds...)...)
	if err := selector.Err(); err != nil {
		return err
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := prgb.build.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}

// PayRechargeSelect is the builder for selecting fields of PayRecharge entities.
type PayRechargeSelect struct {
	*PayRechargeQuery
	selector
}

// Aggregate adds the given aggregation functions to the selector query.
func (prs *PayRechargeSelect) Aggregate(fns ...AggregateFunc) *PayRechargeSelect {
	prs.fns = append(prs.fns, fns...)
	return prs
}

// Scan applies the selector query and scans the result into the given value.
func (prs *PayRechargeSelect) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, prs.ctx, "Select")
	if err := prs.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*PayRechargeQuery, *PayRechargeSelect](ctx, prs.PayRechargeQuery, prs, prs.inters, v)
}

func (prs *PayRechargeSelect) sqlScan(ctx context.Context, root *PayRechargeQuery, v any) error {
	selector := root.sqlQuery(ctx)
	aggregation := make([]string, 0, len(prs.fns))
	for _, fn := range prs.fns {
		aggregation = append(aggregation, fn(selector))
	}
	switch n := len(*prs.selector.flds); {
	case n == 0 && len(aggregation) > 0:
		selector.Select(aggregation...)
	case n != 0 && len(aggregation) > 0:
		selector.AppendSelect(aggregation...)
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := prs.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}
