// Code generated by ent, DO NOT EDIT.

package serversitemicros

import (
	"context"
	"convert-server/domain/entity/serversitemicros/docservercallback"
	"errors"
	"fmt"
	"time"

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

// DocServerCallbackCreate is the builder for creating a DocServerCallback entity.
type DocServerCallbackCreate struct {
	config
	mutation *DocServerCallbackMutation
	hooks    []Hook
}

// SetServerProjectID sets the "server_project_id" field.
func (dscc *DocServerCallbackCreate) SetServerProjectID(i int32) *DocServerCallbackCreate {
	dscc.mutation.SetServerProjectID(i)
	return dscc
}

// SetNillableServerProjectID sets the "server_project_id" field if the given value is not nil.
func (dscc *DocServerCallbackCreate) SetNillableServerProjectID(i *int32) *DocServerCallbackCreate {
	if i != nil {
		dscc.SetServerProjectID(*i)
	}
	return dscc
}

// SetCallbackURL sets the "callback_url" field.
func (dscc *DocServerCallbackCreate) SetCallbackURL(s string) *DocServerCallbackCreate {
	dscc.mutation.SetCallbackURL(s)
	return dscc
}

// SetNillableCallbackURL sets the "callback_url" field if the given value is not nil.
func (dscc *DocServerCallbackCreate) SetNillableCallbackURL(s *string) *DocServerCallbackCreate {
	if s != nil {
		dscc.SetCallbackURL(*s)
	}
	return dscc
}

// SetCallbackData sets the "callback_data" field.
func (dscc *DocServerCallbackCreate) SetCallbackData(s string) *DocServerCallbackCreate {
	dscc.mutation.SetCallbackData(s)
	return dscc
}

// SetNillableCallbackData sets the "callback_data" field if the given value is not nil.
func (dscc *DocServerCallbackCreate) SetNillableCallbackData(s *string) *DocServerCallbackCreate {
	if s != nil {
		dscc.SetCallbackData(*s)
	}
	return dscc
}

// SetCallbackType sets the "callback_type" field.
func (dscc *DocServerCallbackCreate) SetCallbackType(i int8) *DocServerCallbackCreate {
	dscc.mutation.SetCallbackType(i)
	return dscc
}

// SetNillableCallbackType sets the "callback_type" field if the given value is not nil.
func (dscc *DocServerCallbackCreate) SetNillableCallbackType(i *int8) *DocServerCallbackCreate {
	if i != nil {
		dscc.SetCallbackType(*i)
	}
	return dscc
}

// SetStatus sets the "status" field.
func (dscc *DocServerCallbackCreate) SetStatus(i int8) *DocServerCallbackCreate {
	dscc.mutation.SetStatus(i)
	return dscc
}

// SetCurrNum sets the "curr_num" field.
func (dscc *DocServerCallbackCreate) SetCurrNum(i int32) *DocServerCallbackCreate {
	dscc.mutation.SetCurrNum(i)
	return dscc
}

// SetNillableCurrNum sets the "curr_num" field if the given value is not nil.
func (dscc *DocServerCallbackCreate) SetNillableCurrNum(i *int32) *DocServerCallbackCreate {
	if i != nil {
		dscc.SetCurrNum(*i)
	}
	return dscc
}

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

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

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

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

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

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

// SetID sets the "id" field.
func (dscc *DocServerCallbackCreate) SetID(i int32) *DocServerCallbackCreate {
	dscc.mutation.SetID(i)
	return dscc
}

// Mutation returns the DocServerCallbackMutation object of the builder.
func (dscc *DocServerCallbackCreate) Mutation() *DocServerCallbackMutation {
	return dscc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (dscc *DocServerCallbackCreate) defaults() error {
	if _, ok := dscc.mutation.ServerProjectID(); !ok {
		v := docservercallback.DefaultServerProjectID
		dscc.mutation.SetServerProjectID(v)
	}
	if _, ok := dscc.mutation.CallbackURL(); !ok {
		v := docservercallback.DefaultCallbackURL
		dscc.mutation.SetCallbackURL(v)
	}
	if _, ok := dscc.mutation.CallbackData(); !ok {
		v := docservercallback.DefaultCallbackData
		dscc.mutation.SetCallbackData(v)
	}
	if _, ok := dscc.mutation.CallbackType(); !ok {
		v := docservercallback.DefaultCallbackType
		dscc.mutation.SetCallbackType(v)
	}
	if _, ok := dscc.mutation.CurrNum(); !ok {
		v := docservercallback.DefaultCurrNum
		dscc.mutation.SetCurrNum(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (dscc *DocServerCallbackCreate) check() error {
	if _, ok := dscc.mutation.ServerProjectID(); !ok {
		return &ValidationError{Name: "server_project_id", err: errors.New(`serversitemicros: missing required field "DocServerCallback.server_project_id"`)}
	}
	if _, ok := dscc.mutation.CallbackURL(); !ok {
		return &ValidationError{Name: "callback_url", err: errors.New(`serversitemicros: missing required field "DocServerCallback.callback_url"`)}
	}
	if _, ok := dscc.mutation.CallbackData(); !ok {
		return &ValidationError{Name: "callback_data", err: errors.New(`serversitemicros: missing required field "DocServerCallback.callback_data"`)}
	}
	if _, ok := dscc.mutation.CallbackType(); !ok {
		return &ValidationError{Name: "callback_type", err: errors.New(`serversitemicros: missing required field "DocServerCallback.callback_type"`)}
	}
	if _, ok := dscc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`serversitemicros: missing required field "DocServerCallback.status"`)}
	}
	if _, ok := dscc.mutation.CurrNum(); !ok {
		return &ValidationError{Name: "curr_num", err: errors.New(`serversitemicros: missing required field "DocServerCallback.curr_num"`)}
	}
	return nil
}

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

func (dscc *DocServerCallbackCreate) createSpec() (*DocServerCallback, *sqlgraph.CreateSpec) {
	var (
		_node = &DocServerCallback{config: dscc.config}
		_spec = sqlgraph.NewCreateSpec(docservercallback.Table, sqlgraph.NewFieldSpec(docservercallback.FieldID, field.TypeInt32))
	)
	if id, ok := dscc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := dscc.mutation.ServerProjectID(); ok {
		_spec.SetField(docservercallback.FieldServerProjectID, field.TypeInt32, value)
		_node.ServerProjectID = value
	}
	if value, ok := dscc.mutation.CallbackURL(); ok {
		_spec.SetField(docservercallback.FieldCallbackURL, field.TypeString, value)
		_node.CallbackURL = value
	}
	if value, ok := dscc.mutation.CallbackData(); ok {
		_spec.SetField(docservercallback.FieldCallbackData, field.TypeString, value)
		_node.CallbackData = value
	}
	if value, ok := dscc.mutation.CallbackType(); ok {
		_spec.SetField(docservercallback.FieldCallbackType, field.TypeInt8, value)
		_node.CallbackType = value
	}
	if value, ok := dscc.mutation.Status(); ok {
		_spec.SetField(docservercallback.FieldStatus, field.TypeInt8, value)
		_node.Status = value
	}
	if value, ok := dscc.mutation.CurrNum(); ok {
		_spec.SetField(docservercallback.FieldCurrNum, field.TypeInt32, value)
		_node.CurrNum = value
	}
	if value, ok := dscc.mutation.CreatedAt(); ok {
		_spec.SetField(docservercallback.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = &value
	}
	if value, ok := dscc.mutation.UpdatedAt(); ok {
		_spec.SetField(docservercallback.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = &value
	}
	if value, ok := dscc.mutation.DeletedAt(); ok {
		_spec.SetField(docservercallback.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	return _node, _spec
}

// DocServerCallbackCreateBulk is the builder for creating many DocServerCallback entities in bulk.
type DocServerCallbackCreateBulk struct {
	config
	err      error
	builders []*DocServerCallbackCreate
}

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

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

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

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