// Code generated by ent, DO NOT EDIT.

package ent

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

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

// WxCardUserCreate is the builder for creating a WxCardUser entity.
type WxCardUserCreate struct {
	config
	mutation *WxCardUserMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (wcuc *WxCardUserCreate) SetCreatedAt(t time.Time) *WxCardUserCreate {
	wcuc.mutation.SetCreatedAt(t)
	return wcuc
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (wcuc *WxCardUserCreate) SetNillableCreatedAt(t *time.Time) *WxCardUserCreate {
	if t != nil {
		wcuc.SetCreatedAt(*t)
	}
	return wcuc
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (wcuc *WxCardUserCreate) SetNillableUpdatedAt(t *time.Time) *WxCardUserCreate {
	if t != nil {
		wcuc.SetUpdatedAt(*t)
	}
	return wcuc
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// SetIsVip sets the "is_vip" field.
func (wcuc *WxCardUserCreate) SetIsVip(i int) *WxCardUserCreate {
	wcuc.mutation.SetIsVip(i)
	return wcuc
}

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

// SetID sets the "id" field.
func (wcuc *WxCardUserCreate) SetID(u uint64) *WxCardUserCreate {
	wcuc.mutation.SetID(u)
	return wcuc
}

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

// Save creates the WxCardUser in the database.
func (wcuc *WxCardUserCreate) Save(ctx context.Context) (*WxCardUser, error) {
	if err := wcuc.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, wcuc.sqlSave, wcuc.mutation, wcuc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (wcuc *WxCardUserCreate) SaveX(ctx context.Context) *WxCardUser {
	v, err := wcuc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// defaults sets the default values of the builder before save.
func (wcuc *WxCardUserCreate) defaults() error {
	if _, ok := wcuc.mutation.CreatedAt(); !ok {
		if wxcarduser.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized wxcarduser.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := wxcarduser.DefaultCreatedAt()
		wcuc.mutation.SetCreatedAt(v)
	}
	if _, ok := wcuc.mutation.UpdatedAt(); !ok {
		if wxcarduser.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized wxcarduser.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := wxcarduser.DefaultUpdatedAt()
		wcuc.mutation.SetUpdatedAt(v)
	}
	if _, ok := wcuc.mutation.Wxid(); !ok {
		v := wxcarduser.DefaultWxid
		wcuc.mutation.SetWxid(v)
	}
	if _, ok := wcuc.mutation.Account(); !ok {
		v := wxcarduser.DefaultAccount
		wcuc.mutation.SetAccount(v)
	}
	if _, ok := wcuc.mutation.Avatar(); !ok {
		v := wxcarduser.DefaultAvatar
		wcuc.mutation.SetAvatar(v)
	}
	if _, ok := wcuc.mutation.Nickname(); !ok {
		v := wxcarduser.DefaultNickname
		wcuc.mutation.SetNickname(v)
	}
	if _, ok := wcuc.mutation.Remark(); !ok {
		v := wxcarduser.DefaultRemark
		wcuc.mutation.SetRemark(v)
	}
	if _, ok := wcuc.mutation.Phone(); !ok {
		v := wxcarduser.DefaultPhone
		wcuc.mutation.SetPhone(v)
	}
	if _, ok := wcuc.mutation.OpenID(); !ok {
		v := wxcarduser.DefaultOpenID
		wcuc.mutation.SetOpenID(v)
	}
	if _, ok := wcuc.mutation.UnionID(); !ok {
		v := wxcarduser.DefaultUnionID
		wcuc.mutation.SetUnionID(v)
	}
	if _, ok := wcuc.mutation.SessionKey(); !ok {
		v := wxcarduser.DefaultSessionKey
		wcuc.mutation.SetSessionKey(v)
	}
	if _, ok := wcuc.mutation.IsVip(); !ok {
		v := wxcarduser.DefaultIsVip
		wcuc.mutation.SetIsVip(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (wcuc *WxCardUserCreate) check() error {
	if _, ok := wcuc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "WxCardUser.created_at"`)}
	}
	if _, ok := wcuc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "WxCardUser.updated_at"`)}
	}
	if _, ok := wcuc.mutation.Wxid(); !ok {
		return &ValidationError{Name: "wxid", err: errors.New(`ent: missing required field "WxCardUser.wxid"`)}
	}
	if _, ok := wcuc.mutation.Account(); !ok {
		return &ValidationError{Name: "account", err: errors.New(`ent: missing required field "WxCardUser.account"`)}
	}
	if _, ok := wcuc.mutation.Avatar(); !ok {
		return &ValidationError{Name: "avatar", err: errors.New(`ent: missing required field "WxCardUser.avatar"`)}
	}
	if _, ok := wcuc.mutation.Nickname(); !ok {
		return &ValidationError{Name: "nickname", err: errors.New(`ent: missing required field "WxCardUser.nickname"`)}
	}
	if _, ok := wcuc.mutation.Remark(); !ok {
		return &ValidationError{Name: "remark", err: errors.New(`ent: missing required field "WxCardUser.remark"`)}
	}
	if _, ok := wcuc.mutation.Phone(); !ok {
		return &ValidationError{Name: "phone", err: errors.New(`ent: missing required field "WxCardUser.phone"`)}
	}
	if _, ok := wcuc.mutation.OpenID(); !ok {
		return &ValidationError{Name: "open_id", err: errors.New(`ent: missing required field "WxCardUser.open_id"`)}
	}
	if _, ok := wcuc.mutation.UnionID(); !ok {
		return &ValidationError{Name: "union_id", err: errors.New(`ent: missing required field "WxCardUser.union_id"`)}
	}
	if _, ok := wcuc.mutation.SessionKey(); !ok {
		return &ValidationError{Name: "session_key", err: errors.New(`ent: missing required field "WxCardUser.session_key"`)}
	}
	if _, ok := wcuc.mutation.IsVip(); !ok {
		return &ValidationError{Name: "is_vip", err: errors.New(`ent: missing required field "WxCardUser.is_vip"`)}
	}
	return nil
}

func (wcuc *WxCardUserCreate) sqlSave(ctx context.Context) (*WxCardUser, error) {
	if err := wcuc.check(); err != nil {
		return nil, err
	}
	_node, _spec := wcuc.createSpec()
	if err := sqlgraph.CreateNode(ctx, wcuc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint64(id)
	}
	wcuc.mutation.id = &_node.ID
	wcuc.mutation.done = true
	return _node, nil
}

func (wcuc *WxCardUserCreate) createSpec() (*WxCardUser, *sqlgraph.CreateSpec) {
	var (
		_node = &WxCardUser{config: wcuc.config}
		_spec = sqlgraph.NewCreateSpec(wxcarduser.Table, sqlgraph.NewFieldSpec(wxcarduser.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = wcuc.conflict
	if id, ok := wcuc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := wcuc.mutation.CreatedAt(); ok {
		_spec.SetField(wxcarduser.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := wcuc.mutation.UpdatedAt(); ok {
		_spec.SetField(wxcarduser.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := wcuc.mutation.DeletedAt(); ok {
		_spec.SetField(wxcarduser.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := wcuc.mutation.Wxid(); ok {
		_spec.SetField(wxcarduser.FieldWxid, field.TypeString, value)
		_node.Wxid = value
	}
	if value, ok := wcuc.mutation.Account(); ok {
		_spec.SetField(wxcarduser.FieldAccount, field.TypeString, value)
		_node.Account = value
	}
	if value, ok := wcuc.mutation.Avatar(); ok {
		_spec.SetField(wxcarduser.FieldAvatar, field.TypeString, value)
		_node.Avatar = value
	}
	if value, ok := wcuc.mutation.Nickname(); ok {
		_spec.SetField(wxcarduser.FieldNickname, field.TypeString, value)
		_node.Nickname = value
	}
	if value, ok := wcuc.mutation.Remark(); ok {
		_spec.SetField(wxcarduser.FieldRemark, field.TypeString, value)
		_node.Remark = value
	}
	if value, ok := wcuc.mutation.Phone(); ok {
		_spec.SetField(wxcarduser.FieldPhone, field.TypeString, value)
		_node.Phone = value
	}
	if value, ok := wcuc.mutation.OpenID(); ok {
		_spec.SetField(wxcarduser.FieldOpenID, field.TypeString, value)
		_node.OpenID = value
	}
	if value, ok := wcuc.mutation.UnionID(); ok {
		_spec.SetField(wxcarduser.FieldUnionID, field.TypeString, value)
		_node.UnionID = value
	}
	if value, ok := wcuc.mutation.SessionKey(); ok {
		_spec.SetField(wxcarduser.FieldSessionKey, field.TypeString, value)
		_node.SessionKey = value
	}
	if value, ok := wcuc.mutation.IsVip(); ok {
		_spec.SetField(wxcarduser.FieldIsVip, field.TypeInt, value)
		_node.IsVip = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.WxCardUser.Create().
//		SetCreatedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.WxCardUserUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (wcuc *WxCardUserCreate) OnConflict(opts ...sql.ConflictOption) *WxCardUserUpsertOne {
	wcuc.conflict = opts
	return &WxCardUserUpsertOne{
		create: wcuc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.WxCardUser.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (wcuc *WxCardUserCreate) OnConflictColumns(columns ...string) *WxCardUserUpsertOne {
	wcuc.conflict = append(wcuc.conflict, sql.ConflictColumns(columns...))
	return &WxCardUserUpsertOne{
		create: wcuc,
	}
}

type (
	// WxCardUserUpsertOne is the builder for "upsert"-ing
	//  one WxCardUser node.
	WxCardUserUpsertOne struct {
		create *WxCardUserCreate
	}

	// WxCardUserUpsert is the "OnConflict" setter.
	WxCardUserUpsert struct {
		*sql.UpdateSet
	}
)

// SetUpdatedAt sets the "updated_at" field.
func (u *WxCardUserUpsert) SetUpdatedAt(v time.Time) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateUpdatedAt() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldUpdatedAt)
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *WxCardUserUpsert) SetDeletedAt(v time.Time) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateDeletedAt() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *WxCardUserUpsert) ClearDeletedAt() *WxCardUserUpsert {
	u.SetNull(wxcarduser.FieldDeletedAt)
	return u
}

// SetWxid sets the "wxid" field.
func (u *WxCardUserUpsert) SetWxid(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldWxid, v)
	return u
}

// UpdateWxid sets the "wxid" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateWxid() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldWxid)
	return u
}

// SetAccount sets the "account" field.
func (u *WxCardUserUpsert) SetAccount(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldAccount, v)
	return u
}

// UpdateAccount sets the "account" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateAccount() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldAccount)
	return u
}

// SetAvatar sets the "avatar" field.
func (u *WxCardUserUpsert) SetAvatar(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldAvatar, v)
	return u
}

// UpdateAvatar sets the "avatar" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateAvatar() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldAvatar)
	return u
}

// SetNickname sets the "nickname" field.
func (u *WxCardUserUpsert) SetNickname(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldNickname, v)
	return u
}

// UpdateNickname sets the "nickname" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateNickname() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldNickname)
	return u
}

// SetRemark sets the "remark" field.
func (u *WxCardUserUpsert) SetRemark(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldRemark, v)
	return u
}

// UpdateRemark sets the "remark" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateRemark() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldRemark)
	return u
}

// SetPhone sets the "phone" field.
func (u *WxCardUserUpsert) SetPhone(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldPhone, v)
	return u
}

// UpdatePhone sets the "phone" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdatePhone() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldPhone)
	return u
}

// SetOpenID sets the "open_id" field.
func (u *WxCardUserUpsert) SetOpenID(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldOpenID, v)
	return u
}

// UpdateOpenID sets the "open_id" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateOpenID() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldOpenID)
	return u
}

// SetUnionID sets the "union_id" field.
func (u *WxCardUserUpsert) SetUnionID(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldUnionID, v)
	return u
}

