// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/Wei-Shaw/sub2api/ent/usagecleanuptask"
)

// UsageCleanupTaskCreate is the builder for creating a UsageCleanupTask entity.
type UsageCleanupTaskCreate struct {
	config
	mutation *UsageCleanupTaskMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (_c *UsageCleanupTaskCreate) SetCreatedAt(v time.Time) *UsageCleanupTaskCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

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

// SetUpdatedAt sets the "updated_at" field.
func (_c *UsageCleanupTaskCreate) SetUpdatedAt(v time.Time) *UsageCleanupTaskCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

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

// SetStatus sets the "status" field.
func (_c *UsageCleanupTaskCreate) SetStatus(v string) *UsageCleanupTaskCreate {
	_c.mutation.SetStatus(v)
	return _c
}

// SetFilters sets the "filters" field.
func (_c *UsageCleanupTaskCreate) SetFilters(v json.RawMessage) *UsageCleanupTaskCreate {
	_c.mutation.SetFilters(v)
	return _c
}

// SetCreatedBy sets the "created_by" field.
func (_c *UsageCleanupTaskCreate) SetCreatedBy(v int64) *UsageCleanupTaskCreate {
	_c.mutation.SetCreatedBy(v)
	return _c
}

// SetDeletedRows sets the "deleted_rows" field.
func (_c *UsageCleanupTaskCreate) SetDeletedRows(v int64) *UsageCleanupTaskCreate {
	_c.mutation.SetDeletedRows(v)
	return _c
}

// SetNillableDeletedRows sets the "deleted_rows" field if the given value is not nil.
func (_c *UsageCleanupTaskCreate) SetNillableDeletedRows(v *int64) *UsageCleanupTaskCreate {
	if v != nil {
		_c.SetDeletedRows(*v)
	}
	return _c
}

// SetErrorMessage sets the "error_message" field.
func (_c *UsageCleanupTaskCreate) SetErrorMessage(v string) *UsageCleanupTaskCreate {
	_c.mutation.SetErrorMessage(v)
	return _c
}

// SetNillableErrorMessage sets the "error_message" field if the given value is not nil.
func (_c *UsageCleanupTaskCreate) SetNillableErrorMessage(v *string) *UsageCleanupTaskCreate {
	if v != nil {
		_c.SetErrorMessage(*v)
	}
	return _c
}

// SetCanceledBy sets the "canceled_by" field.
func (_c *UsageCleanupTaskCreate) SetCanceledBy(v int64) *UsageCleanupTaskCreate {
	_c.mutation.SetCanceledBy(v)
	return _c
}

// SetNillableCanceledBy sets the "canceled_by" field if the given value is not nil.
func (_c *UsageCleanupTaskCreate) SetNillableCanceledBy(v *int64) *UsageCleanupTaskCreate {
	if v != nil {
		_c.SetCanceledBy(*v)
	}
	return _c
}

// SetCanceledAt sets the "canceled_at" field.
func (_c *UsageCleanupTaskCreate) SetCanceledAt(v time.Time) *UsageCleanupTaskCreate {
	_c.mutation.SetCanceledAt(v)
	return _c
}

// SetNillableCanceledAt sets the "canceled_at" field if the given value is not nil.
func (_c *UsageCleanupTaskCreate) SetNillableCanceledAt(v *time.Time) *UsageCleanupTaskCreate {
	if v != nil {
		_c.SetCanceledAt(*v)
	}
	return _c
}

// SetStartedAt sets the "started_at" field.
func (_c *UsageCleanupTaskCreate) SetStartedAt(v time.Time) *UsageCleanupTaskCreate {
	_c.mutation.SetStartedAt(v)
	return _c
}

// SetNillableStartedAt sets the "started_at" field if the given value is not nil.
func (_c *UsageCleanupTaskCreate) SetNillableStartedAt(v *time.Time) *UsageCleanupTaskCreate {
	if v != nil {
		_c.SetStartedAt(*v)
	}
	return _c
}

// SetFinishedAt sets the "finished_at" field.
func (_c *UsageCleanupTaskCreate) SetFinishedAt(v time.Time) *UsageCleanupTaskCreate {
	_c.mutation.SetFinishedAt(v)
	return _c
}

// SetNillableFinishedAt sets the "finished_at" field if the given value is not nil.
func (_c *UsageCleanupTaskCreate) SetNillableFinishedAt(v *time.Time) *UsageCleanupTaskCreate {
	if v != nil {
		_c.SetFinishedAt(*v)
	}
	return _c
}

// Mutation returns the UsageCleanupTaskMutation object of the builder.
func (_c *UsageCleanupTaskCreate) Mutation() *UsageCleanupTaskMutation {
	return _c.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_c *UsageCleanupTaskCreate) defaults() {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := usagecleanuptask.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := usagecleanuptask.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.DeletedRows(); !ok {
		v := usagecleanuptask.DefaultDeletedRows
		_c.mutation.SetDeletedRows(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *UsageCleanupTaskCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "UsageCleanupTask.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "UsageCleanupTask.updated_at"`)}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "UsageCleanupTask.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := usagecleanuptask.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "UsageCleanupTask.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Filters(); !ok {
		return &ValidationError{Name: "filters", err: errors.New(`ent: missing required field "UsageCleanupTask.filters"`)}
	}
	if _, ok := _c.mutation.CreatedBy(); !ok {
		return &ValidationError{Name: "created_by", err: errors.New(`ent: missing required field "UsageCleanupTask.created_by"`)}
	}
	if _, ok := _c.mutation.DeletedRows(); !ok {
		return &ValidationError{Name: "deleted_rows", err: errors.New(`ent: missing required field "UsageCleanupTask.deleted_rows"`)}
	}
	return nil
}

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

func (_c *UsageCleanupTaskCreate) createSpec() (*UsageCleanupTask, *sqlgraph.CreateSpec) {
	var (
		_node = &UsageCleanupTask{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(usagecleanuptask.Table, sqlgraph.NewFieldSpec(usagecleanuptask.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(usagecleanuptask.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(usagecleanuptask.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(usagecleanuptask.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.Filters(); ok {
		_spec.SetField(usagecleanuptask.FieldFilters, field.TypeJSON, value)
		_node.Filters = value
	}
	if value, ok := _c.mutation.CreatedBy(); ok {
		_spec.SetField(usagecleanuptask.FieldCreatedBy, field.TypeInt64, value)
		_node.CreatedBy = value
	}
	if value, ok := _c.mutation.DeletedRows(); ok {
		_spec.SetField(usagecleanuptask.FieldDeletedRows, field.TypeInt64, value)
		_node.DeletedRows = value
	}
	if value, ok := _c.mutation.ErrorMessage(); ok {
		_spec.SetField(usagecleanuptask.FieldErrorMessage, field.TypeString, value)
		_node.ErrorMessage = &value
	}
	if value, ok := _c.mutation.CanceledBy(); ok {
		_spec.SetField(usagecleanuptask.FieldCanceledBy, field.TypeInt64, value)
		_node.CanceledBy = &value
	}
	if value, ok := _c.mutation.CanceledAt(); ok {
		_spec.SetField(usagecleanuptask.FieldCanceledAt, field.TypeTime, value)
		_node.CanceledAt = &value
	}
	if value, ok := _c.mutation.StartedAt(); ok {
		_spec.SetField(usagecleanuptask.FieldStartedAt, field.TypeTime, value)
		_node.StartedAt = &value
	}
	if value, ok := _c.mutation.FinishedAt(); ok {
		_spec.SetField(usagecleanuptask.FieldFinishedAt, field.TypeTime, value)
		_node.FinishedAt = &value
	}
	return _node, _spec
}

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

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

type (
	// UsageCleanupTaskUpsertOne is the builder for "upsert"-ing
	//  one UsageCleanupTask node.
	UsageCleanupTaskUpsertOne struct {
		create *UsageCleanupTaskCreate
	}

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

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

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

// SetStatus sets the "status" field.
func (u *UsageCleanupTaskUpsert) SetStatus(v string) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldStatus, v)
	return u
}

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

// SetFilters sets the "filters" field.
func (u *UsageCleanupTaskUpsert) SetFilters(v json.RawMessage) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldFilters, v)
	return u
}

// UpdateFilters sets the "filters" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateFilters() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldFilters)
	return u
}

// SetCreatedBy sets the "created_by" field.
func (u *UsageCleanupTaskUpsert) SetCreatedBy(v int64) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldCreatedBy, v)
	return u
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateCreatedBy() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldCreatedBy)
	return u
}

// AddCreatedBy adds v to the "created_by" field.
func (u *UsageCleanupTaskUpsert) AddCreatedBy(v int64) *UsageCleanupTaskUpsert {
	u.Add(usagecleanuptask.FieldCreatedBy, v)
	return u
}

// SetDeletedRows sets the "deleted_rows" field.
func (u *UsageCleanupTaskUpsert) SetDeletedRows(v int64) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldDeletedRows, v)
	return u
}

