// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/payrecharge"

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

// PayRechargeCreate is the builder for creating a PayRecharge entity.
type PayRechargeCreate struct {
	config
	mutation *PayRechargeMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (prc *PayRechargeCreate) SetCreatedAt(t time.Time) *PayRechargeCreate {
	prc.mutation.SetCreatedAt(t)
	return prc
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableCreatedAt(t *time.Time) *PayRechargeCreate {
	if t != nil {
		prc.SetCreatedAt(*t)
	}
	return prc
}

// SetUpdatedAt sets the "updated_at" field.
func (prc *PayRechargeCreate) SetUpdatedAt(t time.Time) *PayRechargeCreate {
	prc.mutation.SetUpdatedAt(t)
	return prc
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableUpdatedAt(t *time.Time) *PayRechargeCreate {
	if t != nil {
		prc.SetUpdatedAt(*t)
	}
	return prc
}

// SetDeletedAt sets the "deleted_at" field.
func (prc *PayRechargeCreate) SetDeletedAt(t time.Time) *PayRechargeCreate {
	prc.mutation.SetDeletedAt(t)
	return prc
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableDeletedAt(t *time.Time) *PayRechargeCreate {
	if t != nil {
		prc.SetDeletedAt(*t)
	}
	return prc
}

// SetUserID sets the "user_id" field.
func (prc *PayRechargeCreate) SetUserID(s string) *PayRechargeCreate {
	prc.mutation.SetUserID(s)
	return prc
}

// SetNumber sets the "number" field.
func (prc *PayRechargeCreate) SetNumber(f float32) *PayRechargeCreate {
	prc.mutation.SetNumber(f)
	return prc
}

// SetNillableNumber sets the "number" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableNumber(f *float32) *PayRechargeCreate {
	if f != nil {
		prc.SetNumber(*f)
	}
	return prc
}

// SetStatus sets the "status" field.
func (prc *PayRechargeCreate) SetStatus(i int) *PayRechargeCreate {
	prc.mutation.SetStatus(i)
	return prc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableStatus(i *int) *PayRechargeCreate {
	if i != nil {
		prc.SetStatus(*i)
	}
	return prc
}

// SetMoney sets the "money" field.
func (prc *PayRechargeCreate) SetMoney(f float32) *PayRechargeCreate {
	prc.mutation.SetMoney(f)
	return prc
}

// SetNillableMoney sets the "money" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableMoney(f *float32) *PayRechargeCreate {
	if f != nil {
		prc.SetMoney(*f)
	}
	return prc
}

// SetOutTradeNo sets the "out_trade_no" field.
func (prc *PayRechargeCreate) SetOutTradeNo(s string) *PayRechargeCreate {
	prc.mutation.SetOutTradeNo(s)
	return prc
}

// SetNillableOutTradeNo sets the "out_trade_no" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableOutTradeNo(s *string) *PayRechargeCreate {
	if s != nil {
		prc.SetOutTradeNo(*s)
	}
	return prc
}

// SetOrganizationID sets the "organization_id" field.
func (prc *PayRechargeCreate) SetOrganizationID(u uint64) *PayRechargeCreate {
	prc.mutation.SetOrganizationID(u)
	return prc
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (prc *PayRechargeCreate) SetNillableOrganizationID(u *uint64) *PayRechargeCreate {
	if u != nil {
		prc.SetOrganizationID(*u)
	}
	return prc
}

// SetID sets the "id" field.
func (prc *PayRechargeCreate) SetID(u uint64) *PayRechargeCreate {
	prc.mutation.SetID(u)
	return prc
}

// Mutation returns the PayRechargeMutation object of the builder.
func (prc *PayRechargeCreate) Mutation() *PayRechargeMutation {
	return prc.mutation
}

// Save creates the PayRecharge in the database.
func (prc *PayRechargeCreate) Save(ctx context.Context) (*PayRecharge, error) {
	if err := prc.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, prc.sqlSave, prc.mutation, prc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (prc *PayRechargeCreate) SaveX(ctx context.Context) *PayRecharge {
	v, err := prc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (prc *PayRechargeCreate) Exec(ctx context.Context) error {
	_, err := prc.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (prc *PayRechargeCreate) ExecX(ctx context.Context) {
	if err := prc.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (prc *PayRechargeCreate) defaults() error {
	if _, ok := prc.mutation.CreatedAt(); !ok {
		if payrecharge.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized payrecharge.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := payrecharge.DefaultCreatedAt()
		prc.mutation.SetCreatedAt(v)
	}
	if _, ok := prc.mutation.UpdatedAt(); !ok {
		if payrecharge.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized payrecharge.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := payrecharge.DefaultUpdatedAt()
		prc.mutation.SetUpdatedAt(v)
	}
	if _, ok := prc.mutation.Number(); !ok {
		v := payrecharge.DefaultNumber
		prc.mutation.SetNumber(v)
	}
	if _, ok := prc.mutation.Status(); !ok {
		v := payrecharge.DefaultStatus
		prc.mutation.SetStatus(v)
	}
	if _, ok := prc.mutation.Money(); !ok {
		v := payrecharge.DefaultMoney
		prc.mutation.SetMoney(v)
	}
	if _, ok := prc.mutation.OutTradeNo(); !ok {
		v := payrecharge.DefaultOutTradeNo
		prc.mutation.SetOutTradeNo(v)
	}
	if _, ok := prc.mutation.OrganizationID(); !ok {
		v := payrecharge.DefaultOrganizationID
		prc.mutation.SetOrganizationID(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (prc *PayRechargeCreate) check() error {
	if _, ok := prc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "PayRecharge.created_at"`)}
	}
	if _, ok := prc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "PayRecharge.updated_at"`)}
	}
	if _, ok := prc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "PayRecharge.user_id"`)}
	}
	if v, ok := prc.mutation.UserID(); ok {
		if err := payrecharge.UserIDValidator(v); err != nil {
			return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "PayRecharge.user_id": %w`, err)}
		}
	}
	if _, ok := prc.mutation.Number(); !ok {
		return &ValidationError{Name: "number", err: errors.New(`ent: missing required field "PayRecharge.number"`)}
	}
	if v, ok := prc.mutation.Status(); ok {
		if err := payrecharge.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PayRecharge.status": %w`, err)}
		}
	}
	if _, ok := prc.mutation.OrganizationID(); !ok {
		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "PayRecharge.organization_id"`)}
	}
	if v, ok := prc.mutation.OrganizationID(); ok {
		if err := payrecharge.OrganizationIDValidator(v); err != nil {
			return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "PayRecharge.organization_id": %w`, err)}
		}
	}
	return nil
}