// UpdateUnionID sets the "union_id" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateUnionID() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldUnionID)
	return u
}

// SetSessionKey sets the "session_key" field.
func (u *WxCardUserUpsert) SetSessionKey(v string) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldSessionKey, v)
	return u
}

// UpdateSessionKey sets the "session_key" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateSessionKey() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldSessionKey)
	return u
}

// SetIsVip sets the "is_vip" field.
func (u *WxCardUserUpsert) SetIsVip(v int) *WxCardUserUpsert {
	u.Set(wxcarduser.FieldIsVip, v)
	return u
}

// UpdateIsVip sets the "is_vip" field to the value that was provided on create.
func (u *WxCardUserUpsert) UpdateIsVip() *WxCardUserUpsert {
	u.SetExcluded(wxcarduser.FieldIsVip)
	return u
}

// AddIsVip adds v to the "is_vip" field.
func (u *WxCardUserUpsert) AddIsVip(v int) *WxCardUserUpsert {
	u.Add(wxcarduser.FieldIsVip, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.WxCardUser.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(wxcarduser.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *WxCardUserUpsertOne) UpdateNewValues() *WxCardUserUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(wxcarduser.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(wxcarduser.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.WxCardUser.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *WxCardUserUpsertOne) Ignore() *WxCardUserUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *WxCardUserUpsertOne) DoNothing() *WxCardUserUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the WxCardUserCreate.OnConflict
// documentation for more info.
func (u *WxCardUserUpsertOne) Update(set func(*WxCardUserUpsert)) *WxCardUserUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&WxCardUserUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *WxCardUserUpsertOne) SetUpdatedAt(v time.Time) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateUpdatedAt() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *WxCardUserUpsertOne) SetDeletedAt(v time.Time) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateDeletedAt() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *WxCardUserUpsertOne) ClearDeletedAt() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.ClearDeletedAt()
	})
}

