// Code generated by ent, DO NOT EDIT.

package ent

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

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

// XunjiServiceCreate is the builder for creating a XunjiService entity.
type XunjiServiceCreate struct {
	config
	mutation *XunjiServiceMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

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

// SetXunjiID sets the "xunji_id" field.
func (xsc *XunjiServiceCreate) SetXunjiID(u uint64) *XunjiServiceCreate {
	xsc.mutation.SetXunjiID(u)
	return xsc
}

// SetAgentID sets the "agent_id" field.
func (xsc *XunjiServiceCreate) SetAgentID(u uint64) *XunjiServiceCreate {
	xsc.mutation.SetAgentID(u)
	return xsc
}

// SetNillableAgentID sets the "agent_id" field if the given value is not nil.
func (xsc *XunjiServiceCreate) SetNillableAgentID(u *uint64) *XunjiServiceCreate {
	if u != nil {
		xsc.SetAgentID(*u)
	}
	return xsc
}

// SetOrganizationID sets the "organization_id" field.
func (xsc *XunjiServiceCreate) SetOrganizationID(u uint64) *XunjiServiceCreate {
	xsc.mutation.SetOrganizationID(u)
	return xsc
}

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

// SetAPIBase sets the "api_base" field.
func (xsc *XunjiServiceCreate) SetAPIBase(s string) *XunjiServiceCreate {
	xsc.mutation.SetAPIBase(s)
	return xsc
}

// SetNillableAPIBase sets the "api_base" field if the given value is not nil.
func (xsc *XunjiServiceCreate) SetNillableAPIBase(s *string) *XunjiServiceCreate {
	if s != nil {
		xsc.SetAPIBase(*s)
	}
	return xsc
}

// SetAPIKey sets the "api_key" field.
func (xsc *XunjiServiceCreate) SetAPIKey(s string) *XunjiServiceCreate {
	xsc.mutation.SetAPIKey(s)
	return xsc
}

// SetNillableAPIKey sets the "api_key" field if the given value is not nil.
func (xsc *XunjiServiceCreate) SetNillableAPIKey(s *string) *XunjiServiceCreate {
	if s != nil {
		xsc.SetAPIKey(*s)
	}
	return xsc
}

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

// SetAgent sets the "agent" edge to the Agent entity.
func (xsc *XunjiServiceCreate) SetAgent(a *Agent) *XunjiServiceCreate {
	return xsc.SetAgentID(a.ID)
}

// Mutation returns the XunjiServiceMutation object of the builder.
func (xsc *XunjiServiceCreate) Mutation() *XunjiServiceMutation {
	return xsc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (xsc *XunjiServiceCreate) defaults() error {
	if _, ok := xsc.mutation.CreatedAt(); !ok {
		if xunjiservice.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized xunjiservice.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := xunjiservice.DefaultCreatedAt()
		xsc.mutation.SetCreatedAt(v)
	}
	if _, ok := xsc.mutation.UpdatedAt(); !ok {
		if xunjiservice.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized xunjiservice.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := xunjiservice.DefaultUpdatedAt()
		xsc.mutation.SetUpdatedAt(v)
	}
	if _, ok := xsc.mutation.Status(); !ok {
		v := xunjiservice.DefaultStatus
		xsc.mutation.SetStatus(v)
	}
	if _, ok := xsc.mutation.AgentID(); !ok {
		v := xunjiservice.DefaultAgentID
		xsc.mutation.SetAgentID(v)
	}
	if _, ok := xsc.mutation.APIBase(); !ok {
		v := xunjiservice.DefaultAPIBase
		xsc.mutation.SetAPIBase(v)
	}
	if _, ok := xsc.mutation.APIKey(); !ok {
		v := xunjiservice.DefaultAPIKey
		xsc.mutation.SetAPIKey(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (xsc *XunjiServiceCreate) check() error {
	if _, ok := xsc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "XunjiService.created_at"`)}
	}
	if _, ok := xsc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "XunjiService.updated_at"`)}
	}
	if _, ok := xsc.mutation.XunjiID(); !ok {
		return &ValidationError{Name: "xunji_id", err: errors.New(`ent: missing required field "XunjiService.xunji_id"`)}
	}
	if _, ok := xsc.mutation.AgentID(); !ok {
		return &ValidationError{Name: "agent_id", err: errors.New(`ent: missing required field "XunjiService.agent_id"`)}
	}
	if _, ok := xsc.mutation.OrganizationID(); !ok {
		return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "XunjiService.organization_id"`)}
	}
	if v, ok := xsc.mutation.OrganizationID(); ok {
		if err := xunjiservice.OrganizationIDValidator(v); err != nil {
			return &ValidationError{Name: "organization_id", err: fmt.Errorf(`ent: validator failed for field "XunjiService.organization_id": %w`, err)}
		}
	}
	if _, ok := xsc.mutation.Wxid(); !ok {
		return &ValidationError{Name: "wxid", err: errors.New(`ent: missing required field "XunjiService.wxid"`)}
	}
	if _, ok := xsc.mutation.AgentID(); !ok {
		return &ValidationError{Name: "agent", err: errors.New(`ent: missing required edge "XunjiService.agent"`)}
	}
	return nil
}

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

func (xsc *XunjiServiceCreate) createSpec() (*XunjiService, *sqlgraph.CreateSpec) {
	var (
		_node = &XunjiService{config: xsc.config}
		_spec = sqlgraph.NewCreateSpec(xunjiservice.Table, sqlgraph.NewFieldSpec(xunjiservice.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = xsc.conflict
	if id, ok := xsc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := xsc.mutation.CreatedAt(); ok {
		_spec.SetField(xunjiservice.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := xsc.mutation.UpdatedAt(); ok {
		_spec.SetField(xunjiservice.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := xsc.mutation.Status(); ok {
		_spec.SetField(xunjiservice.FieldStatus, field.TypeUint8, value)
		_node.Status = value
	}
	if value, ok := xsc.mutation.DeletedAt(); ok {
		_spec.SetField(xunjiservice.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := xsc.mutation.XunjiID(); ok {
		_spec.SetField(xunjiservice.FieldXunjiID, field.TypeUint64, value)
		_node.XunjiID = value
	}
	if value, ok := xsc.mutation.OrganizationID(); ok {
		_spec.SetField(xunjiservice.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if value, ok := xsc.mutation.Wxid(); ok {
		_spec.SetField(xunjiservice.FieldWxid, field.TypeString, value)
		_node.Wxid = value
	}
	if value, ok := xsc.mutation.APIBase(); ok {
		_spec.SetField(xunjiservice.FieldAPIBase, field.TypeString, value)
		_node.APIBase = value
	}
	if value, ok := xsc.mutation.APIKey(); ok {
		_spec.SetField(xunjiservice.FieldAPIKey, field.TypeString, value)
		_node.APIKey = value
	}
	if nodes := xsc.mutation.AgentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   xunjiservice.AgentTable,
			Columns: []string{xunjiservice.AgentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.AgentID = 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.XunjiService.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.XunjiServiceUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (xsc *XunjiServiceCreate) OnConflict(opts ...sql.ConflictOption) *XunjiServiceUpsertOne {
	xsc.conflict = opts
	return &XunjiServiceUpsertOne{
		create: xsc,
	}
}

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

type (
	// XunjiServiceUpsertOne is the builder for "upsert"-ing
	//  one XunjiService node.
	XunjiServiceUpsertOne struct {
		create *XunjiServiceCreate
	}

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

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

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

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

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

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

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

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

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

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

// SetXunjiID sets the "xunji_id" field.
func (u *XunjiServiceUpsert) SetXunjiID(v uint64) *XunjiServiceUpsert {
	u.Set(xunjiservice.FieldXunjiID, v)
	return u
}

// UpdateXunjiID sets the "xunji_id" field to the value that was provided on create.
func (u *XunjiServiceUpsert) UpdateXunjiID() *XunjiServiceUpsert {
	u.SetExcluded(xunjiservice.FieldXunjiID)
	return u
}

// AddXunjiID adds v to the "xunji_id" field.
func (u *XunjiServiceUpsert) AddXunjiID(v uint64) *XunjiServiceUpsert {
	u.Add(xunjiservice.FieldXunjiID, v)
	return u
}

// SetAgentID sets the "agent_id" field.
func (u *XunjiServiceUpsert) SetAgentID(v uint64) *XunjiServiceUpsert {
	u.Set(xunjiservice.FieldAgentID, v)
	return u
}

// UpdateAgentID sets the "agent_id" field to the value that was provided on create.
func (u *XunjiServiceUpsert) UpdateAgentID() *XunjiServiceUpsert {
	u.SetExcluded(xunjiservice.FieldAgentID)
	return u
}

// SetOrganizationID sets the "organization_id" field.
func (u *XunjiServiceUpsert) SetOrganizationID(v uint64) *XunjiServiceUpsert {
	u.Set(xunjiservice.FieldOrganizationID, v)
	return u
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *XunjiServiceUpsert) UpdateOrganizationID() *XunjiServiceUpsert {
	u.SetExcluded(xunjiservice.FieldOrganizationID)
	return u
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *XunjiServiceUpsert) AddOrganizationID(v uint64) *XunjiServiceUpsert {
	u.Add(xunjiservice.FieldOrganizationID, v)
	return u
}

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

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

// SetAPIBase sets the "api_base" field.
func (u *XunjiServiceUpsert) SetAPIBase(v string) *XunjiServiceUpsert {
	u.Set(xunjiservice.FieldAPIBase, v)
	return u
}

// UpdateAPIBase sets the "api_base" field to the value that was provided on create.
func (u *XunjiServiceUpsert) UpdateAPIBase() *XunjiServiceUpsert {
	u.SetExcluded(xunjiservice.FieldAPIBase)
	return u
}

// ClearAPIBase clears the value of the "api_base" field.
func (u *XunjiServiceUpsert) ClearAPIBase() *XunjiServiceUpsert {
	u.SetNull(xunjiservice.FieldAPIBase)
	return u
}

// SetAPIKey sets the "api_key" field.
func (u *XunjiServiceUpsert) SetAPIKey(v string) *XunjiServiceUpsert {
	u.Set(xunjiservice.FieldAPIKey, v)
	return u
}

// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
func (u *XunjiServiceUpsert) UpdateAPIKey() *XunjiServiceUpsert {
	u.SetExcluded(xunjiservice.FieldAPIKey)
	return u
}

// ClearAPIKey clears the value of the "api_key" field.
func (u *XunjiServiceUpsert) ClearAPIKey() *XunjiServiceUpsert {
	u.SetNull(xunjiservice.FieldAPIKey)
	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.XunjiService.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(xunjiservice.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *XunjiServiceUpsertOne) UpdateNewValues() *XunjiServiceUpsertOne {
	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(xunjiservice.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(xunjiservice.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

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

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

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

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

// SetXunjiID sets the "xunji_id" field.
func (u *XunjiServiceUpsertOne) SetXunjiID(v uint64) *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetXunjiID(v)
	})
}

// AddXunjiID adds v to the "xunji_id" field.
func (u *XunjiServiceUpsertOne) AddXunjiID(v uint64) *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.AddXunjiID(v)
	})
}

// UpdateXunjiID sets the "xunji_id" field to the value that was provided on create.
func (u *XunjiServiceUpsertOne) UpdateXunjiID() *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateXunjiID()
	})
}

// SetAgentID sets the "agent_id" field.
func (u *XunjiServiceUpsertOne) SetAgentID(v uint64) *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetAgentID(v)
	})
}

// UpdateAgentID sets the "agent_id" field to the value that was provided on create.
func (u *XunjiServiceUpsertOne) UpdateAgentID() *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateAgentID()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *XunjiServiceUpsertOne) SetOrganizationID(v uint64) *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *XunjiServiceUpsertOne) AddOrganizationID(v uint64) *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *XunjiServiceUpsertOne) UpdateOrganizationID() *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateOrganizationID()
	})
}

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

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

// SetAPIBase sets the "api_base" field.
func (u *XunjiServiceUpsertOne) SetAPIBase(v string) *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetAPIBase(v)
	})
}

// UpdateAPIBase sets the "api_base" field to the value that was provided on create.
func (u *XunjiServiceUpsertOne) UpdateAPIBase() *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateAPIBase()
	})
}

// ClearAPIBase clears the value of the "api_base" field.
func (u *XunjiServiceUpsertOne) ClearAPIBase() *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.ClearAPIBase()
	})
}

// SetAPIKey sets the "api_key" field.
func (u *XunjiServiceUpsertOne) SetAPIKey(v string) *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetAPIKey(v)
	})
}

// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
func (u *XunjiServiceUpsertOne) UpdateAPIKey() *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateAPIKey()
	})
}

// ClearAPIKey clears the value of the "api_key" field.
func (u *XunjiServiceUpsertOne) ClearAPIKey() *XunjiServiceUpsertOne {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.ClearAPIKey()
	})
}

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

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

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

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

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

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

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

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

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

// XunjiServiceUpsertBulk is the builder for "upsert"-ing
// a bulk of XunjiService nodes.
type XunjiServiceUpsertBulk struct {
	create *XunjiServiceCreateBulk
}

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

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

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

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

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

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

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

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

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

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

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

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

// SetXunjiID sets the "xunji_id" field.
func (u *XunjiServiceUpsertBulk) SetXunjiID(v uint64) *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetXunjiID(v)
	})
}

// AddXunjiID adds v to the "xunji_id" field.
func (u *XunjiServiceUpsertBulk) AddXunjiID(v uint64) *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.AddXunjiID(v)
	})
}

// UpdateXunjiID sets the "xunji_id" field to the value that was provided on create.
func (u *XunjiServiceUpsertBulk) UpdateXunjiID() *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateXunjiID()
	})
}

// SetAgentID sets the "agent_id" field.
func (u *XunjiServiceUpsertBulk) SetAgentID(v uint64) *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetAgentID(v)
	})
}

// UpdateAgentID sets the "agent_id" field to the value that was provided on create.
func (u *XunjiServiceUpsertBulk) UpdateAgentID() *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateAgentID()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *XunjiServiceUpsertBulk) SetOrganizationID(v uint64) *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *XunjiServiceUpsertBulk) AddOrganizationID(v uint64) *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *XunjiServiceUpsertBulk) UpdateOrganizationID() *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateOrganizationID()
	})
}

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

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

// SetAPIBase sets the "api_base" field.
func (u *XunjiServiceUpsertBulk) SetAPIBase(v string) *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetAPIBase(v)
	})
}

// UpdateAPIBase sets the "api_base" field to the value that was provided on create.
func (u *XunjiServiceUpsertBulk) UpdateAPIBase() *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateAPIBase()
	})
}

// ClearAPIBase clears the value of the "api_base" field.
func (u *XunjiServiceUpsertBulk) ClearAPIBase() *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.ClearAPIBase()
	})
}

// SetAPIKey sets the "api_key" field.
func (u *XunjiServiceUpsertBulk) SetAPIKey(v string) *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.SetAPIKey(v)
	})
}

// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
func (u *XunjiServiceUpsertBulk) UpdateAPIKey() *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.UpdateAPIKey()
	})
}

// ClearAPIKey clears the value of the "api_key" field.
func (u *XunjiServiceUpsertBulk) ClearAPIKey() *XunjiServiceUpsertBulk {
	return u.Update(func(s *XunjiServiceUpsert) {
		s.ClearAPIKey()
	})
}

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

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