// Code generated by ent, DO NOT EDIT.

package ent

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

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

// CreditUsageUpdate is the builder for updating CreditUsage entities.
type CreditUsageUpdate struct {
	config
	hooks    []Hook
	mutation *CreditUsageMutation
}

// Where appends a list predicates to the CreditUsageUpdate builder.
func (cuu *CreditUsageUpdate) Where(ps ...predicate.CreditUsage) *CreditUsageUpdate {
	cuu.mutation.Where(ps...)
	return cuu
}

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (cuu *CreditUsageUpdate) ClearDeletedAt() *CreditUsageUpdate {
	cuu.mutation.ClearDeletedAt()
	return cuu
}

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

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

// ClearUserID clears the value of the "user_id" field.
func (cuu *CreditUsageUpdate) ClearUserID() *CreditUsageUpdate {
	cuu.mutation.ClearUserID()
	return cuu
}

// SetNumber sets the "number" field.
func (cuu *CreditUsageUpdate) SetNumber(f float32) *CreditUsageUpdate {
	cuu.mutation.ResetNumber()
	cuu.mutation.SetNumber(f)
	return cuu
}

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

// AddNumber adds f to the "number" field.
func (cuu *CreditUsageUpdate) AddNumber(f float32) *CreditUsageUpdate {
	cuu.mutation.AddNumber(f)
	return cuu
}

// SetStatus sets the "status" field.
func (cuu *CreditUsageUpdate) SetStatus(i int) *CreditUsageUpdate {
	cuu.mutation.ResetStatus()
	cuu.mutation.SetStatus(i)
	return cuu
}

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

// AddStatus adds i to the "status" field.
func (cuu *CreditUsageUpdate) AddStatus(i int) *CreditUsageUpdate {
	cuu.mutation.AddStatus(i)
	return cuu
}

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

// SetNtype sets the "ntype" field.
func (cuu *CreditUsageUpdate) SetNtype(i int) *CreditUsageUpdate {
	cuu.mutation.ResetNtype()
	cuu.mutation.SetNtype(i)
	return cuu
}

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

// AddNtype adds i to the "ntype" field.
func (cuu *CreditUsageUpdate) AddNtype(i int) *CreditUsageUpdate {
	cuu.mutation.AddNtype(i)
	return cuu
}

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

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

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

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

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

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

// SetNid sets the "nid" field.
func (cuu *CreditUsageUpdate) SetNid(u uint64) *CreditUsageUpdate {
	cuu.mutation.ResetNid()
	cuu.mutation.SetNid(u)
	return cuu
}

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

