// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"
	"wechat-api/ent/contact"
	"wechat-api/ent/custom_types"
	"wechat-api/ent/messagerecords"
	"wechat-api/ent/sopnode"
	"wechat-api/ent/sopstage"

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

// MessageRecordsCreate is the builder for creating a MessageRecords entity.
type MessageRecordsCreate struct {
	config
	mutation *MessageRecordsMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

// SetStatus sets the "status" field.
func (mrc *MessageRecordsCreate) SetStatus(u uint8) *MessageRecordsCreate {
	mrc.mutation.SetStatus(u)
	return mrc
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableStatus(u *uint8) *MessageRecordsCreate {
	if u != nil {
		mrc.SetStatus(*u)
	}
	return mrc
}

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

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

// SetBotWxid sets the "bot_wxid" field.
func (mrc *MessageRecordsCreate) SetBotWxid(s string) *MessageRecordsCreate {
	mrc.mutation.SetBotWxid(s)
	return mrc
}

// SetContactID sets the "contact_id" field.
func (mrc *MessageRecordsCreate) SetContactID(u uint64) *MessageRecordsCreate {
	mrc.mutation.SetContactID(u)
	return mrc
}

// SetNillableContactID sets the "contact_id" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableContactID(u *uint64) *MessageRecordsCreate {
	if u != nil {
		mrc.SetContactID(*u)
	}
	return mrc
}

// SetContactType sets the "contact_type" field.
func (mrc *MessageRecordsCreate) SetContactType(i int) *MessageRecordsCreate {
	mrc.mutation.SetContactType(i)
	return mrc
}

// SetNillableContactType sets the "contact_type" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableContactType(i *int) *MessageRecordsCreate {
	if i != nil {
		mrc.SetContactType(*i)
	}
	return mrc
}

// SetContactWxid sets the "contact_wxid" field.
func (mrc *MessageRecordsCreate) SetContactWxid(s string) *MessageRecordsCreate {
	mrc.mutation.SetContactWxid(s)
	return mrc
}

// SetNillableContactWxid sets the "contact_wxid" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableContactWxid(s *string) *MessageRecordsCreate {
	if s != nil {
		mrc.SetContactWxid(*s)
	}
	return mrc
}

// SetContentType sets the "content_type" field.
func (mrc *MessageRecordsCreate) SetContentType(i int) *MessageRecordsCreate {
	mrc.mutation.SetContentType(i)
	return mrc
}

// SetNillableContentType sets the "content_type" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableContentType(i *int) *MessageRecordsCreate {
	if i != nil {
		mrc.SetContentType(*i)
	}
	return mrc
}

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

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

// SetMeta sets the "meta" field.
func (mrc *MessageRecordsCreate) SetMeta(ct custom_types.Meta) *MessageRecordsCreate {
	mrc.mutation.SetMeta(ct)
	return mrc
}

// SetNillableMeta sets the "meta" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableMeta(ct *custom_types.Meta) *MessageRecordsCreate {
	if ct != nil {
		mrc.SetMeta(*ct)
	}
	return mrc
}

// SetErrorDetail sets the "error_detail" field.
func (mrc *MessageRecordsCreate) SetErrorDetail(s string) *MessageRecordsCreate {
	mrc.mutation.SetErrorDetail(s)
	return mrc
}

// SetNillableErrorDetail sets the "error_detail" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableErrorDetail(s *string) *MessageRecordsCreate {
	if s != nil {
		mrc.SetErrorDetail(*s)
	}
	return mrc
}

// SetSendTime sets the "send_time" field.
func (mrc *MessageRecordsCreate) SetSendTime(t time.Time) *MessageRecordsCreate {
	mrc.mutation.SetSendTime(t)
	return mrc
}

// SetNillableSendTime sets the "send_time" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableSendTime(t *time.Time) *MessageRecordsCreate {
	if t != nil {
		mrc.SetSendTime(*t)
	}
	return mrc
}

// SetSourceType sets the "source_type" field.
func (mrc *MessageRecordsCreate) SetSourceType(i int) *MessageRecordsCreate {
	mrc.mutation.SetSourceType(i)
	return mrc
}

// SetNillableSourceType sets the "source_type" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableSourceType(i *int) *MessageRecordsCreate {
	if i != nil {
		mrc.SetSourceType(*i)
	}
	return mrc
}