// UpdateDeletedRows sets the "deleted_rows" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateDeletedRows() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldDeletedRows)
	return u
}

// AddDeletedRows adds v to the "deleted_rows" field.
func (u *UsageCleanupTaskUpsert) AddDeletedRows(v int64) *UsageCleanupTaskUpsert {
	u.Add(usagecleanuptask.FieldDeletedRows, v)
	return u
}

// SetErrorMessage sets the "error_message" field.
func (u *UsageCleanupTaskUpsert) SetErrorMessage(v string) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldErrorMessage, v)
	return u
}

// UpdateErrorMessage sets the "error_message" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateErrorMessage() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldErrorMessage)
	return u
}

// ClearErrorMessage clears the value of the "error_message" field.
func (u *UsageCleanupTaskUpsert) ClearErrorMessage() *UsageCleanupTaskUpsert {
	u.SetNull(usagecleanuptask.FieldErrorMessage)
	return u
}

// SetCanceledBy sets the "canceled_by" field.
func (u *UsageCleanupTaskUpsert) SetCanceledBy(v int64) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldCanceledBy, v)
	return u
}

// UpdateCanceledBy sets the "canceled_by" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateCanceledBy() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldCanceledBy)
	return u
}

// AddCanceledBy adds v to the "canceled_by" field.
func (u *UsageCleanupTaskUpsert) AddCanceledBy(v int64) *UsageCleanupTaskUpsert {
	u.Add(usagecleanuptask.FieldCanceledBy, v)
	return u
}

