// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/custom_types"
	"wechat-api/ent/predicate"
	"wechat-api/ent/usagedetail"

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

// UsageDetailUpdate is the builder for updating UsageDetail entities.
type UsageDetailUpdate struct {
	config
	hooks    []Hook
	mutation *UsageDetailMutation
}

// Where appends a list predicates to the UsageDetailUpdate builder.
func (udu *UsageDetailUpdate) Where(ps ...predicate.UsageDetail) *UsageDetailUpdate {
	udu.mutation.Where(ps...)
	return udu
}

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

// SetStatus sets the "status" field.
func (udu *UsageDetailUpdate) SetStatus(u uint8) *UsageDetailUpdate {
	udu.mutation.ResetStatus()
	udu.mutation.SetStatus(u)
	return udu
}

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

// AddStatus adds u to the "status" field.
func (udu *UsageDetailUpdate) AddStatus(u int8) *UsageDetailUpdate {
	udu.mutation.AddStatus(u)
	return udu
}

// ClearStatus clears the value of the "status" field.
func (udu *UsageDetailUpdate) ClearStatus() *UsageDetailUpdate {
	udu.mutation.ClearStatus()
	return udu
}

// SetType sets the "type" field.
func (udu *UsageDetailUpdate) SetType(i int) *UsageDetailUpdate {
	udu.mutation.ResetType()
	udu.mutation.SetType(i)
	return udu
}

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

// AddType adds i to the "type" field.
func (udu *UsageDetailUpdate) AddType(i int) *UsageDetailUpdate {
	udu.mutation.AddType(i)
	return udu
}

// ClearType clears the value of the "type" field.
func (udu *UsageDetailUpdate) ClearType() *UsageDetailUpdate {
	udu.mutation.ClearType()
	return udu
}

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

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

// SetReceiverID sets the "receiver_id" field.
func (udu *UsageDetailUpdate) SetReceiverID(s string) *UsageDetailUpdate {
	udu.mutation.SetReceiverID(s)
	return udu
}

// SetNillableReceiverID sets the "receiver_id" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillableReceiverID(s *string) *UsageDetailUpdate {
	if s != nil {
		udu.SetReceiverID(*s)
	}
	return udu
}

// SetApp sets the "app" field.
func (udu *UsageDetailUpdate) SetApp(i int) *UsageDetailUpdate {
	udu.mutation.ResetApp()
	udu.mutation.SetApp(i)
	return udu
}

// SetNillableApp sets the "app" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillableApp(i *int) *UsageDetailUpdate {
	if i != nil {
		udu.SetApp(*i)
	}
	return udu
}

// AddApp adds i to the "app" field.
func (udu *UsageDetailUpdate) AddApp(i int) *UsageDetailUpdate {
	udu.mutation.AddApp(i)
	return udu
}

// ClearApp clears the value of the "app" field.
func (udu *UsageDetailUpdate) ClearApp() *UsageDetailUpdate {
	udu.mutation.ClearApp()
	return udu
}

// SetSessionID sets the "session_id" field.
func (udu *UsageDetailUpdate) SetSessionID(u uint64) *UsageDetailUpdate {
	udu.mutation.ResetSessionID()
	udu.mutation.SetSessionID(u)
	return udu
}

// SetNillableSessionID sets the "session_id" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillableSessionID(u *uint64) *UsageDetailUpdate {
	if u != nil {
		udu.SetSessionID(*u)
	}
	return udu
}

// AddSessionID adds u to the "session_id" field.
func (udu *UsageDetailUpdate) AddSessionID(u int64) *UsageDetailUpdate {
	udu.mutation.AddSessionID(u)
	return udu
}

// ClearSessionID clears the value of the "session_id" field.
func (udu *UsageDetailUpdate) ClearSessionID() *UsageDetailUpdate {
	udu.mutation.ClearSessionID()
	return udu
}

// SetRequest sets the "request" field.
func (udu *UsageDetailUpdate) SetRequest(s string) *UsageDetailUpdate {
	udu.mutation.SetRequest(s)
	return udu
}

// SetNillableRequest sets the "request" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillableRequest(s *string) *UsageDetailUpdate {
	if s != nil {
		udu.SetRequest(*s)
	}
	return udu
}