// SetSourceID sets the "source_id" field.
func (mrc *MessageRecordsCreate) SetSourceID(u uint64) *MessageRecordsCreate {
	mrc.mutation.SetSourceID(u)
	return mrc
}

// SetNillableSourceID sets the "source_id" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableSourceID(u *uint64) *MessageRecordsCreate {
	if u != nil {
		mrc.SetSourceID(*u)
	}
	return mrc
}

// SetSubSourceID sets the "sub_source_id" field.
func (mrc *MessageRecordsCreate) SetSubSourceID(u uint64) *MessageRecordsCreate {
	mrc.mutation.SetSubSourceID(u)
	return mrc
}

// SetNillableSubSourceID sets the "sub_source_id" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableSubSourceID(u *uint64) *MessageRecordsCreate {
	if u != nil {
		mrc.SetSubSourceID(*u)
	}
	return mrc
}

// SetOrganizationID sets the "organization_id" field.
func (mrc *MessageRecordsCreate) SetOrganizationID(u uint64) *MessageRecordsCreate {
	mrc.mutation.SetOrganizationID(u)
	return mrc
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableOrganizationID(u *uint64) *MessageRecordsCreate {
	if u != nil {
		mrc.SetOrganizationID(*u)
	}
	return mrc
}

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

// SetSopStageID sets the "sop_stage" edge to the SopStage entity by ID.
func (mrc *MessageRecordsCreate) SetSopStageID(id uint64) *MessageRecordsCreate {
	mrc.mutation.SetSopStageID(id)
	return mrc
}

// SetNillableSopStageID sets the "sop_stage" edge to the SopStage entity by ID if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableSopStageID(id *uint64) *MessageRecordsCreate {
	if id != nil {
		mrc = mrc.SetSopStageID(*id)
	}
	return mrc
}

// SetSopStage sets the "sop_stage" edge to the SopStage entity.
func (mrc *MessageRecordsCreate) SetSopStage(s *SopStage) *MessageRecordsCreate {
	return mrc.SetSopStageID(s.ID)
}

// SetSopNodeID sets the "sop_node" edge to the SopNode entity by ID.
func (mrc *MessageRecordsCreate) SetSopNodeID(id uint64) *MessageRecordsCreate {
	mrc.mutation.SetSopNodeID(id)
	return mrc
}

// SetNillableSopNodeID sets the "sop_node" edge to the SopNode entity by ID if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableSopNodeID(id *uint64) *MessageRecordsCreate {
	if id != nil {
		mrc = mrc.SetSopNodeID(*id)
	}
	return mrc
}

// SetSopNode sets the "sop_node" edge to the SopNode entity.
func (mrc *MessageRecordsCreate) SetSopNode(s *SopNode) *MessageRecordsCreate {
	return mrc.SetSopNodeID(s.ID)
}

// SetMessageContactID sets the "message_contact" edge to the Contact entity by ID.
func (mrc *MessageRecordsCreate) SetMessageContactID(id uint64) *MessageRecordsCreate {
	mrc.mutation.SetMessageContactID(id)
	return mrc
}

// SetNillableMessageContactID sets the "message_contact" edge to the Contact entity by ID if the given value is not nil.
func (mrc *MessageRecordsCreate) SetNillableMessageContactID(id *uint64) *MessageRecordsCreate {
	if id != nil {
		mrc = mrc.SetMessageContactID(*id)
	}
	return mrc
}

// SetMessageContact sets the "message_contact" edge to the Contact entity.
func (mrc *MessageRecordsCreate) SetMessageContact(c *Contact) *MessageRecordsCreate {
	return mrc.SetMessageContactID(c.ID)
}

