// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/predicate"
	"github.com/anzhiyu-c/anheyu-app/ent/urlstat"
)

// URLStatUpdate is the builder for updating URLStat entities.
type URLStatUpdate struct {
	config
	hooks     []Hook
	mutation  *URLStatMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the URLStatUpdate builder.
func (_u *URLStatUpdate) Where(ps ...predicate.URLStat) *URLStatUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *URLStatUpdate) SetUpdatedAt(v time.Time) *URLStatUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetURLPath sets the "url_path" field.
func (_u *URLStatUpdate) SetURLPath(v string) *URLStatUpdate {
	_u.mutation.SetURLPath(v)
	return _u
}

// SetNillableURLPath sets the "url_path" field if the given value is not nil.
func (_u *URLStatUpdate) SetNillableURLPath(v *string) *URLStatUpdate {
	if v != nil {
		_u.SetURLPath(*v)
	}
	return _u
}

// SetPageTitle sets the "page_title" field.
func (_u *URLStatUpdate) SetPageTitle(v string) *URLStatUpdate {
	_u.mutation.SetPageTitle(v)
	return _u
}

// SetNillablePageTitle sets the "page_title" field if the given value is not nil.
func (_u *URLStatUpdate) SetNillablePageTitle(v *string) *URLStatUpdate {
	if v != nil {
		_u.SetPageTitle(*v)
	}
	return _u
}

// ClearPageTitle clears the value of the "page_title" field.
func (_u *URLStatUpdate) ClearPageTitle() *URLStatUpdate {
	_u.mutation.ClearPageTitle()
	return _u
}

// SetTotalViews sets the "total_views" field.
func (_u *URLStatUpdate) SetTotalViews(v int64) *URLStatUpdate {
	_u.mutation.ResetTotalViews()
	_u.mutation.SetTotalViews(v)
	return _u
}

// SetNillableTotalViews sets the "total_views" field if the given value is not nil.
func (_u *URLStatUpdate) SetNillableTotalViews(v *int64) *URLStatUpdate {
	if v != nil {
		_u.SetTotalViews(*v)
	}
	return _u
}

// AddTotalViews adds value to the "total_views" field.
func (_u *URLStatUpdate) AddTotalViews(v int64) *URLStatUpdate {
	_u.mutation.AddTotalViews(v)
	return _u
}

// SetUniqueViews sets the "unique_views" field.
func (_u *URLStatUpdate) SetUniqueViews(v int64) *URLStatUpdate {
	_u.mutation.ResetUniqueViews()
	_u.mutation.SetUniqueViews(v)
	return _u
}

// SetNillableUniqueViews sets the "unique_views" field if the given value is not nil.
func (_u *URLStatUpdate) SetNillableUniqueViews(v *int64) *URLStatUpdate {
	if v != nil {
		_u.SetUniqueViews(*v)
	}
	return _u
}

// AddUniqueViews adds value to the "unique_views" field.
func (_u *URLStatUpdate) AddUniqueViews(v int64) *URLStatUpdate {
	_u.mutation.AddUniqueViews(v)
	return _u
}

// SetBounceCount sets the "bounce_count" field.
func (_u *URLStatUpdate) SetBounceCount(v int64) *URLStatUpdate {
	_u.mutation.ResetBounceCount()
	_u.mutation.SetBounceCount(v)
	return _u
}

// SetNillableBounceCount sets the "bounce_count" field if the given value is not nil.
func (_u *URLStatUpdate) SetNillableBounceCount(v *int64) *URLStatUpdate {
	if v != nil {
		_u.SetBounceCount(*v)
	}
	return _u
}

// AddBounceCount adds value to the "bounce_count" field.
func (_u *URLStatUpdate) AddBounceCount(v int64) *URLStatUpdate {
	_u.mutation.AddBounceCount(v)
	return _u
}