func (prc *PayRechargeCreate) sqlSave(ctx context.Context) (*PayRecharge, error) {
	if err := prc.check(); err != nil {
		return nil, err
	}
	_node, _spec := prc.createSpec()
	if err := sqlgraph.CreateNode(ctx, prc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint64(id)
	}
	prc.mutation.id = &_node.ID
	prc.mutation.done = true
	return _node, nil
}

func (prc *PayRechargeCreate) createSpec() (*PayRecharge, *sqlgraph.CreateSpec) {
	var (
		_node = &PayRecharge{config: prc.config}
		_spec = sqlgraph.NewCreateSpec(payrecharge.Table, sqlgraph.NewFieldSpec(payrecharge.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = prc.conflict
	if id, ok := prc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := prc.mutation.CreatedAt(); ok {
		_spec.SetField(payrecharge.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := prc.mutation.UpdatedAt(); ok {
		_spec.SetField(payrecharge.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := prc.mutation.DeletedAt(); ok {
		_spec.SetField(payrecharge.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := prc.mutation.UserID(); ok {
		_spec.SetField(payrecharge.FieldUserID, field.TypeString, value)
		_node.UserID = value
	}
	if value, ok := prc.mutation.Number(); ok {
		_spec.SetField(payrecharge.FieldNumber, field.TypeFloat32, value)
		_node.Number = value
	}
	if value, ok := prc.mutation.Status(); ok {
		_spec.SetField(payrecharge.FieldStatus, field.TypeInt, value)
		_node.Status = value
	}
	if value, ok := prc.mutation.Money(); ok {
		_spec.SetField(payrecharge.FieldMoney, field.TypeFloat32, value)
		_node.Money = value
	}
	if value, ok := prc.mutation.OutTradeNo(); ok {
		_spec.SetField(payrecharge.FieldOutTradeNo, field.TypeString, value)
		_node.OutTradeNo = value
	}
	if value, ok := prc.mutation.OrganizationID(); ok {
		_spec.SetField(payrecharge.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PayRecharge.Create().
//		SetCreatedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.PayRechargeUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (prc *PayRechargeCreate) OnConflict(opts ...sql.ConflictOption) *PayRechargeUpsertOne {
	prc.conflict = opts
	return &PayRechargeUpsertOne{
		create: prc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.PayRecharge.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (prc *PayRechargeCreate) OnConflictColumns(columns ...string) *PayRechargeUpsertOne {
	prc.conflict = append(prc.conflict, sql.ConflictColumns(columns...))
	return &PayRechargeUpsertOne{
		create: prc,
	}
}

type (
	// PayRechargeUpsertOne is the builder for "upsert"-ing
	//  one PayRecharge node.
	PayRechargeUpsertOne struct {
		create *PayRechargeCreate
	}

	// PayRechargeUpsert is the "OnConflict" setter.
	PayRechargeUpsert struct {
		*sql.UpdateSet
	}
)

// SetUpdatedAt sets the "updated_at" field.
func (u *PayRechargeUpsert) SetUpdatedAt(v time.Time) *PayRechargeUpsert {
	u.Set(payrecharge.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateUpdatedAt() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldUpdatedAt)
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *PayRechargeUpsert) SetDeletedAt(v time.Time) *PayRechargeUpsert {
	u.Set(payrecharge.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateDeletedAt() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *PayRechargeUpsert) ClearDeletedAt() *PayRechargeUpsert {
	u.SetNull(payrecharge.FieldDeletedAt)
	return u
}

// SetUserID sets the "user_id" field.
func (u *PayRechargeUpsert) SetUserID(v string) *PayRechargeUpsert {
	u.Set(payrecharge.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateUserID() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldUserID)
	return u
}

// SetNumber sets the "number" field.
func (u *PayRechargeUpsert) SetNumber(v float32) *PayRechargeUpsert {
	u.Set(payrecharge.FieldNumber, v)
	return u
}

// UpdateNumber sets the "number" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateNumber() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldNumber)
	return u
}

// AddNumber adds v to the "number" field.
func (u *PayRechargeUpsert) AddNumber(v float32) *PayRechargeUpsert {
	u.Add(payrecharge.FieldNumber, v)
	return u
}

// SetStatus sets the "status" field.
func (u *PayRechargeUpsert) SetStatus(v int) *PayRechargeUpsert {
	u.Set(payrecharge.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateStatus() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldStatus)
	return u
}

// AddStatus adds v to the "status" field.
func (u *PayRechargeUpsert) AddStatus(v int) *PayRechargeUpsert {
	u.Add(payrecharge.FieldStatus, v)
	return u
}

// ClearStatus clears the value of the "status" field.
func (u *PayRechargeUpsert) ClearStatus() *PayRechargeUpsert {
	u.SetNull(payrecharge.FieldStatus)
	return u
}

// SetMoney sets the "money" field.
func (u *PayRechargeUpsert) SetMoney(v float32) *PayRechargeUpsert {
	u.Set(payrecharge.FieldMoney, v)
	return u
}

// UpdateMoney sets the "money" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateMoney() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldMoney)
	return u
}

// AddMoney adds v to the "money" field.
func (u *PayRechargeUpsert) AddMoney(v float32) *PayRechargeUpsert {
	u.Add(payrecharge.FieldMoney, v)
	return u
}

// ClearMoney clears the value of the "money" field.
func (u *PayRechargeUpsert) ClearMoney() *PayRechargeUpsert {
	u.SetNull(payrecharge.FieldMoney)
	return u
}

// SetOutTradeNo sets the "out_trade_no" field.
func (u *PayRechargeUpsert) SetOutTradeNo(v string) *PayRechargeUpsert {
	u.Set(payrecharge.FieldOutTradeNo, v)
	return u
}

// UpdateOutTradeNo sets the "out_trade_no" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateOutTradeNo() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldOutTradeNo)
	return u
}

// ClearOutTradeNo clears the value of the "out_trade_no" field.
func (u *PayRechargeUpsert) ClearOutTradeNo() *PayRechargeUpsert {
	u.SetNull(payrecharge.FieldOutTradeNo)
	return u
}

// SetOrganizationID sets the "organization_id" field.
func (u *PayRechargeUpsert) SetOrganizationID(v uint64) *PayRechargeUpsert {
	u.Set(payrecharge.FieldOrganizationID, v)
	return u
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *PayRechargeUpsert) UpdateOrganizationID() *PayRechargeUpsert {
	u.SetExcluded(payrecharge.FieldOrganizationID)
	return u
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *PayRechargeUpsert) AddOrganizationID(v uint64) *PayRechargeUpsert {
	u.Add(payrecharge.FieldOrganizationID, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.PayRecharge.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(payrecharge.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *PayRechargeUpsertOne) UpdateNewValues() *PayRechargeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(payrecharge.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(payrecharge.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.PayRecharge.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *PayRechargeUpsertOne) Ignore() *PayRechargeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PayRechargeUpsertOne) DoNothing() *PayRechargeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the PayRechargeCreate.OnConflict
// documentation for more info.
func (u *PayRechargeUpsertOne) Update(set func(*PayRechargeUpsert)) *PayRechargeUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&PayRechargeUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PayRechargeUpsertOne) SetUpdatedAt(v time.Time) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateUpdatedAt() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *PayRechargeUpsertOne) SetDeletedAt(v time.Time) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateDeletedAt() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *PayRechargeUpsertOne) ClearDeletedAt() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUserID sets the "user_id" field.
func (u *PayRechargeUpsertOne) SetUserID(v string) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateUserID() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateUserID()
	})
}

// SetNumber sets the "number" field.
func (u *PayRechargeUpsertOne) SetNumber(v float32) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetNumber(v)
	})
}

// AddNumber adds v to the "number" field.
func (u *PayRechargeUpsertOne) AddNumber(v float32) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddNumber(v)
	})
}

// UpdateNumber sets the "number" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateNumber() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateNumber()
	})
}

