// 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/Wei-Shaw/sub2api/ent/predicate"
	"github.com/Wei-Shaw/sub2api/ent/user"
	"github.com/Wei-Shaw/sub2api/ent/userplatformquota"
)

// UserPlatformQuotaUpdate is the builder for updating UserPlatformQuota entities.
type UserPlatformQuotaUpdate struct {
	config
	hooks    []Hook
	mutation *UserPlatformQuotaMutation
}

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

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

// SetDeletedAt sets the "deleted_at" field.
func (_u *UserPlatformQuotaUpdate) SetDeletedAt(v time.Time) *UserPlatformQuotaUpdate {
	_u.mutation.SetDeletedAt(v)
	return _u
}

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

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

// SetUserID sets the "user_id" field.
func (_u *UserPlatformQuotaUpdate) SetUserID(v int64) *UserPlatformQuotaUpdate {
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableUserID(v *int64) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// SetPlatform sets the "platform" field.
func (_u *UserPlatformQuotaUpdate) SetPlatform(v string) *UserPlatformQuotaUpdate {
	_u.mutation.SetPlatform(v)
	return _u
}

// SetNillablePlatform sets the "platform" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillablePlatform(v *string) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetPlatform(*v)
	}
	return _u
}

// SetDailyLimitUsd sets the "daily_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) SetDailyLimitUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.ResetDailyLimitUsd()
	_u.mutation.SetDailyLimitUsd(v)
	return _u
}

// SetNillableDailyLimitUsd sets the "daily_limit_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableDailyLimitUsd(v *float64) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetDailyLimitUsd(*v)
	}
	return _u
}

// AddDailyLimitUsd adds value to the "daily_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) AddDailyLimitUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.AddDailyLimitUsd(v)
	return _u
}

// ClearDailyLimitUsd clears the value of the "daily_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) ClearDailyLimitUsd() *UserPlatformQuotaUpdate {
	_u.mutation.ClearDailyLimitUsd()
	return _u
}

// SetWeeklyLimitUsd sets the "weekly_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) SetWeeklyLimitUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.ResetWeeklyLimitUsd()
	_u.mutation.SetWeeklyLimitUsd(v)
	return _u
}

// SetNillableWeeklyLimitUsd sets the "weekly_limit_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableWeeklyLimitUsd(v *float64) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetWeeklyLimitUsd(*v)
	}
	return _u
}

// AddWeeklyLimitUsd adds value to the "weekly_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) AddWeeklyLimitUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.AddWeeklyLimitUsd(v)
	return _u
}

// ClearWeeklyLimitUsd clears the value of the "weekly_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) ClearWeeklyLimitUsd() *UserPlatformQuotaUpdate {
	_u.mutation.ClearWeeklyLimitUsd()
	return _u
}

// SetMonthlyLimitUsd sets the "monthly_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) SetMonthlyLimitUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.ResetMonthlyLimitUsd()
	_u.mutation.SetMonthlyLimitUsd(v)
	return _u
}

// SetNillableMonthlyLimitUsd sets the "monthly_limit_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableMonthlyLimitUsd(v *float64) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetMonthlyLimitUsd(*v)
	}
	return _u
}

// AddMonthlyLimitUsd adds value to the "monthly_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) AddMonthlyLimitUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.AddMonthlyLimitUsd(v)
	return _u
}

// ClearMonthlyLimitUsd clears the value of the "monthly_limit_usd" field.
func (_u *UserPlatformQuotaUpdate) ClearMonthlyLimitUsd() *UserPlatformQuotaUpdate {
	_u.mutation.ClearMonthlyLimitUsd()
	return _u
}

// SetDailyUsageUsd sets the "daily_usage_usd" field.
func (_u *UserPlatformQuotaUpdate) SetDailyUsageUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.ResetDailyUsageUsd()
	_u.mutation.SetDailyUsageUsd(v)
	return _u
}

// SetNillableDailyUsageUsd sets the "daily_usage_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableDailyUsageUsd(v *float64) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetDailyUsageUsd(*v)
	}
	return _u
}

