// Code generated by ent, DO NOT EDIT.

package ent

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

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

// UsageTotalCreate is the builder for creating a UsageTotal entity.
type UsageTotalCreate struct {
	config
	mutation *UsageTotalMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetStatus sets the "status" field.
func (utc *UsageTotalCreate) SetStatus(u uint8) *UsageTotalCreate {
	utc.mutation.SetStatus(u)
	return utc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (utc *UsageTotalCreate) SetNillableStatus(u *uint8) *UsageTotalCreate {
	if u != nil {
		utc.SetStatus(*u)
	}
	return utc
}

// SetType sets the "type" field.
func (utc *UsageTotalCreate) SetType(i int) *UsageTotalCreate {
	utc.mutation.SetType(i)
	return utc
}

// SetNillableType sets the "type" field if the given value is not nil.
func (utc *UsageTotalCreate) SetNillableType(i *int) *UsageTotalCreate {
	if i != nil {
		utc.SetType(*i)
	}
	return utc
}

// SetBotID sets the "bot_id" field.
func (utc *UsageTotalCreate) SetBotID(s string) *UsageTotalCreate {
	utc.mutation.SetBotID(s)
	return utc
}

// SetNillableBotID sets the "bot_id" field if the given value is not nil.
func (utc *UsageTotalCreate) SetNillableBotID(s *string) *UsageTotalCreate {
	if s != nil {
		utc.SetBotID(*s)
	}
	return utc
}

// SetTotalTokens sets the "total_tokens" field.
func (utc *UsageTotalCreate) SetTotalTokens(u uint64) *UsageTotalCreate {
	utc.mutation.SetTotalTokens(u)
	return utc
}

// SetNillableTotalTokens sets the "total_tokens" field if the given value is not nil.
func (utc *UsageTotalCreate) SetNillableTotalTokens(u *uint64) *UsageTotalCreate {
	if u != nil {
		utc.SetTotalTokens(*u)
	}
	return utc
}

// SetStartIndex sets the "start_index" field.
func (utc *UsageTotalCreate) SetStartIndex(u uint64) *UsageTotalCreate {
	utc.mutation.SetStartIndex(u)
	return utc
}

// SetNillableStartIndex sets the "start_index" field if the given value is not nil.
func (utc *UsageTotalCreate) SetNillableStartIndex(u *uint64) *UsageTotalCreate {
	if u != nil {
		utc.SetStartIndex(*u)
	}
	return utc
}

// SetEndIndex sets the "end_index" field.
func (utc *UsageTotalCreate) SetEndIndex(u uint64) *UsageTotalCreate {
	utc.mutation.SetEndIndex(u)
	return utc
}

// SetNillableEndIndex sets the "end_index" field if the given value is not nil.
func (utc *UsageTotalCreate) SetNillableEndIndex(u *uint64) *UsageTotalCreate {
	if u != nil {
		utc.SetEndIndex(*u)
	}
	return utc
}

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

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

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

// Mutation returns the UsageTotalMutation object of the builder.
func (utc *UsageTotalCreate) Mutation() *UsageTotalMutation {
	return utc.mutation
}

// Save creates the UsageTotal in the database.
func (utc *UsageTotalCreate) Save(ctx context.Context) (*UsageTotal, error) {
	utc.defaults()
	return withHooks(ctx, utc.sqlSave, utc.mutation, utc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (utc *UsageTotalCreate) defaults() {
	if _, ok := utc.mutation.CreatedAt(); !ok {
		v := usagetotal.DefaultCreatedAt()
		utc.mutation.SetCreatedAt(v)
	}
	if _, ok := utc.mutation.UpdatedAt(); !ok {
		v := usagetotal.DefaultUpdatedAt()
		utc.mutation.SetUpdatedAt(v)
	}
	if _, ok := utc.mutation.Status(); !ok {
		v := usagetotal.DefaultStatus
		utc.mutation.SetStatus(v)
	}
	if _, ok := utc.mutation.GetType(); !ok {
		v := usagetotal.DefaultType
		utc.mutation.SetType(v)
	}
	if _, ok := utc.mutation.BotID(); !ok {
		v := usagetotal.DefaultBotID
		utc.mutation.SetBotID(v)
	}
	if _, ok := utc.mutation.TotalTokens(); !ok {
		v := usagetotal.DefaultTotalTokens
		utc.mutation.SetTotalTokens(v)
	}
	if _, ok := utc.mutation.StartIndex(); !ok {
		v := usagetotal.DefaultStartIndex
		utc.mutation.SetStartIndex(v)
	}
	if _, ok := utc.mutation.EndIndex(); !ok {
		v := usagetotal.DefaultEndIndex
		utc.mutation.SetEndIndex(v)
	}
	if _, ok := utc.mutation.OrganizationID(); !ok {
		v := usagetotal.DefaultOrganizationID
		utc.mutation.SetOrganizationID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (utc *UsageTotalCreate) check() error {
	if _, ok := utc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "UsageTotal.created_at"`)}
	}
	if _, ok := utc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "UsageTotal.updated_at"`)}
	}
	if _, ok := utc.mutation.BotID(); !ok {
		return &ValidationError{Name: "bot_id", err: errors.New(`ent: missing required field "UsageTotal.bot_id"`)}
	}
	return nil
}

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

func (utc *UsageTotalCreate) createSpec() (*UsageTotal, *sqlgraph.CreateSpec) {
	var (
		_node = &UsageTotal{config: utc.config}
		_spec = sqlgraph.NewCreateSpec(usagetotal.Table, sqlgraph.NewFieldSpec(usagetotal.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = utc.conflict
	if id, ok := utc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := utc.mutation.CreatedAt(); ok {
		_spec.SetField(usagetotal.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := utc.mutation.UpdatedAt(); ok {
		_spec.SetField(usagetotal.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := utc.mutation.Status(); ok {
		_spec.SetField(usagetotal.FieldStatus, field.TypeUint8, value)
		_node.Status = value
	}
	if value, ok := utc.mutation.GetType(); ok {
		_spec.SetField(usagetotal.FieldType, field.TypeInt, value)
		_node.Type = value
	}
	if value, ok := utc.mutation.BotID(); ok {
		_spec.SetField(usagetotal.FieldBotID, field.TypeString, value)
		_node.BotID = value
	}
	if value, ok := utc.mutation.TotalTokens(); ok {
		_spec.SetField(usagetotal.FieldTotalTokens, field.TypeUint64, value)
		_node.TotalTokens = value
	}
	if value, ok := utc.mutation.StartIndex(); ok {
		_spec.SetField(usagetotal.FieldStartIndex, field.TypeUint64, value)
		_node.StartIndex = value
	}
	if value, ok := utc.mutation.EndIndex(); ok {
		_spec.SetField(usagetotal.FieldEndIndex, field.TypeUint64, value)
		_node.EndIndex = value
	}
	if value, ok := utc.mutation.OrganizationID(); ok {
		_spec.SetField(usagetotal.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.UsageTotal.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.UsageTotalUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (utc *UsageTotalCreate) OnConflict(opts ...sql.ConflictOption) *UsageTotalUpsertOne {
	utc.conflict = opts
	return &UsageTotalUpsertOne{
		create: utc,
	}
}

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

type (
	// UsageTotalUpsertOne is the builder for "upsert"-ing
	//  one UsageTotal node.
	UsageTotalUpsertOne struct {
		create *UsageTotalCreate
	}

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

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

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

// SetStatus sets the "status" field.
func (u *UsageTotalUpsert) SetStatus(v uint8) *UsageTotalUpsert {
	u.Set(usagetotal.FieldStatus, v)
	return u
}

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

// AddStatus adds v to the "status" field.
func (u *UsageTotalUpsert) AddStatus(v uint8) *UsageTotalUpsert {
	u.Add(usagetotal.FieldStatus, v)
	return u
}

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

// SetType sets the "type" field.
func (u *UsageTotalUpsert) SetType(v int) *UsageTotalUpsert {
	u.Set(usagetotal.FieldType, v)
	return u
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *UsageTotalUpsert) UpdateType() *UsageTotalUpsert {
	u.SetExcluded(usagetotal.FieldType)
	return u
}

// AddType adds v to the "type" field.
func (u *UsageTotalUpsert) AddType(v int) *UsageTotalUpsert {
	u.Add(usagetotal.FieldType, v)
	return u
}

// ClearType clears the value of the "type" field.
func (u *UsageTotalUpsert) ClearType() *UsageTotalUpsert {
	u.SetNull(usagetotal.FieldType)
	return u
}

// SetBotID sets the "bot_id" field.
func (u *UsageTotalUpsert) SetBotID(v string) *UsageTotalUpsert {
	u.Set(usagetotal.FieldBotID, v)
	return u
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *UsageTotalUpsert) UpdateBotID() *UsageTotalUpsert {
	u.SetExcluded(usagetotal.FieldBotID)
	return u
}

// SetTotalTokens sets the "total_tokens" field.
func (u *UsageTotalUpsert) SetTotalTokens(v uint64) *UsageTotalUpsert {
	u.Set(usagetotal.FieldTotalTokens, v)
	return u
}

// UpdateTotalTokens sets the "total_tokens" field to the value that was provided on create.
func (u *UsageTotalUpsert) UpdateTotalTokens() *UsageTotalUpsert {
	u.SetExcluded(usagetotal.FieldTotalTokens)
	return u
}

// AddTotalTokens adds v to the "total_tokens" field.
func (u *UsageTotalUpsert) AddTotalTokens(v uint64) *UsageTotalUpsert {
	u.Add(usagetotal.FieldTotalTokens, v)
	return u
}

// ClearTotalTokens clears the value of the "total_tokens" field.
func (u *UsageTotalUpsert) ClearTotalTokens() *UsageTotalUpsert {
	u.SetNull(usagetotal.FieldTotalTokens)
	return u
}

// SetStartIndex sets the "start_index" field.
func (u *UsageTotalUpsert) SetStartIndex(v uint64) *UsageTotalUpsert {
	u.Set(usagetotal.FieldStartIndex, v)
	return u
}

// UpdateStartIndex sets the "start_index" field to the value that was provided on create.
func (u *UsageTotalUpsert) UpdateStartIndex() *UsageTotalUpsert {
	u.SetExcluded(usagetotal.FieldStartIndex)
	return u
}

// AddStartIndex adds v to the "start_index" field.
func (u *UsageTotalUpsert) AddStartIndex(v uint64) *UsageTotalUpsert {
	u.Add(usagetotal.FieldStartIndex, v)
	return u
}

// ClearStartIndex clears the value of the "start_index" field.
func (u *UsageTotalUpsert) ClearStartIndex() *UsageTotalUpsert {
	u.SetNull(usagetotal.FieldStartIndex)
	return u
}

// SetEndIndex sets the "end_index" field.
func (u *UsageTotalUpsert) SetEndIndex(v uint64) *UsageTotalUpsert {
	u.Set(usagetotal.FieldEndIndex, v)
	return u
}

// UpdateEndIndex sets the "end_index" field to the value that was provided on create.
func (u *UsageTotalUpsert) UpdateEndIndex() *UsageTotalUpsert {
	u.SetExcluded(usagetotal.FieldEndIndex)
	return u
}

// AddEndIndex adds v to the "end_index" field.
func (u *UsageTotalUpsert) AddEndIndex(v uint64) *UsageTotalUpsert {
	u.Add(usagetotal.FieldEndIndex, v)
	return u
}

// ClearEndIndex clears the value of the "end_index" field.
func (u *UsageTotalUpsert) ClearEndIndex() *UsageTotalUpsert {
	u.SetNull(usagetotal.FieldEndIndex)
	return u
}

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

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

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

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *UsageTotalUpsert) ClearOrganizationID() *UsageTotalUpsert {
	u.SetNull(usagetotal.FieldOrganizationID)
	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.UsageTotal.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(usagetotal.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *UsageTotalUpsertOne) UpdateNewValues() *UsageTotalUpsertOne {
	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(usagetotal.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(usagetotal.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

// SetStatus sets the "status" field.
func (u *UsageTotalUpsertOne) SetStatus(v uint8) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *UsageTotalUpsertOne) AddStatus(v uint8) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddStatus(v)
	})
}

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

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

// SetType sets the "type" field.
func (u *UsageTotalUpsertOne) SetType(v int) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetType(v)
	})
}

// AddType adds v to the "type" field.
func (u *UsageTotalUpsertOne) AddType(v int) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *UsageTotalUpsertOne) UpdateType() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateType()
	})
}

// ClearType clears the value of the "type" field.
func (u *UsageTotalUpsertOne) ClearType() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearType()
	})
}

// SetBotID sets the "bot_id" field.
func (u *UsageTotalUpsertOne) SetBotID(v string) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetBotID(v)
	})
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *UsageTotalUpsertOne) UpdateBotID() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateBotID()
	})
}