// SetWxid sets the "wxid" field.
func (u *WxCardUserUpsertOne) SetWxid(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetWxid(v)
	})
}

// UpdateWxid sets the "wxid" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateWxid() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateWxid()
	})
}

// SetAccount sets the "account" field.
func (u *WxCardUserUpsertOne) SetAccount(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetAccount(v)
	})
}

// UpdateAccount sets the "account" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateAccount() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateAccount()
	})
}

// SetAvatar sets the "avatar" field.
func (u *WxCardUserUpsertOne) SetAvatar(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetAvatar(v)
	})
}

// UpdateAvatar sets the "avatar" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateAvatar() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateAvatar()
	})
}

// SetNickname sets the "nickname" field.
func (u *WxCardUserUpsertOne) SetNickname(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetNickname(v)
	})
}

// UpdateNickname sets the "nickname" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateNickname() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateNickname()
	})
}

// SetRemark sets the "remark" field.
func (u *WxCardUserUpsertOne) SetRemark(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetRemark(v)
	})
}

// UpdateRemark sets the "remark" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateRemark() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateRemark()
	})
}

// SetPhone sets the "phone" field.
func (u *WxCardUserUpsertOne) SetPhone(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetPhone(v)
	})
}

// UpdatePhone sets the "phone" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdatePhone() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdatePhone()
	})
}