// SetResponse sets the "response" field.
func (udu *UsageDetailUpdate) SetResponse(s string) *UsageDetailUpdate {
	udu.mutation.SetResponse(s)
	return udu
}

// SetNillableResponse sets the "response" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillableResponse(s *string) *UsageDetailUpdate {
	if s != nil {
		udu.SetResponse(*s)
	}
	return udu
}

// SetOriginalData sets the "original_data" field.
func (udu *UsageDetailUpdate) SetOriginalData(ctd custom_types.OriginalData) *UsageDetailUpdate {
	udu.mutation.SetOriginalData(ctd)
	return udu
}

// SetNillableOriginalData sets the "original_data" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillableOriginalData(ctd *custom_types.OriginalData) *UsageDetailUpdate {
	if ctd != nil {
		udu.SetOriginalData(*ctd)
	}
	return udu
}

// SetTotalTokens sets the "total_tokens" field.
func (udu *UsageDetailUpdate) SetTotalTokens(u uint64) *UsageDetailUpdate {
	udu.mutation.ResetTotalTokens()
	udu.mutation.SetTotalTokens(u)
	return udu
}

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

// AddTotalTokens adds u to the "total_tokens" field.
func (udu *UsageDetailUpdate) AddTotalTokens(u int64) *UsageDetailUpdate {
	udu.mutation.AddTotalTokens(u)
	return udu
}

// ClearTotalTokens clears the value of the "total_tokens" field.
func (udu *UsageDetailUpdate) ClearTotalTokens() *UsageDetailUpdate {
	udu.mutation.ClearTotalTokens()
	return udu
}

// SetPromptTokens sets the "prompt_tokens" field.
func (udu *UsageDetailUpdate) SetPromptTokens(u uint64) *UsageDetailUpdate {
	udu.mutation.ResetPromptTokens()
	udu.mutation.SetPromptTokens(u)
	return udu
}

// SetNillablePromptTokens sets the "prompt_tokens" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillablePromptTokens(u *uint64) *UsageDetailUpdate {
	if u != nil {
		udu.SetPromptTokens(*u)
	}
	return udu
}

// AddPromptTokens adds u to the "prompt_tokens" field.
func (udu *UsageDetailUpdate) AddPromptTokens(u int64) *UsageDetailUpdate {
	udu.mutation.AddPromptTokens(u)
	return udu
}

// ClearPromptTokens clears the value of the "prompt_tokens" field.
func (udu *UsageDetailUpdate) ClearPromptTokens() *UsageDetailUpdate {
	udu.mutation.ClearPromptTokens()
	return udu
}

// SetCompletionTokens sets the "completion_tokens" field.
func (udu *UsageDetailUpdate) SetCompletionTokens(u uint64) *UsageDetailUpdate {
	udu.mutation.ResetCompletionTokens()
	udu.mutation.SetCompletionTokens(u)
	return udu
}

// SetNillableCompletionTokens sets the "completion_tokens" field if the given value is not nil.
func (udu *UsageDetailUpdate) SetNillableCompletionTokens(u *uint64) *UsageDetailUpdate {
	if u != nil {
		udu.SetCompletionTokens(*u)
	}
	return udu
}

// AddCompletionTokens adds u to the "completion_tokens" field.
func (udu *UsageDetailUpdate) AddCompletionTokens(u int64) *UsageDetailUpdate {
	udu.mutation.AddCompletionTokens(u)
	return udu
}

// ClearCompletionTokens clears the value of the "completion_tokens" field.
func (udu *UsageDetailUpdate) ClearCompletionTokens() *UsageDetailUpdate {
	udu.mutation.ClearCompletionTokens()
	return udu
}

