// 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/visitorstat"
)

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

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

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

// SetDate sets the "date" field.
func (_u *VisitorStatUpdate) SetDate(v time.Time) *VisitorStatUpdate {
	_u.mutation.SetDate(v)
	return _u
}

// SetNillableDate sets the "date" field if the given value is not nil.
func (_u *VisitorStatUpdate) SetNillableDate(v *time.Time) *VisitorStatUpdate {
	if v != nil {
		_u.SetDate(*v)
	}
	return _u
}

// SetUniqueVisitors sets the "unique_visitors" field.
func (_u *VisitorStatUpdate) SetUniqueVisitors(v int64) *VisitorStatUpdate {
	_u.mutation.ResetUniqueVisitors()
	_u.mutation.SetUniqueVisitors(v)
	return _u
}

// SetNillableUniqueVisitors sets the "unique_visitors" field if the given value is not nil.
func (_u *VisitorStatUpdate) SetNillableUniqueVisitors(v *int64) *VisitorStatUpdate {
	if v != nil {
		_u.SetUniqueVisitors(*v)
	}
	return _u
}

// AddUniqueVisitors adds value to the "unique_visitors" field.
func (_u *VisitorStatUpdate) AddUniqueVisitors(v int64) *VisitorStatUpdate {
	_u.mutation.AddUniqueVisitors(v)
	return _u
}

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

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

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

// SetPageViews sets the "page_views" field.
func (_u *VisitorStatUpdate) SetPageViews(v int64) *VisitorStatUpdate {
	_u.mutation.ResetPageViews()
	_u.mutation.SetPageViews(v)
	return _u
}

// SetNillablePageViews sets the "page_views" field if the given value is not nil.
func (_u *VisitorStatUpdate) SetNillablePageViews(v *int64) *VisitorStatUpdate {
	if v != nil {
		_u.SetPageViews(*v)
	}
	return _u
}

// AddPageViews adds value to the "page_views" field.
func (_u *VisitorStatUpdate) AddPageViews(v int64) *VisitorStatUpdate {
	_u.mutation.AddPageViews(v)
	return _u
}

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

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

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

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *VisitorStatUpdate) 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 *VisitorStatUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *VisitorStatUpdate) 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 *VisitorStatUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := visitorstat.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

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

func (_u *VisitorStatUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(visitorstat.Table, visitorstat.Columns, sqlgraph.NewFieldSpec(visitorstat.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(visitorstat.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Date(); ok {
		_spec.SetField(visitorstat.FieldDate, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UniqueVisitors(); ok {
		_spec.SetField(visitorstat.FieldUniqueVisitors, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUniqueVisitors(); ok {
		_spec.AddField(visitorstat.FieldUniqueVisitors, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.TotalViews(); ok {
		_spec.SetField(visitorstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedTotalViews(); ok {
		_spec.AddField(visitorstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PageViews(); ok {
		_spec.SetField(visitorstat.FieldPageViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPageViews(); ok {
		_spec.AddField(visitorstat.FieldPageViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.BounceCount(); ok {
		_spec.SetField(visitorstat.FieldBounceCount, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedBounceCount(); ok {
		_spec.AddField(visitorstat.FieldBounceCount, field.TypeInt64, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{visitorstat.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

// SetDate sets the "date" field.
func (_u *VisitorStatUpdateOne) SetDate(v time.Time) *VisitorStatUpdateOne {
	_u.mutation.SetDate(v)
	return _u
}

// SetNillableDate sets the "date" field if the given value is not nil.
func (_u *VisitorStatUpdateOne) SetNillableDate(v *time.Time) *VisitorStatUpdateOne {
	if v != nil {
		_u.SetDate(*v)
	}
	return _u
}

// SetUniqueVisitors sets the "unique_visitors" field.
func (_u *VisitorStatUpdateOne) SetUniqueVisitors(v int64) *VisitorStatUpdateOne {
	_u.mutation.ResetUniqueVisitors()
	_u.mutation.SetUniqueVisitors(v)
	return _u
}

// SetNillableUniqueVisitors sets the "unique_visitors" field if the given value is not nil.
func (_u *VisitorStatUpdateOne) SetNillableUniqueVisitors(v *int64) *VisitorStatUpdateOne {
	if v != nil {
		_u.SetUniqueVisitors(*v)
	}
	return _u
}

// AddUniqueVisitors adds value to the "unique_visitors" field.
func (_u *VisitorStatUpdateOne) AddUniqueVisitors(v int64) *VisitorStatUpdateOne {
	_u.mutation.AddUniqueVisitors(v)
	return _u
}

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

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

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

// SetPageViews sets the "page_views" field.
func (_u *VisitorStatUpdateOne) SetPageViews(v int64) *VisitorStatUpdateOne {
	_u.mutation.ResetPageViews()
	_u.mutation.SetPageViews(v)
	return _u
}

// SetNillablePageViews sets the "page_views" field if the given value is not nil.
func (_u *VisitorStatUpdateOne) SetNillablePageViews(v *int64) *VisitorStatUpdateOne {
	if v != nil {
		_u.SetPageViews(*v)
	}
	return _u
}

// AddPageViews adds value to the "page_views" field.
func (_u *VisitorStatUpdateOne) AddPageViews(v int64) *VisitorStatUpdateOne {
	_u.mutation.AddPageViews(v)
	return _u
}

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

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

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

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

// Where appends a list predicates to the VisitorStatUpdate builder.
func (_u *VisitorStatUpdateOne) Where(ps ...predicate.VisitorStat) *VisitorStatUpdateOne {
	_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 *VisitorStatUpdateOne) Select(field string, fields ...string) *VisitorStatUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *VisitorStatUpdateOne) 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 *VisitorStatUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := visitorstat.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

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

func (_u *VisitorStatUpdateOne) sqlSave(ctx context.Context) (_node *VisitorStat, err error) {
	_spec := sqlgraph.NewUpdateSpec(visitorstat.Table, visitorstat.Columns, sqlgraph.NewFieldSpec(visitorstat.FieldID, field.TypeUint))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "VisitorStat.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, visitorstat.FieldID)
		for _, f := range fields {
			if !visitorstat.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != visitorstat.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(visitorstat.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Date(); ok {
		_spec.SetField(visitorstat.FieldDate, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UniqueVisitors(); ok {
		_spec.SetField(visitorstat.FieldUniqueVisitors, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUniqueVisitors(); ok {
		_spec.AddField(visitorstat.FieldUniqueVisitors, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.TotalViews(); ok {
		_spec.SetField(visitorstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedTotalViews(); ok {
		_spec.AddField(visitorstat.FieldTotalViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.PageViews(); ok {
		_spec.SetField(visitorstat.FieldPageViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPageViews(); ok {
		_spec.AddField(visitorstat.FieldPageViews, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.BounceCount(); ok {
		_spec.SetField(visitorstat.FieldBounceCount, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedBounceCount(); ok {
		_spec.AddField(visitorstat.FieldBounceCount, field.TypeInt64, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &VisitorStat{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{visitorstat.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
