// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/loginrecord"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/user"
)

// LoginRecordCreate is the builder for creating a LoginRecord entity.
type LoginRecordCreate struct {
	config
	mutation *LoginRecordMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetVersion sets the "version" field.
func (lrc *LoginRecordCreate) SetVersion(i int64) *LoginRecordCreate {
	lrc.mutation.SetVersion(i)
	return lrc
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (lrc *LoginRecordCreate) SetNillableVersion(i *int64) *LoginRecordCreate {
	if i != nil {
		lrc.SetVersion(*i)
	}
	return lrc
}

// SetUserID sets the "user_id" field.
func (lrc *LoginRecordCreate) SetUserID(i int64) *LoginRecordCreate {
	lrc.mutation.SetUserID(i)
	return lrc
}

// SetLastLoginAt sets the "last_login_at" field.
func (lrc *LoginRecordCreate) SetLastLoginAt(t time.Time) *LoginRecordCreate {
	lrc.mutation.SetLastLoginAt(t)
	return lrc
}

// SetNillableLastLoginAt sets the "last_login_at" field if the given value is not nil.
func (lrc *LoginRecordCreate) SetNillableLastLoginAt(t *time.Time) *LoginRecordCreate {
	if t != nil {
		lrc.SetLastLoginAt(*t)
	}
	return lrc
}

// SetLastLoginIpv4 sets the "last_login_ipv4" field.
func (lrc *LoginRecordCreate) SetLastLoginIpv4(s string) *LoginRecordCreate {
	lrc.mutation.SetLastLoginIpv4(s)
	return lrc
}

// SetNillableLastLoginIpv4 sets the "last_login_ipv4" field if the given value is not nil.
func (lrc *LoginRecordCreate) SetNillableLastLoginIpv4(s *string) *LoginRecordCreate {
	if s != nil {
		lrc.SetLastLoginIpv4(*s)
	}
	return lrc
}

// SetLastLoginDevice sets the "last_login_device" field.
func (lrc *LoginRecordCreate) SetLastLoginDevice(s string) *LoginRecordCreate {
	lrc.mutation.SetLastLoginDevice(s)
	return lrc
}

// SetNillableLastLoginDevice sets the "last_login_device" field if the given value is not nil.
func (lrc *LoginRecordCreate) SetNillableLastLoginDevice(s *string) *LoginRecordCreate {
	if s != nil {
		lrc.SetLastLoginDevice(*s)
	}
	return lrc
}

// SetLastLoginUA sets the "last_login_UA" field.
func (lrc *LoginRecordCreate) SetLastLoginUA(s string) *LoginRecordCreate {
	lrc.mutation.SetLastLoginUA(s)
	return lrc
}

// SetNillableLastLoginUA sets the "last_login_UA" field if the given value is not nil.
func (lrc *LoginRecordCreate) SetNillableLastLoginUA(s *string) *LoginRecordCreate {
	if s != nil {
		lrc.SetLastLoginUA(*s)
	}
	return lrc
}

// SetLoginCount sets the "login_count" field.
func (lrc *LoginRecordCreate) SetLoginCount(u uint64) *LoginRecordCreate {
	lrc.mutation.SetLoginCount(u)
	return lrc
}

// SetNillableLoginCount sets the "login_count" field if the given value is not nil.
func (lrc *LoginRecordCreate) SetNillableLoginCount(u *uint64) *LoginRecordCreate {
	if u != nil {
		lrc.SetLoginCount(*u)
	}
	return lrc
}

// SetID sets the "id" field.
func (lrc *LoginRecordCreate) SetID(i int64) *LoginRecordCreate {
	lrc.mutation.SetID(i)
	return lrc
}

// SetNillableID sets the "id" field if the given value is not nil.
func (lrc *LoginRecordCreate) SetNillableID(i *int64) *LoginRecordCreate {
	if i != nil {
		lrc.SetID(*i)
	}
	return lrc
}

// SetUser sets the "user" edge to the User entity.
func (lrc *LoginRecordCreate) SetUser(u *User) *LoginRecordCreate {
	return lrc.SetUserID(u.ID)
}

// Mutation returns the LoginRecordMutation object of the builder.
func (lrc *LoginRecordCreate) Mutation() *LoginRecordMutation {
	return lrc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (lrc *LoginRecordCreate) defaults() error {
	if _, ok := lrc.mutation.CreatedAt(); !ok {
		if loginrecord.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized loginrecord.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := loginrecord.DefaultCreatedAt()
		lrc.mutation.SetCreatedAt(v)
	}
	if _, ok := lrc.mutation.UpdatedAt(); !ok {
		if loginrecord.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized loginrecord.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := loginrecord.DefaultUpdatedAt()
		lrc.mutation.SetUpdatedAt(v)
	}
	if _, ok := lrc.mutation.Version(); !ok {
		v := loginrecord.DefaultVersion
		lrc.mutation.SetVersion(v)
	}
	if _, ok := lrc.mutation.LoginCount(); !ok {
		v := loginrecord.DefaultLoginCount
		lrc.mutation.SetLoginCount(v)
	}
	if _, ok := lrc.mutation.ID(); !ok {
		if loginrecord.DefaultID == nil {
			return fmt.Errorf("ent: uninitialized loginrecord.DefaultID (forgotten import ent/runtime?)")
		}
		v := loginrecord.DefaultID()
		lrc.mutation.SetID(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (lrc *LoginRecordCreate) check() error {
	if _, ok := lrc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "LoginRecord.created_at"`)}
	}
	if _, ok := lrc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "LoginRecord.updated_at"`)}
	}
	if _, ok := lrc.mutation.Version(); !ok {
		return &ValidationError{Name: "version", err: errors.New(`ent: missing required field "LoginRecord.version"`)}
	}
	if _, ok := lrc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "LoginRecord.user_id"`)}
	}
	if _, ok := lrc.mutation.LoginCount(); !ok {
		return &ValidationError{Name: "login_count", err: errors.New(`ent: missing required field "LoginRecord.login_count"`)}
	}
	if _, ok := lrc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "LoginRecord.user"`)}
	}
	return nil
}

