// Code generated by ent, DO NOT EDIT.

package serversitemicros

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

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

// DocServerGetProjectCreate is the builder for creating a DocServerGetProject entity.
type DocServerGetProjectCreate struct {
	config
	mutation *DocServerGetProjectMutation
	hooks    []Hook
}

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

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

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

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

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

// SetAliResult sets the "ali_result" field.
func (dsgpc *DocServerGetProjectCreate) SetAliResult(s string) *DocServerGetProjectCreate {
	dsgpc.mutation.SetAliResult(s)
	return dsgpc
}

// SetNillableAliResult sets the "ali_result" field if the given value is not nil.
func (dsgpc *DocServerGetProjectCreate) SetNillableAliResult(s *string) *DocServerGetProjectCreate {
	if s != nil {
		dsgpc.SetAliResult(*s)
	}
	return dsgpc
}

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

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

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

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

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

// Mutation returns the DocServerGetProjectMutation object of the builder.
func (dsgpc *DocServerGetProjectCreate) Mutation() *DocServerGetProjectMutation {
	return dsgpc.mutation
}

// Save creates the DocServerGetProject in the database.
func (dsgpc *DocServerGetProjectCreate) Save(ctx context.Context) (*DocServerGetProject, error) {
	dsgpc.defaults()
	return withHooks(ctx, dsgpc.sqlSave, dsgpc.mutation, dsgpc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (dsgpc *DocServerGetProjectCreate) defaults() {
	if _, ok := dsgpc.mutation.ServerProjectID(); !ok {
		v := docservergetproject.DefaultServerProjectID
		dsgpc.mutation.SetServerProjectID(v)
	}
	if _, ok := dsgpc.mutation.CurrNum(); !ok {
		v := docservergetproject.DefaultCurrNum
		dsgpc.mutation.SetCurrNum(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (dsgpc *DocServerGetProjectCreate) check() error {
	if _, ok := dsgpc.mutation.ServerProjectID(); !ok {
		return &ValidationError{Name: "server_project_id", err: errors.New(`serversitemicros: missing required field "DocServerGetProject.server_project_id"`)}
	}
	if _, ok := dsgpc.mutation.CurrNum(); !ok {
		return &ValidationError{Name: "curr_num", err: errors.New(`serversitemicros: missing required field "DocServerGetProject.curr_num"`)}
	}
	if _, ok := dsgpc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`serversitemicros: missing required field "DocServerGetProject.status"`)}
	}
	if v, ok := dsgpc.mutation.AliResult(); ok {
		if err := docservergetproject.AliResultValidator(v); err != nil {
			return &ValidationError{Name: "ali_result", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerGetProject.ali_result": %w`, err)}
		}
	}
	return nil
}

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

func (dsgpc *DocServerGetProjectCreate) createSpec() (*DocServerGetProject, *sqlgraph.CreateSpec) {
	var (
		_node = &DocServerGetProject{config: dsgpc.config}
		_spec = sqlgraph.NewCreateSpec(docservergetproject.Table, sqlgraph.NewFieldSpec(docservergetproject.FieldID, field.TypeInt32))
	)
	if id, ok := dsgpc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := dsgpc.mutation.ServerProjectID(); ok {
		_spec.SetField(docservergetproject.FieldServerProjectID, field.TypeInt32, value)
		_node.ServerProjectID = value
	}
	if value, ok := dsgpc.mutation.CurrNum(); ok {
		_spec.SetField(docservergetproject.FieldCurrNum, field.TypeInt32, value)
		_node.CurrNum = value
	}
	if value, ok := dsgpc.mutation.Status(); ok {
		_spec.SetField(docservergetproject.FieldStatus, field.TypeInt8, value)
		_node.Status = value
	}
	if value, ok := dsgpc.mutation.AliResult(); ok {
		_spec.SetField(docservergetproject.FieldAliResult, field.TypeString, value)
		_node.AliResult = &value
	}
	if value, ok := dsgpc.mutation.CreatedAt(); ok {
		_spec.SetField(docservergetproject.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = &value
	}
	if value, ok := dsgpc.mutation.UpdatedAt(); ok {
		_spec.SetField(docservergetproject.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = &value
	}
	return _node, _spec
}

// DocServerGetProjectCreateBulk is the builder for creating many DocServerGetProject entities in bulk.
type DocServerGetProjectCreateBulk struct {
	config
	err      error
	builders []*DocServerGetProjectCreate
}

// Save creates the DocServerGetProject entities in the database.
func (dsgpcb *DocServerGetProjectCreateBulk) Save(ctx context.Context) ([]*DocServerGetProject, error) {
	if dsgpcb.err != nil {
		return nil, dsgpcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(dsgpcb.builders))
	nodes := make([]*DocServerGetProject, len(dsgpcb.builders))
	mutators := make([]Mutator, len(dsgpcb.builders))
	for i := range dsgpcb.builders {
		func(i int, root context.Context) {
			builder := dsgpcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*DocServerGetProjectMutation)
				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, dsgpcb.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, dsgpcb.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, dsgpcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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