// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/apitag"
	"time"

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

// ApiTagCreate is the builder for creating a ApiTag entity.
type ApiTagCreate struct {
	config
	mutation *ApiTagMutation
	hooks    []Hook
}

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

// SetSort sets the "sort" field.
func (atc *ApiTagCreate) SetSort(i int32) *ApiTagCreate {
	atc.mutation.SetSort(i)
	return atc
}

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

// SetCreatedBy sets the "created_by" field.
func (atc *ApiTagCreate) SetCreatedBy(u uint32) *ApiTagCreate {
	atc.mutation.SetCreatedBy(u)
	return atc
}

// SetUpdateAt sets the "update_at" field.
func (atc *ApiTagCreate) SetUpdateAt(t time.Time) *ApiTagCreate {
	atc.mutation.SetUpdateAt(t)
	return atc
}

// SetUpdateBy sets the "update_by" field.
func (atc *ApiTagCreate) SetUpdateBy(u uint32) *ApiTagCreate {
	atc.mutation.SetUpdateBy(u)
	return atc
}

// SetDescription sets the "description" field.
func (atc *ApiTagCreate) SetDescription(s string) *ApiTagCreate {
	atc.mutation.SetDescription(s)
	return atc
}

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

// Mutation returns the ApiTagMutation object of the builder.
func (atc *ApiTagCreate) Mutation() *ApiTagMutation {
	return atc.mutation
}

// Save creates the ApiTag in the database.
func (atc *ApiTagCreate) Save(ctx context.Context) (*ApiTag, error) {
	return withHooks[*ApiTag, ApiTagMutation](ctx, atc.sqlSave, atc.mutation, atc.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (atc *ApiTagCreate) check() error {
	if _, ok := atc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "ApiTag.name"`)}
	}
	if _, ok := atc.mutation.Sort(); !ok {
		return &ValidationError{Name: "sort", err: errors.New(`ent: missing required field "ApiTag.sort"`)}
	}
	if _, ok := atc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ApiTag.created_at"`)}
	}
	if _, ok := atc.mutation.CreatedBy(); !ok {
		return &ValidationError{Name: "created_by", err: errors.New(`ent: missing required field "ApiTag.created_by"`)}
	}
	if _, ok := atc.mutation.UpdateAt(); !ok {
		return &ValidationError{Name: "update_at", err: errors.New(`ent: missing required field "ApiTag.update_at"`)}
	}
	if _, ok := atc.mutation.UpdateBy(); !ok {
		return &ValidationError{Name: "update_by", err: errors.New(`ent: missing required field "ApiTag.update_by"`)}
	}
	if _, ok := atc.mutation.Description(); !ok {
		return &ValidationError{Name: "description", err: errors.New(`ent: missing required field "ApiTag.description"`)}
	}
	return nil
}

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

func (atc *ApiTagCreate) createSpec() (*ApiTag, *sqlgraph.CreateSpec) {
	var (
		_node = &ApiTag{config: atc.config}
		_spec = sqlgraph.NewCreateSpec(apitag.Table, sqlgraph.NewFieldSpec(apitag.FieldID, field.TypeInt32))
	)
	if id, ok := atc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := atc.mutation.Name(); ok {
		_spec.SetField(apitag.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := atc.mutation.Sort(); ok {
		_spec.SetField(apitag.FieldSort, field.TypeInt32, value)
		_node.Sort = value
	}
	if value, ok := atc.mutation.CreatedAt(); ok {
		_spec.SetField(apitag.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := atc.mutation.CreatedBy(); ok {
		_spec.SetField(apitag.FieldCreatedBy, field.TypeUint32, value)
		_node.CreatedBy = value
	}
	if value, ok := atc.mutation.UpdateAt(); ok {
		_spec.SetField(apitag.FieldUpdateAt, field.TypeTime, value)
		_node.UpdateAt = value
	}
	if value, ok := atc.mutation.UpdateBy(); ok {
		_spec.SetField(apitag.FieldUpdateBy, field.TypeUint32, value)
		_node.UpdateBy = value
	}
	if value, ok := atc.mutation.Description(); ok {
		_spec.SetField(apitag.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	return _node, _spec
}

// ApiTagCreateBulk is the builder for creating many ApiTag entities in bulk.
type ApiTagCreateBulk struct {
	config
	builders []*ApiTagCreate
}

// Save creates the ApiTag entities in the database.
func (atcb *ApiTagCreateBulk) Save(ctx context.Context) ([]*ApiTag, error) {
	specs := make([]*sqlgraph.CreateSpec, len(atcb.builders))
	nodes := make([]*ApiTag, len(atcb.builders))
	mutators := make([]Mutator, len(atcb.builders))
	for i := range atcb.builders {
		func(i int, root context.Context) {
			builder := atcb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*ApiTagMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				nodes[i], specs[i] = builder.createSpec()
				var err error
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, atcb.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, atcb.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, atcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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