// AddDailyUsageUsd adds value to the "daily_usage_usd" field.
func (_u *UserPlatformQuotaUpdate) AddDailyUsageUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.AddDailyUsageUsd(v)
	return _u
}

// SetWeeklyUsageUsd sets the "weekly_usage_usd" field.
func (_u *UserPlatformQuotaUpdate) SetWeeklyUsageUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.ResetWeeklyUsageUsd()
	_u.mutation.SetWeeklyUsageUsd(v)
	return _u
}

// SetNillableWeeklyUsageUsd sets the "weekly_usage_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableWeeklyUsageUsd(v *float64) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetWeeklyUsageUsd(*v)
	}
	return _u
}

// AddWeeklyUsageUsd adds value to the "weekly_usage_usd" field.
func (_u *UserPlatformQuotaUpdate) AddWeeklyUsageUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.AddWeeklyUsageUsd(v)
	return _u
}

// SetMonthlyUsageUsd sets the "monthly_usage_usd" field.
func (_u *UserPlatformQuotaUpdate) SetMonthlyUsageUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.ResetMonthlyUsageUsd()
	_u.mutation.SetMonthlyUsageUsd(v)
	return _u
}

// SetNillableMonthlyUsageUsd sets the "monthly_usage_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableMonthlyUsageUsd(v *float64) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetMonthlyUsageUsd(*v)
	}
	return _u
}

// AddMonthlyUsageUsd adds value to the "monthly_usage_usd" field.
func (_u *UserPlatformQuotaUpdate) AddMonthlyUsageUsd(v float64) *UserPlatformQuotaUpdate {
	_u.mutation.AddMonthlyUsageUsd(v)
	return _u
}

// SetDailyWindowStart sets the "daily_window_start" field.
func (_u *UserPlatformQuotaUpdate) SetDailyWindowStart(v time.Time) *UserPlatformQuotaUpdate {
	_u.mutation.SetDailyWindowStart(v)
	return _u
}

// SetNillableDailyWindowStart sets the "daily_window_start" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableDailyWindowStart(v *time.Time) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetDailyWindowStart(*v)
	}
	return _u
}

// ClearDailyWindowStart clears the value of the "daily_window_start" field.
func (_u *UserPlatformQuotaUpdate) ClearDailyWindowStart() *UserPlatformQuotaUpdate {
	_u.mutation.ClearDailyWindowStart()
	return _u
}

// SetWeeklyWindowStart sets the "weekly_window_start" field.
func (_u *UserPlatformQuotaUpdate) SetWeeklyWindowStart(v time.Time) *UserPlatformQuotaUpdate {
	_u.mutation.SetWeeklyWindowStart(v)
	return _u
}

// SetNillableWeeklyWindowStart sets the "weekly_window_start" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableWeeklyWindowStart(v *time.Time) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetWeeklyWindowStart(*v)
	}
	return _u
}

// ClearWeeklyWindowStart clears the value of the "weekly_window_start" field.
func (_u *UserPlatformQuotaUpdate) ClearWeeklyWindowStart() *UserPlatformQuotaUpdate {
	_u.mutation.ClearWeeklyWindowStart()
	return _u
}

// SetMonthlyWindowStart sets the "monthly_window_start" field.
func (_u *UserPlatformQuotaUpdate) SetMonthlyWindowStart(v time.Time) *UserPlatformQuotaUpdate {
	_u.mutation.SetMonthlyWindowStart(v)
	return _u
}

// SetNillableMonthlyWindowStart sets the "monthly_window_start" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdate) SetNillableMonthlyWindowStart(v *time.Time) *UserPlatformQuotaUpdate {
	if v != nil {
		_u.SetMonthlyWindowStart(*v)
	}
	return _u
}

