// Code generated by ent, DO NOT EDIT.

package ent

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

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

// WxCardUserUpdate is the builder for updating WxCardUser entities.
type WxCardUserUpdate struct {
	config
	hooks    []Hook
	mutation *WxCardUserMutation
}

// Where appends a list predicates to the WxCardUserUpdate builder.
func (wcuu *WxCardUserUpdate) Where(ps ...predicate.WxCardUser) *WxCardUserUpdate {
	wcuu.mutation.Where(ps...)
	return wcuu
}

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

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

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

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

// SetWxid sets the "wxid" field.
func (wcuu *WxCardUserUpdate) SetWxid(s string) *WxCardUserUpdate {
	wcuu.mutation.SetWxid(s)
	return wcuu
}

// SetNillableWxid sets the "wxid" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableWxid(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetWxid(*s)
	}
	return wcuu
}

// SetAccount sets the "account" field.
func (wcuu *WxCardUserUpdate) SetAccount(s string) *WxCardUserUpdate {
	wcuu.mutation.SetAccount(s)
	return wcuu
}

// SetNillableAccount sets the "account" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableAccount(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetAccount(*s)
	}
	return wcuu
}

// SetAvatar sets the "avatar" field.
func (wcuu *WxCardUserUpdate) SetAvatar(s string) *WxCardUserUpdate {
	wcuu.mutation.SetAvatar(s)
	return wcuu
}

// SetNillableAvatar sets the "avatar" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableAvatar(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetAvatar(*s)
	}
	return wcuu
}

// SetNickname sets the "nickname" field.
func (wcuu *WxCardUserUpdate) SetNickname(s string) *WxCardUserUpdate {
	wcuu.mutation.SetNickname(s)
	return wcuu
}

// SetNillableNickname sets the "nickname" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableNickname(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetNickname(*s)
	}
	return wcuu
}

// SetRemark sets the "remark" field.
func (wcuu *WxCardUserUpdate) SetRemark(s string) *WxCardUserUpdate {
	wcuu.mutation.SetRemark(s)
	return wcuu
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableRemark(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetRemark(*s)
	}
	return wcuu
}

// SetPhone sets the "phone" field.
func (wcuu *WxCardUserUpdate) SetPhone(s string) *WxCardUserUpdate {
	wcuu.mutation.SetPhone(s)
	return wcuu
}

// SetNillablePhone sets the "phone" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillablePhone(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetPhone(*s)
	}
	return wcuu
}

// SetOpenID sets the "open_id" field.
func (wcuu *WxCardUserUpdate) SetOpenID(s string) *WxCardUserUpdate {
	wcuu.mutation.SetOpenID(s)
	return wcuu
}

// SetNillableOpenID sets the "open_id" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableOpenID(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetOpenID(*s)
	}
	return wcuu
}

// SetUnionID sets the "union_id" field.
func (wcuu *WxCardUserUpdate) SetUnionID(s string) *WxCardUserUpdate {
	wcuu.mutation.SetUnionID(s)
	return wcuu
}

// SetNillableUnionID sets the "union_id" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableUnionID(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetUnionID(*s)
	}
	return wcuu
}

// SetSessionKey sets the "session_key" field.
func (wcuu *WxCardUserUpdate) SetSessionKey(s string) *WxCardUserUpdate {
	wcuu.mutation.SetSessionKey(s)
	return wcuu
}

// SetNillableSessionKey sets the "session_key" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableSessionKey(s *string) *WxCardUserUpdate {
	if s != nil {
		wcuu.SetSessionKey(*s)
	}
	return wcuu
}

// SetIsVip sets the "is_vip" field.
func (wcuu *WxCardUserUpdate) SetIsVip(i int) *WxCardUserUpdate {
	wcuu.mutation.ResetIsVip()
	wcuu.mutation.SetIsVip(i)
	return wcuu
}

// SetNillableIsVip sets the "is_vip" field if the given value is not nil.
func (wcuu *WxCardUserUpdate) SetNillableIsVip(i *int) *WxCardUserUpdate {
	if i != nil {
		wcuu.SetIsVip(*i)
	}
	return wcuu
}

// AddIsVip adds i to the "is_vip" field.
func (wcuu *WxCardUserUpdate) AddIsVip(i int) *WxCardUserUpdate {
	wcuu.mutation.AddIsVip(i)
	return wcuu
}

