// Code generated by ent, DO NOT EDIT.

package serversitemicros

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

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

// DocServerProjectCreate is the builder for creating a DocServerProject entity.
type DocServerProjectCreate struct {
	config
	mutation *DocServerProjectMutation
	hooks    []Hook
}

// SetProjectName sets the "project_name" field.
func (dspc *DocServerProjectCreate) SetProjectName(s string) *DocServerProjectCreate {
	dspc.mutation.SetProjectName(s)
	return dspc
}

// SetNillableProjectName sets the "project_name" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableProjectName(s *string) *DocServerProjectCreate {
	if s != nil {
		dspc.SetProjectName(*s)
	}
	return dspc
}

// SetSourceType sets the "source_type" field.
func (dspc *DocServerProjectCreate) SetSourceType(s string) *DocServerProjectCreate {
	dspc.mutation.SetSourceType(s)
	return dspc
}

// SetNillableSourceType sets the "source_type" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableSourceType(s *string) *DocServerProjectCreate {
	if s != nil {
		dspc.SetSourceType(*s)
	}
	return dspc
}

// SetTargetType sets the "target_type" field.
func (dspc *DocServerProjectCreate) SetTargetType(s string) *DocServerProjectCreate {
	dspc.mutation.SetTargetType(s)
	return dspc
}

// SetNillableTargetType sets the "target_type" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableTargetType(s *string) *DocServerProjectCreate {
	if s != nil {
		dspc.SetTargetType(*s)
	}
	return dspc
}

// SetSourceURL sets the "source_url" field.
func (dspc *DocServerProjectCreate) SetSourceURL(s string) *DocServerProjectCreate {
	dspc.mutation.SetSourceURL(s)
	return dspc
}

// SetNillableSourceURL sets the "source_url" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableSourceURL(s *string) *DocServerProjectCreate {
	if s != nil {
		dspc.SetSourceURL(*s)
	}
	return dspc
}

// SetTargetURL sets the "target_url" field.
func (dspc *DocServerProjectCreate) SetTargetURL(s string) *DocServerProjectCreate {
	dspc.mutation.SetTargetURL(s)
	return dspc
}

// SetNillableTargetURL sets the "target_url" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableTargetURL(s *string) *DocServerProjectCreate {
	if s != nil {
		dspc.SetTargetURL(*s)
	}
	return dspc
}

// SetTaskID sets the "task_id" field.
func (dspc *DocServerProjectCreate) SetTaskID(s string) *DocServerProjectCreate {
	dspc.mutation.SetTaskID(s)
	return dspc
}

// SetNillableTaskID sets the "task_id" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableTaskID(s *string) *DocServerProjectCreate {
	if s != nil {
		dspc.SetTaskID(*s)
	}
	return dspc
}

// SetAppID sets the "app_id" field.
func (dspc *DocServerProjectCreate) SetAppID(i int32) *DocServerProjectCreate {
	dspc.mutation.SetAppID(i)
	return dspc
}

// SetNillableAppID sets the "app_id" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableAppID(i *int32) *DocServerProjectCreate {
	if i != nil {
		dspc.SetAppID(*i)
	}
	return dspc
}

