// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/article"
	"github.com/anzhiyu-c/anheyu-app/ent/postcategory"
)

// PostCategoryCreate is the builder for creating a PostCategory entity.
type PostCategoryCreate struct {
	config
	mutation *PostCategoryMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetDeletedAt sets the "deleted_at" field.
func (_c *PostCategoryCreate) SetDeletedAt(v time.Time) *PostCategoryCreate {
	_c.mutation.SetDeletedAt(v)
	return _c
}

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

// SetCreatedAt sets the "created_at" field.
func (_c *PostCategoryCreate) SetCreatedAt(v time.Time) *PostCategoryCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

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

// SetUpdatedAt sets the "updated_at" field.
func (_c *PostCategoryCreate) SetUpdatedAt(v time.Time) *PostCategoryCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

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

// SetName sets the "name" field.
func (_c *PostCategoryCreate) SetName(v string) *PostCategoryCreate {
	_c.mutation.SetName(v)
	return _c
}

// SetDescription sets the "description" field.
func (_c *PostCategoryCreate) SetDescription(v string) *PostCategoryCreate {
	_c.mutation.SetDescription(v)
	return _c
}

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

// SetCount sets the "count" field.
func (_c *PostCategoryCreate) SetCount(v int) *PostCategoryCreate {
	_c.mutation.SetCount(v)
	return _c
}

// SetNillableCount sets the "count" field if the given value is not nil.
func (_c *PostCategoryCreate) SetNillableCount(v *int) *PostCategoryCreate {
	if v != nil {
		_c.SetCount(*v)
	}
	return _c
}

// SetIsSeries sets the "is_series" field.
func (_c *PostCategoryCreate) SetIsSeries(v bool) *PostCategoryCreate {
	_c.mutation.SetIsSeries(v)
	return _c
}

// SetNillableIsSeries sets the "is_series" field if the given value is not nil.
func (_c *PostCategoryCreate) SetNillableIsSeries(v *bool) *PostCategoryCreate {
	if v != nil {
		_c.SetIsSeries(*v)
	}
	return _c
}

// SetID sets the "id" field.
func (_c *PostCategoryCreate) SetID(v uint) *PostCategoryCreate {
	_c.mutation.SetID(v)
	return _c
}

// AddArticleIDs adds the "articles" edge to the Article entity by IDs.
func (_c *PostCategoryCreate) AddArticleIDs(ids ...uint) *PostCategoryCreate {
	_c.mutation.AddArticleIDs(ids...)
	return _c
}

// AddArticles adds the "articles" edges to the Article entity.
func (_c *PostCategoryCreate) AddArticles(v ...*Article) *PostCategoryCreate {
	ids := make([]uint, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddArticleIDs(ids...)
}

// Mutation returns the PostCategoryMutation object of the builder.
func (_c *PostCategoryCreate) Mutation() *PostCategoryMutation {
	return _c.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_c *PostCategoryCreate) defaults() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		if postcategory.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized postcategory.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := postcategory.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		if postcategory.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized postcategory.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := postcategory.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.Count(); !ok {
		v := postcategory.DefaultCount
		_c.mutation.SetCount(v)
	}
	if _, ok := _c.mutation.IsSeries(); !ok {
		v := postcategory.DefaultIsSeries
		_c.mutation.SetIsSeries(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_c *PostCategoryCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "PostCategory.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "PostCategory.updated_at"`)}
	}
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "PostCategory.name"`)}
	}
	if v, ok := _c.mutation.Name(); ok {
		if err := postcategory.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "PostCategory.name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Count(); !ok {
		return &ValidationError{Name: "count", err: errors.New(`ent: missing required field "PostCategory.count"`)}
	}
	if v, ok := _c.mutation.Count(); ok {
		if err := postcategory.CountValidator(v); err != nil {
			return &ValidationError{Name: "count", err: fmt.Errorf(`ent: validator failed for field "PostCategory.count": %w`, err)}
		}
	}
	if _, ok := _c.mutation.IsSeries(); !ok {
		return &ValidationError{Name: "is_series", err: errors.New(`ent: missing required field "PostCategory.is_series"`)}
	}
	return nil
}

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

func (_c *PostCategoryCreate) createSpec() (*PostCategory, *sqlgraph.CreateSpec) {
	var (
		_node = &PostCategory{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(postcategory.Table, sqlgraph.NewFieldSpec(postcategory.FieldID, field.TypeUint))
	)
	_spec.OnConflict = _c.conflict
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(postcategory.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(postcategory.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(postcategory.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(postcategory.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Description(); ok {
		_spec.SetField(postcategory.FieldDescription, field.TypeString, value)
		_node.Description = value
	}
	if value, ok := _c.mutation.Count(); ok {
		_spec.SetField(postcategory.FieldCount, field.TypeInt, value)
		_node.Count = value
	}
	if value, ok := _c.mutation.IsSeries(); ok {
		_spec.SetField(postcategory.FieldIsSeries, field.TypeBool, value)
		_node.IsSeries = value
	}
	if nodes := _c.mutation.ArticlesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   postcategory.ArticlesTable,
			Columns: postcategory.ArticlesPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(article.FieldID, field.TypeUint),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PostCategory.Create().
//		SetDeletedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.PostCategoryUpsert) {
//			SetDeletedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *PostCategoryCreate) OnConflict(opts ...sql.ConflictOption) *PostCategoryUpsertOne {
	_c.conflict = opts
	return &PostCategoryUpsertOne{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.PostCategory.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *PostCategoryCreate) OnConflictColumns(columns ...string) *PostCategoryUpsertOne {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &PostCategoryUpsertOne{
		create: _c,
	}
}

type (
	// PostCategoryUpsertOne is the builder for "upsert"-ing
	//  one PostCategory node.
	PostCategoryUpsertOne struct {
		create *PostCategoryCreate
	}

	// PostCategoryUpsert is the "OnConflict" setter.
	PostCategoryUpsert struct {
		*sql.UpdateSet
	}
)

// SetDeletedAt sets the "deleted_at" field.
func (u *PostCategoryUpsert) SetDeletedAt(v time.Time) *PostCategoryUpsert {
	u.Set(postcategory.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *PostCategoryUpsert) UpdateDeletedAt() *PostCategoryUpsert {
	u.SetExcluded(postcategory.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *PostCategoryUpsert) ClearDeletedAt() *PostCategoryUpsert {
	u.SetNull(postcategory.FieldDeletedAt)
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PostCategoryUpsert) SetUpdatedAt(v time.Time) *PostCategoryUpsert {
	u.Set(postcategory.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PostCategoryUpsert) UpdateUpdatedAt() *PostCategoryUpsert {
	u.SetExcluded(postcategory.FieldUpdatedAt)
	return u
}

// SetName sets the "name" field.
func (u *PostCategoryUpsert) SetName(v string) *PostCategoryUpsert {
	u.Set(postcategory.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *PostCategoryUpsert) UpdateName() *PostCategoryUpsert {
	u.SetExcluded(postcategory.FieldName)
	return u
}

// SetDescription sets the "description" field.
func (u *PostCategoryUpsert) SetDescription(v string) *PostCategoryUpsert {
	u.Set(postcategory.FieldDescription, v)
	return u
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *PostCategoryUpsert) UpdateDescription() *PostCategoryUpsert {
	u.SetExcluded(postcategory.FieldDescription)
	return u
}

// ClearDescription clears the value of the "description" field.
func (u *PostCategoryUpsert) ClearDescription() *PostCategoryUpsert {
	u.SetNull(postcategory.FieldDescription)
	return u
}

// SetCount sets the "count" field.
func (u *PostCategoryUpsert) SetCount(v int) *PostCategoryUpsert {
	u.Set(postcategory.FieldCount, v)
	return u
}

// UpdateCount sets the "count" field to the value that was provided on create.
func (u *PostCategoryUpsert) UpdateCount() *PostCategoryUpsert {
	u.SetExcluded(postcategory.FieldCount)
	return u
}

// AddCount adds v to the "count" field.
func (u *PostCategoryUpsert) AddCount(v int) *PostCategoryUpsert {
	u.Add(postcategory.FieldCount, v)
	return u
}

// SetIsSeries sets the "is_series" field.
func (u *PostCategoryUpsert) SetIsSeries(v bool) *PostCategoryUpsert {
	u.Set(postcategory.FieldIsSeries, v)
	return u
}

// UpdateIsSeries sets the "is_series" field to the value that was provided on create.
func (u *PostCategoryUpsert) UpdateIsSeries() *PostCategoryUpsert {
	u.SetExcluded(postcategory.FieldIsSeries)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.PostCategory.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(postcategory.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *PostCategoryUpsertOne) UpdateNewValues() *PostCategoryUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(postcategory.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(postcategory.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.PostCategory.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *PostCategoryUpsertOne) Ignore() *PostCategoryUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PostCategoryUpsertOne) DoNothing() *PostCategoryUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the PostCategoryCreate.OnConflict
// documentation for more info.
func (u *PostCategoryUpsertOne) Update(set func(*PostCategoryUpsert)) *PostCategoryUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&PostCategoryUpsert{UpdateSet: update})
	}))
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *PostCategoryUpsertOne) SetDeletedAt(v time.Time) *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *PostCategoryUpsertOne) UpdateDeletedAt() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *PostCategoryUpsertOne) ClearDeletedAt() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PostCategoryUpsertOne) SetUpdatedAt(v time.Time) *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PostCategoryUpsertOne) UpdateUpdatedAt() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetName sets the "name" field.
func (u *PostCategoryUpsertOne) SetName(v string) *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *PostCategoryUpsertOne) UpdateName() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateName()
	})
}

// SetDescription sets the "description" field.
func (u *PostCategoryUpsertOne) SetDescription(v string) *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetDescription(v)
	})
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *PostCategoryUpsertOne) UpdateDescription() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateDescription()
	})
}

// ClearDescription clears the value of the "description" field.
func (u *PostCategoryUpsertOne) ClearDescription() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.ClearDescription()
	})
}

// SetCount sets the "count" field.
func (u *PostCategoryUpsertOne) SetCount(v int) *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetCount(v)
	})
}

// AddCount adds v to the "count" field.
func (u *PostCategoryUpsertOne) AddCount(v int) *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.AddCount(v)
	})
}

// UpdateCount sets the "count" field to the value that was provided on create.
func (u *PostCategoryUpsertOne) UpdateCount() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateCount()
	})
}

// SetIsSeries sets the "is_series" field.
func (u *PostCategoryUpsertOne) SetIsSeries(v bool) *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetIsSeries(v)
	})
}

// UpdateIsSeries sets the "is_series" field to the value that was provided on create.
func (u *PostCategoryUpsertOne) UpdateIsSeries() *PostCategoryUpsertOne {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateIsSeries()
	})
}

// Exec executes the query.
func (u *PostCategoryUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for PostCategoryCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *PostCategoryUpsertOne) ID(ctx context.Context) (id uint, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *PostCategoryUpsertOne) IDX(ctx context.Context) uint {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// PostCategoryCreateBulk is the builder for creating many PostCategory entities in bulk.
type PostCategoryCreateBulk struct {
	config
	err      error
	builders []*PostCategoryCreate
	conflict []sql.ConflictOption
}

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

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.PostCategory.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.PostCategoryUpsert) {
//			SetDeletedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *PostCategoryCreateBulk) OnConflict(opts ...sql.ConflictOption) *PostCategoryUpsertBulk {
	_c.conflict = opts
	return &PostCategoryUpsertBulk{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.PostCategory.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *PostCategoryCreateBulk) OnConflictColumns(columns ...string) *PostCategoryUpsertBulk {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &PostCategoryUpsertBulk{
		create: _c,
	}
}

// PostCategoryUpsertBulk is the builder for "upsert"-ing
// a bulk of PostCategory nodes.
type PostCategoryUpsertBulk struct {
	create *PostCategoryCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.PostCategory.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(postcategory.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *PostCategoryUpsertBulk) UpdateNewValues() *PostCategoryUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(postcategory.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(postcategory.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.PostCategory.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *PostCategoryUpsertBulk) Ignore() *PostCategoryUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PostCategoryUpsertBulk) DoNothing() *PostCategoryUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the PostCategoryCreateBulk.OnConflict
// documentation for more info.
func (u *PostCategoryUpsertBulk) Update(set func(*PostCategoryUpsert)) *PostCategoryUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&PostCategoryUpsert{UpdateSet: update})
	}))
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *PostCategoryUpsertBulk) SetDeletedAt(v time.Time) *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *PostCategoryUpsertBulk) UpdateDeletedAt() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *PostCategoryUpsertBulk) ClearDeletedAt() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUpdatedAt sets the "updated_at" field.
func (u *PostCategoryUpsertBulk) SetUpdatedAt(v time.Time) *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *PostCategoryUpsertBulk) UpdateUpdatedAt() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetName sets the "name" field.
func (u *PostCategoryUpsertBulk) SetName(v string) *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *PostCategoryUpsertBulk) UpdateName() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateName()
	})
}

// SetDescription sets the "description" field.
func (u *PostCategoryUpsertBulk) SetDescription(v string) *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetDescription(v)
	})
}

// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *PostCategoryUpsertBulk) UpdateDescription() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateDescription()
	})
}

// ClearDescription clears the value of the "description" field.
func (u *PostCategoryUpsertBulk) ClearDescription() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.ClearDescription()
	})
}

// SetCount sets the "count" field.
func (u *PostCategoryUpsertBulk) SetCount(v int) *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetCount(v)
	})
}

// AddCount adds v to the "count" field.
func (u *PostCategoryUpsertBulk) AddCount(v int) *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.AddCount(v)
	})
}

// UpdateCount sets the "count" field to the value that was provided on create.
func (u *PostCategoryUpsertBulk) UpdateCount() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateCount()
	})
}

// SetIsSeries sets the "is_series" field.
func (u *PostCategoryUpsertBulk) SetIsSeries(v bool) *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.SetIsSeries(v)
	})
}

// UpdateIsSeries sets the "is_series" field to the value that was provided on create.
func (u *PostCategoryUpsertBulk) UpdateIsSeries() *PostCategoryUpsertBulk {
	return u.Update(func(s *PostCategoryUpsert) {
		s.UpdateIsSeries()
	})
}

// Exec executes the query.
func (u *PostCategoryUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PostCategoryCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for PostCategoryCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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