// Code generated by ent, DO NOT EDIT.

package ent

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

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

// CreditUsageCreate is the builder for creating a CreditUsage entity.
type CreditUsageCreate struct {
	config
	mutation *CreditUsageMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (cuc *CreditUsageCreate) SetNillableUserID(s *string) *CreditUsageCreate {
	if s != nil {
		cuc.SetUserID(*s)
	}
	return cuc
}

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

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

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

// SetNtype sets the "ntype" field.
func (cuc *CreditUsageCreate) SetNtype(i int) *CreditUsageCreate {
	cuc.mutation.SetNtype(i)
	return cuc
}

// SetNillableNtype sets the "ntype" field if the given value is not nil.
func (cuc *CreditUsageCreate) SetNillableNtype(i *int) *CreditUsageCreate {
	if i != nil {
		cuc.SetNtype(*i)
	}
	return cuc
}

// SetTable sets the "table" field.
func (cuc *CreditUsageCreate) SetTable(s string) *CreditUsageCreate {
	cuc.mutation.SetTable(s)
	return cuc
}

// SetNillableTable sets the "table" field if the given value is not nil.
func (cuc *CreditUsageCreate) SetNillableTable(s *string) *CreditUsageCreate {
	if s != nil {
		cuc.SetTable(*s)
	}
	return cuc
}

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

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

// SetNid sets the "nid" field.
func (cuc *CreditUsageCreate) SetNid(u uint64) *CreditUsageCreate {
	cuc.mutation.SetNid(u)
	return cuc
}

// SetNillableNid sets the "nid" field if the given value is not nil.
func (cuc *CreditUsageCreate) SetNillableNid(u *uint64) *CreditUsageCreate {
	if u != nil {
		cuc.SetNid(*u)
	}
	return cuc
}

// SetReason sets the "reason" field.
func (cuc *CreditUsageCreate) SetReason(s string) *CreditUsageCreate {
	cuc.mutation.SetReason(s)
	return cuc
}

// SetNillableReason sets the "reason" field if the given value is not nil.
func (cuc *CreditUsageCreate) SetNillableReason(s *string) *CreditUsageCreate {
	if s != nil {
		cuc.SetReason(*s)
	}
	return cuc
}

// SetOperator sets the "operator" field.
func (cuc *CreditUsageCreate) SetOperator(s string) *CreditUsageCreate {
	cuc.mutation.SetOperator(s)
	return cuc
}

// SetNillableOperator sets the "operator" field if the given value is not nil.
func (cuc *CreditUsageCreate) SetNillableOperator(s *string) *CreditUsageCreate {
	if s != nil {
		cuc.SetOperator(*s)
	}
	return cuc
}

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

// Mutation returns the CreditUsageMutation object of the builder.
func (cuc *CreditUsageCreate) Mutation() *CreditUsageMutation {
	return cuc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (cuc *CreditUsageCreate) defaults() error {
	if _, ok := cuc.mutation.CreatedAt(); !ok {
		if creditusage.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized creditusage.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := creditusage.DefaultCreatedAt()
		cuc.mutation.SetCreatedAt(v)
	}
	if _, ok := cuc.mutation.UpdatedAt(); !ok {
		if creditusage.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized creditusage.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := creditusage.DefaultUpdatedAt()
		cuc.mutation.SetUpdatedAt(v)
	}
	if _, ok := cuc.mutation.Status(); !ok {
		v := creditusage.DefaultStatus
		cuc.mutation.SetStatus(v)
	}
	if _, ok := cuc.mutation.Ntype(); !ok {
		v := creditusage.DefaultNtype
		cuc.mutation.SetNtype(v)
	}
	if _, ok := cuc.mutation.Table(); !ok {
		v := creditusage.DefaultTable
		cuc.mutation.SetTable(v)
	}
	if _, ok := cuc.mutation.Nid(); !ok {
		v := creditusage.DefaultNid
		cuc.mutation.SetNid(v)
	}
	if _, ok := cuc.mutation.Reason(); !ok {
		v := creditusage.DefaultReason
		cuc.mutation.SetReason(v)
	}
	if _, ok := cuc.mutation.Operator(); !ok {
		v := creditusage.DefaultOperator
		cuc.mutation.SetOperator(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (cuc *CreditUsageCreate) check() error {
	if _, ok := cuc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "CreditUsage.created_at"`)}
	}
	if _, ok := cuc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "CreditUsage.updated_at"`)}
	}
	if v, ok := cuc.mutation.UserID(); ok {
		if err := creditusage.UserIDValidator(v); err != nil {
			return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.user_id": %w`, err)}
		}
	}
	if _, ok := cuc.mutation.Number(); !ok {
		return &ValidationError{Name: "number", err: errors.New(`ent: missing required field "CreditUsage.number"`)}
	}
	if v, ok := cuc.mutation.Status(); ok {
		if err := creditusage.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.status": %w`, err)}
		}
	}
	if _, ok := cuc.mutation.Ntype(); !ok {
		return &ValidationError{Name: "ntype", err: errors.New(`ent: missing required field "CreditUsage.ntype"`)}
	}
	if _, ok := cuc.mutation.Table(); !ok {
		return &ValidationError{Name: "table", err: errors.New(`ent: missing required field "CreditUsage.table"`)}
	}
	if _, ok := cuc.mutation.Nid(); !ok {
		return &ValidationError{Name: "nid", err: errors.New(`ent: missing required field "CreditUsage.nid"`)}
	}
	if _, ok := cuc.mutation.Reason(); !ok {
		return &ValidationError{Name: "reason", err: errors.New(`ent: missing required field "CreditUsage.reason"`)}
	}
	if v, ok := cuc.mutation.Reason(); ok {
		if err := creditusage.ReasonValidator(v); err != nil {
			return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.reason": %w`, err)}
		}
	}
	if _, ok := cuc.mutation.Operator(); !ok {
		return &ValidationError{Name: "operator", err: errors.New(`ent: missing required field "CreditUsage.operator"`)}
	}
	if v, ok := cuc.mutation.Operator(); ok {
		if err := creditusage.OperatorValidator(v); err != nil {
			return &ValidationError{Name: "operator", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.operator": %w`, err)}
		}
	}
	return nil
}

func (cuc *CreditUsageCreate) sqlSave(ctx context.Context) (*CreditUsage, error) {
	if err := cuc.check(); err != nil {
		return nil, err
	}
	_node, _spec := cuc.createSpec()
	if err := sqlgraph.CreateNode(ctx, cuc.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)
	}
	cuc.mutation.id = &_node.ID
	cuc.mutation.done = true
	return _node, nil
}

func (cuc *CreditUsageCreate) createSpec() (*CreditUsage, *sqlgraph.CreateSpec) {
	var (
		_node = &CreditUsage{config: cuc.config}
		_spec = sqlgraph.NewCreateSpec(creditusage.Table, sqlgraph.NewFieldSpec(creditusage.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = cuc.conflict
	if id, ok := cuc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := cuc.mutation.CreatedAt(); ok {
		_spec.SetField(creditusage.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := cuc.mutation.UpdatedAt(); ok {
		_spec.SetField(creditusage.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := cuc.mutation.DeletedAt(); ok {
		_spec.SetField(creditusage.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := cuc.mutation.UserID(); ok {
		_spec.SetField(creditusage.FieldUserID, field.TypeString, value)
		_node.UserID = value
	}
	if value, ok := cuc.mutation.Number(); ok {
		_spec.SetField(creditusage.FieldNumber, field.TypeFloat32, value)
		_node.Number = value
	}
	if value, ok := cuc.mutation.Status(); ok {
		_spec.SetField(creditusage.FieldStatus, field.TypeInt, value)
		_node.Status = value
	}
	if value, ok := cuc.mutation.Ntype(); ok {
		_spec.SetField(creditusage.FieldNtype, field.TypeInt, value)
		_node.Ntype = value
	}
	if value, ok := cuc.mutation.Table(); ok {
		_spec.SetField(creditusage.FieldTable, field.TypeString, value)
		_node.Table = value
	}
	if value, ok := cuc.mutation.OrganizationID(); ok {
		_spec.SetField(creditusage.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if value, ok := cuc.mutation.Nid(); ok {
		_spec.SetField(creditusage.FieldNid, field.TypeUint64, value)
		_node.Nid = value
	}
	if value, ok := cuc.mutation.Reason(); ok {
		_spec.SetField(creditusage.FieldReason, field.TypeString, value)
		_node.Reason = value
	}
	if value, ok := cuc.mutation.Operator(); ok {
		_spec.SetField(creditusage.FieldOperator, field.TypeString, value)
		_node.Operator = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.CreditUsage.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.CreditUsageUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (cuc *CreditUsageCreate) OnConflict(opts ...sql.ConflictOption) *CreditUsageUpsertOne {
	cuc.conflict = opts
	return &CreditUsageUpsertOne{
		create: cuc,
	}
}

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

type (
	// CreditUsageUpsertOne is the builder for "upsert"-ing
	//  one CreditUsage node.
	CreditUsageUpsertOne struct {
		create *CreditUsageCreate
	}

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

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

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

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

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

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

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

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

// ClearUserID clears the value of the "user_id" field.
func (u *CreditUsageUpsert) ClearUserID() *CreditUsageUpsert {
	u.SetNull(creditusage.FieldUserID)
	return u
}

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

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

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

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

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

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

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

// SetNtype sets the "ntype" field.
func (u *CreditUsageUpsert) SetNtype(v int) *CreditUsageUpsert {
	u.Set(creditusage.FieldNtype, v)
	return u
}

// UpdateNtype sets the "ntype" field to the value that was provided on create.
func (u *CreditUsageUpsert) UpdateNtype() *CreditUsageUpsert {
	u.SetExcluded(creditusage.FieldNtype)
	return u
}

// AddNtype adds v to the "ntype" field.
func (u *CreditUsageUpsert) AddNtype(v int) *CreditUsageUpsert {
	u.Add(creditusage.FieldNtype, v)
	return u
}

// SetTable sets the "table" field.
func (u *CreditUsageUpsert) SetTable(v string) *CreditUsageUpsert {
	u.Set(creditusage.FieldTable, v)
	return u
}

// UpdateTable sets the "table" field to the value that was provided on create.
func (u *CreditUsageUpsert) UpdateTable() *CreditUsageUpsert {
	u.SetExcluded(creditusage.FieldTable)
	return u
}

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

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

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

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *CreditUsageUpsert) ClearOrganizationID() *CreditUsageUpsert {
	u.SetNull(creditusage.FieldOrganizationID)
	return u
}

// SetNid sets the "nid" field.
func (u *CreditUsageUpsert) SetNid(v uint64) *CreditUsageUpsert {
	u.Set(creditusage.FieldNid, v)
	return u
}

// UpdateNid sets the "nid" field to the value that was provided on create.
func (u *CreditUsageUpsert) UpdateNid() *CreditUsageUpsert {
	u.SetExcluded(creditusage.FieldNid)
	return u
}

// AddNid adds v to the "nid" field.
func (u *CreditUsageUpsert) AddNid(v uint64) *CreditUsageUpsert {
	u.Add(creditusage.FieldNid, v)
	return u
}

// SetReason sets the "reason" field.
func (u *CreditUsageUpsert) SetReason(v string) *CreditUsageUpsert {
	u.Set(creditusage.FieldReason, v)
	return u
}

// UpdateReason sets the "reason" field to the value that was provided on create.
func (u *CreditUsageUpsert) UpdateReason() *CreditUsageUpsert {
	u.SetExcluded(creditusage.FieldReason)
	return u
}

// SetOperator sets the "operator" field.
func (u *CreditUsageUpsert) SetOperator(v string) *CreditUsageUpsert {
	u.Set(creditusage.FieldOperator, v)
	return u
}

// UpdateOperator sets the "operator" field to the value that was provided on create.
func (u *CreditUsageUpsert) UpdateOperator() *CreditUsageUpsert {
	u.SetExcluded(creditusage.FieldOperator)
	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.CreditUsage.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(creditusage.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *CreditUsageUpsertOne) UpdateNewValues() *CreditUsageUpsertOne {
	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(creditusage.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(creditusage.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.CreditUsage.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *CreditUsageUpsertOne) Ignore() *CreditUsageUpsertOne {
	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 *CreditUsageUpsertOne) DoNothing() *CreditUsageUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

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

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

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

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

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

// ClearUserID clears the value of the "user_id" field.
func (u *CreditUsageUpsertOne) ClearUserID() *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.ClearUserID()
	})
}

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

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

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

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

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

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

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

// SetNtype sets the "ntype" field.
func (u *CreditUsageUpsertOne) SetNtype(v int) *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetNtype(v)
	})
}

// AddNtype adds v to the "ntype" field.
func (u *CreditUsageUpsertOne) AddNtype(v int) *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.AddNtype(v)
	})
}

// UpdateNtype sets the "ntype" field to the value that was provided on create.
func (u *CreditUsageUpsertOne) UpdateNtype() *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateNtype()
	})
}

// SetTable sets the "table" field.
func (u *CreditUsageUpsertOne) SetTable(v string) *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetTable(v)
	})
}

// UpdateTable sets the "table" field to the value that was provided on create.
func (u *CreditUsageUpsertOne) UpdateTable() *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateTable()
	})
}

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

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

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

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *CreditUsageUpsertOne) ClearOrganizationID() *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.ClearOrganizationID()
	})
}