// Mutation returns the MessageRecordsMutation object of the builder.
func (mrc *MessageRecordsCreate) Mutation() *MessageRecordsMutation {
	return mrc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (mrc *MessageRecordsCreate) defaults() error {
	if _, ok := mrc.mutation.CreatedAt(); !ok {
		if messagerecords.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized messagerecords.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := messagerecords.DefaultCreatedAt()
		mrc.mutation.SetCreatedAt(v)
	}
	if _, ok := mrc.mutation.UpdatedAt(); !ok {
		if messagerecords.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized messagerecords.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := messagerecords.DefaultUpdatedAt()
		mrc.mutation.SetUpdatedAt(v)
	}
	if _, ok := mrc.mutation.Status(); !ok {
		v := messagerecords.DefaultStatus
		mrc.mutation.SetStatus(v)
	}
	if _, ok := mrc.mutation.ContactType(); !ok {
		v := messagerecords.DefaultContactType
		mrc.mutation.SetContactType(v)
	}
	if _, ok := mrc.mutation.ContactWxid(); !ok {
		v := messagerecords.DefaultContactWxid
		mrc.mutation.SetContactWxid(v)
	}
	if _, ok := mrc.mutation.ContentType(); !ok {
		v := messagerecords.DefaultContentType
		mrc.mutation.SetContentType(v)
	}
	if _, ok := mrc.mutation.Content(); !ok {
		v := messagerecords.DefaultContent
		mrc.mutation.SetContent(v)
	}
	if _, ok := mrc.mutation.ErrorDetail(); !ok {
		v := messagerecords.DefaultErrorDetail
		mrc.mutation.SetErrorDetail(v)
	}
	if _, ok := mrc.mutation.SourceType(); !ok {
		v := messagerecords.DefaultSourceType
		mrc.mutation.SetSourceType(v)
	}
	if _, ok := mrc.mutation.SourceID(); !ok {
		v := messagerecords.DefaultSourceID
		mrc.mutation.SetSourceID(v)
	}
	if _, ok := mrc.mutation.SubSourceID(); !ok {
		v := messagerecords.DefaultSubSourceID
		mrc.mutation.SetSubSourceID(v)
	}
	if _, ok := mrc.mutation.OrganizationID(); !ok {
		v := messagerecords.DefaultOrganizationID
		mrc.mutation.SetOrganizationID(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (mrc *MessageRecordsCreate) check() error {
	if _, ok := mrc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "MessageRecords.created_at"`)}
	}
	if _, ok := mrc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "MessageRecords.updated_at"`)}
	}
	if _, ok := mrc.mutation.BotWxid(); !ok {
		return &ValidationError{Name: "bot_wxid", err: errors.New(`ent: missing required field "MessageRecords.bot_wxid"`)}
	}
	if _, ok := mrc.mutation.ContactType(); !ok {
		return &ValidationError{Name: "contact_type", err: errors.New(`ent: missing required field "MessageRecords.contact_type"`)}
	}
	if _, ok := mrc.mutation.ContactWxid(); !ok {
		return &ValidationError{Name: "contact_wxid", err: errors.New(`ent: missing required field "MessageRecords.contact_wxid"`)}
	}
	if _, ok := mrc.mutation.ContentType(); !ok {
		return &ValidationError{Name: "content_type", err: errors.New(`ent: missing required field "MessageRecords.content_type"`)}
	}
	if _, ok := mrc.mutation.Content(); !ok {
		return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "MessageRecords.content"`)}
	}
	if _, ok := mrc.mutation.ErrorDetail(); !ok {
		return &ValidationError{Name: "error_detail", err: errors.New(`ent: missing required field "MessageRecords.error_detail"`)}
	}
	if _, ok := mrc.mutation.SourceType(); !ok {
		return &ValidationError{Name: "source_type", err: errors.New(`ent: missing required field "MessageRecords.source_type"`)}
	}
	return nil
}

func (mrc *MessageRecordsCreate) sqlSave(ctx context.Context) (*MessageRecords, error) {
	if err := mrc.check(); err != nil {
		return nil, err
	}
	_node, _spec := mrc.createSpec()
	if err := sqlgraph.CreateNode(ctx, mrc.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)
	}
	mrc.mutation.id = &_node.ID
	mrc.mutation.done = true
	return _node, nil
}

func (mrc *MessageRecordsCreate) createSpec() (*MessageRecords, *sqlgraph.CreateSpec) {
	var (
		_node = &MessageRecords{config: mrc.config}
		_spec = sqlgraph.NewCreateSpec(messagerecords.Table, sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = mrc.conflict
	if id, ok := mrc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := mrc.mutation.CreatedAt(); ok {
		_spec.SetField(messagerecords.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := mrc.mutation.UpdatedAt(); ok {
		_spec.SetField(messagerecords.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := mrc.mutation.Status(); ok {
		_spec.SetField(messagerecords.FieldStatus, field.TypeUint8, value)
		_node.Status = value
	}
	if value, ok := mrc.mutation.DeletedAt(); ok {
		_spec.SetField(messagerecords.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := mrc.mutation.BotWxid(); ok {
		_spec.SetField(messagerecords.FieldBotWxid, field.TypeString, value)
		_node.BotWxid = value
	}
	if value, ok := mrc.mutation.ContactType(); ok {
		_spec.SetField(messagerecords.FieldContactType, field.TypeInt, value)
		_node.ContactType = value
	}
	if value, ok := mrc.mutation.ContactWxid(); ok {
		_spec.SetField(messagerecords.FieldContactWxid, field.TypeString, value)
		_node.ContactWxid = value
	}
	if value, ok := mrc.mutation.ContentType(); ok {
		_spec.SetField(messagerecords.FieldContentType, field.TypeInt, value)
		_node.ContentType = value
	}
	if value, ok := mrc.mutation.Content(); ok {
		_spec.SetField(messagerecords.FieldContent, field.TypeString, value)
		_node.Content = value
	}
	if value, ok := mrc.mutation.Meta(); ok {
		_spec.SetField(messagerecords.FieldMeta, field.TypeJSON, value)
		_node.Meta = value
	}
	if value, ok := mrc.mutation.ErrorDetail(); ok {
		_spec.SetField(messagerecords.FieldErrorDetail, field.TypeString, value)
		_node.ErrorDetail = value
	}
	if value, ok := mrc.mutation.SendTime(); ok {
		_spec.SetField(messagerecords.FieldSendTime, field.TypeTime, value)
		_node.SendTime = value
	}
	if value, ok := mrc.mutation.SourceType(); ok {
		_spec.SetField(messagerecords.FieldSourceType, field.TypeInt, value)
		_node.SourceType = value
	}
	if value, ok := mrc.mutation.OrganizationID(); ok {
		_spec.SetField(messagerecords.FieldOrganizationID, field.TypeUint64, value)
		_node.OrganizationID = value
	}
	if nodes := mrc.mutation.SopStageIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   messagerecords.SopStageTable,
			Columns: []string{messagerecords.SopStageColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopstage.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.SourceID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mrc.mutation.SopNodeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   messagerecords.SopNodeTable,
			Columns: []string{messagerecords.SopNodeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.SubSourceID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := mrc.mutation.MessageContactIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   messagerecords.MessageContactTable,
			Columns: []string{messagerecords.MessageContactColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(contact.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.ContactID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.MessageRecords.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.MessageRecordsUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (mrc *MessageRecordsCreate) OnConflict(opts ...sql.ConflictOption) *MessageRecordsUpsertOne {
	mrc.conflict = opts
	return &MessageRecordsUpsertOne{
		create: mrc,
	}
}

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

type (
	// MessageRecordsUpsertOne is the builder for "upsert"-ing
	//  one MessageRecords node.
	MessageRecordsUpsertOne struct {
		create *MessageRecordsCreate
	}

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

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

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

// SetStatus sets the "status" field.
func (u *MessageRecordsUpsert) SetStatus(v uint8) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateStatus() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldStatus)
	return u
}

// AddStatus adds v to the "status" field.
func (u *MessageRecordsUpsert) AddStatus(v uint8) *MessageRecordsUpsert {
	u.Add(messagerecords.FieldStatus, v)
	return u
}

// ClearStatus clears the value of the "status" field.
func (u *MessageRecordsUpsert) ClearStatus() *MessageRecordsUpsert {
	u.SetNull(messagerecords.FieldStatus)
	return u
}

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

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

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

// SetBotWxid sets the "bot_wxid" field.
func (u *MessageRecordsUpsert) SetBotWxid(v string) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldBotWxid, v)
	return u
}

// UpdateBotWxid sets the "bot_wxid" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateBotWxid() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldBotWxid)
	return u
}

// SetContactID sets the "contact_id" field.
func (u *MessageRecordsUpsert) SetContactID(v uint64) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldContactID, v)
	return u
}

// UpdateContactID sets the "contact_id" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateContactID() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldContactID)
	return u
}

// ClearContactID clears the value of the "contact_id" field.
func (u *MessageRecordsUpsert) ClearContactID() *MessageRecordsUpsert {
	u.SetNull(messagerecords.FieldContactID)
	return u
}

// SetContactType sets the "contact_type" field.
func (u *MessageRecordsUpsert) SetContactType(v int) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldContactType, v)
	return u
}

// UpdateContactType sets the "contact_type" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateContactType() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldContactType)
	return u
}

// AddContactType adds v to the "contact_type" field.
func (u *MessageRecordsUpsert) AddContactType(v int) *MessageRecordsUpsert {
	u.Add(messagerecords.FieldContactType, v)
	return u
}

// SetContactWxid sets the "contact_wxid" field.
func (u *MessageRecordsUpsert) SetContactWxid(v string) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldContactWxid, v)
	return u
}

// UpdateContactWxid sets the "contact_wxid" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateContactWxid() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldContactWxid)
	return u
}

// SetContentType sets the "content_type" field.
func (u *MessageRecordsUpsert) SetContentType(v int) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldContentType, v)
	return u
}

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

// AddContentType adds v to the "content_type" field.
func (u *MessageRecordsUpsert) AddContentType(v int) *MessageRecordsUpsert {
	u.Add(messagerecords.FieldContentType, v)
	return u
}

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

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

// SetMeta sets the "meta" field.
func (u *MessageRecordsUpsert) SetMeta(v custom_types.Meta) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldMeta, v)
	return u
}

// UpdateMeta sets the "meta" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateMeta() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldMeta)
	return u
}

// ClearMeta clears the value of the "meta" field.
func (u *MessageRecordsUpsert) ClearMeta() *MessageRecordsUpsert {
	u.SetNull(messagerecords.FieldMeta)
	return u
}

// SetErrorDetail sets the "error_detail" field.
func (u *MessageRecordsUpsert) SetErrorDetail(v string) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldErrorDetail, v)
	return u
}

// UpdateErrorDetail sets the "error_detail" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateErrorDetail() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldErrorDetail)
	return u
}

// SetSendTime sets the "send_time" field.
func (u *MessageRecordsUpsert) SetSendTime(v time.Time) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldSendTime, v)
	return u
}

// UpdateSendTime sets the "send_time" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateSendTime() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldSendTime)
	return u
}

// ClearSendTime clears the value of the "send_time" field.
func (u *MessageRecordsUpsert) ClearSendTime() *MessageRecordsUpsert {
	u.SetNull(messagerecords.FieldSendTime)
	return u
}

// SetSourceType sets the "source_type" field.
func (u *MessageRecordsUpsert) SetSourceType(v int) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldSourceType, v)
	return u
}

// UpdateSourceType sets the "source_type" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateSourceType() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldSourceType)
	return u
}

// AddSourceType adds v to the "source_type" field.
func (u *MessageRecordsUpsert) AddSourceType(v int) *MessageRecordsUpsert {
	u.Add(messagerecords.FieldSourceType, v)
	return u
}

// SetSourceID sets the "source_id" field.
func (u *MessageRecordsUpsert) SetSourceID(v uint64) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldSourceID, v)
	return u
}

// UpdateSourceID sets the "source_id" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateSourceID() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldSourceID)
	return u
}

// ClearSourceID clears the value of the "source_id" field.
func (u *MessageRecordsUpsert) ClearSourceID() *MessageRecordsUpsert {
	u.SetNull(messagerecords.FieldSourceID)
	return u
}

// SetSubSourceID sets the "sub_source_id" field.
func (u *MessageRecordsUpsert) SetSubSourceID(v uint64) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldSubSourceID, v)
	return u
}

// UpdateSubSourceID sets the "sub_source_id" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateSubSourceID() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldSubSourceID)
	return u
}

// ClearSubSourceID clears the value of the "sub_source_id" field.
func (u *MessageRecordsUpsert) ClearSubSourceID() *MessageRecordsUpsert {
	u.SetNull(messagerecords.FieldSubSourceID)
	return u
}

// SetOrganizationID sets the "organization_id" field.
func (u *MessageRecordsUpsert) SetOrganizationID(v uint64) *MessageRecordsUpsert {
	u.Set(messagerecords.FieldOrganizationID, v)
	return u
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *MessageRecordsUpsert) UpdateOrganizationID() *MessageRecordsUpsert {
	u.SetExcluded(messagerecords.FieldOrganizationID)
	return u
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *MessageRecordsUpsert) AddOrganizationID(v uint64) *MessageRecordsUpsert {
	u.Add(messagerecords.FieldOrganizationID, v)
	return u
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *MessageRecordsUpsert) ClearOrganizationID() *MessageRecordsUpsert {
	u.SetNull(messagerecords.FieldOrganizationID)
	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.MessageRecords.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(messagerecords.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *MessageRecordsUpsertOne) UpdateNewValues() *MessageRecordsUpsertOne {
	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(messagerecords.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(messagerecords.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.MessageRecords.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *MessageRecordsUpsertOne) Ignore() *MessageRecordsUpsertOne {
	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 *MessageRecordsUpsertOne) DoNothing() *MessageRecordsUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetStatus sets the "status" field.
func (u *MessageRecordsUpsertOne) SetStatus(v uint8) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *MessageRecordsUpsertOne) AddStatus(v uint8) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateStatus() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *MessageRecordsUpsertOne) ClearStatus() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearStatus()
	})
}

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

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

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

// SetBotWxid sets the "bot_wxid" field.
func (u *MessageRecordsUpsertOne) SetBotWxid(v string) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetBotWxid(v)
	})
}

// UpdateBotWxid sets the "bot_wxid" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateBotWxid() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateBotWxid()
	})
}

// SetContactID sets the "contact_id" field.
func (u *MessageRecordsUpsertOne) SetContactID(v uint64) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContactID(v)
	})
}

// UpdateContactID sets the "contact_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateContactID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateContactID()
	})
}

// ClearContactID clears the value of the "contact_id" field.
func (u *MessageRecordsUpsertOne) ClearContactID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearContactID()
	})
}

// SetContactType sets the "contact_type" field.
func (u *MessageRecordsUpsertOne) SetContactType(v int) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContactType(v)
	})
}

// AddContactType adds v to the "contact_type" field.
func (u *MessageRecordsUpsertOne) AddContactType(v int) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddContactType(v)
	})
}

// UpdateContactType sets the "contact_type" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateContactType() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateContactType()
	})
}

// SetContactWxid sets the "contact_wxid" field.
func (u *MessageRecordsUpsertOne) SetContactWxid(v string) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContactWxid(v)
	})
}

// UpdateContactWxid sets the "contact_wxid" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateContactWxid() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateContactWxid()
	})
}

// SetContentType sets the "content_type" field.
func (u *MessageRecordsUpsertOne) SetContentType(v int) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContentType(v)
	})
}

// AddContentType adds v to the "content_type" field.
func (u *MessageRecordsUpsertOne) AddContentType(v int) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddContentType(v)
	})
}

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

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

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

// SetMeta sets the "meta" field.
func (u *MessageRecordsUpsertOne) SetMeta(v custom_types.Meta) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetMeta(v)
	})
}

// UpdateMeta sets the "meta" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateMeta() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateMeta()
	})
}

// ClearMeta clears the value of the "meta" field.
func (u *MessageRecordsUpsertOne) ClearMeta() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearMeta()
	})
}

// SetErrorDetail sets the "error_detail" field.
func (u *MessageRecordsUpsertOne) SetErrorDetail(v string) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetErrorDetail(v)
	})
}

// UpdateErrorDetail sets the "error_detail" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateErrorDetail() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateErrorDetail()
	})
}

// SetSendTime sets the "send_time" field.
func (u *MessageRecordsUpsertOne) SetSendTime(v time.Time) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSendTime(v)
	})
}

// UpdateSendTime sets the "send_time" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateSendTime() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSendTime()
	})
}

// ClearSendTime clears the value of the "send_time" field.
func (u *MessageRecordsUpsertOne) ClearSendTime() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearSendTime()
	})
}

// SetSourceType sets the "source_type" field.
func (u *MessageRecordsUpsertOne) SetSourceType(v int) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSourceType(v)
	})
}

// AddSourceType adds v to the "source_type" field.
func (u *MessageRecordsUpsertOne) AddSourceType(v int) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddSourceType(v)
	})
}

// UpdateSourceType sets the "source_type" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateSourceType() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSourceType()
	})
}

// SetSourceID sets the "source_id" field.
func (u *MessageRecordsUpsertOne) SetSourceID(v uint64) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSourceID(v)
	})
}

// UpdateSourceID sets the "source_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateSourceID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSourceID()
	})
}

// ClearSourceID clears the value of the "source_id" field.
func (u *MessageRecordsUpsertOne) ClearSourceID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearSourceID()
	})
}

// SetSubSourceID sets the "sub_source_id" field.
func (u *MessageRecordsUpsertOne) SetSubSourceID(v uint64) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSubSourceID(v)
	})
}

// UpdateSubSourceID sets the "sub_source_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateSubSourceID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSubSourceID()
	})
}

// ClearSubSourceID clears the value of the "sub_source_id" field.
func (u *MessageRecordsUpsertOne) ClearSubSourceID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearSubSourceID()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *MessageRecordsUpsertOne) SetOrganizationID(v uint64) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *MessageRecordsUpsertOne) AddOrganizationID(v uint64) *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertOne) UpdateOrganizationID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateOrganizationID()
	})
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *MessageRecordsUpsertOne) ClearOrganizationID() *MessageRecordsUpsertOne {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearOrganizationID()
	})
}

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

// ExecX is like Exec, but panics if an error occurs.
func (u *MessageRecordsUpsertOne) 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 *MessageRecordsUpsertOne) 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 *MessageRecordsUpsertOne) IDX(ctx context.Context) uint64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

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

// Save creates the MessageRecords entities in the database.
func (mrcb *MessageRecordsCreateBulk) Save(ctx context.Context) ([]*MessageRecords, error) {
	if mrcb.err != nil {
		return nil, mrcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(mrcb.builders))
	nodes := make([]*MessageRecords, len(mrcb.builders))
	mutators := make([]Mutator, len(mrcb.builders))
	for i := range mrcb.builders {
		func(i int, root context.Context) {
			builder := mrcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*MessageRecordsMutation)
				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, mrcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = mrcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, mrcb.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, mrcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.MessageRecords.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.MessageRecordsUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (mrcb *MessageRecordsCreateBulk) OnConflict(opts ...sql.ConflictOption) *MessageRecordsUpsertBulk {
	mrcb.conflict = opts
	return &MessageRecordsUpsertBulk{
		create: mrcb,
	}
}

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

// MessageRecordsUpsertBulk is the builder for "upsert"-ing
// a bulk of MessageRecords nodes.
type MessageRecordsUpsertBulk struct {
	create *MessageRecordsCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.MessageRecords.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(messagerecords.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *MessageRecordsUpsertBulk) UpdateNewValues() *MessageRecordsUpsertBulk {
	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(messagerecords.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(messagerecords.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.MessageRecords.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *MessageRecordsUpsertBulk) Ignore() *MessageRecordsUpsertBulk {
	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 *MessageRecordsUpsertBulk) DoNothing() *MessageRecordsUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

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

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

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

// SetStatus sets the "status" field.
func (u *MessageRecordsUpsertBulk) SetStatus(v uint8) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetStatus(v)
	})
}

// AddStatus adds v to the "status" field.
func (u *MessageRecordsUpsertBulk) AddStatus(v uint8) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateStatus() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateStatus()
	})
}

// ClearStatus clears the value of the "status" field.
func (u *MessageRecordsUpsertBulk) ClearStatus() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearStatus()
	})
}

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

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

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

// SetBotWxid sets the "bot_wxid" field.
func (u *MessageRecordsUpsertBulk) SetBotWxid(v string) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetBotWxid(v)
	})
}

// UpdateBotWxid sets the "bot_wxid" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateBotWxid() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateBotWxid()
	})
}

// SetContactID sets the "contact_id" field.
func (u *MessageRecordsUpsertBulk) SetContactID(v uint64) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContactID(v)
	})
}

// UpdateContactID sets the "contact_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateContactID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateContactID()
	})
}

// ClearContactID clears the value of the "contact_id" field.
func (u *MessageRecordsUpsertBulk) ClearContactID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearContactID()
	})
}

// SetContactType sets the "contact_type" field.
func (u *MessageRecordsUpsertBulk) SetContactType(v int) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContactType(v)
	})
}

// AddContactType adds v to the "contact_type" field.
func (u *MessageRecordsUpsertBulk) AddContactType(v int) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddContactType(v)
	})
}

// UpdateContactType sets the "contact_type" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateContactType() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateContactType()
	})
}

// SetContactWxid sets the "contact_wxid" field.
func (u *MessageRecordsUpsertBulk) SetContactWxid(v string) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContactWxid(v)
	})
}

// UpdateContactWxid sets the "contact_wxid" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateContactWxid() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateContactWxid()
	})
}

// SetContentType sets the "content_type" field.
func (u *MessageRecordsUpsertBulk) SetContentType(v int) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetContentType(v)
	})
}

// AddContentType adds v to the "content_type" field.
func (u *MessageRecordsUpsertBulk) AddContentType(v int) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddContentType(v)
	})
}

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

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

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

// SetMeta sets the "meta" field.
func (u *MessageRecordsUpsertBulk) SetMeta(v custom_types.Meta) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetMeta(v)
	})
}

// UpdateMeta sets the "meta" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateMeta() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateMeta()
	})
}

// ClearMeta clears the value of the "meta" field.
func (u *MessageRecordsUpsertBulk) ClearMeta() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearMeta()
	})
}

// SetErrorDetail sets the "error_detail" field.
func (u *MessageRecordsUpsertBulk) SetErrorDetail(v string) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetErrorDetail(v)
	})
}

// UpdateErrorDetail sets the "error_detail" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateErrorDetail() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateErrorDetail()
	})
}

// SetSendTime sets the "send_time" field.
func (u *MessageRecordsUpsertBulk) SetSendTime(v time.Time) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSendTime(v)
	})
}

// UpdateSendTime sets the "send_time" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateSendTime() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSendTime()
	})
}

// ClearSendTime clears the value of the "send_time" field.
func (u *MessageRecordsUpsertBulk) ClearSendTime() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearSendTime()
	})
}

// SetSourceType sets the "source_type" field.
func (u *MessageRecordsUpsertBulk) SetSourceType(v int) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSourceType(v)
	})
}

// AddSourceType adds v to the "source_type" field.
func (u *MessageRecordsUpsertBulk) AddSourceType(v int) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddSourceType(v)
	})
}

// UpdateSourceType sets the "source_type" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateSourceType() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSourceType()
	})
}

// SetSourceID sets the "source_id" field.
func (u *MessageRecordsUpsertBulk) SetSourceID(v uint64) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSourceID(v)
	})
}

// UpdateSourceID sets the "source_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateSourceID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSourceID()
	})
}

// ClearSourceID clears the value of the "source_id" field.
func (u *MessageRecordsUpsertBulk) ClearSourceID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearSourceID()
	})
}

// SetSubSourceID sets the "sub_source_id" field.
func (u *MessageRecordsUpsertBulk) SetSubSourceID(v uint64) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetSubSourceID(v)
	})
}

// UpdateSubSourceID sets the "sub_source_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateSubSourceID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateSubSourceID()
	})
}

// ClearSubSourceID clears the value of the "sub_source_id" field.
func (u *MessageRecordsUpsertBulk) ClearSubSourceID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearSubSourceID()
	})
}

// SetOrganizationID sets the "organization_id" field.
func (u *MessageRecordsUpsertBulk) SetOrganizationID(v uint64) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.SetOrganizationID(v)
	})
}

// AddOrganizationID adds v to the "organization_id" field.
func (u *MessageRecordsUpsertBulk) AddOrganizationID(v uint64) *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.AddOrganizationID(v)
	})
}

// UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
func (u *MessageRecordsUpsertBulk) UpdateOrganizationID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.UpdateOrganizationID()
	})
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (u *MessageRecordsUpsertBulk) ClearOrganizationID() *MessageRecordsUpsertBulk {
	return u.Update(func(s *MessageRecordsUpsert) {
		s.ClearOrganizationID()
	})
}

// Exec executes the query.
func (u *MessageRecordsUpsertBulk) 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 MessageRecordsCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for MessageRecordsCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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