// SetTotalTokens sets the "total_tokens" field.
func (u *UsageTotalUpsertOne) SetTotalTokens(v uint64) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetTotalTokens(v)
	})
}

// AddTotalTokens adds v to the "total_tokens" field.
func (u *UsageTotalUpsertOne) AddTotalTokens(v uint64) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddTotalTokens(v)
	})
}

// UpdateTotalTokens sets the "total_tokens" field to the value that was provided on create.
func (u *UsageTotalUpsertOne) UpdateTotalTokens() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateTotalTokens()
	})
}

// ClearTotalTokens clears the value of the "total_tokens" field.
func (u *UsageTotalUpsertOne) ClearTotalTokens() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearTotalTokens()
	})
}

// SetStartIndex sets the "start_index" field.
func (u *UsageTotalUpsertOne) SetStartIndex(v uint64) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetStartIndex(v)
	})
}

// AddStartIndex adds v to the "start_index" field.
func (u *UsageTotalUpsertOne) AddStartIndex(v uint64) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddStartIndex(v)
	})
}

// UpdateStartIndex sets the "start_index" field to the value that was provided on create.
func (u *UsageTotalUpsertOne) UpdateStartIndex() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateStartIndex()
	})
}

// ClearStartIndex clears the value of the "start_index" field.
func (u *UsageTotalUpsertOne) ClearStartIndex() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearStartIndex()
	})
}

