// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/metaevent"
	"galileo/ent/predicate"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
)

// MetaEventUpdate is the builder for updating MetaEvent entities.
type MetaEventUpdate struct {
	config
	hooks    []Hook
	mutation *MetaEventMutation
}

// Where appends a list predicates to the MetaEventUpdate builder.
func (meu *MetaEventUpdate) Where(ps ...predicate.MetaEvent) *MetaEventUpdate {
	meu.mutation.Where(ps...)
	return meu
}

// SetEventName sets the "event_name" field.
func (meu *MetaEventUpdate) SetEventName(s string) *MetaEventUpdate {
	meu.mutation.SetEventName(s)
	return meu
}

// SetUUID sets the "uuid" field.
func (meu *MetaEventUpdate) SetUUID(u uuid.UUID) *MetaEventUpdate {
	meu.mutation.SetUUID(u)
	return meu
}

// SetNillableUUID sets the "uuid" field if the given value is not nil.
func (meu *MetaEventUpdate) SetNillableUUID(u *uuid.UUID) *MetaEventUpdate {
	if u != nil {
		meu.SetUUID(*u)
	}
	return meu
}

// SetDisplayName sets the "display_name" field.
func (meu *MetaEventUpdate) SetDisplayName(s string) *MetaEventUpdate {
	meu.mutation.SetDisplayName(s)
	return meu
}

// SetEventDesc sets the "event_desc" field.
func (meu *MetaEventUpdate) SetEventDesc(s string) *MetaEventUpdate {
	meu.mutation.SetEventDesc(s)
	return meu
}

// SetNillableEventDesc sets the "event_desc" field if the given value is not nil.
func (meu *MetaEventUpdate) SetNillableEventDesc(s *string) *MetaEventUpdate {
	if s != nil {
		meu.SetEventDesc(*s)
	}
	return meu
}

// ClearEventDesc clears the value of the "event_desc" field.
func (meu *MetaEventUpdate) ClearEventDesc() *MetaEventUpdate {
	meu.mutation.ClearEventDesc()
	return meu
}