func (lrc *LoginRecordCreate) sqlSave(ctx context.Context) (*LoginRecord, error) {
	if err := lrc.check(); err != nil {
		return nil, err
	}
	_node, _spec := lrc.createSpec()
	if err := sqlgraph.CreateNode(ctx, lrc.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 = int64(id)
	}
	lrc.mutation.id = &_node.ID
	lrc.mutation.done = true
	return _node, nil
}

func (lrc *LoginRecordCreate) createSpec() (*LoginRecord, *sqlgraph.CreateSpec) {
	var (
		_node = &LoginRecord{config: lrc.config}
		_spec = sqlgraph.NewCreateSpec(loginrecord.Table, sqlgraph.NewFieldSpec(loginrecord.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = lrc.conflict
	if id, ok := lrc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := lrc.mutation.CreatedAt(); ok {
		_spec.SetField(loginrecord.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := lrc.mutation.UpdatedAt(); ok {
		_spec.SetField(loginrecord.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := lrc.mutation.Version(); ok {
		_spec.SetField(loginrecord.FieldVersion, field.TypeInt64, value)
		_node.Version = value
	}
	if value, ok := lrc.mutation.LastLoginAt(); ok {
		_spec.SetField(loginrecord.FieldLastLoginAt, field.TypeTime, value)
		_node.LastLoginAt = value
	}
	if value, ok := lrc.mutation.LastLoginIpv4(); ok {
		_spec.SetField(loginrecord.FieldLastLoginIpv4, field.TypeString, value)
		_node.LastLoginIpv4 = value
	}
	if value, ok := lrc.mutation.LastLoginDevice(); ok {
		_spec.SetField(loginrecord.FieldLastLoginDevice, field.TypeString, value)
		_node.LastLoginDevice = value
	}
	if value, ok := lrc.mutation.LastLoginUA(); ok {
		_spec.SetField(loginrecord.FieldLastLoginUA, field.TypeString, value)
		_node.LastLoginUA = value
	}
	if value, ok := lrc.mutation.LoginCount(); ok {
		_spec.SetField(loginrecord.FieldLoginCount, field.TypeUint64, value)
		_node.LoginCount = value
	}
	if nodes := lrc.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   loginrecord.UserTable,
			Columns: []string{loginrecord.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UserID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.LoginRecord.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.LoginRecordUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (lrc *LoginRecordCreate) OnConflict(opts ...sql.ConflictOption) *LoginRecordUpsertOne {
	lrc.conflict = opts
	return &LoginRecordUpsertOne{
		create: lrc,
	}
}

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

type (
	// LoginRecordUpsertOne is the builder for "upsert"-ing
	//  one LoginRecord node.
	LoginRecordUpsertOne struct {
		create *LoginRecordCreate
	}

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

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

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

// SetVersion sets the "version" field.
func (u *LoginRecordUpsert) SetVersion(v int64) *LoginRecordUpsert {
	u.Set(loginrecord.FieldVersion, v)
	return u
}

// UpdateVersion sets the "version" field to the value that was provided on create.
func (u *LoginRecordUpsert) UpdateVersion() *LoginRecordUpsert {
	u.SetExcluded(loginrecord.FieldVersion)
	return u
}

// AddVersion adds v to the "version" field.
func (u *LoginRecordUpsert) AddVersion(v int64) *LoginRecordUpsert {
	u.Add(loginrecord.FieldVersion, v)
	return u
}

// SetUserID sets the "user_id" field.
func (u *LoginRecordUpsert) SetUserID(v int64) *LoginRecordUpsert {
	u.Set(loginrecord.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *LoginRecordUpsert) UpdateUserID() *LoginRecordUpsert {
	u.SetExcluded(loginrecord.FieldUserID)
	return u
}

// SetLastLoginAt sets the "last_login_at" field.
func (u *LoginRecordUpsert) SetLastLoginAt(v time.Time) *LoginRecordUpsert {
	u.Set(loginrecord.FieldLastLoginAt, v)
	return u
}

// UpdateLastLoginAt sets the "last_login_at" field to the value that was provided on create.
func (u *LoginRecordUpsert) UpdateLastLoginAt() *LoginRecordUpsert {
	u.SetExcluded(loginrecord.FieldLastLoginAt)
	return u
}

// ClearLastLoginAt clears the value of the "last_login_at" field.
func (u *LoginRecordUpsert) ClearLastLoginAt() *LoginRecordUpsert {
	u.SetNull(loginrecord.FieldLastLoginAt)
	return u
}

// SetLastLoginIpv4 sets the "last_login_ipv4" field.
func (u *LoginRecordUpsert) SetLastLoginIpv4(v string) *LoginRecordUpsert {
	u.Set(loginrecord.FieldLastLoginIpv4, v)
	return u
}

// UpdateLastLoginIpv4 sets the "last_login_ipv4" field to the value that was provided on create.
func (u *LoginRecordUpsert) UpdateLastLoginIpv4() *LoginRecordUpsert {
	u.SetExcluded(loginrecord.FieldLastLoginIpv4)
	return u
}

// ClearLastLoginIpv4 clears the value of the "last_login_ipv4" field.
func (u *LoginRecordUpsert) ClearLastLoginIpv4() *LoginRecordUpsert {
	u.SetNull(loginrecord.FieldLastLoginIpv4)
	return u
}

// SetLastLoginDevice sets the "last_login_device" field.
func (u *LoginRecordUpsert) SetLastLoginDevice(v string) *LoginRecordUpsert {
	u.Set(loginrecord.FieldLastLoginDevice, v)
	return u
}

// UpdateLastLoginDevice sets the "last_login_device" field to the value that was provided on create.
func (u *LoginRecordUpsert) UpdateLastLoginDevice() *LoginRecordUpsert {
	u.SetExcluded(loginrecord.FieldLastLoginDevice)
	return u
}

// ClearLastLoginDevice clears the value of the "last_login_device" field.
func (u *LoginRecordUpsert) ClearLastLoginDevice() *LoginRecordUpsert {
	u.SetNull(loginrecord.FieldLastLoginDevice)
	return u
}

// SetLastLoginUA sets the "last_login_UA" field.
func (u *LoginRecordUpsert) SetLastLoginUA(v string) *LoginRecordUpsert {
	u.Set(loginrecord.FieldLastLoginUA, v)
	return u
}

// UpdateLastLoginUA sets the "last_login_UA" field to the value that was provided on create.
func (u *LoginRecordUpsert) UpdateLastLoginUA() *LoginRecordUpsert {
	u.SetExcluded(loginrecord.FieldLastLoginUA)
	return u
}

// ClearLastLoginUA clears the value of the "last_login_UA" field.
func (u *LoginRecordUpsert) ClearLastLoginUA() *LoginRecordUpsert {
	u.SetNull(loginrecord.FieldLastLoginUA)
	return u
}

// SetLoginCount sets the "login_count" field.
func (u *LoginRecordUpsert) SetLoginCount(v uint64) *LoginRecordUpsert {
	u.Set(loginrecord.FieldLoginCount, v)
	return u
}

// UpdateLoginCount sets the "login_count" field to the value that was provided on create.
func (u *LoginRecordUpsert) UpdateLoginCount() *LoginRecordUpsert {
	u.SetExcluded(loginrecord.FieldLoginCount)
	return u
}

// AddLoginCount adds v to the "login_count" field.
func (u *LoginRecordUpsert) AddLoginCount(v uint64) *LoginRecordUpsert {
	u.Add(loginrecord.FieldLoginCount, 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.LoginRecord.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(loginrecord.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *LoginRecordUpsertOne) UpdateNewValues() *LoginRecordUpsertOne {
	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(loginrecord.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(loginrecord.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.LoginRecord.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *LoginRecordUpsertOne) Ignore() *LoginRecordUpsertOne {
	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 *LoginRecordUpsertOne) DoNothing() *LoginRecordUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetVersion sets the "version" field.
func (u *LoginRecordUpsertOne) SetVersion(v int64) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetVersion(v)
	})
}

// AddVersion adds v to the "version" field.
func (u *LoginRecordUpsertOne) AddVersion(v int64) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.AddVersion(v)
	})
}

// UpdateVersion sets the "version" field to the value that was provided on create.
func (u *LoginRecordUpsertOne) UpdateVersion() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateVersion()
	})
}

// SetUserID sets the "user_id" field.
func (u *LoginRecordUpsertOne) SetUserID(v int64) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *LoginRecordUpsertOne) UpdateUserID() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateUserID()
	})
}

// SetLastLoginAt sets the "last_login_at" field.
func (u *LoginRecordUpsertOne) SetLastLoginAt(v time.Time) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginAt(v)
	})
}

