// 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/predicate"
	"wechat-api/ent/sopnode"
	"wechat-api/ent/sopstage"

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

// MessageRecordsUpdate is the builder for updating MessageRecords entities.
type MessageRecordsUpdate struct {
	config
	hooks    []Hook
	mutation *MessageRecordsMutation
}

// Where appends a list predicates to the MessageRecordsUpdate builder.
func (mru *MessageRecordsUpdate) Where(ps ...predicate.MessageRecords) *MessageRecordsUpdate {
	mru.mutation.Where(ps...)
	return mru
}

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

// SetStatus sets the "status" field.
func (mru *MessageRecordsUpdate) SetStatus(u uint8) *MessageRecordsUpdate {
	mru.mutation.ResetStatus()
	mru.mutation.SetStatus(u)
	return mru
}

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

// AddStatus adds u to the "status" field.
func (mru *MessageRecordsUpdate) AddStatus(u int8) *MessageRecordsUpdate {
	mru.mutation.AddStatus(u)
	return mru
}

// ClearStatus clears the value of the "status" field.
func (mru *MessageRecordsUpdate) ClearStatus() *MessageRecordsUpdate {
	mru.mutation.ClearStatus()
	return mru
}

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

// SetNillableBotWxid sets the "bot_wxid" field if the given value is not nil.
func (mru *MessageRecordsUpdate) SetNillableBotWxid(s *string) *MessageRecordsUpdate {
	if s != nil {
		mru.SetBotWxid(*s)
	}
	return mru
}

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

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

// ClearContactID clears the value of the "contact_id" field.
func (mru *MessageRecordsUpdate) ClearContactID() *MessageRecordsUpdate {
	mru.mutation.ClearContactID()
	return mru
}

// SetContactType sets the "contact_type" field.
func (mru *MessageRecordsUpdate) SetContactType(i int) *MessageRecordsUpdate {
	mru.mutation.ResetContactType()
	mru.mutation.SetContactType(i)
	return mru
}

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

// AddContactType adds i to the "contact_type" field.
func (mru *MessageRecordsUpdate) AddContactType(i int) *MessageRecordsUpdate {
	mru.mutation.AddContactType(i)
	return mru
}

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

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

// SetContentType sets the "content_type" field.
func (mru *MessageRecordsUpdate) SetContentType(i int) *MessageRecordsUpdate {
	mru.mutation.ResetContentType()
	mru.mutation.SetContentType(i)
	return mru
}

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

// AddContentType adds i to the "content_type" field.
func (mru *MessageRecordsUpdate) AddContentType(i int) *MessageRecordsUpdate {
	mru.mutation.AddContentType(i)
	return mru
}

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

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

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

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

// ClearMeta clears the value of the "meta" field.
func (mru *MessageRecordsUpdate) ClearMeta() *MessageRecordsUpdate {
	mru.mutation.ClearMeta()
	return mru
}

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

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

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

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

// ClearSendTime clears the value of the "send_time" field.
func (mru *MessageRecordsUpdate) ClearSendTime() *MessageRecordsUpdate {
	mru.mutation.ClearSendTime()
	return mru
}

// SetSourceType sets the "source_type" field.
func (mru *MessageRecordsUpdate) SetSourceType(i int) *MessageRecordsUpdate {
	mru.mutation.ResetSourceType()
	mru.mutation.SetSourceType(i)
	return mru
}

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

// AddSourceType adds i to the "source_type" field.
func (mru *MessageRecordsUpdate) AddSourceType(i int) *MessageRecordsUpdate {
	mru.mutation.AddSourceType(i)
	return mru
}

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

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

// ClearSourceID clears the value of the "source_id" field.
func (mru *MessageRecordsUpdate) ClearSourceID() *MessageRecordsUpdate {
	mru.mutation.ClearSourceID()
	return mru
}

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

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