// SetRemark sets the "remark" field.
func (meu *MetaEventUpdate) SetRemark(s string) *MetaEventUpdate {
	meu.mutation.SetRemark(s)
	return meu
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (meu *MetaEventUpdate) SetNillableRemark(s *string) *MetaEventUpdate {
	if s != nil {
		meu.SetRemark(*s)
	}
	return meu
}

// ClearRemark clears the value of the "remark" field.
func (meu *MetaEventUpdate) ClearRemark() *MetaEventUpdate {
	meu.mutation.ClearRemark()
	return meu
}

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

// ClearUpdatedAt clears the value of the "updated_at" field.
func (meu *MetaEventUpdate) ClearUpdatedAt() *MetaEventUpdate {
	meu.mutation.ClearUpdatedAt()
	return meu
}

// Mutation returns the MetaEventMutation object of the builder.
func (meu *MetaEventUpdate) Mutation() *MetaEventMutation {
	return meu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (meu *MetaEventUpdate) Save(ctx context.Context) (int, error) {
	meu.defaults()
	return withHooks[int, MetaEventMutation](ctx, meu.sqlSave, meu.mutation, meu.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (meu *MetaEventUpdate) defaults() {
	if _, ok := meu.mutation.UpdatedAt(); !ok && !meu.mutation.UpdatedAtCleared() {
		v := metaevent.UpdateDefaultUpdatedAt()
		meu.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (meu *MetaEventUpdate) check() error {
	if v, ok := meu.mutation.EventName(); ok {
		if err := metaevent.EventNameValidator(v); err != nil {
			return &ValidationError{Name: "event_name", err: fmt.Errorf(`ent: validator failed for field "MetaEvent.event_name": %w`, err)}
		}
	}
	if v, ok := meu.mutation.DisplayName(); ok {
		if err := metaevent.DisplayNameValidator(v); err != nil {
			return &ValidationError{Name: "display_name", err: fmt.Errorf(`ent: validator failed for field "MetaEvent.display_name": %w`, err)}
		}
	}
	return nil
}

func (meu *MetaEventUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := meu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(metaevent.Table, metaevent.Columns, sqlgraph.NewFieldSpec(metaevent.FieldID, field.TypeInt64))
	if ps := meu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := meu.mutation.EventName(); ok {
		_spec.SetField(metaevent.FieldEventName, field.TypeString, value)
	}
	if value, ok := meu.mutation.UUID(); ok {
		_spec.SetField(metaevent.FieldUUID, field.TypeUUID, value)
	}
	if value, ok := meu.mutation.DisplayName(); ok {
		_spec.SetField(metaevent.FieldDisplayName, field.TypeString, value)
	}
	if value, ok := meu.mutation.EventDesc(); ok {
		_spec.SetField(metaevent.FieldEventDesc, field.TypeString, value)
	}
	if meu.mutation.EventDescCleared() {
		_spec.ClearField(metaevent.FieldEventDesc, field.TypeString)
	}
	if value, ok := meu.mutation.Remark(); ok {
		_spec.SetField(metaevent.FieldRemark, field.TypeString, value)
	}
	if meu.mutation.RemarkCleared() {
		_spec.ClearField(metaevent.FieldRemark, field.TypeString)
	}
	if value, ok := meu.mutation.UpdatedAt(); ok {
		_spec.SetField(metaevent.FieldUpdatedAt, field.TypeTime, value)
	}
	if meu.mutation.UpdatedAtCleared() {
		_spec.ClearField(metaevent.FieldUpdatedAt, field.TypeTime)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, meu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{metaevent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	meu.mutation.done = true
	return n, nil
}

// MetaEventUpdateOne is the builder for updating a single MetaEvent entity.
type MetaEventUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MetaEventMutation
}

// SetEventName sets the "event_name" field.
func (meuo *MetaEventUpdateOne) SetEventName(s string) *MetaEventUpdateOne {
	meuo.mutation.SetEventName(s)
	return meuo
}

// SetUUID sets the "uuid" field.
func (meuo *MetaEventUpdateOne) SetUUID(u uuid.UUID) *MetaEventUpdateOne {
	meuo.mutation.SetUUID(u)
	return meuo
}

// SetNillableUUID sets the "uuid" field if the given value is not nil.
func (meuo *MetaEventUpdateOne) SetNillableUUID(u *uuid.UUID) *MetaEventUpdateOne {
	if u != nil {
		meuo.SetUUID(*u)
	}
	return meuo
}

// SetDisplayName sets the "display_name" field.
func (meuo *MetaEventUpdateOne) SetDisplayName(s string) *MetaEventUpdateOne {
	meuo.mutation.SetDisplayName(s)
	return meuo
}

// SetEventDesc sets the "event_desc" field.
func (meuo *MetaEventUpdateOne) SetEventDesc(s string) *MetaEventUpdateOne {
	meuo.mutation.SetEventDesc(s)
	return meuo
}

// SetNillableEventDesc sets the "event_desc" field if the given value is not nil.
func (meuo *MetaEventUpdateOne) SetNillableEventDesc(s *string) *MetaEventUpdateOne {
	if s != nil {
		meuo.SetEventDesc(*s)
	}
	return meuo
}

// ClearEventDesc clears the value of the "event_desc" field.
func (meuo *MetaEventUpdateOne) ClearEventDesc() *MetaEventUpdateOne {
	meuo.mutation.ClearEventDesc()
	return meuo
}

// SetRemark sets the "remark" field.
func (meuo *MetaEventUpdateOne) SetRemark(s string) *MetaEventUpdateOne {
	meuo.mutation.SetRemark(s)
	return meuo
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (meuo *MetaEventUpdateOne) SetNillableRemark(s *string) *MetaEventUpdateOne {
	if s != nil {
		meuo.SetRemark(*s)
	}
	return meuo
}

// ClearRemark clears the value of the "remark" field.
func (meuo *MetaEventUpdateOne) ClearRemark() *MetaEventUpdateOne {
	meuo.mutation.ClearRemark()
	return meuo
}

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

// ClearUpdatedAt clears the value of the "updated_at" field.
func (meuo *MetaEventUpdateOne) ClearUpdatedAt() *MetaEventUpdateOne {
	meuo.mutation.ClearUpdatedAt()
	return meuo
}

// Mutation returns the MetaEventMutation object of the builder.
func (meuo *MetaEventUpdateOne) Mutation() *MetaEventMutation {
	return meuo.mutation
}

// Where appends a list predicates to the MetaEventUpdate builder.
func (meuo *MetaEventUpdateOne) Where(ps ...predicate.MetaEvent) *MetaEventUpdateOne {
	meuo.mutation.Where(ps...)
	return meuo
}

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

// Save executes the query and returns the updated MetaEvent entity.
func (meuo *MetaEventUpdateOne) Save(ctx context.Context) (*MetaEvent, error) {
	meuo.defaults()
	return withHooks[*MetaEvent, MetaEventMutation](ctx, meuo.sqlSave, meuo.mutation, meuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (meuo *MetaEventUpdateOne) defaults() {
	if _, ok := meuo.mutation.UpdatedAt(); !ok && !meuo.mutation.UpdatedAtCleared() {
		v := metaevent.UpdateDefaultUpdatedAt()
		meuo.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (meuo *MetaEventUpdateOne) check() error {
	if v, ok := meuo.mutation.EventName(); ok {
		if err := metaevent.EventNameValidator(v); err != nil {
			return &ValidationError{Name: "event_name", err: fmt.Errorf(`ent: validator failed for field "MetaEvent.event_name": %w`, err)}
		}
	}
	if v, ok := meuo.mutation.DisplayName(); ok {
		if err := metaevent.DisplayNameValidator(v); err != nil {
			return &ValidationError{Name: "display_name", err: fmt.Errorf(`ent: validator failed for field "MetaEvent.display_name": %w`, err)}
		}
	}
	return nil
}

func (meuo *MetaEventUpdateOne) sqlSave(ctx context.Context) (_node *MetaEvent, err error) {
	if err := meuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(metaevent.Table, metaevent.Columns, sqlgraph.NewFieldSpec(metaevent.FieldID, field.TypeInt64))
	id, ok := meuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "MetaEvent.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := meuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, metaevent.FieldID)
		for _, f := range fields {
			if !metaevent.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != metaevent.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := meuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := meuo.mutation.EventName(); ok {
		_spec.SetField(metaevent.FieldEventName, field.TypeString, value)
	}
	if value, ok := meuo.mutation.UUID(); ok {
		_spec.SetField(metaevent.FieldUUID, field.TypeUUID, value)
	}
	if value, ok := meuo.mutation.DisplayName(); ok {
		_spec.SetField(metaevent.FieldDisplayName, field.TypeString, value)
	}
	if value, ok := meuo.mutation.EventDesc(); ok {
		_spec.SetField(metaevent.FieldEventDesc, field.TypeString, value)
	}
	if meuo.mutation.EventDescCleared() {
		_spec.ClearField(metaevent.FieldEventDesc, field.TypeString)
	}
	if value, ok := meuo.mutation.Remark(); ok {
		_spec.SetField(metaevent.FieldRemark, field.TypeString, value)
	}
	if meuo.mutation.RemarkCleared() {
		_spec.ClearField(metaevent.FieldRemark, field.TypeString)
	}
	if value, ok := meuo.mutation.UpdatedAt(); ok {
		_spec.SetField(metaevent.FieldUpdatedAt, field.TypeTime, value)
	}
	if meuo.mutation.UpdatedAtCleared() {
		_spec.ClearField(metaevent.FieldUpdatedAt, field.TypeTime)
	}
	_node = &MetaEvent{config: meuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, meuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{metaevent.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	meuo.mutation.done = true
	return _node, nil
}