// SetStatus sets the "status" field.
func (u *PayRechargeUpsertOne) SetStatus(v int) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *PayRechargeUpsertOne) AddStatus(v int) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateStatus() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *PayRechargeUpsertOne) ClearStatus() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearStatus()
	})
}

// SetMoney sets the "money" field.
func (u *PayRechargeUpsertOne) SetMoney(v float32) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetMoney(v)
	})
}

// AddMoney adds v to the "money" field.
func (u *PayRechargeUpsertOne) AddMoney(v float32) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddMoney(v)
	})
}

// UpdateMoney sets the "money" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateMoney() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateMoney()
	})
}

// ClearMoney clears the value of the "money" field.
func (u *PayRechargeUpsertOne) ClearMoney() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearMoney()
	})
}

// SetOutTradeNo sets the "out_trade_no" field.
func (u *PayRechargeUpsertOne) SetOutTradeNo(v string) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetOutTradeNo(v)
	})
}

// UpdateOutTradeNo sets the "out_trade_no" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateOutTradeNo() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateOutTradeNo()
	})
}

// ClearOutTradeNo clears the value of the "out_trade_no" field.
func (u *PayRechargeUpsertOne) ClearOutTradeNo() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearOutTradeNo()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *PayRechargeUpsertOne) SetOrganizationID(v uint64) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *PayRechargeUpsertOne) AddOrganizationID(v uint64) *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *PayRechargeUpsertOne) UpdateOrganizationID() *PayRechargeUpsertOne {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateOrganizationID()
	})
}