// SetOpenID sets the "open_id" field.
func (u *WxCardUserUpsertOne) SetOpenID(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetOpenID(v)
	})
}

// UpdateOpenID sets the "open_id" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateOpenID() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateOpenID()
	})
}

// SetUnionID sets the "union_id" field.
func (u *WxCardUserUpsertOne) SetUnionID(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetUnionID(v)
	})
}

// UpdateUnionID sets the "union_id" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateUnionID() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateUnionID()
	})
}

// SetSessionKey sets the "session_key" field.
func (u *WxCardUserUpsertOne) SetSessionKey(v string) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetSessionKey(v)
	})
}

// UpdateSessionKey sets the "session_key" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateSessionKey() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateSessionKey()
	})
}

// SetIsVip sets the "is_vip" field.
func (u *WxCardUserUpsertOne) SetIsVip(v int) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetIsVip(v)
	})
}

// AddIsVip adds v to the "is_vip" field.
func (u *WxCardUserUpsertOne) AddIsVip(v int) *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.AddIsVip(v)
	})
}

// UpdateIsVip sets the "is_vip" field to the value that was provided on create.
func (u *WxCardUserUpsertOne) UpdateIsVip() *WxCardUserUpsertOne {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateIsVip()
	})
}

// Exec executes the query.
func (u *WxCardUserUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for WxCardUserCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *WxCardUserUpsertOne) ID(ctx context.Context) (id uint64, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *WxCardUserUpsertOne) IDX(ctx context.Context) uint64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// WxCardUserCreateBulk is the builder for creating many WxCardUser entities in bulk.
type WxCardUserCreateBulk struct {
	config
	err      error
	builders []*WxCardUserCreate
	conflict []sql.ConflictOption
}

