// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/labeltagging"
	"wechat-api/ent/predicate"

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

// LabelTaggingUpdate is the builder for updating LabelTagging entities.
type LabelTaggingUpdate struct {
	config
	hooks    []Hook
	mutation *LabelTaggingMutation
}

// Where appends a list predicates to the LabelTaggingUpdate builder.
func (ltu *LabelTaggingUpdate) Where(ps ...predicate.LabelTagging) *LabelTaggingUpdate {
	ltu.mutation.Where(ps...)
	return ltu
}

// SetUpdatedAt sets the "updated_at" field.
func (ltu *LabelTaggingUpdate) SetUpdatedAt(t time.Time) *LabelTaggingUpdate {
	ltu.mutation.SetUpdatedAt(t)
	return ltu
}

// SetStatus sets the "status" field.
func (ltu *LabelTaggingUpdate) SetStatus(u uint8) *LabelTaggingUpdate {
	ltu.mutation.ResetStatus()
	ltu.mutation.SetStatus(u)
	return ltu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (ltu *LabelTaggingUpdate) SetNillableStatus(u *uint8) *LabelTaggingUpdate {
	if u != nil {
		ltu.SetStatus(*u)
	}
	return ltu
}

// AddStatus adds u to the "status" field.
func (ltu *LabelTaggingUpdate) AddStatus(u int8) *LabelTaggingUpdate {
	ltu.mutation.AddStatus(u)
	return ltu
}

// ClearStatus clears the value of the "status" field.
func (ltu *LabelTaggingUpdate) ClearStatus() *LabelTaggingUpdate {
	ltu.mutation.ClearStatus()
	return ltu
}

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (ltu *LabelTaggingUpdate) ClearDeletedAt() *LabelTaggingUpdate {
	ltu.mutation.ClearDeletedAt()
	return ltu
}

// SetOrganizationID sets the "organization_id" field.
func (ltu *LabelTaggingUpdate) SetOrganizationID(u uint64) *LabelTaggingUpdate {
	ltu.mutation.ResetOrganizationID()
	ltu.mutation.SetOrganizationID(u)
	return ltu
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (ltu *LabelTaggingUpdate) SetNillableOrganizationID(u *uint64) *LabelTaggingUpdate {
	if u != nil {
		ltu.SetOrganizationID(*u)
	}
	return ltu
}

// AddOrganizationID adds u to the "organization_id" field.
func (ltu *LabelTaggingUpdate) AddOrganizationID(u int64) *LabelTaggingUpdate {
	ltu.mutation.AddOrganizationID(u)
	return ltu
}

// SetType sets the "type" field.
func (ltu *LabelTaggingUpdate) SetType(i int) *LabelTaggingUpdate {
	ltu.mutation.ResetType()
	ltu.mutation.SetType(i)
	return ltu
}

// SetNillableType sets the "type" field if the given value is not nil.
func (ltu *LabelTaggingUpdate) SetNillableType(i *int) *LabelTaggingUpdate {
	if i != nil {
		ltu.SetType(*i)
	}
	return ltu
}

// AddType adds i to the "type" field.
func (ltu *LabelTaggingUpdate) AddType(i int) *LabelTaggingUpdate {
	ltu.mutation.AddType(i)
	return ltu
}

// SetConditions sets the "conditions" field.
func (ltu *LabelTaggingUpdate) SetConditions(s string) *LabelTaggingUpdate {
	ltu.mutation.SetConditions(s)
	return ltu
}

// SetNillableConditions sets the "conditions" field if the given value is not nil.
func (ltu *LabelTaggingUpdate) SetNillableConditions(s *string) *LabelTaggingUpdate {
	if s != nil {
		ltu.SetConditions(*s)
	}
	return ltu
}

// SetActionLabelAdd sets the "action_label_add" field.
func (ltu *LabelTaggingUpdate) SetActionLabelAdd(u []uint64) *LabelTaggingUpdate {
	ltu.mutation.SetActionLabelAdd(u)
	return ltu
}

// AppendActionLabelAdd appends u to the "action_label_add" field.
func (ltu *LabelTaggingUpdate) AppendActionLabelAdd(u []uint64) *LabelTaggingUpdate {
	ltu.mutation.AppendActionLabelAdd(u)
	return ltu
}

// ClearActionLabelAdd clears the value of the "action_label_add" field.
func (ltu *LabelTaggingUpdate) ClearActionLabelAdd() *LabelTaggingUpdate {
	ltu.mutation.ClearActionLabelAdd()
	return ltu
}

// SetActionLabelDel sets the "action_label_del" field.
func (ltu *LabelTaggingUpdate) SetActionLabelDel(u []uint64) *LabelTaggingUpdate {
	ltu.mutation.SetActionLabelDel(u)
	return ltu
}

// AppendActionLabelDel appends u to the "action_label_del" field.
func (ltu *LabelTaggingUpdate) AppendActionLabelDel(u []uint64) *LabelTaggingUpdate {
	ltu.mutation.AppendActionLabelDel(u)
	return ltu
}

// ClearActionLabelDel clears the value of the "action_label_del" field.
func (ltu *LabelTaggingUpdate) ClearActionLabelDel() *LabelTaggingUpdate {
	ltu.mutation.ClearActionLabelDel()
	return ltu
}

// Mutation returns the LabelTaggingMutation object of the builder.
func (ltu *LabelTaggingUpdate) Mutation() *LabelTaggingMutation {
	return ltu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (ltu *LabelTaggingUpdate) Save(ctx context.Context) (int, error) {
	if err := ltu.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, ltu.sqlSave, ltu.mutation, ltu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (ltu *LabelTaggingUpdate) SaveX(ctx context.Context) int {
	affected, err := ltu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

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

// defaults sets the default values of the builder before save.
func (ltu *LabelTaggingUpdate) defaults() error {
	if _, ok := ltu.mutation.UpdatedAt(); !ok {
		if labeltagging.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized labeltagging.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := labeltagging.UpdateDefaultUpdatedAt()
		ltu.mutation.SetUpdatedAt(v)
	}
	return nil
}

func (ltu *LabelTaggingUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(labeltagging.Table, labeltagging.Columns, sqlgraph.NewFieldSpec(labeltagging.FieldID, field.TypeUint64))
	if ps := ltu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ltu.mutation.UpdatedAt(); ok {
		_spec.SetField(labeltagging.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := ltu.mutation.Status(); ok {
		_spec.SetField(labeltagging.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := ltu.mutation.AddedStatus(); ok {
		_spec.AddField(labeltagging.FieldStatus, field.TypeUint8, value)
	}
	if ltu.mutation.StatusCleared() {
		_spec.ClearField(labeltagging.FieldStatus, field.TypeUint8)
	}
	if value, ok := ltu.mutation.DeletedAt(); ok {
		_spec.SetField(labeltagging.FieldDeletedAt, field.TypeTime, value)
	}
	if ltu.mutation.DeletedAtCleared() {
		_spec.ClearField(labeltagging.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := ltu.mutation.OrganizationID(); ok {
		_spec.SetField(labeltagging.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := ltu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(labeltagging.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := ltu.mutation.GetType(); ok {
		_spec.SetField(labeltagging.FieldType, field.TypeInt, value)
	}
	if value, ok := ltu.mutation.AddedType(); ok {
		_spec.AddField(labeltagging.FieldType, field.TypeInt, value)
	}
	if value, ok := ltu.mutation.Conditions(); ok {
		_spec.SetField(labeltagging.FieldConditions, field.TypeString, value)
	}
	if value, ok := ltu.mutation.ActionLabelAdd(); ok {
		_spec.SetField(labeltagging.FieldActionLabelAdd, field.TypeJSON, value)
	}
	if value, ok := ltu.mutation.AppendedActionLabelAdd(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, labeltagging.FieldActionLabelAdd, value)
		})
	}
	if ltu.mutation.ActionLabelAddCleared() {
		_spec.ClearField(labeltagging.FieldActionLabelAdd, field.TypeJSON)
	}
	if value, ok := ltu.mutation.ActionLabelDel(); ok {
		_spec.SetField(labeltagging.FieldActionLabelDel, field.TypeJSON, value)
	}
	if value, ok := ltu.mutation.AppendedActionLabelDel(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, labeltagging.FieldActionLabelDel, value)
		})
	}
	if ltu.mutation.ActionLabelDelCleared() {
		_spec.ClearField(labeltagging.FieldActionLabelDel, field.TypeJSON)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ltu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{labeltagging.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ltu.mutation.done = true
	return n, nil
}

// LabelTaggingUpdateOne is the builder for updating a single LabelTagging entity.
type LabelTaggingUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *LabelTaggingMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (ltuo *LabelTaggingUpdateOne) SetUpdatedAt(t time.Time) *LabelTaggingUpdateOne {
	ltuo.mutation.SetUpdatedAt(t)
	return ltuo
}

// SetStatus sets the "status" field.
func (ltuo *LabelTaggingUpdateOne) SetStatus(u uint8) *LabelTaggingUpdateOne {
	ltuo.mutation.ResetStatus()
	ltuo.mutation.SetStatus(u)
	return ltuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (ltuo *LabelTaggingUpdateOne) SetNillableStatus(u *uint8) *LabelTaggingUpdateOne {
	if u != nil {
		ltuo.SetStatus(*u)
	}
	return ltuo
}

// AddStatus adds u to the "status" field.
func (ltuo *LabelTaggingUpdateOne) AddStatus(u int8) *LabelTaggingUpdateOne {
	ltuo.mutation.AddStatus(u)
	return ltuo
}

// ClearStatus clears the value of the "status" field.
func (ltuo *LabelTaggingUpdateOne) ClearStatus() *LabelTaggingUpdateOne {
	ltuo.mutation.ClearStatus()
	return ltuo
}

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

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

// ClearDeletedAt clears the value of the "deleted_at" field.
func (ltuo *LabelTaggingUpdateOne) ClearDeletedAt() *LabelTaggingUpdateOne {
	ltuo.mutation.ClearDeletedAt()
	return ltuo
}

// SetOrganizationID sets the "organization_id" field.
func (ltuo *LabelTaggingUpdateOne) SetOrganizationID(u uint64) *LabelTaggingUpdateOne {
	ltuo.mutation.ResetOrganizationID()
	ltuo.mutation.SetOrganizationID(u)
	return ltuo
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (ltuo *LabelTaggingUpdateOne) SetNillableOrganizationID(u *uint64) *LabelTaggingUpdateOne {
	if u != nil {
		ltuo.SetOrganizationID(*u)
	}
	return ltuo
}

// AddOrganizationID adds u to the "organization_id" field.
func (ltuo *LabelTaggingUpdateOne) AddOrganizationID(u int64) *LabelTaggingUpdateOne {
	ltuo.mutation.AddOrganizationID(u)
	return ltuo
}

// SetType sets the "type" field.
func (ltuo *LabelTaggingUpdateOne) SetType(i int) *LabelTaggingUpdateOne {
	ltuo.mutation.ResetType()
	ltuo.mutation.SetType(i)
	return ltuo
}

// SetNillableType sets the "type" field if the given value is not nil.
func (ltuo *LabelTaggingUpdateOne) SetNillableType(i *int) *LabelTaggingUpdateOne {
	if i != nil {
		ltuo.SetType(*i)
	}
	return ltuo
}

// AddType adds i to the "type" field.
func (ltuo *LabelTaggingUpdateOne) AddType(i int) *LabelTaggingUpdateOne {
	ltuo.mutation.AddType(i)
	return ltuo
}

// SetConditions sets the "conditions" field.
func (ltuo *LabelTaggingUpdateOne) SetConditions(s string) *LabelTaggingUpdateOne {
	ltuo.mutation.SetConditions(s)
	return ltuo
}

// SetNillableConditions sets the "conditions" field if the given value is not nil.
func (ltuo *LabelTaggingUpdateOne) SetNillableConditions(s *string) *LabelTaggingUpdateOne {
	if s != nil {
		ltuo.SetConditions(*s)
	}
	return ltuo
}

// SetActionLabelAdd sets the "action_label_add" field.
func (ltuo *LabelTaggingUpdateOne) SetActionLabelAdd(u []uint64) *LabelTaggingUpdateOne {
	ltuo.mutation.SetActionLabelAdd(u)
	return ltuo
}

// AppendActionLabelAdd appends u to the "action_label_add" field.
func (ltuo *LabelTaggingUpdateOne) AppendActionLabelAdd(u []uint64) *LabelTaggingUpdateOne {
	ltuo.mutation.AppendActionLabelAdd(u)
	return ltuo
}

// ClearActionLabelAdd clears the value of the "action_label_add" field.
func (ltuo *LabelTaggingUpdateOne) ClearActionLabelAdd() *LabelTaggingUpdateOne {
	ltuo.mutation.ClearActionLabelAdd()
	return ltuo
}

// SetActionLabelDel sets the "action_label_del" field.
func (ltuo *LabelTaggingUpdateOne) SetActionLabelDel(u []uint64) *LabelTaggingUpdateOne {
	ltuo.mutation.SetActionLabelDel(u)
	return ltuo
}

// AppendActionLabelDel appends u to the "action_label_del" field.
func (ltuo *LabelTaggingUpdateOne) AppendActionLabelDel(u []uint64) *LabelTaggingUpdateOne {
	ltuo.mutation.AppendActionLabelDel(u)
	return ltuo
}

// ClearActionLabelDel clears the value of the "action_label_del" field.
func (ltuo *LabelTaggingUpdateOne) ClearActionLabelDel() *LabelTaggingUpdateOne {
	ltuo.mutation.ClearActionLabelDel()
	return ltuo
}

// Mutation returns the LabelTaggingMutation object of the builder.
func (ltuo *LabelTaggingUpdateOne) Mutation() *LabelTaggingMutation {
	return ltuo.mutation
}

// Where appends a list predicates to the LabelTaggingUpdate builder.
func (ltuo *LabelTaggingUpdateOne) Where(ps ...predicate.LabelTagging) *LabelTaggingUpdateOne {
	ltuo.mutation.Where(ps...)
	return ltuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (ltuo *LabelTaggingUpdateOne) Select(field string, fields ...string) *LabelTaggingUpdateOne {
	ltuo.fields = append([]string{field}, fields...)
	return ltuo
}

// Save executes the query and returns the updated LabelTagging entity.
func (ltuo *LabelTaggingUpdateOne) Save(ctx context.Context) (*LabelTagging, error) {
	if err := ltuo.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, ltuo.sqlSave, ltuo.mutation, ltuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (ltuo *LabelTaggingUpdateOne) SaveX(ctx context.Context) *LabelTagging {
	node, err := ltuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (ltuo *LabelTaggingUpdateOne) Exec(ctx context.Context) error {
	_, err := ltuo.Save(ctx)
	return err
}

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

// defaults sets the default values of the builder before save.
func (ltuo *LabelTaggingUpdateOne) defaults() error {
	if _, ok := ltuo.mutation.UpdatedAt(); !ok {
		if labeltagging.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized labeltagging.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := labeltagging.UpdateDefaultUpdatedAt()
		ltuo.mutation.SetUpdatedAt(v)
	}
	return nil
}

func (ltuo *LabelTaggingUpdateOne) sqlSave(ctx context.Context) (_node *LabelTagging, err error) {
	_spec := sqlgraph.NewUpdateSpec(labeltagging.Table, labeltagging.Columns, sqlgraph.NewFieldSpec(labeltagging.FieldID, field.TypeUint64))
	id, ok := ltuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "LabelTagging.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := ltuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, labeltagging.FieldID)
		for _, f := range fields {
			if !labeltagging.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != labeltagging.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := ltuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ltuo.mutation.UpdatedAt(); ok {
		_spec.SetField(labeltagging.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := ltuo.mutation.Status(); ok {
		_spec.SetField(labeltagging.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := ltuo.mutation.AddedStatus(); ok {
		_spec.AddField(labeltagging.FieldStatus, field.TypeUint8, value)
	}
	if ltuo.mutation.StatusCleared() {
		_spec.ClearField(labeltagging.FieldStatus, field.TypeUint8)
	}
	if value, ok := ltuo.mutation.DeletedAt(); ok {
		_spec.SetField(labeltagging.FieldDeletedAt, field.TypeTime, value)
	}
	if ltuo.mutation.DeletedAtCleared() {
		_spec.ClearField(labeltagging.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := ltuo.mutation.OrganizationID(); ok {
		_spec.SetField(labeltagging.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := ltuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(labeltagging.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := ltuo.mutation.GetType(); ok {
		_spec.SetField(labeltagging.FieldType, field.TypeInt, value)
	}
	if value, ok := ltuo.mutation.AddedType(); ok {
		_spec.AddField(labeltagging.FieldType, field.TypeInt, value)
	}
	if value, ok := ltuo.mutation.Conditions(); ok {
		_spec.SetField(labeltagging.FieldConditions, field.TypeString, value)
	}
	if value, ok := ltuo.mutation.ActionLabelAdd(); ok {
		_spec.SetField(labeltagging.FieldActionLabelAdd, field.TypeJSON, value)
	}
	if value, ok := ltuo.mutation.AppendedActionLabelAdd(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, labeltagging.FieldActionLabelAdd, value)
		})
	}
	if ltuo.mutation.ActionLabelAddCleared() {
		_spec.ClearField(labeltagging.FieldActionLabelAdd, field.TypeJSON)
	}
	if value, ok := ltuo.mutation.ActionLabelDel(); ok {
		_spec.SetField(labeltagging.FieldActionLabelDel, field.TypeJSON, value)
	}
	if value, ok := ltuo.mutation.AppendedActionLabelDel(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, labeltagging.FieldActionLabelDel, value)
		})
	}
	if ltuo.mutation.ActionLabelDelCleared() {
		_spec.ClearField(labeltagging.FieldActionLabelDel, field.TypeJSON)
	}
	_node = &LabelTagging{config: ltuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, ltuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{labeltagging.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	ltuo.mutation.done = true
	return _node, nil
}
