// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ServerCreate is the builder for creating a Server entity.
type ServerCreate struct {
	config
	mutation *ServerMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetStatus sets the "status" field.
func (sc *ServerCreate) SetStatus(u uint8) *ServerCreate {
	sc.mutation.SetStatus(u)
	return sc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (sc *ServerCreate) SetNillableStatus(u *uint8) *ServerCreate {
	if u != nil {
		sc.SetStatus(*u)
	}
	return sc
}

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

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

// SetName sets the "name" field.
func (sc *ServerCreate) SetName(s string) *ServerCreate {
	sc.mutation.SetName(s)
	return sc
}

// SetPublicIP sets the "public_ip" field.
func (sc *ServerCreate) SetPublicIP(s string) *ServerCreate {
	sc.mutation.SetPublicIP(s)
	return sc
}

// SetPrivateIP sets the "private_ip" field.
func (sc *ServerCreate) SetPrivateIP(s string) *ServerCreate {
	sc.mutation.SetPrivateIP(s)
	return sc
}

// SetAdminPort sets the "admin_port" field.
func (sc *ServerCreate) SetAdminPort(s string) *ServerCreate {
	sc.mutation.SetAdminPort(s)
	return sc
}

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

// AddWxIDs adds the "wxs" edge to the Wx entity by IDs.
func (sc *ServerCreate) AddWxIDs(ids ...uint64) *ServerCreate {
	sc.mutation.AddWxIDs(ids...)
	return sc
}

// AddWxs adds the "wxs" edges to the Wx entity.
func (sc *ServerCreate) AddWxs(w ...*Wx) *ServerCreate {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return sc.AddWxIDs(ids...)
}

// Mutation returns the ServerMutation object of the builder.
func (sc *ServerCreate) Mutation() *ServerMutation {
	return sc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (sc *ServerCreate) defaults() error {
	if _, ok := sc.mutation.CreatedAt(); !ok {
		if server.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized server.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := server.DefaultCreatedAt()
		sc.mutation.SetCreatedAt(v)
	}
	if _, ok := sc.mutation.UpdatedAt(); !ok {
		if server.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized server.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := server.DefaultUpdatedAt()
		sc.mutation.SetUpdatedAt(v)
	}
	if _, ok := sc.mutation.Status(); !ok {
		v := server.DefaultStatus
		sc.mutation.SetStatus(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (sc *ServerCreate) check() error {
	if _, ok := sc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Server.created_at"`)}
	}
	if _, ok := sc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Server.updated_at"`)}
	}
	if _, ok := sc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Server.name"`)}
	}
	if _, ok := sc.mutation.PublicIP(); !ok {
		return &ValidationError{Name: "public_ip", err: errors.New(`ent: missing required field "Server.public_ip"`)}
	}
	if _, ok := sc.mutation.PrivateIP(); !ok {
		return &ValidationError{Name: "private_ip", err: errors.New(`ent: missing required field "Server.private_ip"`)}
	}
	if _, ok := sc.mutation.AdminPort(); !ok {
		return &ValidationError{Name: "admin_port", err: errors.New(`ent: missing required field "Server.admin_port"`)}
	}
	return nil
}

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

func (sc *ServerCreate) createSpec() (*Server, *sqlgraph.CreateSpec) {
	var (
		_node = &Server{config: sc.config}
		_spec = sqlgraph.NewCreateSpec(server.Table, sqlgraph.NewFieldSpec(server.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = sc.conflict
	if id, ok := sc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := sc.mutation.CreatedAt(); ok {
		_spec.SetField(server.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := sc.mutation.UpdatedAt(); ok {
		_spec.SetField(server.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := sc.mutation.Status(); ok {
		_spec.SetField(server.FieldStatus, field.TypeUint8, value)
		_node.Status = value
	}
	if value, ok := sc.mutation.DeletedAt(); ok {
		_spec.SetField(server.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := sc.mutation.Name(); ok {
		_spec.SetField(server.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := sc.mutation.PublicIP(); ok {
		_spec.SetField(server.FieldPublicIP, field.TypeString, value)
		_node.PublicIP = value
	}
	if value, ok := sc.mutation.PrivateIP(); ok {
		_spec.SetField(server.FieldPrivateIP, field.TypeString, value)
		_node.PrivateIP = value
	}
	if value, ok := sc.mutation.AdminPort(); ok {
		_spec.SetField(server.FieldAdminPort, field.TypeString, value)
		_node.AdminPort = value
	}
	if nodes := sc.mutation.WxsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   server.WxsTable,
			Columns: []string{server.WxsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_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.Server.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.ServerUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (sc *ServerCreate) OnConflict(opts ...sql.ConflictOption) *ServerUpsertOne {
	sc.conflict = opts
	return &ServerUpsertOne{
		create: sc,
	}
}

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

type (
	// ServerUpsertOne is the builder for "upsert"-ing
	//  one Server node.
	ServerUpsertOne struct {
		create *ServerCreate
	}

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

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

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

// SetStatus sets the "status" field.
func (u *ServerUpsert) SetStatus(v uint8) *ServerUpsert {
	u.Set(server.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *ServerUpsert) UpdateStatus() *ServerUpsert {
	u.SetExcluded(server.FieldStatus)
	return u
}

// AddStatus adds v to the "status" field.
func (u *ServerUpsert) AddStatus(v uint8) *ServerUpsert {
	u.Add(server.FieldStatus, v)
	return u
}

// ClearStatus clears the value of the "status" field.
func (u *ServerUpsert) ClearStatus() *ServerUpsert {
	u.SetNull(server.FieldStatus)
	return u
}

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

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

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

// SetName sets the "name" field.
func (u *ServerUpsert) SetName(v string) *ServerUpsert {
	u.Set(server.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *ServerUpsert) UpdateName() *ServerUpsert {
	u.SetExcluded(server.FieldName)
	return u
}

// SetPublicIP sets the "public_ip" field.
func (u *ServerUpsert) SetPublicIP(v string) *ServerUpsert {
	u.Set(server.FieldPublicIP, v)
	return u
}

// UpdatePublicIP sets the "public_ip" field to the value that was provided on create.
func (u *ServerUpsert) UpdatePublicIP() *ServerUpsert {
	u.SetExcluded(server.FieldPublicIP)
	return u
}

// SetPrivateIP sets the "private_ip" field.
func (u *ServerUpsert) SetPrivateIP(v string) *ServerUpsert {
	u.Set(server.FieldPrivateIP, v)
	return u
}

// UpdatePrivateIP sets the "private_ip" field to the value that was provided on create.
func (u *ServerUpsert) UpdatePrivateIP() *ServerUpsert {
	u.SetExcluded(server.FieldPrivateIP)
	return u
}

// SetAdminPort sets the "admin_port" field.
func (u *ServerUpsert) SetAdminPort(v string) *ServerUpsert {
	u.Set(server.FieldAdminPort, v)
	return u
}

// UpdateAdminPort sets the "admin_port" field to the value that was provided on create.
func (u *ServerUpsert) UpdateAdminPort() *ServerUpsert {
	u.SetExcluded(server.FieldAdminPort)
	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.Server.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(server.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ServerUpsertOne) UpdateNewValues() *ServerUpsertOne {
	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(server.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(server.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

// SetStatus sets the "status" field.
func (u *ServerUpsertOne) SetStatus(v uint8) *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *ServerUpsertOne) AddStatus(v uint8) *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *ServerUpsertOne) UpdateStatus() *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *ServerUpsertOne) ClearStatus() *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.ClearStatus()
	})
}

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

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

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

// SetName sets the "name" field.
func (u *ServerUpsertOne) SetName(v string) *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *ServerUpsertOne) UpdateName() *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.UpdateName()
	})
}

// SetPublicIP sets the "public_ip" field.
func (u *ServerUpsertOne) SetPublicIP(v string) *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.SetPublicIP(v)
	})
}

// UpdatePublicIP sets the "public_ip" field to the value that was provided on create.
func (u *ServerUpsertOne) UpdatePublicIP() *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.UpdatePublicIP()
	})
}

// SetPrivateIP sets the "private_ip" field.
func (u *ServerUpsertOne) SetPrivateIP(v string) *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.SetPrivateIP(v)
	})
}

// UpdatePrivateIP sets the "private_ip" field to the value that was provided on create.
func (u *ServerUpsertOne) UpdatePrivateIP() *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.UpdatePrivateIP()
	})
}

// SetAdminPort sets the "admin_port" field.
func (u *ServerUpsertOne) SetAdminPort(v string) *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.SetAdminPort(v)
	})
}