// Save creates the WxCardUser entities in the database.
func (wcucb *WxCardUserCreateBulk) Save(ctx context.Context) ([]*WxCardUser, error) {
	if wcucb.err != nil {
		return nil, wcucb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(wcucb.builders))
	nodes := make([]*WxCardUser, len(wcucb.builders))
	mutators := make([]Mutator, len(wcucb.builders))
	for i := range wcucb.builders {
		func(i int, root context.Context) {
			builder := wcucb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*WxCardUserMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, wcucb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = wcucb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, wcucb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, wcucb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (wcucb *WxCardUserCreateBulk) SaveX(ctx context.Context) []*WxCardUser {
	v, err := wcucb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.WxCardUser.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.WxCardUserUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (wcucb *WxCardUserCreateBulk) OnConflict(opts ...sql.ConflictOption) *WxCardUserUpsertBulk {
	wcucb.conflict = opts
	return &WxCardUserUpsertBulk{
		create: wcucb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.WxCardUser.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (wcucb *WxCardUserCreateBulk) OnConflictColumns(columns ...string) *WxCardUserUpsertBulk {
	wcucb.conflict = append(wcucb.conflict, sql.ConflictColumns(columns...))
	return &WxCardUserUpsertBulk{
		create: wcucb,
	}
}

// WxCardUserUpsertBulk is the builder for "upsert"-ing
// a bulk of WxCardUser nodes.
type WxCardUserUpsertBulk struct {
	create *WxCardUserCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.WxCardUser.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(wxcarduser.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *WxCardUserUpsertBulk) UpdateNewValues() *WxCardUserUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(wxcarduser.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(wxcarduser.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.WxCardUser.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *WxCardUserUpsertBulk) Ignore() *WxCardUserUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *WxCardUserUpsertBulk) DoNothing() *WxCardUserUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the WxCardUserCreateBulk.OnConflict
// documentation for more info.
func (u *WxCardUserUpsertBulk) Update(set func(*WxCardUserUpsert)) *WxCardUserUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&WxCardUserUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *WxCardUserUpsertBulk) SetUpdatedAt(v time.Time) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateUpdatedAt() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *WxCardUserUpsertBulk) SetDeletedAt(v time.Time) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateDeletedAt() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *WxCardUserUpsertBulk) ClearDeletedAt() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.ClearDeletedAt()
	})
}

// SetWxid sets the "wxid" field.
func (u *WxCardUserUpsertBulk) SetWxid(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetWxid(v)
	})
}

// UpdateWxid sets the "wxid" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateWxid() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateWxid()
	})
}

// SetAccount sets the "account" field.
func (u *WxCardUserUpsertBulk) SetAccount(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetAccount(v)
	})
}

// UpdateAccount sets the "account" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateAccount() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateAccount()
	})
}

// SetAvatar sets the "avatar" field.
func (u *WxCardUserUpsertBulk) SetAvatar(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetAvatar(v)
	})
}

// UpdateAvatar sets the "avatar" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateAvatar() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateAvatar()
	})
}

// SetNickname sets the "nickname" field.
func (u *WxCardUserUpsertBulk) SetNickname(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetNickname(v)
	})
}

// UpdateNickname sets the "nickname" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateNickname() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateNickname()
	})
}

// SetRemark sets the "remark" field.
func (u *WxCardUserUpsertBulk) SetRemark(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetRemark(v)
	})
}

// UpdateRemark sets the "remark" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateRemark() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateRemark()
	})
}

// SetPhone sets the "phone" field.
func (u *WxCardUserUpsertBulk) SetPhone(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetPhone(v)
	})
}

// UpdatePhone sets the "phone" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdatePhone() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdatePhone()
	})
}

// SetOpenID sets the "open_id" field.
func (u *WxCardUserUpsertBulk) SetOpenID(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetOpenID(v)
	})
}

// UpdateOpenID sets the "open_id" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateOpenID() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateOpenID()
	})
}

// SetUnionID sets the "union_id" field.
func (u *WxCardUserUpsertBulk) SetUnionID(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetUnionID(v)
	})
}

// UpdateUnionID sets the "union_id" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateUnionID() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateUnionID()
	})
}

// SetSessionKey sets the "session_key" field.
func (u *WxCardUserUpsertBulk) SetSessionKey(v string) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetSessionKey(v)
	})
}

// UpdateSessionKey sets the "session_key" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateSessionKey() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateSessionKey()
	})
}

// SetIsVip sets the "is_vip" field.
func (u *WxCardUserUpsertBulk) SetIsVip(v int) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.SetIsVip(v)
	})
}

// AddIsVip adds v to the "is_vip" field.
func (u *WxCardUserUpsertBulk) AddIsVip(v int) *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.AddIsVip(v)
	})
}

// UpdateIsVip sets the "is_vip" field to the value that was provided on create.
func (u *WxCardUserUpsertBulk) UpdateIsVip() *WxCardUserUpsertBulk {
	return u.Update(func(s *WxCardUserUpsert) {
		s.UpdateIsVip()
	})
}

// Exec executes the query.
func (u *WxCardUserUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the WxCardUserCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for WxCardUserCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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