// AddNid adds u to the "nid" field.
func (cuu *CreditUsageUpdate) AddNid(u int64) *CreditUsageUpdate {
	cuu.mutation.AddNid(u)
	return cuu
}

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

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

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

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

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (cuu *CreditUsageUpdate) Save(ctx context.Context) (int, error) {
	if err := cuu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, cuu.sqlSave, cuu.mutation, cuu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (cuu *CreditUsageUpdate) defaults() error {
	if _, ok := cuu.mutation.UpdatedAt(); !ok {
		if creditusage.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized creditusage.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := creditusage.UpdateDefaultUpdatedAt()
		cuu.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (cuu *CreditUsageUpdate) check() error {
	if v, ok := cuu.mutation.UserID(); ok {
		if err := creditusage.UserIDValidator(v); err != nil {
			return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.user_id": %w`, err)}
		}
	}
	if v, ok := cuu.mutation.Status(); ok {
		if err := creditusage.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.status": %w`, err)}
		}
	}
	if v, ok := cuu.mutation.Reason(); ok {
		if err := creditusage.ReasonValidator(v); err != nil {
			return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.reason": %w`, err)}
		}
	}
	if v, ok := cuu.mutation.Operator(); ok {
		if err := creditusage.OperatorValidator(v); err != nil {
			return &ValidationError{Name: "operator", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.operator": %w`, err)}
		}
	}
	return nil
}

func (cuu *CreditUsageUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := cuu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(creditusage.Table, creditusage.Columns, sqlgraph.NewFieldSpec(creditusage.FieldID, field.TypeUint64))
	if ps := cuu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cuu.mutation.UpdatedAt(); ok {
		_spec.SetField(creditusage.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cuu.mutation.DeletedAt(); ok {
		_spec.SetField(creditusage.FieldDeletedAt, field.TypeTime, value)
	}
	if cuu.mutation.DeletedAtCleared() {
		_spec.ClearField(creditusage.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := cuu.mutation.UserID(); ok {
		_spec.SetField(creditusage.FieldUserID, field.TypeString, value)
	}
	if cuu.mutation.UserIDCleared() {
		_spec.ClearField(creditusage.FieldUserID, field.TypeString)
	}
	if value, ok := cuu.mutation.Number(); ok {
		_spec.SetField(creditusage.FieldNumber, field.TypeFloat32, value)
	}
	if value, ok := cuu.mutation.AddedNumber(); ok {
		_spec.AddField(creditusage.FieldNumber, field.TypeFloat32, value)
	}
	if value, ok := cuu.mutation.Status(); ok {
		_spec.SetField(creditusage.FieldStatus, field.TypeInt, value)
	}
	if value, ok := cuu.mutation.AddedStatus(); ok {
		_spec.AddField(creditusage.FieldStatus, field.TypeInt, value)
	}
	if cuu.mutation.StatusCleared() {
		_spec.ClearField(creditusage.FieldStatus, field.TypeInt)
	}
	if value, ok := cuu.mutation.Ntype(); ok {
		_spec.SetField(creditusage.FieldNtype, field.TypeInt, value)
	}
	if value, ok := cuu.mutation.AddedNtype(); ok {
		_spec.AddField(creditusage.FieldNtype, field.TypeInt, value)
	}
	if value, ok := cuu.mutation.Table(); ok {
		_spec.SetField(creditusage.FieldTable, field.TypeString, value)
	}
	if value, ok := cuu.mutation.OrganizationID(); ok {
		_spec.SetField(creditusage.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cuu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(creditusage.FieldOrganizationID, field.TypeUint64, value)
	}
	if cuu.mutation.OrganizationIDCleared() {
		_spec.ClearField(creditusage.FieldOrganizationID, field.TypeUint64)
	}
	if value, ok := cuu.mutation.Nid(); ok {
		_spec.SetField(creditusage.FieldNid, field.TypeUint64, value)
	}
	if value, ok := cuu.mutation.AddedNid(); ok {
		_spec.AddField(creditusage.FieldNid, field.TypeUint64, value)
	}
	if value, ok := cuu.mutation.Reason(); ok {
		_spec.SetField(creditusage.FieldReason, field.TypeString, value)
	}
	if value, ok := cuu.mutation.Operator(); ok {
		_spec.SetField(creditusage.FieldOperator, field.TypeString, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, cuu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{creditusage.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	cuu.mutation.done = true
	return n, nil
}

// CreditUsageUpdateOne is the builder for updating a single CreditUsage entity.
type CreditUsageUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *CreditUsageMutation
}

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

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (cuuo *CreditUsageUpdateOne) ClearDeletedAt() *CreditUsageUpdateOne {
	cuuo.mutation.ClearDeletedAt()
	return cuuo
}

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

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

// ClearUserID clears the value of the "user_id" field.
func (cuuo *CreditUsageUpdateOne) ClearUserID() *CreditUsageUpdateOne {
	cuuo.mutation.ClearUserID()
	return cuuo
}

// SetNumber sets the "number" field.
func (cuuo *CreditUsageUpdateOne) SetNumber(f float32) *CreditUsageUpdateOne {
	cuuo.mutation.ResetNumber()
	cuuo.mutation.SetNumber(f)
	return cuuo
}

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

// AddNumber adds f to the "number" field.
func (cuuo *CreditUsageUpdateOne) AddNumber(f float32) *CreditUsageUpdateOne {
	cuuo.mutation.AddNumber(f)
	return cuuo
}

// SetStatus sets the "status" field.
func (cuuo *CreditUsageUpdateOne) SetStatus(i int) *CreditUsageUpdateOne {
	cuuo.mutation.ResetStatus()
	cuuo.mutation.SetStatus(i)
	return cuuo
}

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

// AddStatus adds i to the "status" field.
func (cuuo *CreditUsageUpdateOne) AddStatus(i int) *CreditUsageUpdateOne {
	cuuo.mutation.AddStatus(i)
	return cuuo
}

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

// SetNtype sets the "ntype" field.
func (cuuo *CreditUsageUpdateOne) SetNtype(i int) *CreditUsageUpdateOne {
	cuuo.mutation.ResetNtype()
	cuuo.mutation.SetNtype(i)
	return cuuo
}

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

// AddNtype adds i to the "ntype" field.
func (cuuo *CreditUsageUpdateOne) AddNtype(i int) *CreditUsageUpdateOne {
	cuuo.mutation.AddNtype(i)
	return cuuo
}

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

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

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

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

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

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

// SetNid sets the "nid" field.
func (cuuo *CreditUsageUpdateOne) SetNid(u uint64) *CreditUsageUpdateOne {
	cuuo.mutation.ResetNid()
	cuuo.mutation.SetNid(u)
	return cuuo
}

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

// AddNid adds u to the "nid" field.
func (cuuo *CreditUsageUpdateOne) AddNid(u int64) *CreditUsageUpdateOne {
	cuuo.mutation.AddNid(u)
	return cuuo
}

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

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

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

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

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

// Where appends a list predicates to the CreditUsageUpdate builder.
func (cuuo *CreditUsageUpdateOne) Where(ps ...predicate.CreditUsage) *CreditUsageUpdateOne {
	cuuo.mutation.Where(ps...)
	return cuuo
}

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

// Save executes the query and returns the updated CreditUsage entity.
func (cuuo *CreditUsageUpdateOne) Save(ctx context.Context) (*CreditUsage, error) {
	if err := cuuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, cuuo.sqlSave, cuuo.mutation, cuuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (cuuo *CreditUsageUpdateOne) defaults() error {
	if _, ok := cuuo.mutation.UpdatedAt(); !ok {
		if creditusage.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized creditusage.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := creditusage.UpdateDefaultUpdatedAt()
		cuuo.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (cuuo *CreditUsageUpdateOne) check() error {
	if v, ok := cuuo.mutation.UserID(); ok {
		if err := creditusage.UserIDValidator(v); err != nil {
			return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.user_id": %w`, err)}
		}
	}
	if v, ok := cuuo.mutation.Status(); ok {
		if err := creditusage.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.status": %w`, err)}
		}
	}
	if v, ok := cuuo.mutation.Reason(); ok {
		if err := creditusage.ReasonValidator(v); err != nil {
			return &ValidationError{Name: "reason", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.reason": %w`, err)}
		}
	}
	if v, ok := cuuo.mutation.Operator(); ok {
		if err := creditusage.OperatorValidator(v); err != nil {
			return &ValidationError{Name: "operator", err: fmt.Errorf(`ent: validator failed for field "CreditUsage.operator": %w`, err)}
		}
	}
	return nil
}

func (cuuo *CreditUsageUpdateOne) sqlSave(ctx context.Context) (_node *CreditUsage, err error) {
	if err := cuuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(creditusage.Table, creditusage.Columns, sqlgraph.NewFieldSpec(creditusage.FieldID, field.TypeUint64))
	id, ok := cuuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "CreditUsage.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := cuuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, creditusage.FieldID)
		for _, f := range fields {
			if !creditusage.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != creditusage.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := cuuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cuuo.mutation.UpdatedAt(); ok {
		_spec.SetField(creditusage.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cuuo.mutation.DeletedAt(); ok {
		_spec.SetField(creditusage.FieldDeletedAt, field.TypeTime, value)
	}
	if cuuo.mutation.DeletedAtCleared() {
		_spec.ClearField(creditusage.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := cuuo.mutation.UserID(); ok {
		_spec.SetField(creditusage.FieldUserID, field.TypeString, value)
	}
	if cuuo.mutation.UserIDCleared() {
		_spec.ClearField(creditusage.FieldUserID, field.TypeString)
	}
	if value, ok := cuuo.mutation.Number(); ok {
		_spec.SetField(creditusage.FieldNumber, field.TypeFloat32, value)
	}
	if value, ok := cuuo.mutation.AddedNumber(); ok {
		_spec.AddField(creditusage.FieldNumber, field.TypeFloat32, value)
	}
	if value, ok := cuuo.mutation.Status(); ok {
		_spec.SetField(creditusage.FieldStatus, field.TypeInt, value)
	}
	if value, ok := cuuo.mutation.AddedStatus(); ok {
		_spec.AddField(creditusage.FieldStatus, field.TypeInt, value)
	}
	if cuuo.mutation.StatusCleared() {
		_spec.ClearField(creditusage.FieldStatus, field.TypeInt)
	}
	if value, ok := cuuo.mutation.Ntype(); ok {
		_spec.SetField(creditusage.FieldNtype, field.TypeInt, value)
	}
	if value, ok := cuuo.mutation.AddedNtype(); ok {
		_spec.AddField(creditusage.FieldNtype, field.TypeInt, value)
	}
	if value, ok := cuuo.mutation.Table(); ok {
		_spec.SetField(creditusage.FieldTable, field.TypeString, value)
	}
	if value, ok := cuuo.mutation.OrganizationID(); ok {
		_spec.SetField(creditusage.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cuuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(creditusage.FieldOrganizationID, field.TypeUint64, value)
	}
	if cuuo.mutation.OrganizationIDCleared() {
		_spec.ClearField(creditusage.FieldOrganizationID, field.TypeUint64)
	}
	if value, ok := cuuo.mutation.Nid(); ok {
		_spec.SetField(creditusage.FieldNid, field.TypeUint64, value)
	}
	if value, ok := cuuo.mutation.AddedNid(); ok {
		_spec.AddField(creditusage.FieldNid, field.TypeUint64, value)
	}
	if value, ok := cuuo.mutation.Reason(); ok {
		_spec.SetField(creditusage.FieldReason, field.TypeString, value)
	}
	if value, ok := cuuo.mutation.Operator(); ok {
		_spec.SetField(creditusage.FieldOperator, field.TypeString, value)
	}
	_node = &CreditUsage{config: cuuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, cuuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{creditusage.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	cuuo.mutation.done = true
	return _node, nil
}