// UpdateLastLoginAt sets the "last_login_at" field to the value that was provided on create.
func (u *LoginRecordUpsertOne) UpdateLastLoginAt() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginAt()
	})
}

// ClearLastLoginAt clears the value of the "last_login_at" field.
func (u *LoginRecordUpsertOne) ClearLastLoginAt() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginAt()
	})
}

// SetLastLoginIpv4 sets the "last_login_ipv4" field.
func (u *LoginRecordUpsertOne) SetLastLoginIpv4(v string) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginIpv4(v)
	})
}

// UpdateLastLoginIpv4 sets the "last_login_ipv4" field to the value that was provided on create.
func (u *LoginRecordUpsertOne) UpdateLastLoginIpv4() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginIpv4()
	})
}

// ClearLastLoginIpv4 clears the value of the "last_login_ipv4" field.
func (u *LoginRecordUpsertOne) ClearLastLoginIpv4() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginIpv4()
	})
}

// SetLastLoginDevice sets the "last_login_device" field.
func (u *LoginRecordUpsertOne) SetLastLoginDevice(v string) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginDevice(v)
	})
}

// UpdateLastLoginDevice sets the "last_login_device" field to the value that was provided on create.
func (u *LoginRecordUpsertOne) UpdateLastLoginDevice() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginDevice()
	})
}

