// Code generated by entc, DO NOT EDIT.

package ent

import (
	"context"
	"fmt"

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

	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/internal"
	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/predicate"
	"github.com/pingcap/test-infra/services/tcms/service/store/v2/ent/resourceduration"
)

// ResourceDurationDelete is the builder for deleting a ResourceDuration entity.
type ResourceDurationDelete struct {
	config
	hooks    []Hook
	mutation *ResourceDurationMutation
}

// Where appends a list predicates to the ResourceDurationDelete builder.
func (rdd *ResourceDurationDelete) Where(ps ...predicate.ResourceDuration) *ResourceDurationDelete {
	rdd.mutation.Where(ps...)
	return rdd
}

// Exec executes the deletion query and returns how many vertices were deleted.
func (rdd *ResourceDurationDelete) Exec(ctx context.Context) (int, error) {
	var (
		err      error
		affected int
	)
	if len(rdd.hooks) == 0 {
		affected, err = rdd.sqlExec(ctx)
	} else {
		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
			mutation, ok := m.(*ResourceDurationMutation)
			if !ok {
				return nil, fmt.Errorf("unexpected mutation type %T", m)
			}
			rdd.mutation = mutation
			affected, err = rdd.sqlExec(ctx)
			mutation.done = true
			return affected, err
		})
		for i := len(rdd.hooks) - 1; i >= 0; i-- {
			if rdd.hooks[i] == nil {
				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
			}
			mut = rdd.hooks[i](mut)
		}
		if _, err := mut.Mutate(ctx, rdd.mutation); err != nil {
			return 0, err
		}
	}
	return affected, err
}

// ExecX is like Exec, but panics if an error occurs.
func (rdd *ResourceDurationDelete) ExecX(ctx context.Context) int {
	n, err := rdd.Exec(ctx)
	if err != nil {
		panic(err)
	}
	return n
}

func (rdd *ResourceDurationDelete) sqlExec(ctx context.Context) (int, error) {
	_spec := &sqlgraph.DeleteSpec{
		Node: &sqlgraph.NodeSpec{
			Table: resourceduration.Table,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeUint64,
				Column: resourceduration.FieldID,
			},
		},
	}
	_spec.Node.Schema = rdd.schemaConfig.ResourceDuration
	ctx = internal.NewSchemaConfigContext(ctx, rdd.schemaConfig)
	if ps := rdd.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return sqlgraph.DeleteNodes(ctx, rdd.driver, _spec)
}

// ResourceDurationDeleteOne is the builder for deleting a single ResourceDuration entity.
type ResourceDurationDeleteOne struct {
	rdd *ResourceDurationDelete
}

// Exec executes the deletion query.
func (rddo *ResourceDurationDeleteOne) Exec(ctx context.Context) error {
	n, err := rddo.rdd.Exec(ctx)
	switch {
	case err != nil:
		return err
	case n == 0:
		return &NotFoundError{resourceduration.Label}
	default:
		return nil
	}
}

// ExecX is like Exec, but panics if an error occurs.
func (rddo *ResourceDurationDeleteOne) ExecX(ctx context.Context) {
	rddo.rdd.ExecX(ctx)
}