// SetNid sets the "nid" field.
func (u *CreditUsageUpsertOne) SetNid(v uint64) *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetNid(v)
	})
}

// AddNid adds v to the "nid" field.
func (u *CreditUsageUpsertOne) AddNid(v uint64) *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.AddNid(v)
	})
}

// UpdateNid sets the "nid" field to the value that was provided on create.
func (u *CreditUsageUpsertOne) UpdateNid() *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateNid()
	})
}

// SetReason sets the "reason" field.
func (u *CreditUsageUpsertOne) SetReason(v string) *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetReason(v)
	})
}

// UpdateReason sets the "reason" field to the value that was provided on create.
func (u *CreditUsageUpsertOne) UpdateReason() *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateReason()
	})
}

// SetOperator sets the "operator" field.
func (u *CreditUsageUpsertOne) SetOperator(v string) *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetOperator(v)
	})
}

// UpdateOperator sets the "operator" field to the value that was provided on create.
func (u *CreditUsageUpsertOne) UpdateOperator() *CreditUsageUpsertOne {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateOperator()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *CreditUsageUpsertOne) 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 *CreditUsageUpsertOne) 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 *CreditUsageUpsertOne) IDX(ctx context.Context) uint64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the CreditUsage entities in the database.
func (cucb *CreditUsageCreateBulk) Save(ctx context.Context) ([]*CreditUsage, error) {
	if cucb.err != nil {
		return nil, cucb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(cucb.builders))
	nodes := make([]*CreditUsage, len(cucb.builders))
	mutators := make([]Mutator, len(cucb.builders))
	for i := range cucb.builders {
		func(i int, root context.Context) {
			builder := cucb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*CreditUsageMutation)
				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, cucb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = cucb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, cucb.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, cucb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.CreditUsage.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.CreditUsageUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (cucb *CreditUsageCreateBulk) OnConflict(opts ...sql.ConflictOption) *CreditUsageUpsertBulk {
	cucb.conflict = opts
	return &CreditUsageUpsertBulk{
		create: cucb,
	}
}

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

// CreditUsageUpsertBulk is the builder for "upsert"-ing
// a bulk of CreditUsage nodes.
type CreditUsageUpsertBulk struct {
	create *CreditUsageCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.CreditUsage.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(creditusage.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *CreditUsageUpsertBulk) UpdateNewValues() *CreditUsageUpsertBulk {
	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(creditusage.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(creditusage.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.CreditUsage.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *CreditUsageUpsertBulk) Ignore() *CreditUsageUpsertBulk {
	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 *CreditUsageUpsertBulk) DoNothing() *CreditUsageUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

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

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

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

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

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

// ClearUserID clears the value of the "user_id" field.
func (u *CreditUsageUpsertBulk) ClearUserID() *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.ClearUserID()
	})
}

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

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

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

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

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

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

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

// SetNtype sets the "ntype" field.
func (u *CreditUsageUpsertBulk) SetNtype(v int) *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetNtype(v)
	})
}