// SetAvgDuration sets the "avg_duration" field.
func (_u *URLStatUpdate) SetAvgDuration(v float64) *URLStatUpdate {
	_u.mutation.ResetAvgDuration()
	_u.mutation.SetAvgDuration(v)
	return _u
}

// SetNillableAvgDuration sets the "avg_duration" field if the given value is not nil.
func (_u *URLStatUpdate) SetNillableAvgDuration(v *float64) *URLStatUpdate {
	if v != nil {
		_u.SetAvgDuration(*v)
	}
	return _u
}

// AddAvgDuration adds value to the "avg_duration" field.
func (_u *URLStatUpdate) AddAvgDuration(v float64) *URLStatUpdate {
	_u.mutation.AddAvgDuration(v)
	return _u
}

// SetLastVisitedAt sets the "last_visited_at" field.
func (_u *URLStatUpdate) SetLastVisitedAt(v time.Time) *URLStatUpdate {
	_u.mutation.SetLastVisitedAt(v)
	return _u
}

// SetNillableLastVisitedAt sets the "last_visited_at" field if the given value is not nil.
func (_u *URLStatUpdate) SetNillableLastVisitedAt(v *time.Time) *URLStatUpdate {
	if v != nil {
		_u.SetLastVisitedAt(*v)
	}
	return _u
}

// ClearLastVisitedAt clears the value of the "last_visited_at" field.
func (_u *URLStatUpdate) ClearLastVisitedAt() *URLStatUpdate {
	_u.mutation.ClearLastVisitedAt()
	return _u
}

