// Code generated by ent, DO NOT EDIT.

package ent

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

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

// WxCardVisitUpdate is the builder for updating WxCardVisit entities.
type WxCardVisitUpdate struct {
	config
	hooks    []Hook
	mutation *WxCardVisitMutation
}

// Where appends a list predicates to the WxCardVisitUpdate builder.
func (wcvu *WxCardVisitUpdate) Where(ps ...predicate.WxCardVisit) *WxCardVisitUpdate {
	wcvu.mutation.Where(ps...)
	return wcvu
}

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

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

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

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

// SetUserID sets the "user_id" field.
func (wcvu *WxCardVisitUpdate) SetUserID(u uint64) *WxCardVisitUpdate {
	wcvu.mutation.ResetUserID()
	wcvu.mutation.SetUserID(u)
	return wcvu
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (wcvu *WxCardVisitUpdate) SetNillableUserID(u *uint64) *WxCardVisitUpdate {
	if u != nil {
		wcvu.SetUserID(*u)
	}
	return wcvu
}

// AddUserID adds u to the "user_id" field.
func (wcvu *WxCardVisitUpdate) AddUserID(u int64) *WxCardVisitUpdate {
	wcvu.mutation.AddUserID(u)
	return wcvu
}

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

// SetBotID sets the "bot_id" field.
func (wcvu *WxCardVisitUpdate) SetBotID(u uint64) *WxCardVisitUpdate {
	wcvu.mutation.ResetBotID()
	wcvu.mutation.SetBotID(u)
	return wcvu
}

// SetNillableBotID sets the "bot_id" field if the given value is not nil.
func (wcvu *WxCardVisitUpdate) SetNillableBotID(u *uint64) *WxCardVisitUpdate {
	if u != nil {
		wcvu.SetBotID(*u)
	}
	return wcvu
}

// AddBotID adds u to the "bot_id" field.
func (wcvu *WxCardVisitUpdate) AddBotID(u int64) *WxCardVisitUpdate {
	wcvu.mutation.AddBotID(u)
	return wcvu
}

// SetBotType sets the "bot_type" field.
func (wcvu *WxCardVisitUpdate) SetBotType(u uint8) *WxCardVisitUpdate {
	wcvu.mutation.ResetBotType()
	wcvu.mutation.SetBotType(u)
	return wcvu
}

// SetNillableBotType sets the "bot_type" field if the given value is not nil.
func (wcvu *WxCardVisitUpdate) SetNillableBotType(u *uint8) *WxCardVisitUpdate {
	if u != nil {
		wcvu.SetBotType(*u)
	}
	return wcvu
}

// AddBotType adds u to the "bot_type" field.
func (wcvu *WxCardVisitUpdate) AddBotType(u int8) *WxCardVisitUpdate {
	wcvu.mutation.AddBotType(u)
	return wcvu
}

// Mutation returns the WxCardVisitMutation object of the builder.
func (wcvu *WxCardVisitUpdate) Mutation() *WxCardVisitMutation {
	return wcvu.mutation
}

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

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

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

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

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

func (wcvu *WxCardVisitUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(wxcardvisit.Table, wxcardvisit.Columns, sqlgraph.NewFieldSpec(wxcardvisit.FieldID, field.TypeUint64))
	if ps := wcvu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcvu.mutation.UpdatedAt(); ok {
		_spec.SetField(wxcardvisit.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcvu.mutation.DeletedAt(); ok {
		_spec.SetField(wxcardvisit.FieldDeletedAt, field.TypeTime, value)
	}
	if wcvu.mutation.DeletedAtCleared() {
		_spec.ClearField(wxcardvisit.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := wcvu.mutation.UserID(); ok {
		_spec.SetField(wxcardvisit.FieldUserID, field.TypeUint64, value)
	}
	if value, ok := wcvu.mutation.AddedUserID(); ok {
		_spec.AddField(wxcardvisit.FieldUserID, field.TypeUint64, value)
	}
	if wcvu.mutation.UserIDCleared() {
		_spec.ClearField(wxcardvisit.FieldUserID, field.TypeUint64)
	}
	if value, ok := wcvu.mutation.BotID(); ok {
		_spec.SetField(wxcardvisit.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := wcvu.mutation.AddedBotID(); ok {
		_spec.AddField(wxcardvisit.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := wcvu.mutation.BotType(); ok {
		_spec.SetField(wxcardvisit.FieldBotType, field.TypeUint8, value)
	}
	if value, ok := wcvu.mutation.AddedBotType(); ok {
		_spec.AddField(wxcardvisit.FieldBotType, field.TypeUint8, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, wcvu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wxcardvisit.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	wcvu.mutation.done = true
	return n, nil
}

// WxCardVisitUpdateOne is the builder for updating a single WxCardVisit entity.
type WxCardVisitUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WxCardVisitMutation
}

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

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

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

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

// SetUserID sets the "user_id" field.
func (wcvuo *WxCardVisitUpdateOne) SetUserID(u uint64) *WxCardVisitUpdateOne {
	wcvuo.mutation.ResetUserID()
	wcvuo.mutation.SetUserID(u)
	return wcvuo
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (wcvuo *WxCardVisitUpdateOne) SetNillableUserID(u *uint64) *WxCardVisitUpdateOne {
	if u != nil {
		wcvuo.SetUserID(*u)
	}
	return wcvuo
}

// AddUserID adds u to the "user_id" field.
func (wcvuo *WxCardVisitUpdateOne) AddUserID(u int64) *WxCardVisitUpdateOne {
	wcvuo.mutation.AddUserID(u)
	return wcvuo
}

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

// SetBotID sets the "bot_id" field.
func (wcvuo *WxCardVisitUpdateOne) SetBotID(u uint64) *WxCardVisitUpdateOne {
	wcvuo.mutation.ResetBotID()
	wcvuo.mutation.SetBotID(u)
	return wcvuo
}

// SetNillableBotID sets the "bot_id" field if the given value is not nil.
func (wcvuo *WxCardVisitUpdateOne) SetNillableBotID(u *uint64) *WxCardVisitUpdateOne {
	if u != nil {
		wcvuo.SetBotID(*u)
	}
	return wcvuo
}

// AddBotID adds u to the "bot_id" field.
func (wcvuo *WxCardVisitUpdateOne) AddBotID(u int64) *WxCardVisitUpdateOne {
	wcvuo.mutation.AddBotID(u)
	return wcvuo
}

// SetBotType sets the "bot_type" field.
func (wcvuo *WxCardVisitUpdateOne) SetBotType(u uint8) *WxCardVisitUpdateOne {
	wcvuo.mutation.ResetBotType()
	wcvuo.mutation.SetBotType(u)
	return wcvuo
}

// SetNillableBotType sets the "bot_type" field if the given value is not nil.
func (wcvuo *WxCardVisitUpdateOne) SetNillableBotType(u *uint8) *WxCardVisitUpdateOne {
	if u != nil {
		wcvuo.SetBotType(*u)
	}
	return wcvuo
}

// AddBotType adds u to the "bot_type" field.
func (wcvuo *WxCardVisitUpdateOne) AddBotType(u int8) *WxCardVisitUpdateOne {
	wcvuo.mutation.AddBotType(u)
	return wcvuo
}

// Mutation returns the WxCardVisitMutation object of the builder.
func (wcvuo *WxCardVisitUpdateOne) Mutation() *WxCardVisitMutation {
	return wcvuo.mutation
}

// Where appends a list predicates to the WxCardVisitUpdate builder.
func (wcvuo *WxCardVisitUpdateOne) Where(ps ...predicate.WxCardVisit) *WxCardVisitUpdateOne {
	wcvuo.mutation.Where(ps...)
	return wcvuo
}

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

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

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

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

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

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

func (wcvuo *WxCardVisitUpdateOne) sqlSave(ctx context.Context) (_node *WxCardVisit, err error) {
	_spec := sqlgraph.NewUpdateSpec(wxcardvisit.Table, wxcardvisit.Columns, sqlgraph.NewFieldSpec(wxcardvisit.FieldID, field.TypeUint64))
	id, ok := wcvuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "WxCardVisit.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := wcvuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, wxcardvisit.FieldID)
		for _, f := range fields {
			if !wxcardvisit.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != wxcardvisit.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := wcvuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcvuo.mutation.UpdatedAt(); ok {
		_spec.SetField(wxcardvisit.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcvuo.mutation.DeletedAt(); ok {
		_spec.SetField(wxcardvisit.FieldDeletedAt, field.TypeTime, value)
	}
	if wcvuo.mutation.DeletedAtCleared() {
		_spec.ClearField(wxcardvisit.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := wcvuo.mutation.UserID(); ok {
		_spec.SetField(wxcardvisit.FieldUserID, field.TypeUint64, value)
	}
	if value, ok := wcvuo.mutation.AddedUserID(); ok {
		_spec.AddField(wxcardvisit.FieldUserID, field.TypeUint64, value)
	}
	if wcvuo.mutation.UserIDCleared() {
		_spec.ClearField(wxcardvisit.FieldUserID, field.TypeUint64)
	}
	if value, ok := wcvuo.mutation.BotID(); ok {
		_spec.SetField(wxcardvisit.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := wcvuo.mutation.AddedBotID(); ok {
		_spec.AddField(wxcardvisit.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := wcvuo.mutation.BotType(); ok {
		_spec.SetField(wxcardvisit.FieldBotType, field.TypeUint8, value)
	}
	if value, ok := wcvuo.mutation.AddedBotType(); ok {
		_spec.AddField(wxcardvisit.FieldBotType, field.TypeUint8, value)
	}
	_node = &WxCardVisit{config: wcvuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, wcvuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wxcardvisit.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	wcvuo.mutation.done = true
	return _node, nil
}