// UpdateAdminPort sets the "admin_port" field to the value that was provided on create.
func (u *ServerUpsertOne) UpdateAdminPort() *ServerUpsertOne {
	return u.Update(func(s *ServerUpsert) {
		s.UpdateAdminPort()
	})
}

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

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

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

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

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

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

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

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

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

// ServerUpsertBulk is the builder for "upsert"-ing
// a bulk of Server nodes.
type ServerUpsertBulk struct {
	create *ServerCreateBulk
}

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

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

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

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

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

// SetStatus sets the "status" field.
func (u *ServerUpsertBulk) SetStatus(v uint8) *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *ServerUpsertBulk) AddStatus(v uint8) *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *ServerUpsertBulk) UpdateStatus() *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *ServerUpsertBulk) ClearStatus() *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.ClearStatus()
	})
}

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

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

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

// SetName sets the "name" field.
func (u *ServerUpsertBulk) SetName(v string) *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *ServerUpsertBulk) UpdateName() *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.UpdateName()
	})
}

// SetPublicIP sets the "public_ip" field.
func (u *ServerUpsertBulk) SetPublicIP(v string) *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.SetPublicIP(v)
	})
}

// UpdatePublicIP sets the "public_ip" field to the value that was provided on create.
func (u *ServerUpsertBulk) UpdatePublicIP() *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.UpdatePublicIP()
	})
}

// SetPrivateIP sets the "private_ip" field.
func (u *ServerUpsertBulk) SetPrivateIP(v string) *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.SetPrivateIP(v)
	})
}

// UpdatePrivateIP sets the "private_ip" field to the value that was provided on create.
func (u *ServerUpsertBulk) UpdatePrivateIP() *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.UpdatePrivateIP()
	})
}

// SetAdminPort sets the "admin_port" field.
func (u *ServerUpsertBulk) SetAdminPort(v string) *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.SetAdminPort(v)
	})
}

// UpdateAdminPort sets the "admin_port" field to the value that was provided on create.
func (u *ServerUpsertBulk) UpdateAdminPort() *ServerUpsertBulk {
	return u.Update(func(s *ServerUpsert) {
		s.UpdateAdminPort()
	})
}

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

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