// Mutation returns the URLStatMutation object of the builder.
func (_u *URLStatUpdate) Mutation() *URLStatMutation {
	return _u.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *URLStatUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := urlstat.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *URLStatUpdate) check() error {
	if v, ok := _u.mutation.URLPath(); ok {
		if err := urlstat.URLPathValidator(v); err != nil {
			return &ValidationError{Name: "url_path", err: fmt.Errorf(`ent: validator failed for field "URLStat.url_path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PageTitle(); ok {
		if err := urlstat.PageTitleValidator(v); err != nil {
			return &ValidationError{Name: "page_title", err: fmt.Errorf(`ent: validator failed for field "URLStat.page_title": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *URLStatUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *URLStatUpdate {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *URLStatUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(urlstat.Table, urlstat.Columns, sqlgraph.NewFieldSpec(urlstat.FieldID, field.TypeUint))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(urlstat.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.URLPath(); ok {
		_spec.SetField(urlstat.FieldURLPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.PageTitle(); ok {
		_spec.SetField(urlstat.FieldPageTitle, field.TypeString, value)
	}
	if _u.mutation.PageTitleCleared() {
		_spec.ClearField(urlstat.FieldPageTitle, field.TypeString)
	}
	if value, ok := _u.mutation.TotalViews(); ok {
		_spec.SetField(urlstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedTotalViews(); ok {
		_spec.AddField(urlstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.UniqueViews(); ok {
		_spec.SetField(urlstat.FieldUniqueViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUniqueViews(); ok {
		_spec.AddField(urlstat.FieldUniqueViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.BounceCount(); ok {
		_spec.SetField(urlstat.FieldBounceCount, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedBounceCount(); ok {
		_spec.AddField(urlstat.FieldBounceCount, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AvgDuration(); ok {
		_spec.SetField(urlstat.FieldAvgDuration, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedAvgDuration(); ok {
		_spec.AddField(urlstat.FieldAvgDuration, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.LastVisitedAt(); ok {
		_spec.SetField(urlstat.FieldLastVisitedAt, field.TypeTime, value)
	}
	if _u.mutation.LastVisitedAtCleared() {
		_spec.ClearField(urlstat.FieldLastVisitedAt, field.TypeTime)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{urlstat.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// URLStatUpdateOne is the builder for updating a single URLStat entity.
type URLStatUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *URLStatMutation
	modifiers []func(*sql.UpdateBuilder)
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *URLStatUpdateOne) SetUpdatedAt(v time.Time) *URLStatUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetURLPath sets the "url_path" field.
func (_u *URLStatUpdateOne) SetURLPath(v string) *URLStatUpdateOne {
	_u.mutation.SetURLPath(v)
	return _u
}

// SetNillableURLPath sets the "url_path" field if the given value is not nil.
func (_u *URLStatUpdateOne) SetNillableURLPath(v *string) *URLStatUpdateOne {
	if v != nil {
		_u.SetURLPath(*v)
	}
	return _u
}

// SetPageTitle sets the "page_title" field.
func (_u *URLStatUpdateOne) SetPageTitle(v string) *URLStatUpdateOne {
	_u.mutation.SetPageTitle(v)
	return _u
}

// SetNillablePageTitle sets the "page_title" field if the given value is not nil.
func (_u *URLStatUpdateOne) SetNillablePageTitle(v *string) *URLStatUpdateOne {
	if v != nil {
		_u.SetPageTitle(*v)
	}
	return _u
}

// ClearPageTitle clears the value of the "page_title" field.
func (_u *URLStatUpdateOne) ClearPageTitle() *URLStatUpdateOne {
	_u.mutation.ClearPageTitle()
	return _u
}

// SetTotalViews sets the "total_views" field.
func (_u *URLStatUpdateOne) SetTotalViews(v int64) *URLStatUpdateOne {
	_u.mutation.ResetTotalViews()
	_u.mutation.SetTotalViews(v)
	return _u
}

// SetNillableTotalViews sets the "total_views" field if the given value is not nil.
func (_u *URLStatUpdateOne) SetNillableTotalViews(v *int64) *URLStatUpdateOne {
	if v != nil {
		_u.SetTotalViews(*v)
	}
	return _u
}

// AddTotalViews adds value to the "total_views" field.
func (_u *URLStatUpdateOne) AddTotalViews(v int64) *URLStatUpdateOne {
	_u.mutation.AddTotalViews(v)
	return _u
}

// SetUniqueViews sets the "unique_views" field.
func (_u *URLStatUpdateOne) SetUniqueViews(v int64) *URLStatUpdateOne {
	_u.mutation.ResetUniqueViews()
	_u.mutation.SetUniqueViews(v)
	return _u
}

// SetNillableUniqueViews sets the "unique_views" field if the given value is not nil.
func (_u *URLStatUpdateOne) SetNillableUniqueViews(v *int64) *URLStatUpdateOne {
	if v != nil {
		_u.SetUniqueViews(*v)
	}
	return _u
}

// AddUniqueViews adds value to the "unique_views" field.
func (_u *URLStatUpdateOne) AddUniqueViews(v int64) *URLStatUpdateOne {
	_u.mutation.AddUniqueViews(v)
	return _u
}

// SetBounceCount sets the "bounce_count" field.
func (_u *URLStatUpdateOne) SetBounceCount(v int64) *URLStatUpdateOne {
	_u.mutation.ResetBounceCount()
	_u.mutation.SetBounceCount(v)
	return _u
}

// SetNillableBounceCount sets the "bounce_count" field if the given value is not nil.
func (_u *URLStatUpdateOne) SetNillableBounceCount(v *int64) *URLStatUpdateOne {
	if v != nil {
		_u.SetBounceCount(*v)
	}
	return _u
}

// AddBounceCount adds value to the "bounce_count" field.
func (_u *URLStatUpdateOne) AddBounceCount(v int64) *URLStatUpdateOne {
	_u.mutation.AddBounceCount(v)
	return _u
}

// SetAvgDuration sets the "avg_duration" field.
func (_u *URLStatUpdateOne) SetAvgDuration(v float64) *URLStatUpdateOne {
	_u.mutation.ResetAvgDuration()
	_u.mutation.SetAvgDuration(v)
	return _u
}

// SetNillableAvgDuration sets the "avg_duration" field if the given value is not nil.
func (_u *URLStatUpdateOne) SetNillableAvgDuration(v *float64) *URLStatUpdateOne {
	if v != nil {
		_u.SetAvgDuration(*v)
	}
	return _u
}

// AddAvgDuration adds value to the "avg_duration" field.
func (_u *URLStatUpdateOne) AddAvgDuration(v float64) *URLStatUpdateOne {
	_u.mutation.AddAvgDuration(v)
	return _u
}

// SetLastVisitedAt sets the "last_visited_at" field.
func (_u *URLStatUpdateOne) SetLastVisitedAt(v time.Time) *URLStatUpdateOne {
	_u.mutation.SetLastVisitedAt(v)
	return _u
}

// SetNillableLastVisitedAt sets the "last_visited_at" field if the given value is not nil.
func (_u *URLStatUpdateOne) SetNillableLastVisitedAt(v *time.Time) *URLStatUpdateOne {
	if v != nil {
		_u.SetLastVisitedAt(*v)
	}
	return _u
}

// ClearLastVisitedAt clears the value of the "last_visited_at" field.
func (_u *URLStatUpdateOne) ClearLastVisitedAt() *URLStatUpdateOne {
	_u.mutation.ClearLastVisitedAt()
	return _u
}

// Mutation returns the URLStatMutation object of the builder.
func (_u *URLStatUpdateOne) Mutation() *URLStatMutation {
	return _u.mutation
}

// Where appends a list predicates to the URLStatUpdate builder.
func (_u *URLStatUpdateOne) Where(ps ...predicate.URLStat) *URLStatUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

// Save executes the query and returns the updated URLStat entity.
func (_u *URLStatUpdateOne) Save(ctx context.Context) (*URLStat, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_u *URLStatUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := urlstat.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *URLStatUpdateOne) check() error {
	if v, ok := _u.mutation.URLPath(); ok {
		if err := urlstat.URLPathValidator(v); err != nil {
			return &ValidationError{Name: "url_path", err: fmt.Errorf(`ent: validator failed for field "URLStat.url_path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.PageTitle(); ok {
		if err := urlstat.PageTitleValidator(v); err != nil {
			return &ValidationError{Name: "page_title", err: fmt.Errorf(`ent: validator failed for field "URLStat.page_title": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *URLStatUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *URLStatUpdateOne {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *URLStatUpdateOne) sqlSave(ctx context.Context) (_node *URLStat, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(urlstat.Table, urlstat.Columns, sqlgraph.NewFieldSpec(urlstat.FieldID, field.TypeUint))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "URLStat.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, urlstat.FieldID)
		for _, f := range fields {
			if !urlstat.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != urlstat.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(urlstat.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.URLPath(); ok {
		_spec.SetField(urlstat.FieldURLPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.PageTitle(); ok {
		_spec.SetField(urlstat.FieldPageTitle, field.TypeString, value)
	}
	if _u.mutation.PageTitleCleared() {
		_spec.ClearField(urlstat.FieldPageTitle, field.TypeString)
	}
	if value, ok := _u.mutation.TotalViews(); ok {
		_spec.SetField(urlstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedTotalViews(); ok {
		_spec.AddField(urlstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.UniqueViews(); ok {
		_spec.SetField(urlstat.FieldUniqueViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUniqueViews(); ok {
		_spec.AddField(urlstat.FieldUniqueViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.BounceCount(); ok {
		_spec.SetField(urlstat.FieldBounceCount, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedBounceCount(); ok {
		_spec.AddField(urlstat.FieldBounceCount, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AvgDuration(); ok {
		_spec.SetField(urlstat.FieldAvgDuration, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedAvgDuration(); ok {
		_spec.AddField(urlstat.FieldAvgDuration, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.LastVisitedAt(); ok {
		_spec.SetField(urlstat.FieldLastVisitedAt, field.TypeTime, value)
	}
	if _u.mutation.LastVisitedAtCleared() {
		_spec.ClearField(urlstat.FieldLastVisitedAt, field.TypeTime)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &URLStat{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{urlstat.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