// Mutation returns the WxCardUserMutation object of the builder.
func (wcuu *WxCardUserUpdate) Mutation() *WxCardUserMutation {
	return wcuu.mutation
}

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

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

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

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

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

func (wcuu *WxCardUserUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(wxcarduser.Table, wxcarduser.Columns, sqlgraph.NewFieldSpec(wxcarduser.FieldID, field.TypeUint64))
	if ps := wcuu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcuu.mutation.UpdatedAt(); ok {
		_spec.SetField(wxcarduser.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcuu.mutation.DeletedAt(); ok {
		_spec.SetField(wxcarduser.FieldDeletedAt, field.TypeTime, value)
	}
	if wcuu.mutation.DeletedAtCleared() {
		_spec.ClearField(wxcarduser.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := wcuu.mutation.Wxid(); ok {
		_spec.SetField(wxcarduser.FieldWxid, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.Account(); ok {
		_spec.SetField(wxcarduser.FieldAccount, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.Avatar(); ok {
		_spec.SetField(wxcarduser.FieldAvatar, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.Nickname(); ok {
		_spec.SetField(wxcarduser.FieldNickname, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.Remark(); ok {
		_spec.SetField(wxcarduser.FieldRemark, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.Phone(); ok {
		_spec.SetField(wxcarduser.FieldPhone, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.OpenID(); ok {
		_spec.SetField(wxcarduser.FieldOpenID, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.UnionID(); ok {
		_spec.SetField(wxcarduser.FieldUnionID, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.SessionKey(); ok {
		_spec.SetField(wxcarduser.FieldSessionKey, field.TypeString, value)
	}
	if value, ok := wcuu.mutation.IsVip(); ok {
		_spec.SetField(wxcarduser.FieldIsVip, field.TypeInt, value)
	}
	if value, ok := wcuu.mutation.AddedIsVip(); ok {
		_spec.AddField(wxcarduser.FieldIsVip, field.TypeInt, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, wcuu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wxcarduser.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	wcuu.mutation.done = true
	return n, nil
}

// WxCardUserUpdateOne is the builder for updating a single WxCardUser entity.
type WxCardUserUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WxCardUserMutation
}

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

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

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

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

// SetWxid sets the "wxid" field.
func (wcuuo *WxCardUserUpdateOne) SetWxid(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetWxid(s)
	return wcuuo
}

// SetNillableWxid sets the "wxid" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableWxid(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetWxid(*s)
	}
	return wcuuo
}

// SetAccount sets the "account" field.
func (wcuuo *WxCardUserUpdateOne) SetAccount(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetAccount(s)
	return wcuuo
}

// SetNillableAccount sets the "account" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableAccount(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetAccount(*s)
	}
	return wcuuo
}

// SetAvatar sets the "avatar" field.
func (wcuuo *WxCardUserUpdateOne) SetAvatar(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetAvatar(s)
	return wcuuo
}

// SetNillableAvatar sets the "avatar" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableAvatar(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetAvatar(*s)
	}
	return wcuuo
}

// SetNickname sets the "nickname" field.
func (wcuuo *WxCardUserUpdateOne) SetNickname(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetNickname(s)
	return wcuuo
}

// SetNillableNickname sets the "nickname" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableNickname(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetNickname(*s)
	}
	return wcuuo
}

// SetRemark sets the "remark" field.
func (wcuuo *WxCardUserUpdateOne) SetRemark(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetRemark(s)
	return wcuuo
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableRemark(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetRemark(*s)
	}
	return wcuuo
}

// SetPhone sets the "phone" field.
func (wcuuo *WxCardUserUpdateOne) SetPhone(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetPhone(s)
	return wcuuo
}

// SetNillablePhone sets the "phone" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillablePhone(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetPhone(*s)
	}
	return wcuuo
}

// SetOpenID sets the "open_id" field.
func (wcuuo *WxCardUserUpdateOne) SetOpenID(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetOpenID(s)
	return wcuuo
}

// SetNillableOpenID sets the "open_id" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableOpenID(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetOpenID(*s)
	}
	return wcuuo
}

// SetUnionID sets the "union_id" field.
func (wcuuo *WxCardUserUpdateOne) SetUnionID(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetUnionID(s)
	return wcuuo
}

// SetNillableUnionID sets the "union_id" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableUnionID(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetUnionID(*s)
	}
	return wcuuo
}

// SetSessionKey sets the "session_key" field.
func (wcuuo *WxCardUserUpdateOne) SetSessionKey(s string) *WxCardUserUpdateOne {
	wcuuo.mutation.SetSessionKey(s)
	return wcuuo
}

// SetNillableSessionKey sets the "session_key" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableSessionKey(s *string) *WxCardUserUpdateOne {
	if s != nil {
		wcuuo.SetSessionKey(*s)
	}
	return wcuuo
}

// SetIsVip sets the "is_vip" field.
func (wcuuo *WxCardUserUpdateOne) SetIsVip(i int) *WxCardUserUpdateOne {
	wcuuo.mutation.ResetIsVip()
	wcuuo.mutation.SetIsVip(i)
	return wcuuo
}

// SetNillableIsVip sets the "is_vip" field if the given value is not nil.
func (wcuuo *WxCardUserUpdateOne) SetNillableIsVip(i *int) *WxCardUserUpdateOne {
	if i != nil {
		wcuuo.SetIsVip(*i)
	}
	return wcuuo
}

// AddIsVip adds i to the "is_vip" field.
func (wcuuo *WxCardUserUpdateOne) AddIsVip(i int) *WxCardUserUpdateOne {
	wcuuo.mutation.AddIsVip(i)
	return wcuuo
}

// Mutation returns the WxCardUserMutation object of the builder.
func (wcuuo *WxCardUserUpdateOne) Mutation() *WxCardUserMutation {
	return wcuuo.mutation
}

// Where appends a list predicates to the WxCardUserUpdate builder.
func (wcuuo *WxCardUserUpdateOne) Where(ps ...predicate.WxCardUser) *WxCardUserUpdateOne {
	wcuuo.mutation.Where(ps...)
	return wcuuo
}

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

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

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

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

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

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

func (wcuuo *WxCardUserUpdateOne) sqlSave(ctx context.Context) (_node *WxCardUser, err error) {
	_spec := sqlgraph.NewUpdateSpec(wxcarduser.Table, wxcarduser.Columns, sqlgraph.NewFieldSpec(wxcarduser.FieldID, field.TypeUint64))
	id, ok := wcuuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "WxCardUser.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := wcuuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, wxcarduser.FieldID)
		for _, f := range fields {
			if !wxcarduser.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != wxcarduser.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := wcuuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcuuo.mutation.UpdatedAt(); ok {
		_spec.SetField(wxcarduser.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcuuo.mutation.DeletedAt(); ok {
		_spec.SetField(wxcarduser.FieldDeletedAt, field.TypeTime, value)
	}
	if wcuuo.mutation.DeletedAtCleared() {
		_spec.ClearField(wxcarduser.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := wcuuo.mutation.Wxid(); ok {
		_spec.SetField(wxcarduser.FieldWxid, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.Account(); ok {
		_spec.SetField(wxcarduser.FieldAccount, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.Avatar(); ok {
		_spec.SetField(wxcarduser.FieldAvatar, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.Nickname(); ok {
		_spec.SetField(wxcarduser.FieldNickname, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.Remark(); ok {
		_spec.SetField(wxcarduser.FieldRemark, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.Phone(); ok {
		_spec.SetField(wxcarduser.FieldPhone, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.OpenID(); ok {
		_spec.SetField(wxcarduser.FieldOpenID, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.UnionID(); ok {
		_spec.SetField(wxcarduser.FieldUnionID, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.SessionKey(); ok {
		_spec.SetField(wxcarduser.FieldSessionKey, field.TypeString, value)
	}
	if value, ok := wcuuo.mutation.IsVip(); ok {
		_spec.SetField(wxcarduser.FieldIsVip, field.TypeInt, value)
	}
	if value, ok := wcuuo.mutation.AddedIsVip(); ok {
		_spec.AddField(wxcarduser.FieldIsVip, field.TypeInt, value)
	}
	_node = &WxCardUser{config: wcuuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, wcuuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wxcarduser.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	wcuuo.mutation.done = true
	return _node, nil
}