// ClearCanceledBy clears the value of the "canceled_by" field.
func (u *UsageCleanupTaskUpsert) ClearCanceledBy() *UsageCleanupTaskUpsert {
	u.SetNull(usagecleanuptask.FieldCanceledBy)
	return u
}

// SetCanceledAt sets the "canceled_at" field.
func (u *UsageCleanupTaskUpsert) SetCanceledAt(v time.Time) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldCanceledAt, v)
	return u
}

// UpdateCanceledAt sets the "canceled_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateCanceledAt() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldCanceledAt)
	return u
}

// ClearCanceledAt clears the value of the "canceled_at" field.
func (u *UsageCleanupTaskUpsert) ClearCanceledAt() *UsageCleanupTaskUpsert {
	u.SetNull(usagecleanuptask.FieldCanceledAt)
	return u
}

// SetStartedAt sets the "started_at" field.
func (u *UsageCleanupTaskUpsert) SetStartedAt(v time.Time) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldStartedAt, v)
	return u
}

// UpdateStartedAt sets the "started_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateStartedAt() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldStartedAt)
	return u
}

// ClearStartedAt clears the value of the "started_at" field.
func (u *UsageCleanupTaskUpsert) ClearStartedAt() *UsageCleanupTaskUpsert {
	u.SetNull(usagecleanuptask.FieldStartedAt)
	return u
}

// SetFinishedAt sets the "finished_at" field.
func (u *UsageCleanupTaskUpsert) SetFinishedAt(v time.Time) *UsageCleanupTaskUpsert {
	u.Set(usagecleanuptask.FieldFinishedAt, v)
	return u
}

// UpdateFinishedAt sets the "finished_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsert) UpdateFinishedAt() *UsageCleanupTaskUpsert {
	u.SetExcluded(usagecleanuptask.FieldFinishedAt)
	return u
}

// ClearFinishedAt clears the value of the "finished_at" field.
func (u *UsageCleanupTaskUpsert) ClearFinishedAt() *UsageCleanupTaskUpsert {
	u.SetNull(usagecleanuptask.FieldFinishedAt)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.UsageCleanupTask.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *UsageCleanupTaskUpsertOne) UpdateNewValues() *UsageCleanupTaskUpsertOne {
	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.CreatedAt(); exists {
			s.SetIgnore(usagecleanuptask.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

// SetStatus sets the "status" field.
func (u *UsageCleanupTaskUpsertOne) SetStatus(v string) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetStatus(v)
	})
}

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