// Exec executes the query.
func (u *PayRechargeUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for PayRechargeCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *PayRechargeUpsertOne) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *PayRechargeUpsertOne) ID(ctx context.Context) (id uint64, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *PayRechargeUpsertOne) IDX(ctx context.Context) uint64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// PayRechargeCreateBulk is the builder for creating many PayRecharge entities in bulk.
type PayRechargeCreateBulk struct {
	config
	err      error
	builders []*PayRechargeCreate
	conflict []sql.ConflictOption
}

// Save creates the PayRecharge entities in the database.
func (prcb *PayRechargeCreateBulk) Save(ctx context.Context) ([]*PayRecharge, error) {
	if prcb.err != nil {
		return nil, prcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(prcb.builders))
	nodes := make([]*PayRecharge, len(prcb.builders))
	mutators := make([]Mutator, len(prcb.builders))
	for i := range prcb.builders {
		func(i int, root context.Context) {
			builder := prcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*PayRechargeMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, prcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = prcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, prcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, prcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (prcb *PayRechargeCreateBulk) SaveX(ctx context.Context) []*PayRecharge {
	v, err := prcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (prcb *PayRechargeCreateBulk) Exec(ctx context.Context) error {
	_, err := prcb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (prcb *PayRechargeCreateBulk) ExecX(ctx context.Context) {
	if err := prcb.Exec(ctx); err != nil {
		panic(err)
	}
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PayRecharge.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.PayRechargeUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (prcb *PayRechargeCreateBulk) OnConflict(opts ...sql.ConflictOption) *PayRechargeUpsertBulk {
	prcb.conflict = opts
	return &PayRechargeUpsertBulk{
		create: prcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.PayRecharge.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (prcb *PayRechargeCreateBulk) OnConflictColumns(columns ...string) *PayRechargeUpsertBulk {
	prcb.conflict = append(prcb.conflict, sql.ConflictColumns(columns...))
	return &PayRechargeUpsertBulk{
		create: prcb,
	}
}

// PayRechargeUpsertBulk is the builder for "upsert"-ing
// a bulk of PayRecharge nodes.
type PayRechargeUpsertBulk struct {
	create *PayRechargeCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.PayRecharge.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(payrecharge.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *PayRechargeUpsertBulk) UpdateNewValues() *PayRechargeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(payrecharge.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(payrecharge.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.PayRecharge.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *PayRechargeUpsertBulk) Ignore() *PayRechargeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PayRechargeUpsertBulk) DoNothing() *PayRechargeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the PayRechargeCreateBulk.OnConflict
// documentation for more info.
func (u *PayRechargeUpsertBulk) Update(set func(*PayRechargeUpsert)) *PayRechargeUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&PayRechargeUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PayRechargeUpsertBulk) SetUpdatedAt(v time.Time) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateUpdatedAt() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *PayRechargeUpsertBulk) SetDeletedAt(v time.Time) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateDeletedAt() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *PayRechargeUpsertBulk) ClearDeletedAt() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUserID sets the "user_id" field.
func (u *PayRechargeUpsertBulk) SetUserID(v string) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateUserID() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateUserID()
	})
}

// SetNumber sets the "number" field.
func (u *PayRechargeUpsertBulk) SetNumber(v float32) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetNumber(v)
	})
}

