// Code generated by ent, DO NOT EDIT.

package ent

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

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

// CreditBalanceUpdate is the builder for updating CreditBalance entities.
type CreditBalanceUpdate struct {
	config
	hooks    []Hook
	mutation *CreditBalanceMutation
}

// Where appends a list predicates to the CreditBalanceUpdate builder.
func (cbu *CreditBalanceUpdate) Where(ps ...predicate.CreditBalance) *CreditBalanceUpdate {
	cbu.mutation.Where(ps...)
	return cbu
}

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

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

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

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

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

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

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

// SetBalance sets the "balance" field.
func (cbu *CreditBalanceUpdate) SetBalance(f float32) *CreditBalanceUpdate {
	cbu.mutation.ResetBalance()
	cbu.mutation.SetBalance(f)
	return cbu
}

// SetNillableBalance sets the "balance" field if the given value is not nil.
func (cbu *CreditBalanceUpdate) SetNillableBalance(f *float32) *CreditBalanceUpdate {
	if f != nil {
		cbu.SetBalance(*f)
	}
	return cbu
}

// AddBalance adds f to the "balance" field.
func (cbu *CreditBalanceUpdate) AddBalance(f float32) *CreditBalanceUpdate {
	cbu.mutation.AddBalance(f)
	return cbu
}

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

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

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

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

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

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

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

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

// Mutation returns the CreditBalanceMutation object of the builder.
func (cbu *CreditBalanceUpdate) Mutation() *CreditBalanceMutation {
	return cbu.mutation
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (cbu *CreditBalanceUpdate) check() error {
	if v, ok := cbu.mutation.Status(); ok {
		if err := creditbalance.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "CreditBalance.status": %w`, err)}
		}
	}
	return nil
}

func (cbu *CreditBalanceUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := cbu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(creditbalance.Table, creditbalance.Columns, sqlgraph.NewFieldSpec(creditbalance.FieldID, field.TypeUint64))
	if ps := cbu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cbu.mutation.UpdatedAt(); ok {
		_spec.SetField(creditbalance.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cbu.mutation.DeletedAt(); ok {
		_spec.SetField(creditbalance.FieldDeletedAt, field.TypeTime, value)
	}
	if cbu.mutation.DeletedAtCleared() {
		_spec.ClearField(creditbalance.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := cbu.mutation.UserID(); ok {
		_spec.SetField(creditbalance.FieldUserID, field.TypeString, value)
	}
	if cbu.mutation.UserIDCleared() {
		_spec.ClearField(creditbalance.FieldUserID, field.TypeString)
	}
	if value, ok := cbu.mutation.Balance(); ok {
		_spec.SetField(creditbalance.FieldBalance, field.TypeFloat32, value)
	}
	if value, ok := cbu.mutation.AddedBalance(); ok {
		_spec.AddField(creditbalance.FieldBalance, field.TypeFloat32, value)
	}
	if value, ok := cbu.mutation.Status(); ok {
		_spec.SetField(creditbalance.FieldStatus, field.TypeInt, value)
	}
	if value, ok := cbu.mutation.AddedStatus(); ok {
		_spec.AddField(creditbalance.FieldStatus, field.TypeInt, value)
	}
	if cbu.mutation.StatusCleared() {
		_spec.ClearField(creditbalance.FieldStatus, field.TypeInt)
	}
	if value, ok := cbu.mutation.OrganizationID(); ok {
		_spec.SetField(creditbalance.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cbu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(creditbalance.FieldOrganizationID, field.TypeUint64, value)
	}
	if cbu.mutation.OrganizationIDCleared() {
		_spec.ClearField(creditbalance.FieldOrganizationID, field.TypeUint64)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, cbu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{creditbalance.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	cbu.mutation.done = true
	return n, nil
}

// CreditBalanceUpdateOne is the builder for updating a single CreditBalance entity.
type CreditBalanceUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *CreditBalanceMutation
}

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

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

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

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

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

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

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

// SetBalance sets the "balance" field.
func (cbuo *CreditBalanceUpdateOne) SetBalance(f float32) *CreditBalanceUpdateOne {
	cbuo.mutation.ResetBalance()
	cbuo.mutation.SetBalance(f)
	return cbuo
}

// SetNillableBalance sets the "balance" field if the given value is not nil.
func (cbuo *CreditBalanceUpdateOne) SetNillableBalance(f *float32) *CreditBalanceUpdateOne {
	if f != nil {
		cbuo.SetBalance(*f)
	}
	return cbuo
}

// AddBalance adds f to the "balance" field.
func (cbuo *CreditBalanceUpdateOne) AddBalance(f float32) *CreditBalanceUpdateOne {
	cbuo.mutation.AddBalance(f)
	return cbuo
}

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

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

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

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

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

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

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

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

// Mutation returns the CreditBalanceMutation object of the builder.
func (cbuo *CreditBalanceUpdateOne) Mutation() *CreditBalanceMutation {
	return cbuo.mutation
}

// Where appends a list predicates to the CreditBalanceUpdate builder.
func (cbuo *CreditBalanceUpdateOne) Where(ps ...predicate.CreditBalance) *CreditBalanceUpdateOne {
	cbuo.mutation.Where(ps...)
	return cbuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (cbuo *CreditBalanceUpdateOne) check() error {
	if v, ok := cbuo.mutation.Status(); ok {
		if err := creditbalance.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "CreditBalance.status": %w`, err)}
		}
	}
	return nil
}

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