// SetFilters sets the "filters" field.
func (u *UsageCleanupTaskUpsertOne) SetFilters(v json.RawMessage) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetFilters(v)
	})
}

// UpdateFilters sets the "filters" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateFilters() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateFilters()
	})
}

// SetCreatedBy sets the "created_by" field.
func (u *UsageCleanupTaskUpsertOne) SetCreatedBy(v int64) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetCreatedBy(v)
	})
}

// AddCreatedBy adds v to the "created_by" field.
func (u *UsageCleanupTaskUpsertOne) AddCreatedBy(v int64) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.AddCreatedBy(v)
	})
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateCreatedBy() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateCreatedBy()
	})
}

// SetDeletedRows sets the "deleted_rows" field.
func (u *UsageCleanupTaskUpsertOne) SetDeletedRows(v int64) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetDeletedRows(v)
	})
}

// AddDeletedRows adds v to the "deleted_rows" field.
func (u *UsageCleanupTaskUpsertOne) AddDeletedRows(v int64) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.AddDeletedRows(v)
	})
}

// UpdateDeletedRows sets the "deleted_rows" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateDeletedRows() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateDeletedRows()
	})
}

// SetErrorMessage sets the "error_message" field.
func (u *UsageCleanupTaskUpsertOne) SetErrorMessage(v string) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetErrorMessage(v)
	})
}

// UpdateErrorMessage sets the "error_message" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateErrorMessage() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateErrorMessage()
	})
}

// ClearErrorMessage clears the value of the "error_message" field.
func (u *UsageCleanupTaskUpsertOne) ClearErrorMessage() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearErrorMessage()
	})
}

// SetCanceledBy sets the "canceled_by" field.
func (u *UsageCleanupTaskUpsertOne) SetCanceledBy(v int64) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetCanceledBy(v)
	})
}

// AddCanceledBy adds v to the "canceled_by" field.
func (u *UsageCleanupTaskUpsertOne) AddCanceledBy(v int64) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.AddCanceledBy(v)
	})
}

// UpdateCanceledBy sets the "canceled_by" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateCanceledBy() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateCanceledBy()
	})
}

// ClearCanceledBy clears the value of the "canceled_by" field.
func (u *UsageCleanupTaskUpsertOne) ClearCanceledBy() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearCanceledBy()
	})
}

// SetCanceledAt sets the "canceled_at" field.
func (u *UsageCleanupTaskUpsertOne) SetCanceledAt(v time.Time) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetCanceledAt(v)
	})
}

// UpdateCanceledAt sets the "canceled_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateCanceledAt() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateCanceledAt()
	})
}

// ClearCanceledAt clears the value of the "canceled_at" field.
func (u *UsageCleanupTaskUpsertOne) ClearCanceledAt() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearCanceledAt()
	})
}

// SetStartedAt sets the "started_at" field.
func (u *UsageCleanupTaskUpsertOne) SetStartedAt(v time.Time) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetStartedAt(v)
	})
}

// UpdateStartedAt sets the "started_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateStartedAt() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateStartedAt()
	})
}

// ClearStartedAt clears the value of the "started_at" field.
func (u *UsageCleanupTaskUpsertOne) ClearStartedAt() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearStartedAt()
	})
}

// SetFinishedAt sets the "finished_at" field.
func (u *UsageCleanupTaskUpsertOne) SetFinishedAt(v time.Time) *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetFinishedAt(v)
	})
}

// UpdateFinishedAt sets the "finished_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertOne) UpdateFinishedAt() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateFinishedAt()
	})
}

// ClearFinishedAt clears the value of the "finished_at" field.
func (u *UsageCleanupTaskUpsertOne) ClearFinishedAt() *UsageCleanupTaskUpsertOne {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearFinishedAt()
	})
}

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

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

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

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

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

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

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

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

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

// UsageCleanupTaskUpsertBulk is the builder for "upsert"-ing
// a bulk of UsageCleanupTask nodes.
type UsageCleanupTaskUpsertBulk struct {
	create *UsageCleanupTaskCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.UsageCleanupTask.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *UsageCleanupTaskUpsertBulk) UpdateNewValues() *UsageCleanupTaskUpsertBulk {
	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.CreatedAt(); exists {
				s.SetIgnore(usagecleanuptask.FieldCreatedAt)
			}
		}
	}))
	return u
}

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

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

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

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