// ClearMonthlyWindowStart clears the value of the "monthly_window_start" field.
func (_u *UserPlatformQuotaUpdate) ClearMonthlyWindowStart() *UserPlatformQuotaUpdate {
	_u.mutation.ClearMonthlyWindowStart()
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *UserPlatformQuotaUpdate) SetUser(v *User) *UserPlatformQuotaUpdate {
	return _u.SetUserID(v.ID)
}

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

// ClearUser clears the "user" edge to the User entity.
func (_u *UserPlatformQuotaUpdate) ClearUser() *UserPlatformQuotaUpdate {
	_u.mutation.ClearUser()
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *UserPlatformQuotaUpdate) 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 *UserPlatformQuotaUpdate) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if userplatformquota.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized userplatformquota.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := userplatformquota.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *UserPlatformQuotaUpdate) check() error {
	if v, ok := _u.mutation.Platform(); ok {
		if err := userplatformquota.PlatformValidator(v); err != nil {
			return &ValidationError{Name: "platform", err: fmt.Errorf(`ent: validator failed for field "UserPlatformQuota.platform": %w`, err)}
		}
	}
	if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "UserPlatformQuota.user"`)
	}
	return nil
}

func (_u *UserPlatformQuotaUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(userplatformquota.Table, userplatformquota.Columns, sqlgraph.NewFieldSpec(userplatformquota.FieldID, field.TypeInt64))
	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(userplatformquota.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(userplatformquota.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(userplatformquota.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Platform(); ok {
		_spec.SetField(userplatformquota.FieldPlatform, field.TypeString, value)
	}
	if value, ok := _u.mutation.DailyLimitUsd(); ok {
		_spec.SetField(userplatformquota.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyLimitUsd(); ok {
		_spec.AddField(userplatformquota.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.DailyLimitUsdCleared() {
		_spec.ClearField(userplatformquota.FieldDailyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.WeeklyLimitUsd(); ok {
		_spec.SetField(userplatformquota.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyLimitUsd(); ok {
		_spec.AddField(userplatformquota.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.WeeklyLimitUsdCleared() {
		_spec.ClearField(userplatformquota.FieldWeeklyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.MonthlyLimitUsd(); ok {
		_spec.SetField(userplatformquota.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyLimitUsd(); ok {
		_spec.AddField(userplatformquota.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.MonthlyLimitUsdCleared() {
		_spec.ClearField(userplatformquota.FieldMonthlyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.DailyUsageUsd(); ok {
		_spec.SetField(userplatformquota.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyUsageUsd(); ok {
		_spec.AddField(userplatformquota.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.WeeklyUsageUsd(); ok {
		_spec.SetField(userplatformquota.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyUsageUsd(); ok {
		_spec.AddField(userplatformquota.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.MonthlyUsageUsd(); ok {
		_spec.SetField(userplatformquota.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyUsageUsd(); ok {
		_spec.AddField(userplatformquota.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.DailyWindowStart(); ok {
		_spec.SetField(userplatformquota.FieldDailyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.DailyWindowStartCleared() {
		_spec.ClearField(userplatformquota.FieldDailyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.WeeklyWindowStart(); ok {
		_spec.SetField(userplatformquota.FieldWeeklyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.WeeklyWindowStartCleared() {
		_spec.ClearField(userplatformquota.FieldWeeklyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.MonthlyWindowStart(); ok {
		_spec.SetField(userplatformquota.FieldMonthlyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.MonthlyWindowStartCleared() {
		_spec.ClearField(userplatformquota.FieldMonthlyWindowStart, field.TypeTime)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   userplatformquota.UserTable,
			Columns: []string{userplatformquota.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   userplatformquota.UserTable,
			Columns: []string{userplatformquota.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{userplatformquota.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// UserPlatformQuotaUpdateOne is the builder for updating a single UserPlatformQuota entity.
type UserPlatformQuotaUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UserPlatformQuotaMutation
}

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

// SetDeletedAt sets the "deleted_at" field.
func (_u *UserPlatformQuotaUpdateOne) SetDeletedAt(v time.Time) *UserPlatformQuotaUpdateOne {
	_u.mutation.SetDeletedAt(v)
	return _u
}

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

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

// SetUserID sets the "user_id" field.
func (_u *UserPlatformQuotaUpdateOne) SetUserID(v int64) *UserPlatformQuotaUpdateOne {
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableUserID(v *int64) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// SetPlatform sets the "platform" field.
func (_u *UserPlatformQuotaUpdateOne) SetPlatform(v string) *UserPlatformQuotaUpdateOne {
	_u.mutation.SetPlatform(v)
	return _u
}

// SetNillablePlatform sets the "platform" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillablePlatform(v *string) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetPlatform(*v)
	}
	return _u
}

// SetDailyLimitUsd sets the "daily_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) SetDailyLimitUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.ResetDailyLimitUsd()
	_u.mutation.SetDailyLimitUsd(v)
	return _u
}

// SetNillableDailyLimitUsd sets the "daily_limit_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableDailyLimitUsd(v *float64) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetDailyLimitUsd(*v)
	}
	return _u
}

// AddDailyLimitUsd adds value to the "daily_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) AddDailyLimitUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.AddDailyLimitUsd(v)
	return _u
}

// ClearDailyLimitUsd clears the value of the "daily_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) ClearDailyLimitUsd() *UserPlatformQuotaUpdateOne {
	_u.mutation.ClearDailyLimitUsd()
	return _u
}

// SetWeeklyLimitUsd sets the "weekly_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) SetWeeklyLimitUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.ResetWeeklyLimitUsd()
	_u.mutation.SetWeeklyLimitUsd(v)
	return _u
}

// SetNillableWeeklyLimitUsd sets the "weekly_limit_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableWeeklyLimitUsd(v *float64) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetWeeklyLimitUsd(*v)
	}
	return _u
}

// AddWeeklyLimitUsd adds value to the "weekly_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) AddWeeklyLimitUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.AddWeeklyLimitUsd(v)
	return _u
}

// ClearWeeklyLimitUsd clears the value of the "weekly_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) ClearWeeklyLimitUsd() *UserPlatformQuotaUpdateOne {
	_u.mutation.ClearWeeklyLimitUsd()
	return _u
}

// SetMonthlyLimitUsd sets the "monthly_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) SetMonthlyLimitUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.ResetMonthlyLimitUsd()
	_u.mutation.SetMonthlyLimitUsd(v)
	return _u
}

// SetNillableMonthlyLimitUsd sets the "monthly_limit_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableMonthlyLimitUsd(v *float64) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetMonthlyLimitUsd(*v)
	}
	return _u
}

// AddMonthlyLimitUsd adds value to the "monthly_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) AddMonthlyLimitUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.AddMonthlyLimitUsd(v)
	return _u
}

// ClearMonthlyLimitUsd clears the value of the "monthly_limit_usd" field.
func (_u *UserPlatformQuotaUpdateOne) ClearMonthlyLimitUsd() *UserPlatformQuotaUpdateOne {
	_u.mutation.ClearMonthlyLimitUsd()
	return _u
}

// SetDailyUsageUsd sets the "daily_usage_usd" field.
func (_u *UserPlatformQuotaUpdateOne) SetDailyUsageUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.ResetDailyUsageUsd()
	_u.mutation.SetDailyUsageUsd(v)
	return _u
}

// SetNillableDailyUsageUsd sets the "daily_usage_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableDailyUsageUsd(v *float64) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetDailyUsageUsd(*v)
	}
	return _u
}

// AddDailyUsageUsd adds value to the "daily_usage_usd" field.
func (_u *UserPlatformQuotaUpdateOne) AddDailyUsageUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.AddDailyUsageUsd(v)
	return _u
}

// SetWeeklyUsageUsd sets the "weekly_usage_usd" field.
func (_u *UserPlatformQuotaUpdateOne) SetWeeklyUsageUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.ResetWeeklyUsageUsd()
	_u.mutation.SetWeeklyUsageUsd(v)
	return _u
}

// SetNillableWeeklyUsageUsd sets the "weekly_usage_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableWeeklyUsageUsd(v *float64) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetWeeklyUsageUsd(*v)
	}
	return _u
}

// AddWeeklyUsageUsd adds value to the "weekly_usage_usd" field.
func (_u *UserPlatformQuotaUpdateOne) AddWeeklyUsageUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.AddWeeklyUsageUsd(v)
	return _u
}

// SetMonthlyUsageUsd sets the "monthly_usage_usd" field.
func (_u *UserPlatformQuotaUpdateOne) SetMonthlyUsageUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.ResetMonthlyUsageUsd()
	_u.mutation.SetMonthlyUsageUsd(v)
	return _u
}

// SetNillableMonthlyUsageUsd sets the "monthly_usage_usd" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableMonthlyUsageUsd(v *float64) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetMonthlyUsageUsd(*v)
	}
	return _u
}

// AddMonthlyUsageUsd adds value to the "monthly_usage_usd" field.
func (_u *UserPlatformQuotaUpdateOne) AddMonthlyUsageUsd(v float64) *UserPlatformQuotaUpdateOne {
	_u.mutation.AddMonthlyUsageUsd(v)
	return _u
}

// SetDailyWindowStart sets the "daily_window_start" field.
func (_u *UserPlatformQuotaUpdateOne) SetDailyWindowStart(v time.Time) *UserPlatformQuotaUpdateOne {
	_u.mutation.SetDailyWindowStart(v)
	return _u
}

// SetNillableDailyWindowStart sets the "daily_window_start" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableDailyWindowStart(v *time.Time) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetDailyWindowStart(*v)
	}
	return _u
}

// ClearDailyWindowStart clears the value of the "daily_window_start" field.
func (_u *UserPlatformQuotaUpdateOne) ClearDailyWindowStart() *UserPlatformQuotaUpdateOne {
	_u.mutation.ClearDailyWindowStart()
	return _u
}

// SetWeeklyWindowStart sets the "weekly_window_start" field.
func (_u *UserPlatformQuotaUpdateOne) SetWeeklyWindowStart(v time.Time) *UserPlatformQuotaUpdateOne {
	_u.mutation.SetWeeklyWindowStart(v)
	return _u
}

// SetNillableWeeklyWindowStart sets the "weekly_window_start" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableWeeklyWindowStart(v *time.Time) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetWeeklyWindowStart(*v)
	}
	return _u
}

// ClearWeeklyWindowStart clears the value of the "weekly_window_start" field.
func (_u *UserPlatformQuotaUpdateOne) ClearWeeklyWindowStart() *UserPlatformQuotaUpdateOne {
	_u.mutation.ClearWeeklyWindowStart()
	return _u
}

// SetMonthlyWindowStart sets the "monthly_window_start" field.
func (_u *UserPlatformQuotaUpdateOne) SetMonthlyWindowStart(v time.Time) *UserPlatformQuotaUpdateOne {
	_u.mutation.SetMonthlyWindowStart(v)
	return _u
}

// SetNillableMonthlyWindowStart sets the "monthly_window_start" field if the given value is not nil.
func (_u *UserPlatformQuotaUpdateOne) SetNillableMonthlyWindowStart(v *time.Time) *UserPlatformQuotaUpdateOne {
	if v != nil {
		_u.SetMonthlyWindowStart(*v)
	}
	return _u
}

// ClearMonthlyWindowStart clears the value of the "monthly_window_start" field.
func (_u *UserPlatformQuotaUpdateOne) ClearMonthlyWindowStart() *UserPlatformQuotaUpdateOne {
	_u.mutation.ClearMonthlyWindowStart()
	return _u
}

// SetUser sets the "user" edge to the User entity.
func (_u *UserPlatformQuotaUpdateOne) SetUser(v *User) *UserPlatformQuotaUpdateOne {
	return _u.SetUserID(v.ID)
}

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

// ClearUser clears the "user" edge to the User entity.
func (_u *UserPlatformQuotaUpdateOne) ClearUser() *UserPlatformQuotaUpdateOne {
	_u.mutation.ClearUser()
	return _u
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *UserPlatformQuotaUpdateOne) 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 *UserPlatformQuotaUpdateOne) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if userplatformquota.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized userplatformquota.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := userplatformquota.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *UserPlatformQuotaUpdateOne) check() error {
	if v, ok := _u.mutation.Platform(); ok {
		if err := userplatformquota.PlatformValidator(v); err != nil {
			return &ValidationError{Name: "platform", err: fmt.Errorf(`ent: validator failed for field "UserPlatformQuota.platform": %w`, err)}
		}
	}
	if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "UserPlatformQuota.user"`)
	}
	return nil
}