// ClearLastLoginDevice clears the value of the "last_login_device" field.
func (u *LoginRecordUpsertOne) ClearLastLoginDevice() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginDevice()
	})
}

// SetLastLoginUA sets the "last_login_UA" field.
func (u *LoginRecordUpsertOne) SetLastLoginUA(v string) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginUA(v)
	})
}

// UpdateLastLoginUA sets the "last_login_UA" field to the value that was provided on create.
func (u *LoginRecordUpsertOne) UpdateLastLoginUA() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginUA()
	})
}

// ClearLastLoginUA clears the value of the "last_login_UA" field.
func (u *LoginRecordUpsertOne) ClearLastLoginUA() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginUA()
	})
}

// SetLoginCount sets the "login_count" field.
func (u *LoginRecordUpsertOne) SetLoginCount(v uint64) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLoginCount(v)
	})
}

// AddLoginCount adds v to the "login_count" field.
func (u *LoginRecordUpsertOne) AddLoginCount(v uint64) *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.AddLoginCount(v)
	})
}

// UpdateLoginCount sets the "login_count" field to the value that was provided on create.
func (u *LoginRecordUpsertOne) UpdateLoginCount() *LoginRecordUpsertOne {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLoginCount()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *LoginRecordUpsertOne) 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 *LoginRecordUpsertOne) ID(ctx context.Context) (id int64, 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 *LoginRecordUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// LoginRecordCreateBulk is the builder for creating many LoginRecord entities in bulk.
type LoginRecordCreateBulk struct {
	config
	builders []*LoginRecordCreate
	conflict []sql.ConflictOption
}

// Save creates the LoginRecord entities in the database.
func (lrcb *LoginRecordCreateBulk) Save(ctx context.Context) ([]*LoginRecord, error) {
	specs := make([]*sqlgraph.CreateSpec, len(lrcb.builders))
	nodes := make([]*LoginRecord, len(lrcb.builders))
	mutators := make([]Mutator, len(lrcb.builders))
	for i := range lrcb.builders {
		func(i int, root context.Context) {
			builder := lrcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*LoginRecordMutation)
				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, lrcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = lrcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, lrcb.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 = int64(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, lrcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.LoginRecord.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.LoginRecordUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (lrcb *LoginRecordCreateBulk) OnConflict(opts ...sql.ConflictOption) *LoginRecordUpsertBulk {
	lrcb.conflict = opts
	return &LoginRecordUpsertBulk{
		create: lrcb,
	}
}

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

// LoginRecordUpsertBulk is the builder for "upsert"-ing
// a bulk of LoginRecord nodes.
type LoginRecordUpsertBulk struct {
	create *LoginRecordCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.LoginRecord.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(loginrecord.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *LoginRecordUpsertBulk) UpdateNewValues() *LoginRecordUpsertBulk {
	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(loginrecord.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(loginrecord.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.LoginRecord.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *LoginRecordUpsertBulk) Ignore() *LoginRecordUpsertBulk {
	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 *LoginRecordUpsertBulk) DoNothing() *LoginRecordUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetVersion sets the "version" field.
func (u *LoginRecordUpsertBulk) SetVersion(v int64) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetVersion(v)
	})
}

// AddVersion adds v to the "version" field.
func (u *LoginRecordUpsertBulk) AddVersion(v int64) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.AddVersion(v)
	})
}

// UpdateVersion sets the "version" field to the value that was provided on create.
func (u *LoginRecordUpsertBulk) UpdateVersion() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateVersion()
	})
}

