// Code generated by ent, DO NOT EDIT.

package ent

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

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

// MsgCreate is the builder for creating a Msg entity.
type MsgCreate struct {
	config
	mutation *MsgMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

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

// SetFromwxid sets the "fromwxid" field.
func (mc *MsgCreate) SetFromwxid(s string) *MsgCreate {
	mc.mutation.SetFromwxid(s)
	return mc
}

// SetNillableFromwxid sets the "fromwxid" field if the given value is not nil.
func (mc *MsgCreate) SetNillableFromwxid(s *string) *MsgCreate {
	if s != nil {
		mc.SetFromwxid(*s)
	}
	return mc
}

// SetToid sets the "toid" field.
func (mc *MsgCreate) SetToid(s string) *MsgCreate {
	mc.mutation.SetToid(s)
	return mc
}

// SetNillableToid sets the "toid" field if the given value is not nil.
func (mc *MsgCreate) SetNillableToid(s *string) *MsgCreate {
	if s != nil {
		mc.SetToid(*s)
	}
	return mc
}

// SetMsgtype sets the "msgtype" field.
func (mc *MsgCreate) SetMsgtype(i int32) *MsgCreate {
	mc.mutation.SetMsgtype(i)
	return mc
}

// SetNillableMsgtype sets the "msgtype" field if the given value is not nil.
func (mc *MsgCreate) SetNillableMsgtype(i *int32) *MsgCreate {
	if i != nil {
		mc.SetMsgtype(*i)
	}
	return mc
}

// SetMsg sets the "msg" field.
func (mc *MsgCreate) SetMsg(s string) *MsgCreate {
	mc.mutation.SetMsg(s)
	return mc
}

// SetNillableMsg sets the "msg" field if the given value is not nil.
func (mc *MsgCreate) SetNillableMsg(s *string) *MsgCreate {
	if s != nil {
		mc.SetMsg(*s)
	}
	return mc
}

// SetBatchNo sets the "batch_no" field.
func (mc *MsgCreate) SetBatchNo(s string) *MsgCreate {
	mc.mutation.SetBatchNo(s)
	return mc
}

// SetNillableBatchNo sets the "batch_no" field if the given value is not nil.
func (mc *MsgCreate) SetNillableBatchNo(s *string) *MsgCreate {
	if s != nil {
		mc.SetBatchNo(*s)
	}
	return mc
}

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

// Mutation returns the MsgMutation object of the builder.
func (mc *MsgCreate) Mutation() *MsgMutation {
	return mc.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (mc *MsgCreate) defaults() error {
	if _, ok := mc.mutation.CreatedAt(); !ok {
		if msg.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized msg.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := msg.DefaultCreatedAt()
		mc.mutation.SetCreatedAt(v)
	}
	if _, ok := mc.mutation.UpdatedAt(); !ok {
		if msg.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized msg.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := msg.DefaultUpdatedAt()
		mc.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (mc *MsgCreate) check() error {
	if _, ok := mc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Msg.created_at"`)}
	}
	if _, ok := mc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Msg.updated_at"`)}
	}
	return nil
}

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

func (mc *MsgCreate) createSpec() (*Msg, *sqlgraph.CreateSpec) {
	var (
		_node = &Msg{config: mc.config}
		_spec = sqlgraph.NewCreateSpec(msg.Table, sqlgraph.NewFieldSpec(msg.FieldID, field.TypeUint64))
	)
	_spec.OnConflict = mc.conflict
	if id, ok := mc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := mc.mutation.CreatedAt(); ok {
		_spec.SetField(msg.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := mc.mutation.UpdatedAt(); ok {
		_spec.SetField(msg.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := mc.mutation.DeletedAt(); ok {
		_spec.SetField(msg.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = value
	}
	if value, ok := mc.mutation.Status(); ok {
		_spec.SetField(msg.FieldStatus, field.TypeUint8, value)
		_node.Status = value
	}
	if value, ok := mc.mutation.Fromwxid(); ok {
		_spec.SetField(msg.FieldFromwxid, field.TypeString, value)
		_node.Fromwxid = value
	}
	if value, ok := mc.mutation.Toid(); ok {
		_spec.SetField(msg.FieldToid, field.TypeString, value)
		_node.Toid = value
	}
	if value, ok := mc.mutation.Msgtype(); ok {
		_spec.SetField(msg.FieldMsgtype, field.TypeInt32, value)
		_node.Msgtype = value
	}
	if value, ok := mc.mutation.Msg(); ok {
		_spec.SetField(msg.FieldMsg, field.TypeString, value)
		_node.Msg = value
	}
	if value, ok := mc.mutation.BatchNo(); ok {
		_spec.SetField(msg.FieldBatchNo, field.TypeString, value)
		_node.BatchNo = value
	}
	return _node, _spec
}

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

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

type (
	// MsgUpsertOne is the builder for "upsert"-ing
	//  one Msg node.
	MsgUpsertOne struct {
		create *MsgCreate
	}

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

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

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

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

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

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

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

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

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

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

// SetFromwxid sets the "fromwxid" field.
func (u *MsgUpsert) SetFromwxid(v string) *MsgUpsert {
	u.Set(msg.FieldFromwxid, v)
	return u
}

// UpdateFromwxid sets the "fromwxid" field to the value that was provided on create.
func (u *MsgUpsert) UpdateFromwxid() *MsgUpsert {
	u.SetExcluded(msg.FieldFromwxid)
	return u
}

// ClearFromwxid clears the value of the "fromwxid" field.
func (u *MsgUpsert) ClearFromwxid() *MsgUpsert {
	u.SetNull(msg.FieldFromwxid)
	return u
}

// SetToid sets the "toid" field.
func (u *MsgUpsert) SetToid(v string) *MsgUpsert {
	u.Set(msg.FieldToid, v)
	return u
}

// UpdateToid sets the "toid" field to the value that was provided on create.
func (u *MsgUpsert) UpdateToid() *MsgUpsert {
	u.SetExcluded(msg.FieldToid)
	return u
}

// ClearToid clears the value of the "toid" field.
func (u *MsgUpsert) ClearToid() *MsgUpsert {
	u.SetNull(msg.FieldToid)
	return u
}

// SetMsgtype sets the "msgtype" field.
func (u *MsgUpsert) SetMsgtype(v int32) *MsgUpsert {
	u.Set(msg.FieldMsgtype, v)
	return u
}

// UpdateMsgtype sets the "msgtype" field to the value that was provided on create.
func (u *MsgUpsert) UpdateMsgtype() *MsgUpsert {
	u.SetExcluded(msg.FieldMsgtype)
	return u
}

// AddMsgtype adds v to the "msgtype" field.
func (u *MsgUpsert) AddMsgtype(v int32) *MsgUpsert {
	u.Add(msg.FieldMsgtype, v)
	return u
}

// ClearMsgtype clears the value of the "msgtype" field.
func (u *MsgUpsert) ClearMsgtype() *MsgUpsert {
	u.SetNull(msg.FieldMsgtype)
	return u
}

// SetMsg sets the "msg" field.
func (u *MsgUpsert) SetMsg(v string) *MsgUpsert {
	u.Set(msg.FieldMsg, v)
	return u
}

// UpdateMsg sets the "msg" field to the value that was provided on create.
func (u *MsgUpsert) UpdateMsg() *MsgUpsert {
	u.SetExcluded(msg.FieldMsg)
	return u
}

// ClearMsg clears the value of the "msg" field.
func (u *MsgUpsert) ClearMsg() *MsgUpsert {
	u.SetNull(msg.FieldMsg)
	return u
}

// SetBatchNo sets the "batch_no" field.
func (u *MsgUpsert) SetBatchNo(v string) *MsgUpsert {
	u.Set(msg.FieldBatchNo, v)
	return u
}

// UpdateBatchNo sets the "batch_no" field to the value that was provided on create.
func (u *MsgUpsert) UpdateBatchNo() *MsgUpsert {
	u.SetExcluded(msg.FieldBatchNo)
	return u
}

// ClearBatchNo clears the value of the "batch_no" field.
func (u *MsgUpsert) ClearBatchNo() *MsgUpsert {
	u.SetNull(msg.FieldBatchNo)
	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.Msg.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(msg.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *MsgUpsertOne) UpdateNewValues() *MsgUpsertOne {
	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(msg.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(msg.FieldCreatedAt)
		}
	}))
	return u
}

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

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

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

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

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

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

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

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

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

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

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

// SetFromwxid sets the "fromwxid" field.
func (u *MsgUpsertOne) SetFromwxid(v string) *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.SetFromwxid(v)
	})
}

// UpdateFromwxid sets the "fromwxid" field to the value that was provided on create.
func (u *MsgUpsertOne) UpdateFromwxid() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateFromwxid()
	})
}