// SetEndIndex sets the "end_index" field.
func (u *UsageTotalUpsertOne) SetEndIndex(v uint64) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetEndIndex(v)
	})
}

// AddEndIndex adds v to the "end_index" field.
func (u *UsageTotalUpsertOne) AddEndIndex(v uint64) *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddEndIndex(v)
	})
}

// UpdateEndIndex sets the "end_index" field to the value that was provided on create.
func (u *UsageTotalUpsertOne) UpdateEndIndex() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateEndIndex()
	})
}

// ClearEndIndex clears the value of the "end_index" field.
func (u *UsageTotalUpsertOne) ClearEndIndex() *UsageTotalUpsertOne {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearEndIndex()
	})
}

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

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

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

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

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

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

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

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

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

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

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

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

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

// UsageTotalUpsertBulk is the builder for "upsert"-ing
// a bulk of UsageTotal nodes.
type UsageTotalUpsertBulk struct {
	create *UsageTotalCreateBulk
}

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

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

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

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

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

// SetStatus sets the "status" field.
func (u *UsageTotalUpsertBulk) SetStatus(v uint8) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *UsageTotalUpsertBulk) AddStatus(v uint8) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddStatus(v)
	})
}

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

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

// SetType sets the "type" field.
func (u *UsageTotalUpsertBulk) SetType(v int) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetType(v)
	})
}