// SetStatus sets the "status" field.
func (dspc *DocServerProjectCreate) SetStatus(i int32) *DocServerProjectCreate {
	dspc.mutation.SetStatus(i)
	return dspc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (dspc *DocServerProjectCreate) SetNillableStatus(i *int32) *DocServerProjectCreate {
	if i != nil {
		dspc.SetStatus(*i)
	}
	return dspc
}

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

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

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

// Mutation returns the DocServerProjectMutation object of the builder.
func (dspc *DocServerProjectCreate) Mutation() *DocServerProjectMutation {
	return dspc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (dspc *DocServerProjectCreate) defaults() error {
	if _, ok := dspc.mutation.ProjectName(); !ok {
		v := docserverproject.DefaultProjectName
		dspc.mutation.SetProjectName(v)
	}
	if _, ok := dspc.mutation.SourceType(); !ok {
		v := docserverproject.DefaultSourceType
		dspc.mutation.SetSourceType(v)
	}
	if _, ok := dspc.mutation.TargetType(); !ok {
		v := docserverproject.DefaultTargetType
		dspc.mutation.SetTargetType(v)
	}
	if _, ok := dspc.mutation.TaskID(); !ok {
		v := docserverproject.DefaultTaskID
		dspc.mutation.SetTaskID(v)
	}
	if _, ok := dspc.mutation.AppID(); !ok {
		v := docserverproject.DefaultAppID
		dspc.mutation.SetAppID(v)
	}
	if _, ok := dspc.mutation.Status(); !ok {
		v := docserverproject.DefaultStatus
		dspc.mutation.SetStatus(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (dspc *DocServerProjectCreate) check() error {
	if _, ok := dspc.mutation.ProjectName(); !ok {
		return &ValidationError{Name: "project_name", err: errors.New(`serversitemicros: missing required field "DocServerProject.project_name"`)}
	}
	if _, ok := dspc.mutation.SourceType(); !ok {
		return &ValidationError{Name: "source_type", err: errors.New(`serversitemicros: missing required field "DocServerProject.source_type"`)}
	}
	if _, ok := dspc.mutation.TargetType(); !ok {
		return &ValidationError{Name: "target_type", err: errors.New(`serversitemicros: missing required field "DocServerProject.target_type"`)}
	}
	if v, ok := dspc.mutation.SourceURL(); ok {
		if err := docserverproject.SourceURLValidator(v); err != nil {
			return &ValidationError{Name: "source_url", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerProject.source_url": %w`, err)}
		}
	}
	if v, ok := dspc.mutation.TargetURL(); ok {
		if err := docserverproject.TargetURLValidator(v); err != nil {
			return &ValidationError{Name: "target_url", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerProject.target_url": %w`, err)}
		}
	}
	if _, ok := dspc.mutation.TaskID(); !ok {
		return &ValidationError{Name: "task_id", err: errors.New(`serversitemicros: missing required field "DocServerProject.task_id"`)}
	}
	if _, ok := dspc.mutation.AppID(); !ok {
		return &ValidationError{Name: "app_id", err: errors.New(`serversitemicros: missing required field "DocServerProject.app_id"`)}
	}
	if _, ok := dspc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`serversitemicros: missing required field "DocServerProject.status"`)}
	}
	return nil
}

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

func (dspc *DocServerProjectCreate) createSpec() (*DocServerProject, *sqlgraph.CreateSpec) {
	var (
		_node = &DocServerProject{config: dspc.config}
		_spec = sqlgraph.NewCreateSpec(docserverproject.Table, sqlgraph.NewFieldSpec(docserverproject.FieldID, field.TypeInt32))
	)
	if id, ok := dspc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := dspc.mutation.ProjectName(); ok {
		_spec.SetField(docserverproject.FieldProjectName, field.TypeString, value)
		_node.ProjectName = value
	}
	if value, ok := dspc.mutation.SourceType(); ok {
		_spec.SetField(docserverproject.FieldSourceType, field.TypeString, value)
		_node.SourceType = value
	}
	if value, ok := dspc.mutation.TargetType(); ok {
		_spec.SetField(docserverproject.FieldTargetType, field.TypeString, value)
		_node.TargetType = value
	}
	if value, ok := dspc.mutation.SourceURL(); ok {
		_spec.SetField(docserverproject.FieldSourceURL, field.TypeString, value)
		_node.SourceURL = &value
	}
	if value, ok := dspc.mutation.TargetURL(); ok {
		_spec.SetField(docserverproject.FieldTargetURL, field.TypeString, value)
		_node.TargetURL = &value
	}
	if value, ok := dspc.mutation.TaskID(); ok {
		_spec.SetField(docserverproject.FieldTaskID, field.TypeString, value)
		_node.TaskID = value
	}
	if value, ok := dspc.mutation.AppID(); ok {
		_spec.SetField(docserverproject.FieldAppID, field.TypeInt32, value)
		_node.AppID = value
	}
	if value, ok := dspc.mutation.Status(); ok {
		_spec.SetField(docserverproject.FieldStatus, field.TypeInt32, value)
		_node.Status = value
	}
	if value, ok := dspc.mutation.CreatedAt(); ok {
		_spec.SetField(docserverproject.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = &value
	}
	return _node, _spec
}

// DocServerProjectCreateBulk is the builder for creating many DocServerProject entities in bulk.
type DocServerProjectCreateBulk struct {
	config
	err      error
	builders []*DocServerProjectCreate
}

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

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

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

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