// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ChatRecordsUpdate is the builder for updating ChatRecords entities.
type ChatRecordsUpdate struct {
	config
	hooks    []Hook
	mutation *ChatRecordsMutation
}

// Where appends a list predicates to the ChatRecordsUpdate builder.
func (cru *ChatRecordsUpdate) Where(ps ...predicate.ChatRecords) *ChatRecordsUpdate {
	cru.mutation.Where(ps...)
	return cru
}

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

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

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

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

// SetContent sets the "content" field.
func (cru *ChatRecordsUpdate) SetContent(s string) *ChatRecordsUpdate {
	cru.mutation.SetContent(s)
	return cru
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (cru *ChatRecordsUpdate) SetNillableContent(s *string) *ChatRecordsUpdate {
	if s != nil {
		cru.SetContent(*s)
	}
	return cru
}

// SetContentType sets the "content_type" field.
func (cru *ChatRecordsUpdate) SetContentType(u uint8) *ChatRecordsUpdate {
	cru.mutation.ResetContentType()
	cru.mutation.SetContentType(u)
	return cru
}

// SetNillableContentType sets the "content_type" field if the given value is not nil.
func (cru *ChatRecordsUpdate) SetNillableContentType(u *uint8) *ChatRecordsUpdate {
	if u != nil {
		cru.SetContentType(*u)
	}
	return cru
}

// AddContentType adds u to the "content_type" field.
func (cru *ChatRecordsUpdate) AddContentType(u int8) *ChatRecordsUpdate {
	cru.mutation.AddContentType(u)
	return cru
}

// SetSessionID sets the "session_id" field.
func (cru *ChatRecordsUpdate) SetSessionID(u uint64) *ChatRecordsUpdate {
	cru.mutation.ResetSessionID()
	cru.mutation.SetSessionID(u)
	return cru
}

// SetNillableSessionID sets the "session_id" field if the given value is not nil.
func (cru *ChatRecordsUpdate) SetNillableSessionID(u *uint64) *ChatRecordsUpdate {
	if u != nil {
		cru.SetSessionID(*u)
	}
	return cru
}

// AddSessionID adds u to the "session_id" field.
func (cru *ChatRecordsUpdate) AddSessionID(u int64) *ChatRecordsUpdate {
	cru.mutation.AddSessionID(u)
	return cru
}

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

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

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

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

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

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

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

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

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

// Mutation returns the ChatRecordsMutation object of the builder.
func (cru *ChatRecordsUpdate) Mutation() *ChatRecordsMutation {
	return cru.mutation
}

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

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

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

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

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

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

// ChatRecordsUpdateOne is the builder for updating a single ChatRecords entity.
type ChatRecordsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ChatRecordsMutation
}

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

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

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

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

// SetContent sets the "content" field.
func (cruo *ChatRecordsUpdateOne) SetContent(s string) *ChatRecordsUpdateOne {
	cruo.mutation.SetContent(s)
	return cruo
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (cruo *ChatRecordsUpdateOne) SetNillableContent(s *string) *ChatRecordsUpdateOne {
	if s != nil {
		cruo.SetContent(*s)
	}
	return cruo
}

// SetContentType sets the "content_type" field.
func (cruo *ChatRecordsUpdateOne) SetContentType(u uint8) *ChatRecordsUpdateOne {
	cruo.mutation.ResetContentType()
	cruo.mutation.SetContentType(u)
	return cruo
}

// SetNillableContentType sets the "content_type" field if the given value is not nil.
func (cruo *ChatRecordsUpdateOne) SetNillableContentType(u *uint8) *ChatRecordsUpdateOne {
	if u != nil {
		cruo.SetContentType(*u)
	}
	return cruo
}

// AddContentType adds u to the "content_type" field.
func (cruo *ChatRecordsUpdateOne) AddContentType(u int8) *ChatRecordsUpdateOne {
	cruo.mutation.AddContentType(u)
	return cruo
}

// SetSessionID sets the "session_id" field.
func (cruo *ChatRecordsUpdateOne) SetSessionID(u uint64) *ChatRecordsUpdateOne {
	cruo.mutation.ResetSessionID()
	cruo.mutation.SetSessionID(u)
	return cruo
}

// SetNillableSessionID sets the "session_id" field if the given value is not nil.
func (cruo *ChatRecordsUpdateOne) SetNillableSessionID(u *uint64) *ChatRecordsUpdateOne {
	if u != nil {
		cruo.SetSessionID(*u)
	}
	return cruo
}

// AddSessionID adds u to the "session_id" field.
func (cruo *ChatRecordsUpdateOne) AddSessionID(u int64) *ChatRecordsUpdateOne {
	cruo.mutation.AddSessionID(u)
	return cruo
}

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

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

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

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

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

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

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

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

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

// Mutation returns the ChatRecordsMutation object of the builder.
func (cruo *ChatRecordsUpdateOne) Mutation() *ChatRecordsMutation {
	return cruo.mutation
}

// Where appends a list predicates to the ChatRecordsUpdate builder.
func (cruo *ChatRecordsUpdateOne) Where(ps ...predicate.ChatRecords) *ChatRecordsUpdateOne {
	cruo.mutation.Where(ps...)
	return cruo
}

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

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

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

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

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

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

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