// ClearFromwxid clears the value of the "fromwxid" field.
func (u *MsgUpsertOne) ClearFromwxid() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.ClearFromwxid()
	})
}

// SetToid sets the "toid" field.
func (u *MsgUpsertOne) SetToid(v string) *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.SetToid(v)
	})
}

// UpdateToid sets the "toid" field to the value that was provided on create.
func (u *MsgUpsertOne) UpdateToid() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateToid()
	})
}

// ClearToid clears the value of the "toid" field.
func (u *MsgUpsertOne) ClearToid() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.ClearToid()
	})
}

// SetMsgtype sets the "msgtype" field.
func (u *MsgUpsertOne) SetMsgtype(v int32) *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.SetMsgtype(v)
	})
}

// AddMsgtype adds v to the "msgtype" field.
func (u *MsgUpsertOne) AddMsgtype(v int32) *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.AddMsgtype(v)
	})
}

// UpdateMsgtype sets the "msgtype" field to the value that was provided on create.
func (u *MsgUpsertOne) UpdateMsgtype() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateMsgtype()
	})
}

// ClearMsgtype clears the value of the "msgtype" field.
func (u *MsgUpsertOne) ClearMsgtype() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.ClearMsgtype()
	})
}

// SetMsg sets the "msg" field.
func (u *MsgUpsertOne) SetMsg(v string) *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.SetMsg(v)
	})
}