// SetUserID sets the "user_id" field.
func (u *LoginRecordUpsertBulk) SetUserID(v int64) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *LoginRecordUpsertBulk) UpdateUserID() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateUserID()
	})
}

// SetLastLoginAt sets the "last_login_at" field.
func (u *LoginRecordUpsertBulk) SetLastLoginAt(v time.Time) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginAt(v)
	})
}

// UpdateLastLoginAt sets the "last_login_at" field to the value that was provided on create.
func (u *LoginRecordUpsertBulk) UpdateLastLoginAt() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginAt()
	})
}

// ClearLastLoginAt clears the value of the "last_login_at" field.
func (u *LoginRecordUpsertBulk) ClearLastLoginAt() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginAt()
	})
}

// SetLastLoginIpv4 sets the "last_login_ipv4" field.
func (u *LoginRecordUpsertBulk) SetLastLoginIpv4(v string) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginIpv4(v)
	})
}

// UpdateLastLoginIpv4 sets the "last_login_ipv4" field to the value that was provided on create.
func (u *LoginRecordUpsertBulk) UpdateLastLoginIpv4() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginIpv4()
	})
}

// ClearLastLoginIpv4 clears the value of the "last_login_ipv4" field.
func (u *LoginRecordUpsertBulk) ClearLastLoginIpv4() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginIpv4()
	})
}

// SetLastLoginDevice sets the "last_login_device" field.
func (u *LoginRecordUpsertBulk) SetLastLoginDevice(v string) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginDevice(v)
	})
}

// UpdateLastLoginDevice sets the "last_login_device" field to the value that was provided on create.
func (u *LoginRecordUpsertBulk) UpdateLastLoginDevice() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginDevice()
	})
}

// ClearLastLoginDevice clears the value of the "last_login_device" field.
func (u *LoginRecordUpsertBulk) ClearLastLoginDevice() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginDevice()
	})
}

// SetLastLoginUA sets the "last_login_UA" field.
func (u *LoginRecordUpsertBulk) SetLastLoginUA(v string) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLastLoginUA(v)
	})
}

// UpdateLastLoginUA sets the "last_login_UA" field to the value that was provided on create.
func (u *LoginRecordUpsertBulk) UpdateLastLoginUA() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLastLoginUA()
	})
}

// ClearLastLoginUA clears the value of the "last_login_UA" field.
func (u *LoginRecordUpsertBulk) ClearLastLoginUA() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.ClearLastLoginUA()
	})
}

// SetLoginCount sets the "login_count" field.
func (u *LoginRecordUpsertBulk) SetLoginCount(v uint64) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.SetLoginCount(v)
	})
}

// AddLoginCount adds v to the "login_count" field.
func (u *LoginRecordUpsertBulk) AddLoginCount(v uint64) *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.AddLoginCount(v)
	})
}

// UpdateLoginCount sets the "login_count" field to the value that was provided on create.
func (u *LoginRecordUpsertBulk) UpdateLoginCount() *LoginRecordUpsertBulk {
	return u.Update(func(s *LoginRecordUpsert) {
		s.UpdateLoginCount()
	})
}

// Exec executes the query.
func (u *LoginRecordUpsertBulk) Exec(ctx context.Context) error {
	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 LoginRecordCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for LoginRecordCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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