// SetStatus sets the "status" field.
func (u *UsageCleanupTaskUpsertBulk) SetStatus(v string) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetStatus(v)
	})
}

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

// SetFilters sets the "filters" field.
func (u *UsageCleanupTaskUpsertBulk) SetFilters(v json.RawMessage) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetFilters(v)
	})
}

// UpdateFilters sets the "filters" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateFilters() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateFilters()
	})
}

// SetCreatedBy sets the "created_by" field.
func (u *UsageCleanupTaskUpsertBulk) SetCreatedBy(v int64) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetCreatedBy(v)
	})
}

// AddCreatedBy adds v to the "created_by" field.
func (u *UsageCleanupTaskUpsertBulk) AddCreatedBy(v int64) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.AddCreatedBy(v)
	})
}

// UpdateCreatedBy sets the "created_by" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateCreatedBy() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateCreatedBy()
	})
}

// SetDeletedRows sets the "deleted_rows" field.
func (u *UsageCleanupTaskUpsertBulk) SetDeletedRows(v int64) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetDeletedRows(v)
	})
}

// AddDeletedRows adds v to the "deleted_rows" field.
func (u *UsageCleanupTaskUpsertBulk) AddDeletedRows(v int64) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.AddDeletedRows(v)
	})
}

// UpdateDeletedRows sets the "deleted_rows" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateDeletedRows() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateDeletedRows()
	})
}

// SetErrorMessage sets the "error_message" field.
func (u *UsageCleanupTaskUpsertBulk) SetErrorMessage(v string) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetErrorMessage(v)
	})
}

// UpdateErrorMessage sets the "error_message" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateErrorMessage() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateErrorMessage()
	})
}

// ClearErrorMessage clears the value of the "error_message" field.
func (u *UsageCleanupTaskUpsertBulk) ClearErrorMessage() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearErrorMessage()
	})
}

// SetCanceledBy sets the "canceled_by" field.
func (u *UsageCleanupTaskUpsertBulk) SetCanceledBy(v int64) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetCanceledBy(v)
	})
}

// AddCanceledBy adds v to the "canceled_by" field.
func (u *UsageCleanupTaskUpsertBulk) AddCanceledBy(v int64) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.AddCanceledBy(v)
	})
}

// UpdateCanceledBy sets the "canceled_by" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateCanceledBy() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateCanceledBy()
	})
}

// ClearCanceledBy clears the value of the "canceled_by" field.
func (u *UsageCleanupTaskUpsertBulk) ClearCanceledBy() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearCanceledBy()
	})
}

// SetCanceledAt sets the "canceled_at" field.
func (u *UsageCleanupTaskUpsertBulk) SetCanceledAt(v time.Time) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetCanceledAt(v)
	})
}

// UpdateCanceledAt sets the "canceled_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateCanceledAt() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateCanceledAt()
	})
}

// ClearCanceledAt clears the value of the "canceled_at" field.
func (u *UsageCleanupTaskUpsertBulk) ClearCanceledAt() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearCanceledAt()
	})
}

// SetStartedAt sets the "started_at" field.
func (u *UsageCleanupTaskUpsertBulk) SetStartedAt(v time.Time) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetStartedAt(v)
	})
}

// UpdateStartedAt sets the "started_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateStartedAt() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateStartedAt()
	})
}

// ClearStartedAt clears the value of the "started_at" field.
func (u *UsageCleanupTaskUpsertBulk) ClearStartedAt() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearStartedAt()
	})
}

// SetFinishedAt sets the "finished_at" field.
func (u *UsageCleanupTaskUpsertBulk) SetFinishedAt(v time.Time) *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.SetFinishedAt(v)
	})
}

// UpdateFinishedAt sets the "finished_at" field to the value that was provided on create.
func (u *UsageCleanupTaskUpsertBulk) UpdateFinishedAt() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.UpdateFinishedAt()
	})
}

// ClearFinishedAt clears the value of the "finished_at" field.
func (u *UsageCleanupTaskUpsertBulk) ClearFinishedAt() *UsageCleanupTaskUpsertBulk {
	return u.Update(func(s *UsageCleanupTaskUpsert) {
		s.ClearFinishedAt()
	})
}

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

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