// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"wechat-api/ent/creditusage"
	"wechat-api/ent/predicate"

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

// CreditUsageDelete is the builder for deleting a CreditUsage entity.
type CreditUsageDelete struct {
	config
	hooks    []Hook
	mutation *CreditUsageMutation
}

// Where appends a list predicates to the CreditUsageDelete builder.
func (cud *CreditUsageDelete) Where(ps ...predicate.CreditUsage) *CreditUsageDelete {
	cud.mutation.Where(ps...)
	return cud
}

// Exec executes the deletion query and returns how many vertices were deleted.
func (cud *CreditUsageDelete) Exec(ctx context.Context) (int, error) {
	return withHooks(ctx, cud.sqlExec, cud.mutation, cud.hooks)
}

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

func (cud *CreditUsageDelete) sqlExec(ctx context.Context) (int, error) {
	_spec := sqlgraph.NewDeleteSpec(creditusage.Table, sqlgraph.NewFieldSpec(creditusage.FieldID, field.TypeUint64))
	if ps := cud.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	affected, err := sqlgraph.DeleteNodes(ctx, cud.driver, _spec)
	if err != nil && sqlgraph.IsConstraintError(err) {
		err = &ConstraintError{msg: err.Error(), wrap: err}
	}
	cud.mutation.done = true
	return affected, err
}

// CreditUsageDeleteOne is the builder for deleting a single CreditUsage entity.
type CreditUsageDeleteOne struct {
	cud *CreditUsageDelete
}

// Where appends a list predicates to the CreditUsageDelete builder.
func (cudo *CreditUsageDeleteOne) Where(ps ...predicate.CreditUsage) *CreditUsageDeleteOne {
	cudo.cud.mutation.Where(ps...)
	return cudo
}

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

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