// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ProjectMemberCreate is the builder for creating a ProjectMember entity.
type ProjectMemberCreate struct {
	config
	mutation *ProjectMemberMutation
	hooks    []Hook
}

// SetProjectID sets the "project_id" field.
func (pmc *ProjectMemberCreate) SetProjectID(i int64) *ProjectMemberCreate {
	pmc.mutation.SetProjectID(i)
	return pmc
}

// SetUserID sets the "user_id" field.
func (pmc *ProjectMemberCreate) SetUserID(u uint32) *ProjectMemberCreate {
	pmc.mutation.SetUserID(u)
	return pmc
}

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

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

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

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

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

// SetDeletedBy sets the "deleted_by" field.
func (pmc *ProjectMemberCreate) SetDeletedBy(u uint32) *ProjectMemberCreate {
	pmc.mutation.SetDeletedBy(u)
	return pmc
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (pmc *ProjectMemberCreate) SetNillableDeletedBy(u *uint32) *ProjectMemberCreate {
	if u != nil {
		pmc.SetDeletedBy(*u)
	}
	return pmc
}

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

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

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

// SetNillableDescription sets the "description" field if the given value is not nil.
func (pmc *ProjectMemberCreate) SetNillableDescription(s *string) *ProjectMemberCreate {
	if s != nil {
		pmc.SetDescription(*s)
	}
	return pmc
}

// SetRemark sets the "remark" field.
func (pmc *ProjectMemberCreate) SetRemark(s string) *ProjectMemberCreate {
	pmc.mutation.SetRemark(s)
	return pmc
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (pmc *ProjectMemberCreate) SetNillableRemark(s *string) *ProjectMemberCreate {
	if s != nil {
		pmc.SetRemark(*s)
	}
	return pmc
}

// SetRole sets the "role" field.
func (pmc *ProjectMemberCreate) SetRole(u uint8) *ProjectMemberCreate {
	pmc.mutation.SetRole(u)
	return pmc
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (pmc *ProjectMemberCreate) SetNillableRole(u *uint8) *ProjectMemberCreate {
	if u != nil {
		pmc.SetRole(*u)
	}
	return pmc
}

// Mutation returns the ProjectMemberMutation object of the builder.
func (pmc *ProjectMemberCreate) Mutation() *ProjectMemberMutation {
	return pmc.mutation
}

// Save creates the ProjectMember in the database.
func (pmc *ProjectMemberCreate) Save(ctx context.Context) (*ProjectMember, error) {
	pmc.defaults()
	return withHooks[*ProjectMember, ProjectMemberMutation](ctx, pmc.sqlSave, pmc.mutation, pmc.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (pmc *ProjectMemberCreate) defaults() {
	if _, ok := pmc.mutation.CreatedAt(); !ok {
		v := projectmember.DefaultCreatedAt()
		pmc.mutation.SetCreatedAt(v)
	}
	if _, ok := pmc.mutation.Status(); !ok {
		v := projectmember.DefaultStatus
		pmc.mutation.SetStatus(v)
	}
	if _, ok := pmc.mutation.Role(); !ok {
		v := projectmember.DefaultRole
		pmc.mutation.SetRole(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (pmc *ProjectMemberCreate) check() error {
	if _, ok := pmc.mutation.ProjectID(); !ok {
		return &ValidationError{Name: "project_id", err: errors.New(`ent: missing required field "ProjectMember.project_id"`)}
	}
	if _, ok := pmc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "ProjectMember.user_id"`)}
	}
	if _, ok := pmc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ProjectMember.created_at"`)}
	}
	if _, ok := pmc.mutation.CreatedBy(); !ok {
		return &ValidationError{Name: "created_by", err: errors.New(`ent: missing required field "ProjectMember.created_by"`)}
	}
	if _, ok := pmc.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "ProjectMember.status"`)}
	}
	if _, ok := pmc.mutation.Role(); !ok {
		return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "ProjectMember.role"`)}
	}
	return nil
}

func (pmc *ProjectMemberCreate) sqlSave(ctx context.Context) (*ProjectMember, error) {
	if err := pmc.check(); err != nil {
		return nil, err
	}
	_node, _spec := pmc.createSpec()
	if err := sqlgraph.CreateNode(ctx, pmc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int(id)
	pmc.mutation.id = &_node.ID
	pmc.mutation.done = true
	return _node, nil
}

func (pmc *ProjectMemberCreate) createSpec() (*ProjectMember, *sqlgraph.CreateSpec) {
	var (
		_node = &ProjectMember{config: pmc.config}
		_spec = sqlgraph.NewCreateSpec(projectmember.Table, sqlgraph.NewFieldSpec(projectmember.FieldID, field.TypeInt))
	)
	if value, ok := pmc.mutation.ProjectID(); ok {
		_spec.SetField(projectmember.FieldProjectID, field.TypeInt64, value)
		_node.ProjectID = value
	}
	if value, ok := pmc.mutation.UserID(); ok {
		_spec.SetField(projectmember.FieldUserID, field.TypeUint32, value)
		_node.UserID = value
	}
	if value, ok := pmc.mutation.CreatedAt(); ok {
		_spec.SetField(projectmember.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := pmc.mutation.CreatedBy(); ok {
		_spec.SetField(projectmember.FieldCreatedBy, field.TypeUint32, value)
		_node.CreatedBy = value
	}
	if value, ok := pmc.mutation.DeletedAt(); ok {
		_spec.SetField(projectmember.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := pmc.mutation.DeletedBy(); ok {
		_spec.SetField(projectmember.FieldDeletedBy, field.TypeUint32, value)
		_node.DeletedBy = value
	}
	if value, ok := pmc.mutation.Status(); ok {
		_spec.SetField(projectmember.FieldStatus, field.TypeInt8, value)
		_node.Status = value
	}
	if value, ok := pmc.mutation.Description(); ok {
		_spec.SetField(projectmember.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := pmc.mutation.Remark(); ok {
		_spec.SetField(projectmember.FieldRemark, field.TypeString, value)
		_node.Remark = value
	}
	if value, ok := pmc.mutation.Role(); ok {
		_spec.SetField(projectmember.FieldRole, field.TypeUint8, value)
		_node.Role = value
	}
	return _node, _spec
}

// ProjectMemberCreateBulk is the builder for creating many ProjectMember entities in bulk.
type ProjectMemberCreateBulk struct {
	config
	builders []*ProjectMemberCreate
}

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

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

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

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