// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ChatSessionUpdate is the builder for updating ChatSession entities.
type ChatSessionUpdate struct {
	config
	hooks    []Hook
	mutation *ChatSessionMutation
}

// Where appends a list predicates to the ChatSessionUpdate builder.
func (csu *ChatSessionUpdate) Where(ps ...predicate.ChatSession) *ChatSessionUpdate {
	csu.mutation.Where(ps...)
	return csu
}

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

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

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

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

// SetName sets the "name" field.
func (csu *ChatSessionUpdate) SetName(s string) *ChatSessionUpdate {
	csu.mutation.SetName(s)
	return csu
}

// SetNillableName sets the "name" field if the given value is not nil.
func (csu *ChatSessionUpdate) SetNillableName(s *string) *ChatSessionUpdate {
	if s != nil {
		csu.SetName(*s)
	}
	return csu
}

// SetUserID sets the "user_id" field.
func (csu *ChatSessionUpdate) SetUserID(u uint64) *ChatSessionUpdate {
	csu.mutation.ResetUserID()
	csu.mutation.SetUserID(u)
	return csu
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (csu *ChatSessionUpdate) SetNillableUserID(u *uint64) *ChatSessionUpdate {
	if u != nil {
		csu.SetUserID(*u)
	}
	return csu
}

// AddUserID adds u to the "user_id" field.
func (csu *ChatSessionUpdate) AddUserID(u int64) *ChatSessionUpdate {
	csu.mutation.AddUserID(u)
	return csu
}

// SetBotID sets the "bot_id" field.
func (csu *ChatSessionUpdate) SetBotID(u uint64) *ChatSessionUpdate {
	csu.mutation.ResetBotID()
	csu.mutation.SetBotID(u)
	return csu
}

// SetNillableBotID sets the "bot_id" field if the given value is not nil.
func (csu *ChatSessionUpdate) SetNillableBotID(u *uint64) *ChatSessionUpdate {
	if u != nil {
		csu.SetBotID(*u)
	}
	return csu
}

// AddBotID adds u to the "bot_id" field.
func (csu *ChatSessionUpdate) AddBotID(u int64) *ChatSessionUpdate {
	csu.mutation.AddBotID(u)
	return csu
}

// SetBotType sets the "bot_type" field.
func (csu *ChatSessionUpdate) SetBotType(u uint8) *ChatSessionUpdate {
	csu.mutation.ResetBotType()
	csu.mutation.SetBotType(u)
	return csu
}

// SetNillableBotType sets the "bot_type" field if the given value is not nil.
func (csu *ChatSessionUpdate) SetNillableBotType(u *uint8) *ChatSessionUpdate {
	if u != nil {
		csu.SetBotType(*u)
	}
	return csu
}

// AddBotType adds u to the "bot_type" field.
func (csu *ChatSessionUpdate) AddBotType(u int8) *ChatSessionUpdate {
	csu.mutation.AddBotType(u)
	return csu
}

// Mutation returns the ChatSessionMutation object of the builder.
func (csu *ChatSessionUpdate) Mutation() *ChatSessionMutation {
	return csu.mutation
}

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

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

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

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

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

func (csu *ChatSessionUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(chatsession.Table, chatsession.Columns, sqlgraph.NewFieldSpec(chatsession.FieldID, field.TypeUint64))
	if ps := csu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := csu.mutation.UpdatedAt(); ok {
		_spec.SetField(chatsession.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := csu.mutation.DeletedAt(); ok {
		_spec.SetField(chatsession.FieldDeletedAt, field.TypeTime, value)
	}
	if csu.mutation.DeletedAtCleared() {
		_spec.ClearField(chatsession.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := csu.mutation.Name(); ok {
		_spec.SetField(chatsession.FieldName, field.TypeString, value)
	}
	if value, ok := csu.mutation.UserID(); ok {
		_spec.SetField(chatsession.FieldUserID, field.TypeUint64, value)
	}
	if value, ok := csu.mutation.AddedUserID(); ok {
		_spec.AddField(chatsession.FieldUserID, field.TypeUint64, value)
	}
	if value, ok := csu.mutation.BotID(); ok {
		_spec.SetField(chatsession.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := csu.mutation.AddedBotID(); ok {
		_spec.AddField(chatsession.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := csu.mutation.BotType(); ok {
		_spec.SetField(chatsession.FieldBotType, field.TypeUint8, value)
	}
	if value, ok := csu.mutation.AddedBotType(); ok {
		_spec.AddField(chatsession.FieldBotType, field.TypeUint8, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, csu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{chatsession.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	csu.mutation.done = true
	return n, nil
}

// ChatSessionUpdateOne is the builder for updating a single ChatSession entity.
type ChatSessionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ChatSessionMutation
}

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

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

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

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

// SetName sets the "name" field.
func (csuo *ChatSessionUpdateOne) SetName(s string) *ChatSessionUpdateOne {
	csuo.mutation.SetName(s)
	return csuo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (csuo *ChatSessionUpdateOne) SetNillableName(s *string) *ChatSessionUpdateOne {
	if s != nil {
		csuo.SetName(*s)
	}
	return csuo
}

// SetUserID sets the "user_id" field.
func (csuo *ChatSessionUpdateOne) SetUserID(u uint64) *ChatSessionUpdateOne {
	csuo.mutation.ResetUserID()
	csuo.mutation.SetUserID(u)
	return csuo
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (csuo *ChatSessionUpdateOne) SetNillableUserID(u *uint64) *ChatSessionUpdateOne {
	if u != nil {
		csuo.SetUserID(*u)
	}
	return csuo
}

// AddUserID adds u to the "user_id" field.
func (csuo *ChatSessionUpdateOne) AddUserID(u int64) *ChatSessionUpdateOne {
	csuo.mutation.AddUserID(u)
	return csuo
}

// SetBotID sets the "bot_id" field.
func (csuo *ChatSessionUpdateOne) SetBotID(u uint64) *ChatSessionUpdateOne {
	csuo.mutation.ResetBotID()
	csuo.mutation.SetBotID(u)
	return csuo
}

// SetNillableBotID sets the "bot_id" field if the given value is not nil.
func (csuo *ChatSessionUpdateOne) SetNillableBotID(u *uint64) *ChatSessionUpdateOne {
	if u != nil {
		csuo.SetBotID(*u)
	}
	return csuo
}

// AddBotID adds u to the "bot_id" field.
func (csuo *ChatSessionUpdateOne) AddBotID(u int64) *ChatSessionUpdateOne {
	csuo.mutation.AddBotID(u)
	return csuo
}

// SetBotType sets the "bot_type" field.
func (csuo *ChatSessionUpdateOne) SetBotType(u uint8) *ChatSessionUpdateOne {
	csuo.mutation.ResetBotType()
	csuo.mutation.SetBotType(u)
	return csuo
}

// SetNillableBotType sets the "bot_type" field if the given value is not nil.
func (csuo *ChatSessionUpdateOne) SetNillableBotType(u *uint8) *ChatSessionUpdateOne {
	if u != nil {
		csuo.SetBotType(*u)
	}
	return csuo
}

// AddBotType adds u to the "bot_type" field.
func (csuo *ChatSessionUpdateOne) AddBotType(u int8) *ChatSessionUpdateOne {
	csuo.mutation.AddBotType(u)
	return csuo
}

// Mutation returns the ChatSessionMutation object of the builder.
func (csuo *ChatSessionUpdateOne) Mutation() *ChatSessionMutation {
	return csuo.mutation
}

// Where appends a list predicates to the ChatSessionUpdate builder.
func (csuo *ChatSessionUpdateOne) Where(ps ...predicate.ChatSession) *ChatSessionUpdateOne {
	csuo.mutation.Where(ps...)
	return csuo
}

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

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

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

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

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

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

func (csuo *ChatSessionUpdateOne) sqlSave(ctx context.Context) (_node *ChatSession, err error) {
	_spec := sqlgraph.NewUpdateSpec(chatsession.Table, chatsession.Columns, sqlgraph.NewFieldSpec(chatsession.FieldID, field.TypeUint64))
	id, ok := csuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ChatSession.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := csuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, chatsession.FieldID)
		for _, f := range fields {
			if !chatsession.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != chatsession.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := csuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := csuo.mutation.UpdatedAt(); ok {
		_spec.SetField(chatsession.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := csuo.mutation.DeletedAt(); ok {
		_spec.SetField(chatsession.FieldDeletedAt, field.TypeTime, value)
	}
	if csuo.mutation.DeletedAtCleared() {
		_spec.ClearField(chatsession.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := csuo.mutation.Name(); ok {
		_spec.SetField(chatsession.FieldName, field.TypeString, value)
	}
	if value, ok := csuo.mutation.UserID(); ok {
		_spec.SetField(chatsession.FieldUserID, field.TypeUint64, value)
	}
	if value, ok := csuo.mutation.AddedUserID(); ok {
		_spec.AddField(chatsession.FieldUserID, field.TypeUint64, value)
	}
	if value, ok := csuo.mutation.BotID(); ok {
		_spec.SetField(chatsession.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := csuo.mutation.AddedBotID(); ok {
		_spec.AddField(chatsession.FieldBotID, field.TypeUint64, value)
	}
	if value, ok := csuo.mutation.BotType(); ok {
		_spec.SetField(chatsession.FieldBotType, field.TypeUint8, value)
	}
	if value, ok := csuo.mutation.AddedBotType(); ok {
		_spec.AddField(chatsession.FieldBotType, field.TypeUint8, value)
	}
	_node = &ChatSession{config: csuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, csuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{chatsession.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	csuo.mutation.done = true
	return _node, nil
}