// AddType adds v to the "type" field.
func (u *UsageTotalUpsertBulk) AddType(v int) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddType(v)
	})
}

// UpdateType sets the "type" field to the value that was provided on create.
func (u *UsageTotalUpsertBulk) UpdateType() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateType()
	})
}

// ClearType clears the value of the "type" field.
func (u *UsageTotalUpsertBulk) ClearType() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearType()
	})
}

// SetBotID sets the "bot_id" field.
func (u *UsageTotalUpsertBulk) SetBotID(v string) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetBotID(v)
	})
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *UsageTotalUpsertBulk) UpdateBotID() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateBotID()
	})
}

// SetTotalTokens sets the "total_tokens" field.
func (u *UsageTotalUpsertBulk) SetTotalTokens(v uint64) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetTotalTokens(v)
	})
}

// AddTotalTokens adds v to the "total_tokens" field.
func (u *UsageTotalUpsertBulk) AddTotalTokens(v uint64) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddTotalTokens(v)
	})
}

// UpdateTotalTokens sets the "total_tokens" field to the value that was provided on create.
func (u *UsageTotalUpsertBulk) UpdateTotalTokens() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateTotalTokens()
	})
}

// ClearTotalTokens clears the value of the "total_tokens" field.
func (u *UsageTotalUpsertBulk) ClearTotalTokens() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearTotalTokens()
	})
}

// SetStartIndex sets the "start_index" field.
func (u *UsageTotalUpsertBulk) SetStartIndex(v uint64) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetStartIndex(v)
	})
}

// AddStartIndex adds v to the "start_index" field.
func (u *UsageTotalUpsertBulk) AddStartIndex(v uint64) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddStartIndex(v)
	})
}

// UpdateStartIndex sets the "start_index" field to the value that was provided on create.
func (u *UsageTotalUpsertBulk) UpdateStartIndex() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateStartIndex()
	})
}

// ClearStartIndex clears the value of the "start_index" field.
func (u *UsageTotalUpsertBulk) ClearStartIndex() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearStartIndex()
	})
}

// SetEndIndex sets the "end_index" field.
func (u *UsageTotalUpsertBulk) SetEndIndex(v uint64) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.SetEndIndex(v)
	})
}

// AddEndIndex adds v to the "end_index" field.
func (u *UsageTotalUpsertBulk) AddEndIndex(v uint64) *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.AddEndIndex(v)
	})
}

// UpdateEndIndex sets the "end_index" field to the value that was provided on create.
func (u *UsageTotalUpsertBulk) UpdateEndIndex() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.UpdateEndIndex()
	})
}

// ClearEndIndex clears the value of the "end_index" field.
func (u *UsageTotalUpsertBulk) ClearEndIndex() *UsageTotalUpsertBulk {
	return u.Update(func(s *UsageTotalUpsert) {
		s.ClearEndIndex()
	})
}

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

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

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

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

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

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