// UpdateMsg sets the "msg" field to the value that was provided on create.
func (u *MsgUpsertOne) UpdateMsg() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateMsg()
	})
}

// ClearMsg clears the value of the "msg" field.
func (u *MsgUpsertOne) ClearMsg() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.ClearMsg()
	})
}

// SetBatchNo sets the "batch_no" field.
func (u *MsgUpsertOne) SetBatchNo(v string) *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.SetBatchNo(v)
	})
}

// UpdateBatchNo sets the "batch_no" field to the value that was provided on create.
func (u *MsgUpsertOne) UpdateBatchNo() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateBatchNo()
	})
}

// ClearBatchNo clears the value of the "batch_no" field.
func (u *MsgUpsertOne) ClearBatchNo() *MsgUpsertOne {
	return u.Update(func(s *MsgUpsert) {
		s.ClearBatchNo()
	})
}

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

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

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

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

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

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

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

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

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

// MsgUpsertBulk is the builder for "upsert"-ing
// a bulk of Msg nodes.
type MsgUpsertBulk struct {
	create *MsgCreateBulk
}

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

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

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

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

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

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

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

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

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

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

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

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

// SetFromwxid sets the "fromwxid" field.
func (u *MsgUpsertBulk) SetFromwxid(v string) *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.SetFromwxid(v)
	})
}

// UpdateFromwxid sets the "fromwxid" field to the value that was provided on create.
func (u *MsgUpsertBulk) UpdateFromwxid() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateFromwxid()
	})
}

// ClearFromwxid clears the value of the "fromwxid" field.
func (u *MsgUpsertBulk) ClearFromwxid() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.ClearFromwxid()
	})
}

// SetToid sets the "toid" field.
func (u *MsgUpsertBulk) SetToid(v string) *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.SetToid(v)
	})
}

// UpdateToid sets the "toid" field to the value that was provided on create.
func (u *MsgUpsertBulk) UpdateToid() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateToid()
	})
}

// ClearToid clears the value of the "toid" field.
func (u *MsgUpsertBulk) ClearToid() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.ClearToid()
	})
}

// SetMsgtype sets the "msgtype" field.
func (u *MsgUpsertBulk) SetMsgtype(v int32) *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.SetMsgtype(v)
	})
}

// AddMsgtype adds v to the "msgtype" field.
func (u *MsgUpsertBulk) AddMsgtype(v int32) *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.AddMsgtype(v)
	})
}

// UpdateMsgtype sets the "msgtype" field to the value that was provided on create.
func (u *MsgUpsertBulk) UpdateMsgtype() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateMsgtype()
	})
}

// ClearMsgtype clears the value of the "msgtype" field.
func (u *MsgUpsertBulk) ClearMsgtype() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.ClearMsgtype()
	})
}

// SetMsg sets the "msg" field.
func (u *MsgUpsertBulk) SetMsg(v string) *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.SetMsg(v)
	})
}

// UpdateMsg sets the "msg" field to the value that was provided on create.
func (u *MsgUpsertBulk) UpdateMsg() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateMsg()
	})
}

// ClearMsg clears the value of the "msg" field.
func (u *MsgUpsertBulk) ClearMsg() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.ClearMsg()
	})
}

// SetBatchNo sets the "batch_no" field.
func (u *MsgUpsertBulk) SetBatchNo(v string) *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.SetBatchNo(v)
	})
}

// UpdateBatchNo sets the "batch_no" field to the value that was provided on create.
func (u *MsgUpsertBulk) UpdateBatchNo() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.UpdateBatchNo()
	})
}

// ClearBatchNo clears the value of the "batch_no" field.
func (u *MsgUpsertBulk) ClearBatchNo() *MsgUpsertBulk {
	return u.Update(func(s *MsgUpsert) {
		s.ClearBatchNo()
	})
}

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

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