// ClearSubSourceID clears the value of the "sub_source_id" field.
func (mru *MessageRecordsUpdate) ClearSubSourceID() *MessageRecordsUpdate {
	mru.mutation.ClearSubSourceID()
	return mru
}

// SetOrganizationID sets the "organization_id" field.
func (mru *MessageRecordsUpdate) SetOrganizationID(u uint64) *MessageRecordsUpdate {
	mru.mutation.ResetOrganizationID()
	mru.mutation.SetOrganizationID(u)
	return mru
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (mru *MessageRecordsUpdate) AddOrganizationID(u int64) *MessageRecordsUpdate {
	mru.mutation.AddOrganizationID(u)
	return mru
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (mru *MessageRecordsUpdate) ClearOrganizationID() *MessageRecordsUpdate {
	mru.mutation.ClearOrganizationID()
	return mru
}

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

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

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

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

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

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

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

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

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

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

// ClearSopStage clears the "sop_stage" edge to the SopStage entity.
func (mru *MessageRecordsUpdate) ClearSopStage() *MessageRecordsUpdate {
	mru.mutation.ClearSopStage()
	return mru
}

// ClearSopNode clears the "sop_node" edge to the SopNode entity.
func (mru *MessageRecordsUpdate) ClearSopNode() *MessageRecordsUpdate {
	mru.mutation.ClearSopNode()
	return mru
}

// ClearMessageContact clears the "message_contact" edge to the Contact entity.
func (mru *MessageRecordsUpdate) ClearMessageContact() *MessageRecordsUpdate {
	mru.mutation.ClearMessageContact()
	return mru
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (mru *MessageRecordsUpdate) Save(ctx context.Context) (int, error) {
	mru.defaults()
	return withHooks(ctx, mru.sqlSave, mru.mutation, mru.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (mru *MessageRecordsUpdate) defaults() {
	if _, ok := mru.mutation.UpdatedAt(); !ok {
		v := messagerecords.UpdateDefaultUpdatedAt()
		mru.mutation.SetUpdatedAt(v)
	}
}

func (mru *MessageRecordsUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(messagerecords.Table, messagerecords.Columns, sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64))
	if ps := mru.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mru.mutation.UpdatedAt(); ok {
		_spec.SetField(messagerecords.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := mru.mutation.Status(); ok {
		_spec.SetField(messagerecords.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := mru.mutation.AddedStatus(); ok {
		_spec.AddField(messagerecords.FieldStatus, field.TypeUint8, value)
	}
	if mru.mutation.StatusCleared() {
		_spec.ClearField(messagerecords.FieldStatus, field.TypeUint8)
	}
	if value, ok := mru.mutation.BotWxid(); ok {
		_spec.SetField(messagerecords.FieldBotWxid, field.TypeString, value)
	}
	if value, ok := mru.mutation.ContactType(); ok {
		_spec.SetField(messagerecords.FieldContactType, field.TypeInt, value)
	}
	if value, ok := mru.mutation.AddedContactType(); ok {
		_spec.AddField(messagerecords.FieldContactType, field.TypeInt, value)
	}
	if value, ok := mru.mutation.ContactWxid(); ok {
		_spec.SetField(messagerecords.FieldContactWxid, field.TypeString, value)
	}
	if value, ok := mru.mutation.ContentType(); ok {
		_spec.SetField(messagerecords.FieldContentType, field.TypeInt, value)
	}
	if value, ok := mru.mutation.AddedContentType(); ok {
		_spec.AddField(messagerecords.FieldContentType, field.TypeInt, value)
	}
	if value, ok := mru.mutation.Content(); ok {
		_spec.SetField(messagerecords.FieldContent, field.TypeString, value)
	}
	if value, ok := mru.mutation.Meta(); ok {
		_spec.SetField(messagerecords.FieldMeta, field.TypeJSON, value)
	}
	if mru.mutation.MetaCleared() {
		_spec.ClearField(messagerecords.FieldMeta, field.TypeJSON)
	}
	if value, ok := mru.mutation.ErrorDetail(); ok {
		_spec.SetField(messagerecords.FieldErrorDetail, field.TypeString, value)
	}
	if value, ok := mru.mutation.SendTime(); ok {
		_spec.SetField(messagerecords.FieldSendTime, field.TypeTime, value)
	}
	if mru.mutation.SendTimeCleared() {
		_spec.ClearField(messagerecords.FieldSendTime, field.TypeTime)
	}
	if value, ok := mru.mutation.SourceType(); ok {
		_spec.SetField(messagerecords.FieldSourceType, field.TypeInt, value)
	}
	if value, ok := mru.mutation.AddedSourceType(); ok {
		_spec.AddField(messagerecords.FieldSourceType, field.TypeInt, value)
	}
	if value, ok := mru.mutation.OrganizationID(); ok {
		_spec.SetField(messagerecords.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := mru.mutation.AddedOrganizationID(); ok {
		_spec.AddField(messagerecords.FieldOrganizationID, field.TypeUint64, value)
	}
	if mru.mutation.OrganizationIDCleared() {
		_spec.ClearField(messagerecords.FieldOrganizationID, field.TypeUint64)
	}
	if mru.mutation.SopStageCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mru.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if mru.mutation.SopNodeCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mru.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if mru.mutation.MessageContactCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mru.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, mru.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{messagerecords.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	mru.mutation.done = true
	return n, nil
}

// MessageRecordsUpdateOne is the builder for updating a single MessageRecords entity.
type MessageRecordsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MessageRecordsMutation
}

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

// SetStatus sets the "status" field.
func (mruo *MessageRecordsUpdateOne) SetStatus(u uint8) *MessageRecordsUpdateOne {
	mruo.mutation.ResetStatus()
	mruo.mutation.SetStatus(u)
	return mruo
}

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

// AddStatus adds u to the "status" field.
func (mruo *MessageRecordsUpdateOne) AddStatus(u int8) *MessageRecordsUpdateOne {
	mruo.mutation.AddStatus(u)
	return mruo
}

// ClearStatus clears the value of the "status" field.
func (mruo *MessageRecordsUpdateOne) ClearStatus() *MessageRecordsUpdateOne {
	mruo.mutation.ClearStatus()
	return mruo
}

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

// SetNillableBotWxid sets the "bot_wxid" field if the given value is not nil.
func (mruo *MessageRecordsUpdateOne) SetNillableBotWxid(s *string) *MessageRecordsUpdateOne {
	if s != nil {
		mruo.SetBotWxid(*s)
	}
	return mruo
}

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

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

// ClearContactID clears the value of the "contact_id" field.
func (mruo *MessageRecordsUpdateOne) ClearContactID() *MessageRecordsUpdateOne {
	mruo.mutation.ClearContactID()
	return mruo
}

// SetContactType sets the "contact_type" field.
func (mruo *MessageRecordsUpdateOne) SetContactType(i int) *MessageRecordsUpdateOne {
	mruo.mutation.ResetContactType()
	mruo.mutation.SetContactType(i)
	return mruo
}

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

// AddContactType adds i to the "contact_type" field.
func (mruo *MessageRecordsUpdateOne) AddContactType(i int) *MessageRecordsUpdateOne {
	mruo.mutation.AddContactType(i)
	return mruo
}

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

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

// SetContentType sets the "content_type" field.
func (mruo *MessageRecordsUpdateOne) SetContentType(i int) *MessageRecordsUpdateOne {
	mruo.mutation.ResetContentType()
	mruo.mutation.SetContentType(i)
	return mruo
}

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

// AddContentType adds i to the "content_type" field.
func (mruo *MessageRecordsUpdateOne) AddContentType(i int) *MessageRecordsUpdateOne {
	mruo.mutation.AddContentType(i)
	return mruo
}

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

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

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

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

// ClearMeta clears the value of the "meta" field.
func (mruo *MessageRecordsUpdateOne) ClearMeta() *MessageRecordsUpdateOne {
	mruo.mutation.ClearMeta()
	return mruo
}

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

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

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

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

// ClearSendTime clears the value of the "send_time" field.
func (mruo *MessageRecordsUpdateOne) ClearSendTime() *MessageRecordsUpdateOne {
	mruo.mutation.ClearSendTime()
	return mruo
}

// SetSourceType sets the "source_type" field.
func (mruo *MessageRecordsUpdateOne) SetSourceType(i int) *MessageRecordsUpdateOne {
	mruo.mutation.ResetSourceType()
	mruo.mutation.SetSourceType(i)
	return mruo
}

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

// AddSourceType adds i to the "source_type" field.
func (mruo *MessageRecordsUpdateOne) AddSourceType(i int) *MessageRecordsUpdateOne {
	mruo.mutation.AddSourceType(i)
	return mruo
}

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

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

// ClearSourceID clears the value of the "source_id" field.
func (mruo *MessageRecordsUpdateOne) ClearSourceID() *MessageRecordsUpdateOne {
	mruo.mutation.ClearSourceID()
	return mruo
}

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

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

// ClearSubSourceID clears the value of the "sub_source_id" field.
func (mruo *MessageRecordsUpdateOne) ClearSubSourceID() *MessageRecordsUpdateOne {
	mruo.mutation.ClearSubSourceID()
	return mruo
}

// SetOrganizationID sets the "organization_id" field.
func (mruo *MessageRecordsUpdateOne) SetOrganizationID(u uint64) *MessageRecordsUpdateOne {
	mruo.mutation.ResetOrganizationID()
	mruo.mutation.SetOrganizationID(u)
	return mruo
}

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

// AddOrganizationID adds u to the "organization_id" field.
func (mruo *MessageRecordsUpdateOne) AddOrganizationID(u int64) *MessageRecordsUpdateOne {
	mruo.mutation.AddOrganizationID(u)
	return mruo
}

// ClearOrganizationID clears the value of the "organization_id" field.
func (mruo *MessageRecordsUpdateOne) ClearOrganizationID() *MessageRecordsUpdateOne {
	mruo.mutation.ClearOrganizationID()
	return mruo
}

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

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

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

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

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

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

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

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

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

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

// ClearSopStage clears the "sop_stage" edge to the SopStage entity.
func (mruo *MessageRecordsUpdateOne) ClearSopStage() *MessageRecordsUpdateOne {
	mruo.mutation.ClearSopStage()
	return mruo
}

// ClearSopNode clears the "sop_node" edge to the SopNode entity.
func (mruo *MessageRecordsUpdateOne) ClearSopNode() *MessageRecordsUpdateOne {
	mruo.mutation.ClearSopNode()
	return mruo
}

// ClearMessageContact clears the "message_contact" edge to the Contact entity.
func (mruo *MessageRecordsUpdateOne) ClearMessageContact() *MessageRecordsUpdateOne {
	mruo.mutation.ClearMessageContact()
	return mruo
}

// Where appends a list predicates to the MessageRecordsUpdate builder.
func (mruo *MessageRecordsUpdateOne) Where(ps ...predicate.MessageRecords) *MessageRecordsUpdateOne {
	mruo.mutation.Where(ps...)
	return mruo
}

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

// Save executes the query and returns the updated MessageRecords entity.
func (mruo *MessageRecordsUpdateOne) Save(ctx context.Context) (*MessageRecords, error) {
	mruo.defaults()
	return withHooks(ctx, mruo.sqlSave, mruo.mutation, mruo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (mruo *MessageRecordsUpdateOne) defaults() {
	if _, ok := mruo.mutation.UpdatedAt(); !ok {
		v := messagerecords.UpdateDefaultUpdatedAt()
		mruo.mutation.SetUpdatedAt(v)
	}
}

func (mruo *MessageRecordsUpdateOne) sqlSave(ctx context.Context) (_node *MessageRecords, err error) {
	_spec := sqlgraph.NewUpdateSpec(messagerecords.Table, messagerecords.Columns, sqlgraph.NewFieldSpec(messagerecords.FieldID, field.TypeUint64))
	id, ok := mruo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "MessageRecords.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := mruo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, messagerecords.FieldID)
		for _, f := range fields {
			if !messagerecords.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != messagerecords.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := mruo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mruo.mutation.UpdatedAt(); ok {
		_spec.SetField(messagerecords.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := mruo.mutation.Status(); ok {
		_spec.SetField(messagerecords.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := mruo.mutation.AddedStatus(); ok {
		_spec.AddField(messagerecords.FieldStatus, field.TypeUint8, value)
	}
	if mruo.mutation.StatusCleared() {
		_spec.ClearField(messagerecords.FieldStatus, field.TypeUint8)
	}
	if value, ok := mruo.mutation.BotWxid(); ok {
		_spec.SetField(messagerecords.FieldBotWxid, field.TypeString, value)
	}
	if value, ok := mruo.mutation.ContactType(); ok {
		_spec.SetField(messagerecords.FieldContactType, field.TypeInt, value)
	}
	if value, ok := mruo.mutation.AddedContactType(); ok {
		_spec.AddField(messagerecords.FieldContactType, field.TypeInt, value)
	}
	if value, ok := mruo.mutation.ContactWxid(); ok {
		_spec.SetField(messagerecords.FieldContactWxid, field.TypeString, value)
	}
	if value, ok := mruo.mutation.ContentType(); ok {
		_spec.SetField(messagerecords.FieldContentType, field.TypeInt, value)
	}
	if value, ok := mruo.mutation.AddedContentType(); ok {
		_spec.AddField(messagerecords.FieldContentType, field.TypeInt, value)
	}
	if value, ok := mruo.mutation.Content(); ok {
		_spec.SetField(messagerecords.FieldContent, field.TypeString, value)
	}
	if value, ok := mruo.mutation.Meta(); ok {
		_spec.SetField(messagerecords.FieldMeta, field.TypeJSON, value)
	}
	if mruo.mutation.MetaCleared() {
		_spec.ClearField(messagerecords.FieldMeta, field.TypeJSON)
	}
	if value, ok := mruo.mutation.ErrorDetail(); ok {
		_spec.SetField(messagerecords.FieldErrorDetail, field.TypeString, value)
	}
	if value, ok := mruo.mutation.SendTime(); ok {
		_spec.SetField(messagerecords.FieldSendTime, field.TypeTime, value)
	}
	if mruo.mutation.SendTimeCleared() {
		_spec.ClearField(messagerecords.FieldSendTime, field.TypeTime)
	}
	if value, ok := mruo.mutation.SourceType(); ok {
		_spec.SetField(messagerecords.FieldSourceType, field.TypeInt, value)
	}
	if value, ok := mruo.mutation.AddedSourceType(); ok {
		_spec.AddField(messagerecords.FieldSourceType, field.TypeInt, value)
	}
	if value, ok := mruo.mutation.OrganizationID(); ok {
		_spec.SetField(messagerecords.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := mruo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(messagerecords.FieldOrganizationID, field.TypeUint64, value)
	}
	if mruo.mutation.OrganizationIDCleared() {
		_spec.ClearField(messagerecords.FieldOrganizationID, field.TypeUint64)
	}
	if mruo.mutation.SopStageCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mruo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if mruo.mutation.SopNodeCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mruo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if mruo.mutation.MessageContactCleared() {
		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),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := mruo.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)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &MessageRecords{config: mruo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, mruo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{messagerecords.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	mruo.mutation.done = true
	return _node, nil
}