func (_u *UserPlatformQuotaUpdateOne) sqlSave(ctx context.Context) (_node *UserPlatformQuota, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(userplatformquota.Table, userplatformquota.Columns, sqlgraph.NewFieldSpec(userplatformquota.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserPlatformQuota.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, userplatformquota.FieldID)
		for _, f := range fields {
			if !userplatformquota.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != userplatformquota.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(userplatformquota.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(userplatformquota.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(userplatformquota.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Platform(); ok {
		_spec.SetField(userplatformquota.FieldPlatform, field.TypeString, value)
	}
	if value, ok := _u.mutation.DailyLimitUsd(); ok {
		_spec.SetField(userplatformquota.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyLimitUsd(); ok {
		_spec.AddField(userplatformquota.FieldDailyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.DailyLimitUsdCleared() {
		_spec.ClearField(userplatformquota.FieldDailyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.WeeklyLimitUsd(); ok {
		_spec.SetField(userplatformquota.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyLimitUsd(); ok {
		_spec.AddField(userplatformquota.FieldWeeklyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.WeeklyLimitUsdCleared() {
		_spec.ClearField(userplatformquota.FieldWeeklyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.MonthlyLimitUsd(); ok {
		_spec.SetField(userplatformquota.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyLimitUsd(); ok {
		_spec.AddField(userplatformquota.FieldMonthlyLimitUsd, field.TypeFloat64, value)
	}
	if _u.mutation.MonthlyLimitUsdCleared() {
		_spec.ClearField(userplatformquota.FieldMonthlyLimitUsd, field.TypeFloat64)
	}
	if value, ok := _u.mutation.DailyUsageUsd(); ok {
		_spec.SetField(userplatformquota.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedDailyUsageUsd(); ok {
		_spec.AddField(userplatformquota.FieldDailyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.WeeklyUsageUsd(); ok {
		_spec.SetField(userplatformquota.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedWeeklyUsageUsd(); ok {
		_spec.AddField(userplatformquota.FieldWeeklyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.MonthlyUsageUsd(); ok {
		_spec.SetField(userplatformquota.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.AddedMonthlyUsageUsd(); ok {
		_spec.AddField(userplatformquota.FieldMonthlyUsageUsd, field.TypeFloat64, value)
	}
	if value, ok := _u.mutation.DailyWindowStart(); ok {
		_spec.SetField(userplatformquota.FieldDailyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.DailyWindowStartCleared() {
		_spec.ClearField(userplatformquota.FieldDailyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.WeeklyWindowStart(); ok {
		_spec.SetField(userplatformquota.FieldWeeklyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.WeeklyWindowStartCleared() {
		_spec.ClearField(userplatformquota.FieldWeeklyWindowStart, field.TypeTime)
	}
	if value, ok := _u.mutation.MonthlyWindowStart(); ok {
		_spec.SetField(userplatformquota.FieldMonthlyWindowStart, field.TypeTime, value)
	}
	if _u.mutation.MonthlyWindowStartCleared() {
		_spec.ClearField(userplatformquota.FieldMonthlyWindowStart, field.TypeTime)
	}
	if _u.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   userplatformquota.UserTable,
			Columns: []string{userplatformquota.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   userplatformquota.UserTable,
			Columns: []string{userplatformquota.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &UserPlatformQuota{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{userplatformquota.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