// SetOrganizationID sets the "organization_id" field.
func (udu *UsageDetailUpdate) SetOrganizationID(u uint64) *UsageDetailUpdate {
	udu.mutation.ResetOrganizationID()
	udu.mutation.SetOrganizationID(u)
	return udu
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (udu *UsageDetailUpdate) AddOrganizationID(u int64) *UsageDetailUpdate {
	udu.mutation.AddOrganizationID(u)
	return udu
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (udu *UsageDetailUpdate) ClearOrganizationID() *UsageDetailUpdate {
	udu.mutation.ClearOrganizationID()
	return udu
}

// Mutation returns the UsageDetailMutation object of the builder.
func (udu *UsageDetailUpdate) Mutation() *UsageDetailMutation {
	return udu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (udu *UsageDetailUpdate) Save(ctx context.Context) (int, error) {
	udu.defaults()
	return withHooks(ctx, udu.sqlSave, udu.mutation, udu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (udu *UsageDetailUpdate) SaveX(ctx context.Context) int {
	affected, err := udu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (udu *UsageDetailUpdate) defaults() {
	if _, ok := udu.mutation.UpdatedAt(); !ok {
		v := usagedetail.UpdateDefaultUpdatedAt()
		udu.mutation.SetUpdatedAt(v)
	}
}

func (udu *UsageDetailUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(usagedetail.Table, usagedetail.Columns, sqlgraph.NewFieldSpec(usagedetail.FieldID, field.TypeUint64))
	if ps := udu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := udu.mutation.UpdatedAt(); ok {
		_spec.SetField(usagedetail.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := udu.mutation.Status(); ok {
		_spec.SetField(usagedetail.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := udu.mutation.AddedStatus(); ok {
		_spec.AddField(usagedetail.FieldStatus, field.TypeUint8, value)
	}
	if udu.mutation.StatusCleared() {
		_spec.ClearField(usagedetail.FieldStatus, field.TypeUint8)
	}
	if value, ok := udu.mutation.GetType(); ok {
		_spec.SetField(usagedetail.FieldType, field.TypeInt, value)
	}
	if value, ok := udu.mutation.AddedType(); ok {
		_spec.AddField(usagedetail.FieldType, field.TypeInt, value)
	}
	if udu.mutation.TypeCleared() {
		_spec.ClearField(usagedetail.FieldType, field.TypeInt)
	}
	if value, ok := udu.mutation.BotID(); ok {
		_spec.SetField(usagedetail.FieldBotID, field.TypeString, value)
	}
	if value, ok := udu.mutation.ReceiverID(); ok {
		_spec.SetField(usagedetail.FieldReceiverID, field.TypeString, value)
	}
	if value, ok := udu.mutation.App(); ok {
		_spec.SetField(usagedetail.FieldApp, field.TypeInt, value)
	}
	if value, ok := udu.mutation.AddedApp(); ok {
		_spec.AddField(usagedetail.FieldApp, field.TypeInt, value)
	}
	if udu.mutation.AppCleared() {
		_spec.ClearField(usagedetail.FieldApp, field.TypeInt)
	}
	if value, ok := udu.mutation.SessionID(); ok {
		_spec.SetField(usagedetail.FieldSessionID, field.TypeUint64, value)
	}
	if value, ok := udu.mutation.AddedSessionID(); ok {
		_spec.AddField(usagedetail.FieldSessionID, field.TypeUint64, value)
	}
	if udu.mutation.SessionIDCleared() {
		_spec.ClearField(usagedetail.FieldSessionID, field.TypeUint64)
	}
	if value, ok := udu.mutation.Request(); ok {
		_spec.SetField(usagedetail.FieldRequest, field.TypeString, value)
	}
	if value, ok := udu.mutation.Response(); ok {
		_spec.SetField(usagedetail.FieldResponse, field.TypeString, value)
	}
	if value, ok := udu.mutation.OriginalData(); ok {
		_spec.SetField(usagedetail.FieldOriginalData, field.TypeJSON, value)
	}
	if value, ok := udu.mutation.TotalTokens(); ok {
		_spec.SetField(usagedetail.FieldTotalTokens, field.TypeUint64, value)
	}
	if value, ok := udu.mutation.AddedTotalTokens(); ok {
		_spec.AddField(usagedetail.FieldTotalTokens, field.TypeUint64, value)
	}
	if udu.mutation.TotalTokensCleared() {
		_spec.ClearField(usagedetail.FieldTotalTokens, field.TypeUint64)
	}
	if value, ok := udu.mutation.PromptTokens(); ok {
		_spec.SetField(usagedetail.FieldPromptTokens, field.TypeUint64, value)
	}
	if value, ok := udu.mutation.AddedPromptTokens(); ok {
		_spec.AddField(usagedetail.FieldPromptTokens, field.TypeUint64, value)
	}
	if udu.mutation.PromptTokensCleared() {
		_spec.ClearField(usagedetail.FieldPromptTokens, field.TypeUint64)
	}
	if value, ok := udu.mutation.CompletionTokens(); ok {
		_spec.SetField(usagedetail.FieldCompletionTokens, field.TypeUint64, value)
	}
	if value, ok := udu.mutation.AddedCompletionTokens(); ok {
		_spec.AddField(usagedetail.FieldCompletionTokens, field.TypeUint64, value)
	}
	if udu.mutation.CompletionTokensCleared() {
		_spec.ClearField(usagedetail.FieldCompletionTokens, field.TypeUint64)
	}
	if value, ok := udu.mutation.OrganizationID(); ok {
		_spec.SetField(usagedetail.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := udu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(usagedetail.FieldOrganizationID, field.TypeUint64, value)
	}
	if udu.mutation.OrganizationIDCleared() {
		_spec.ClearField(usagedetail.FieldOrganizationID, field.TypeUint64)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, udu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usagedetail.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	udu.mutation.done = true
	return n, nil
}

// UsageDetailUpdateOne is the builder for updating a single UsageDetail entity.
type UsageDetailUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UsageDetailMutation
}

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

// SetStatus sets the "status" field.
func (uduo *UsageDetailUpdateOne) SetStatus(u uint8) *UsageDetailUpdateOne {
	uduo.mutation.ResetStatus()
	uduo.mutation.SetStatus(u)
	return uduo
}

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

// AddStatus adds u to the "status" field.
func (uduo *UsageDetailUpdateOne) AddStatus(u int8) *UsageDetailUpdateOne {
	uduo.mutation.AddStatus(u)
	return uduo
}

// ClearStatus clears the value of the "status" field.
func (uduo *UsageDetailUpdateOne) ClearStatus() *UsageDetailUpdateOne {
	uduo.mutation.ClearStatus()
	return uduo
}

// SetType sets the "type" field.
func (uduo *UsageDetailUpdateOne) SetType(i int) *UsageDetailUpdateOne {
	uduo.mutation.ResetType()
	uduo.mutation.SetType(i)
	return uduo
}

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

// AddType adds i to the "type" field.
func (uduo *UsageDetailUpdateOne) AddType(i int) *UsageDetailUpdateOne {
	uduo.mutation.AddType(i)
	return uduo
}

// ClearType clears the value of the "type" field.
func (uduo *UsageDetailUpdateOne) ClearType() *UsageDetailUpdateOne {
	uduo.mutation.ClearType()
	return uduo
}

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

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

// SetReceiverID sets the "receiver_id" field.
func (uduo *UsageDetailUpdateOne) SetReceiverID(s string) *UsageDetailUpdateOne {
	uduo.mutation.SetReceiverID(s)
	return uduo
}

// SetNillableReceiverID sets the "receiver_id" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillableReceiverID(s *string) *UsageDetailUpdateOne {
	if s != nil {
		uduo.SetReceiverID(*s)
	}
	return uduo
}

// SetApp sets the "app" field.
func (uduo *UsageDetailUpdateOne) SetApp(i int) *UsageDetailUpdateOne {
	uduo.mutation.ResetApp()
	uduo.mutation.SetApp(i)
	return uduo
}

// SetNillableApp sets the "app" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillableApp(i *int) *UsageDetailUpdateOne {
	if i != nil {
		uduo.SetApp(*i)
	}
	return uduo
}

// AddApp adds i to the "app" field.
func (uduo *UsageDetailUpdateOne) AddApp(i int) *UsageDetailUpdateOne {
	uduo.mutation.AddApp(i)
	return uduo
}

// ClearApp clears the value of the "app" field.
func (uduo *UsageDetailUpdateOne) ClearApp() *UsageDetailUpdateOne {
	uduo.mutation.ClearApp()
	return uduo
}

// SetSessionID sets the "session_id" field.
func (uduo *UsageDetailUpdateOne) SetSessionID(u uint64) *UsageDetailUpdateOne {
	uduo.mutation.ResetSessionID()
	uduo.mutation.SetSessionID(u)
	return uduo
}

// SetNillableSessionID sets the "session_id" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillableSessionID(u *uint64) *UsageDetailUpdateOne {
	if u != nil {
		uduo.SetSessionID(*u)
	}
	return uduo
}

// AddSessionID adds u to the "session_id" field.
func (uduo *UsageDetailUpdateOne) AddSessionID(u int64) *UsageDetailUpdateOne {
	uduo.mutation.AddSessionID(u)
	return uduo
}

// ClearSessionID clears the value of the "session_id" field.
func (uduo *UsageDetailUpdateOne) ClearSessionID() *UsageDetailUpdateOne {
	uduo.mutation.ClearSessionID()
	return uduo
}

// SetRequest sets the "request" field.
func (uduo *UsageDetailUpdateOne) SetRequest(s string) *UsageDetailUpdateOne {
	uduo.mutation.SetRequest(s)
	return uduo
}

// SetNillableRequest sets the "request" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillableRequest(s *string) *UsageDetailUpdateOne {
	if s != nil {
		uduo.SetRequest(*s)
	}
	return uduo
}

// SetResponse sets the "response" field.
func (uduo *UsageDetailUpdateOne) SetResponse(s string) *UsageDetailUpdateOne {
	uduo.mutation.SetResponse(s)
	return uduo
}

// SetNillableResponse sets the "response" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillableResponse(s *string) *UsageDetailUpdateOne {
	if s != nil {
		uduo.SetResponse(*s)
	}
	return uduo
}

// SetOriginalData sets the "original_data" field.
func (uduo *UsageDetailUpdateOne) SetOriginalData(ctd custom_types.OriginalData) *UsageDetailUpdateOne {
	uduo.mutation.SetOriginalData(ctd)
	return uduo
}

// SetNillableOriginalData sets the "original_data" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillableOriginalData(ctd *custom_types.OriginalData) *UsageDetailUpdateOne {
	if ctd != nil {
		uduo.SetOriginalData(*ctd)
	}
	return uduo
}

// SetTotalTokens sets the "total_tokens" field.
func (uduo *UsageDetailUpdateOne) SetTotalTokens(u uint64) *UsageDetailUpdateOne {
	uduo.mutation.ResetTotalTokens()
	uduo.mutation.SetTotalTokens(u)
	return uduo
}

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

// AddTotalTokens adds u to the "total_tokens" field.
func (uduo *UsageDetailUpdateOne) AddTotalTokens(u int64) *UsageDetailUpdateOne {
	uduo.mutation.AddTotalTokens(u)
	return uduo
}

// ClearTotalTokens clears the value of the "total_tokens" field.
func (uduo *UsageDetailUpdateOne) ClearTotalTokens() *UsageDetailUpdateOne {
	uduo.mutation.ClearTotalTokens()
	return uduo
}

// SetPromptTokens sets the "prompt_tokens" field.
func (uduo *UsageDetailUpdateOne) SetPromptTokens(u uint64) *UsageDetailUpdateOne {
	uduo.mutation.ResetPromptTokens()
	uduo.mutation.SetPromptTokens(u)
	return uduo
}

// SetNillablePromptTokens sets the "prompt_tokens" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillablePromptTokens(u *uint64) *UsageDetailUpdateOne {
	if u != nil {
		uduo.SetPromptTokens(*u)
	}
	return uduo
}

// AddPromptTokens adds u to the "prompt_tokens" field.
func (uduo *UsageDetailUpdateOne) AddPromptTokens(u int64) *UsageDetailUpdateOne {
	uduo.mutation.AddPromptTokens(u)
	return uduo
}

// ClearPromptTokens clears the value of the "prompt_tokens" field.
func (uduo *UsageDetailUpdateOne) ClearPromptTokens() *UsageDetailUpdateOne {
	uduo.mutation.ClearPromptTokens()
	return uduo
}

// SetCompletionTokens sets the "completion_tokens" field.
func (uduo *UsageDetailUpdateOne) SetCompletionTokens(u uint64) *UsageDetailUpdateOne {
	uduo.mutation.ResetCompletionTokens()
	uduo.mutation.SetCompletionTokens(u)
	return uduo
}

// SetNillableCompletionTokens sets the "completion_tokens" field if the given value is not nil.
func (uduo *UsageDetailUpdateOne) SetNillableCompletionTokens(u *uint64) *UsageDetailUpdateOne {
	if u != nil {
		uduo.SetCompletionTokens(*u)
	}
	return uduo
}

// AddCompletionTokens adds u to the "completion_tokens" field.
func (uduo *UsageDetailUpdateOne) AddCompletionTokens(u int64) *UsageDetailUpdateOne {
	uduo.mutation.AddCompletionTokens(u)
	return uduo
}

// ClearCompletionTokens clears the value of the "completion_tokens" field.
func (uduo *UsageDetailUpdateOne) ClearCompletionTokens() *UsageDetailUpdateOne {
	uduo.mutation.ClearCompletionTokens()
	return uduo
}

// SetOrganizationID sets the "organization_id" field.
func (uduo *UsageDetailUpdateOne) SetOrganizationID(u uint64) *UsageDetailUpdateOne {
	uduo.mutation.ResetOrganizationID()
	uduo.mutation.SetOrganizationID(u)
	return uduo
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (uduo *UsageDetailUpdateOne) AddOrganizationID(u int64) *UsageDetailUpdateOne {
	uduo.mutation.AddOrganizationID(u)
	return uduo
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (uduo *UsageDetailUpdateOne) ClearOrganizationID() *UsageDetailUpdateOne {
	uduo.mutation.ClearOrganizationID()
	return uduo
}

// Mutation returns the UsageDetailMutation object of the builder.
func (uduo *UsageDetailUpdateOne) Mutation() *UsageDetailMutation {
	return uduo.mutation
}

// Where appends a list predicates to the UsageDetailUpdate builder.
func (uduo *UsageDetailUpdateOne) Where(ps ...predicate.UsageDetail) *UsageDetailUpdateOne {
	uduo.mutation.Where(ps...)
	return uduo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (uduo *UsageDetailUpdateOne) Select(field string, fields ...string) *UsageDetailUpdateOne {
	uduo.fields = append([]string{field}, fields...)
	return uduo
}

// Save executes the query and returns the updated UsageDetail entity.
func (uduo *UsageDetailUpdateOne) Save(ctx context.Context) (*UsageDetail, error) {
	uduo.defaults()
	return withHooks(ctx, uduo.sqlSave, uduo.mutation, uduo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (uduo *UsageDetailUpdateOne) SaveX(ctx context.Context) *UsageDetail {
	node, err := uduo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (uduo *UsageDetailUpdateOne) Exec(ctx context.Context) error {
	_, err := uduo.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (uduo *UsageDetailUpdateOne) defaults() {
	if _, ok := uduo.mutation.UpdatedAt(); !ok {
		v := usagedetail.UpdateDefaultUpdatedAt()
		uduo.mutation.SetUpdatedAt(v)
	}
}

func (uduo *UsageDetailUpdateOne) sqlSave(ctx context.Context) (_node *UsageDetail, err error) {
	_spec := sqlgraph.NewUpdateSpec(usagedetail.Table, usagedetail.Columns, sqlgraph.NewFieldSpec(usagedetail.FieldID, field.TypeUint64))
	id, ok := uduo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UsageDetail.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := uduo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, usagedetail.FieldID)
		for _, f := range fields {
			if !usagedetail.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != usagedetail.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := uduo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := uduo.mutation.UpdatedAt(); ok {
		_spec.SetField(usagedetail.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := uduo.mutation.Status(); ok {
		_spec.SetField(usagedetail.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := uduo.mutation.AddedStatus(); ok {
		_spec.AddField(usagedetail.FieldStatus, field.TypeUint8, value)
	}
	if uduo.mutation.StatusCleared() {
		_spec.ClearField(usagedetail.FieldStatus, field.TypeUint8)
	}
	if value, ok := uduo.mutation.GetType(); ok {
		_spec.SetField(usagedetail.FieldType, field.TypeInt, value)
	}
	if value, ok := uduo.mutation.AddedType(); ok {
		_spec.AddField(usagedetail.FieldType, field.TypeInt, value)
	}
	if uduo.mutation.TypeCleared() {
		_spec.ClearField(usagedetail.FieldType, field.TypeInt)
	}
	if value, ok := uduo.mutation.BotID(); ok {
		_spec.SetField(usagedetail.FieldBotID, field.TypeString, value)
	}
	if value, ok := uduo.mutation.ReceiverID(); ok {
		_spec.SetField(usagedetail.FieldReceiverID, field.TypeString, value)
	}
	if value, ok := uduo.mutation.App(); ok {
		_spec.SetField(usagedetail.FieldApp, field.TypeInt, value)
	}
	if value, ok := uduo.mutation.AddedApp(); ok {
		_spec.AddField(usagedetail.FieldApp, field.TypeInt, value)
	}
	if uduo.mutation.AppCleared() {
		_spec.ClearField(usagedetail.FieldApp, field.TypeInt)
	}
	if value, ok := uduo.mutation.SessionID(); ok {
		_spec.SetField(usagedetail.FieldSessionID, field.TypeUint64, value)
	}
	if value, ok := uduo.mutation.AddedSessionID(); ok {
		_spec.AddField(usagedetail.FieldSessionID, field.TypeUint64, value)
	}
	if uduo.mutation.SessionIDCleared() {
		_spec.ClearField(usagedetail.FieldSessionID, field.TypeUint64)
	}
	if value, ok := uduo.mutation.Request(); ok {
		_spec.SetField(usagedetail.FieldRequest, field.TypeString, value)
	}
	if value, ok := uduo.mutation.Response(); ok {
		_spec.SetField(usagedetail.FieldResponse, field.TypeString, value)
	}
	if value, ok := uduo.mutation.OriginalData(); ok {
		_spec.SetField(usagedetail.FieldOriginalData, field.TypeJSON, value)
	}
	if value, ok := uduo.mutation.TotalTokens(); ok {
		_spec.SetField(usagedetail.FieldTotalTokens, field.TypeUint64, value)
	}
	if value, ok := uduo.mutation.AddedTotalTokens(); ok {
		_spec.AddField(usagedetail.FieldTotalTokens, field.TypeUint64, value)
	}
	if uduo.mutation.TotalTokensCleared() {
		_spec.ClearField(usagedetail.FieldTotalTokens, field.TypeUint64)
	}
	if value, ok := uduo.mutation.PromptTokens(); ok {
		_spec.SetField(usagedetail.FieldPromptTokens, field.TypeUint64, value)
	}
	if value, ok := uduo.mutation.AddedPromptTokens(); ok {
		_spec.AddField(usagedetail.FieldPromptTokens, field.TypeUint64, value)
	}
	if uduo.mutation.PromptTokensCleared() {
		_spec.ClearField(usagedetail.FieldPromptTokens, field.TypeUint64)
	}
	if value, ok := uduo.mutation.CompletionTokens(); ok {
		_spec.SetField(usagedetail.FieldCompletionTokens, field.TypeUint64, value)
	}
	if value, ok := uduo.mutation.AddedCompletionTokens(); ok {
		_spec.AddField(usagedetail.FieldCompletionTokens, field.TypeUint64, value)
	}
	if uduo.mutation.CompletionTokensCleared() {
		_spec.ClearField(usagedetail.FieldCompletionTokens, field.TypeUint64)
	}
	if value, ok := uduo.mutation.OrganizationID(); ok {
		_spec.SetField(usagedetail.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := uduo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(usagedetail.FieldOrganizationID, field.TypeUint64, value)
	}
	if uduo.mutation.OrganizationIDCleared() {
		_spec.ClearField(usagedetail.FieldOrganizationID, field.TypeUint64)
	}
	_node = &UsageDetail{config: uduo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, uduo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{usagedetail.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	uduo.mutation.done = true
	return _node, nil
}