// AddNumber adds v to the "number" field.
func (u *PayRechargeUpsertBulk) AddNumber(v float32) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddNumber(v)
	})
}

// UpdateNumber sets the "number" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateNumber() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateNumber()
	})
}

// SetStatus sets the "status" field.
func (u *PayRechargeUpsertBulk) SetStatus(v int) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *PayRechargeUpsertBulk) AddStatus(v int) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateStatus() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *PayRechargeUpsertBulk) ClearStatus() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearStatus()
	})
}

// SetMoney sets the "money" field.
func (u *PayRechargeUpsertBulk) SetMoney(v float32) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetMoney(v)
	})
}

// AddMoney adds v to the "money" field.
func (u *PayRechargeUpsertBulk) AddMoney(v float32) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddMoney(v)
	})
}

// UpdateMoney sets the "money" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateMoney() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateMoney()
	})
}

// ClearMoney clears the value of the "money" field.
func (u *PayRechargeUpsertBulk) ClearMoney() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearMoney()
	})
}

// SetOutTradeNo sets the "out_trade_no" field.
func (u *PayRechargeUpsertBulk) SetOutTradeNo(v string) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetOutTradeNo(v)
	})
}

// UpdateOutTradeNo sets the "out_trade_no" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateOutTradeNo() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateOutTradeNo()
	})
}

// ClearOutTradeNo clears the value of the "out_trade_no" field.
func (u *PayRechargeUpsertBulk) ClearOutTradeNo() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.ClearOutTradeNo()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *PayRechargeUpsertBulk) SetOrganizationID(v uint64) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *PayRechargeUpsertBulk) AddOrganizationID(v uint64) *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *PayRechargeUpsertBulk) UpdateOrganizationID() *PayRechargeUpsertBulk {
	return u.Update(func(s *PayRechargeUpsert) {
		s.UpdateOrganizationID()
	})
}

// Exec executes the query.
func (u *PayRechargeUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PayRechargeCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for PayRechargeCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *PayRechargeUpsertBulk) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}
