// Code generated by ent, DO NOT EDIT.

package ent

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

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

// CreditBalanceDelete is the builder for deleting a CreditBalance entity.
type CreditBalanceDelete struct {
	config
	hooks    []Hook
	mutation *CreditBalanceMutation
}

// Where appends a list predicates to the CreditBalanceDelete builder.
func (cbd *CreditBalanceDelete) Where(ps ...predicate.CreditBalance) *CreditBalanceDelete {
	cbd.mutation.Where(ps...)
	return cbd
}

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

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

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

// CreditBalanceDeleteOne is the builder for deleting a single CreditBalance entity.
type CreditBalanceDeleteOne struct {
	cbd *CreditBalanceDelete
}

// Where appends a list predicates to the CreditBalanceDelete builder.
func (cbdo *CreditBalanceDeleteOne) Where(ps ...predicate.CreditBalance) *CreditBalanceDeleteOne {
	cbdo.cbd.mutation.Where(ps...)
	return cbdo
}

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

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