// AddNtype adds v to the "ntype" field.
func (u *CreditUsageUpsertBulk) AddNtype(v int) *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.AddNtype(v)
	})
}

// UpdateNtype sets the "ntype" field to the value that was provided on create.
func (u *CreditUsageUpsertBulk) UpdateNtype() *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateNtype()
	})
}

// SetTable sets the "table" field.
func (u *CreditUsageUpsertBulk) SetTable(v string) *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetTable(v)
	})
}

// UpdateTable sets the "table" field to the value that was provided on create.
func (u *CreditUsageUpsertBulk) UpdateTable() *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateTable()
	})
}

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

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

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

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *CreditUsageUpsertBulk) ClearOrganizationID() *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.ClearOrganizationID()
	})
}

// SetNid sets the "nid" field.
func (u *CreditUsageUpsertBulk) SetNid(v uint64) *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetNid(v)
	})
}

// AddNid adds v to the "nid" field.
func (u *CreditUsageUpsertBulk) AddNid(v uint64) *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.AddNid(v)
	})
}

// UpdateNid sets the "nid" field to the value that was provided on create.
func (u *CreditUsageUpsertBulk) UpdateNid() *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateNid()
	})
}

// SetReason sets the "reason" field.
func (u *CreditUsageUpsertBulk) SetReason(v string) *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetReason(v)
	})
}

// UpdateReason sets the "reason" field to the value that was provided on create.
func (u *CreditUsageUpsertBulk) UpdateReason() *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateReason()
	})
}

// SetOperator sets the "operator" field.
func (u *CreditUsageUpsertBulk) SetOperator(v string) *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.SetOperator(v)
	})
}

// UpdateOperator sets the "operator" field to the value that was provided on create.
func (u *CreditUsageUpsertBulk) UpdateOperator() *CreditUsageUpsertBulk {
	return u.Update(func(s *CreditUsageUpsert) {
		s.UpdateOperator()
	})
}

// Exec executes the query.
func (u *CreditUsageUpsertBulk) 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 CreditUsageCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for CreditUsageCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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