// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ChatRecordsCreate is the builder for creating a ChatRecords entity.
type ChatRecordsCreate struct {
	config
	mutation *ChatRecordsMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (crc *ChatRecordsCreate) SetCreatedAt(t time.Time) *ChatRecordsCreate {
	crc.mutation.SetCreatedAt(t)
	return crc
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (crc *ChatRecordsCreate) SetNillableCreatedAt(t *time.Time) *ChatRecordsCreate {
	if t != nil {
		crc.SetCreatedAt(*t)
	}
	return crc
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (crc *ChatRecordsCreate) SetNillableUpdatedAt(t *time.Time) *ChatRecordsCreate {
	if t != nil {
		crc.SetUpdatedAt(*t)
	}
	return crc
}

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

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

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

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

// SetContentType sets the "content_type" field.
func (crc *ChatRecordsCreate) SetContentType(u uint8) *ChatRecordsCreate {
	crc.mutation.SetContentType(u)
	return crc
}

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

// SetSessionID sets the "session_id" field.
func (crc *ChatRecordsCreate) SetSessionID(u uint64) *ChatRecordsCreate {
	crc.mutation.SetSessionID(u)
	return crc
}

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

// SetUserID sets the "user_id" field.
func (crc *ChatRecordsCreate) SetUserID(u uint64) *ChatRecordsCreate {
	crc.mutation.SetUserID(u)
	return crc
}

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

// SetBotID sets the "bot_id" field.
func (crc *ChatRecordsCreate) SetBotID(u uint64) *ChatRecordsCreate {
	crc.mutation.SetBotID(u)
	return crc
}

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

// SetBotType sets the "bot_type" field.
func (crc *ChatRecordsCreate) SetBotType(u uint8) *ChatRecordsCreate {
	crc.mutation.SetBotType(u)
	return crc
}

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

// SetID sets the "id" field.
func (crc *ChatRecordsCreate) SetID(u uint64) *ChatRecordsCreate {
	crc.mutation.SetID(u)
	return crc
}

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

// Save creates the ChatRecords in the database.
func (crc *ChatRecordsCreate) Save(ctx context.Context) (*ChatRecords, error) {
	if err := crc.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, crc.sqlSave, crc.mutation, crc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (crc *ChatRecordsCreate) SaveX(ctx context.Context) *ChatRecords {
	v, err := crc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// defaults sets the default values of the builder before save.
func (crc *ChatRecordsCreate) defaults() error {
	if _, ok := crc.mutation.CreatedAt(); !ok {
		if chatrecords.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized chatrecords.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := chatrecords.DefaultCreatedAt()
		crc.mutation.SetCreatedAt(v)
	}
	if _, ok := crc.mutation.UpdatedAt(); !ok {
		if chatrecords.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized chatrecords.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := chatrecords.DefaultUpdatedAt()
		crc.mutation.SetUpdatedAt(v)
	}
	if _, ok := crc.mutation.Content(); !ok {
		v := chatrecords.DefaultContent
		crc.mutation.SetContent(v)
	}
	if _, ok := crc.mutation.ContentType(); !ok {
		v := chatrecords.DefaultContentType
		crc.mutation.SetContentType(v)
	}
	if _, ok := crc.mutation.SessionID(); !ok {
		v := chatrecords.DefaultSessionID
		crc.mutation.SetSessionID(v)
	}
	if _, ok := crc.mutation.UserID(); !ok {
		v := chatrecords.DefaultUserID
		crc.mutation.SetUserID(v)
	}
	if _, ok := crc.mutation.BotID(); !ok {
		v := chatrecords.DefaultBotID
		crc.mutation.SetBotID(v)
	}
	if _, ok := crc.mutation.BotType(); !ok {
		v := chatrecords.DefaultBotType
		crc.mutation.SetBotType(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (crc *ChatRecordsCreate) check() error {
	if _, ok := crc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ChatRecords.created_at"`)}
	}
	if _, ok := crc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "ChatRecords.updated_at"`)}
	}
	if _, ok := crc.mutation.Content(); !ok {
		return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "ChatRecords.content"`)}
	}
	if _, ok := crc.mutation.ContentType(); !ok {
		return &ValidationError{Name: "content_type", err: errors.New(`ent: missing required field "ChatRecords.content_type"`)}
	}
	if _, ok := crc.mutation.SessionID(); !ok {
		return &ValidationError{Name: "session_id", err: errors.New(`ent: missing required field "ChatRecords.session_id"`)}
	}
	if _, ok := crc.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "ChatRecords.user_id"`)}
	}
	if _, ok := crc.mutation.BotID(); !ok {
		return &ValidationError{Name: "bot_id", err: errors.New(`ent: missing required field "ChatRecords.bot_id"`)}
	}
	if _, ok := crc.mutation.BotType(); !ok {
		return &ValidationError{Name: "bot_type", err: errors.New(`ent: missing required field "ChatRecords.bot_type"`)}
	}
	return nil
}

func (crc *ChatRecordsCreate) sqlSave(ctx context.Context) (*ChatRecords, error) {
	if err := crc.check(); err != nil {
		return nil, err
	}
	_node, _spec := crc.createSpec()
	if err := sqlgraph.CreateNode(ctx, crc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = uint64(id)
	}
	crc.mutation.id = &_node.ID
	crc.mutation.done = true
	return _node, nil
}

func (crc *ChatRecordsCreate) createSpec() (*ChatRecords, *sqlgraph.CreateSpec) {
	var (
		_node = &ChatRecords{config: crc.config}
		_spec = sqlgraph.NewCreateSpec(chatrecords.Table, sqlgraph.NewFieldSpec(chatrecords.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = crc.conflict
	if id, ok := crc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := crc.mutation.CreatedAt(); ok {
		_spec.SetField(chatrecords.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := crc.mutation.UpdatedAt(); ok {
		_spec.SetField(chatrecords.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := crc.mutation.DeletedAt(); ok {
		_spec.SetField(chatrecords.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := crc.mutation.Content(); ok {
		_spec.SetField(chatrecords.FieldContent, field.TypeString, value)
		_node.Content = value
	}
	if value, ok := crc.mutation.ContentType(); ok {
		_spec.SetField(chatrecords.FieldContentType, field.TypeUint8, value)
		_node.ContentType = value
	}
	if value, ok := crc.mutation.SessionID(); ok {
		_spec.SetField(chatrecords.FieldSessionID, field.TypeUint64, value)
		_node.SessionID = value
	}
	if value, ok := crc.mutation.UserID(); ok {
		_spec.SetField(chatrecords.FieldUserID, field.TypeUint64, value)
		_node.UserID = value
	}
	if value, ok := crc.mutation.BotID(); ok {
		_spec.SetField(chatrecords.FieldBotID, field.TypeUint64, value)
		_node.BotID = value
	}
	if value, ok := crc.mutation.BotType(); ok {
		_spec.SetField(chatrecords.FieldBotType, field.TypeUint8, value)
		_node.BotType = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.ChatRecords.Create().
//		SetCreatedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.ChatRecordsUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (crc *ChatRecordsCreate) OnConflict(opts ...sql.ConflictOption) *ChatRecordsUpsertOne {
	crc.conflict = opts
	return &ChatRecordsUpsertOne{
		create: crc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.ChatRecords.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (crc *ChatRecordsCreate) OnConflictColumns(columns ...string) *ChatRecordsUpsertOne {
	crc.conflict = append(crc.conflict, sql.ConflictColumns(columns...))
	return &ChatRecordsUpsertOne{
		create: crc,
	}
}

type (
	// ChatRecordsUpsertOne is the builder for "upsert"-ing
	//  one ChatRecords node.
	ChatRecordsUpsertOne struct {
		create *ChatRecordsCreate
	}

	// ChatRecordsUpsert is the "OnConflict" setter.
	ChatRecordsUpsert struct {
		*sql.UpdateSet
	}
)

// SetUpdatedAt sets the "updated_at" field.
func (u *ChatRecordsUpsert) SetUpdatedAt(v time.Time) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateUpdatedAt() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldUpdatedAt)
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *ChatRecordsUpsert) SetDeletedAt(v time.Time) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateDeletedAt() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ChatRecordsUpsert) ClearDeletedAt() *ChatRecordsUpsert {
	u.SetNull(chatrecords.FieldDeletedAt)
	return u
}

// SetContent sets the "content" field.
func (u *ChatRecordsUpsert) SetContent(v string) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldContent, v)
	return u
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateContent() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldContent)
	return u
}

// SetContentType sets the "content_type" field.
func (u *ChatRecordsUpsert) SetContentType(v uint8) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldContentType, v)
	return u
}

// UpdateContentType sets the "content_type" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateContentType() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldContentType)
	return u
}

// AddContentType adds v to the "content_type" field.
func (u *ChatRecordsUpsert) AddContentType(v uint8) *ChatRecordsUpsert {
	u.Add(chatrecords.FieldContentType, v)
	return u
}

// SetSessionID sets the "session_id" field.
func (u *ChatRecordsUpsert) SetSessionID(v uint64) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldSessionID, v)
	return u
}

// UpdateSessionID sets the "session_id" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateSessionID() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldSessionID)
	return u
}

// AddSessionID adds v to the "session_id" field.
func (u *ChatRecordsUpsert) AddSessionID(v uint64) *ChatRecordsUpsert {
	u.Add(chatrecords.FieldSessionID, v)
	return u
}

// SetUserID sets the "user_id" field.
func (u *ChatRecordsUpsert) SetUserID(v uint64) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateUserID() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldUserID)
	return u
}

// AddUserID adds v to the "user_id" field.
func (u *ChatRecordsUpsert) AddUserID(v uint64) *ChatRecordsUpsert {
	u.Add(chatrecords.FieldUserID, v)
	return u
}

// SetBotID sets the "bot_id" field.
func (u *ChatRecordsUpsert) SetBotID(v uint64) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldBotID, v)
	return u
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateBotID() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldBotID)
	return u
}

// AddBotID adds v to the "bot_id" field.
func (u *ChatRecordsUpsert) AddBotID(v uint64) *ChatRecordsUpsert {
	u.Add(chatrecords.FieldBotID, v)
	return u
}

// SetBotType sets the "bot_type" field.
func (u *ChatRecordsUpsert) SetBotType(v uint8) *ChatRecordsUpsert {
	u.Set(chatrecords.FieldBotType, v)
	return u
}

// UpdateBotType sets the "bot_type" field to the value that was provided on create.
func (u *ChatRecordsUpsert) UpdateBotType() *ChatRecordsUpsert {
	u.SetExcluded(chatrecords.FieldBotType)
	return u
}

// AddBotType adds v to the "bot_type" field.
func (u *ChatRecordsUpsert) AddBotType(v uint8) *ChatRecordsUpsert {
	u.Add(chatrecords.FieldBotType, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.ChatRecords.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(chatrecords.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ChatRecordsUpsertOne) UpdateNewValues() *ChatRecordsUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(chatrecords.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(chatrecords.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.ChatRecords.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *ChatRecordsUpsertOne) Ignore() *ChatRecordsUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *ChatRecordsUpsertOne) DoNothing() *ChatRecordsUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the ChatRecordsCreate.OnConflict
// documentation for more info.
func (u *ChatRecordsUpsertOne) Update(set func(*ChatRecordsUpsert)) *ChatRecordsUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&ChatRecordsUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *ChatRecordsUpsertOne) SetUpdatedAt(v time.Time) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateUpdatedAt() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *ChatRecordsUpsertOne) SetDeletedAt(v time.Time) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateDeletedAt() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ChatRecordsUpsertOne) ClearDeletedAt() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.ClearDeletedAt()
	})
}

// SetContent sets the "content" field.
func (u *ChatRecordsUpsertOne) SetContent(v string) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetContent(v)
	})
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateContent() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateContent()
	})
}

// SetContentType sets the "content_type" field.
func (u *ChatRecordsUpsertOne) SetContentType(v uint8) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetContentType(v)
	})
}

// AddContentType adds v to the "content_type" field.
func (u *ChatRecordsUpsertOne) AddContentType(v uint8) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddContentType(v)
	})
}

// UpdateContentType sets the "content_type" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateContentType() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateContentType()
	})
}

// SetSessionID sets the "session_id" field.
func (u *ChatRecordsUpsertOne) SetSessionID(v uint64) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetSessionID(v)
	})
}

// AddSessionID adds v to the "session_id" field.
func (u *ChatRecordsUpsertOne) AddSessionID(v uint64) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddSessionID(v)
	})
}

// UpdateSessionID sets the "session_id" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateSessionID() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateSessionID()
	})
}

// SetUserID sets the "user_id" field.
func (u *ChatRecordsUpsertOne) SetUserID(v uint64) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetUserID(v)
	})
}

// AddUserID adds v to the "user_id" field.
func (u *ChatRecordsUpsertOne) AddUserID(v uint64) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateUserID() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateUserID()
	})
}

// SetBotID sets the "bot_id" field.
func (u *ChatRecordsUpsertOne) SetBotID(v uint64) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetBotID(v)
	})
}

// AddBotID adds v to the "bot_id" field.
func (u *ChatRecordsUpsertOne) AddBotID(v uint64) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddBotID(v)
	})
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateBotID() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateBotID()
	})
}

// SetBotType sets the "bot_type" field.
func (u *ChatRecordsUpsertOne) SetBotType(v uint8) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetBotType(v)
	})
}

// AddBotType adds v to the "bot_type" field.
func (u *ChatRecordsUpsertOne) AddBotType(v uint8) *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddBotType(v)
	})
}

// UpdateBotType sets the "bot_type" field to the value that was provided on create.
func (u *ChatRecordsUpsertOne) UpdateBotType() *ChatRecordsUpsertOne {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateBotType()
	})
}

// Exec executes the query.
func (u *ChatRecordsUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for ChatRecordsCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *ChatRecordsUpsertOne) ID(ctx context.Context) (id uint64, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *ChatRecordsUpsertOne) IDX(ctx context.Context) uint64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// ChatRecordsCreateBulk is the builder for creating many ChatRecords entities in bulk.
type ChatRecordsCreateBulk struct {
	config
	err      error
	builders []*ChatRecordsCreate
	conflict []sql.ConflictOption
}

// Save creates the ChatRecords entities in the database.
func (crcb *ChatRecordsCreateBulk) Save(ctx context.Context) ([]*ChatRecords, error) {
	if crcb.err != nil {
		return nil, crcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(crcb.builders))
	nodes := make([]*ChatRecords, len(crcb.builders))
	mutators := make([]Mutator, len(crcb.builders))
	for i := range crcb.builders {
		func(i int, root context.Context) {
			builder := crcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*ChatRecordsMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, crcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = crcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, crcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = uint64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, crcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (crcb *ChatRecordsCreateBulk) SaveX(ctx context.Context) []*ChatRecords {
	v, err := crcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.ChatRecords.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.ChatRecordsUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (crcb *ChatRecordsCreateBulk) OnConflict(opts ...sql.ConflictOption) *ChatRecordsUpsertBulk {
	crcb.conflict = opts
	return &ChatRecordsUpsertBulk{
		create: crcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.ChatRecords.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (crcb *ChatRecordsCreateBulk) OnConflictColumns(columns ...string) *ChatRecordsUpsertBulk {
	crcb.conflict = append(crcb.conflict, sql.ConflictColumns(columns...))
	return &ChatRecordsUpsertBulk{
		create: crcb,
	}
}

// ChatRecordsUpsertBulk is the builder for "upsert"-ing
// a bulk of ChatRecords nodes.
type ChatRecordsUpsertBulk struct {
	create *ChatRecordsCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.ChatRecords.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(chatrecords.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *ChatRecordsUpsertBulk) UpdateNewValues() *ChatRecordsUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(chatrecords.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(chatrecords.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.ChatRecords.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *ChatRecordsUpsertBulk) Ignore() *ChatRecordsUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *ChatRecordsUpsertBulk) DoNothing() *ChatRecordsUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the ChatRecordsCreateBulk.OnConflict
// documentation for more info.
func (u *ChatRecordsUpsertBulk) Update(set func(*ChatRecordsUpsert)) *ChatRecordsUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&ChatRecordsUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *ChatRecordsUpsertBulk) SetUpdatedAt(v time.Time) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateUpdatedAt() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *ChatRecordsUpsertBulk) SetDeletedAt(v time.Time) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateDeletedAt() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ChatRecordsUpsertBulk) ClearDeletedAt() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.ClearDeletedAt()
	})
}

// SetContent sets the "content" field.
func (u *ChatRecordsUpsertBulk) SetContent(v string) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetContent(v)
	})
}

// UpdateContent sets the "content" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateContent() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateContent()
	})
}

// SetContentType sets the "content_type" field.
func (u *ChatRecordsUpsertBulk) SetContentType(v uint8) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetContentType(v)
	})
}

// AddContentType adds v to the "content_type" field.
func (u *ChatRecordsUpsertBulk) AddContentType(v uint8) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddContentType(v)
	})
}

// UpdateContentType sets the "content_type" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateContentType() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateContentType()
	})
}

// SetSessionID sets the "session_id" field.
func (u *ChatRecordsUpsertBulk) SetSessionID(v uint64) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetSessionID(v)
	})
}

// AddSessionID adds v to the "session_id" field.
func (u *ChatRecordsUpsertBulk) AddSessionID(v uint64) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddSessionID(v)
	})
}

// UpdateSessionID sets the "session_id" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateSessionID() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateSessionID()
	})
}

// SetUserID sets the "user_id" field.
func (u *ChatRecordsUpsertBulk) SetUserID(v uint64) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetUserID(v)
	})
}

// AddUserID adds v to the "user_id" field.
func (u *ChatRecordsUpsertBulk) AddUserID(v uint64) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateUserID() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateUserID()
	})
}

// SetBotID sets the "bot_id" field.
func (u *ChatRecordsUpsertBulk) SetBotID(v uint64) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetBotID(v)
	})
}

// AddBotID adds v to the "bot_id" field.
func (u *ChatRecordsUpsertBulk) AddBotID(v uint64) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddBotID(v)
	})
}

// UpdateBotID sets the "bot_id" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateBotID() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateBotID()
	})
}

// SetBotType sets the "bot_type" field.
func (u *ChatRecordsUpsertBulk) SetBotType(v uint8) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.SetBotType(v)
	})
}

// AddBotType adds v to the "bot_type" field.
func (u *ChatRecordsUpsertBulk) AddBotType(v uint8) *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.AddBotType(v)
	})
}

// UpdateBotType sets the "bot_type" field to the value that was provided on create.
func (u *ChatRecordsUpsertBulk) UpdateBotType() *ChatRecordsUpsertBulk {
	return u.Update(func(s *ChatRecordsUpsert) {
		s.UpdateBotType()
	})
}

// Exec executes the query.
func (u *ChatRecordsUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the ChatRecordsCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for ChatRecordsCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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