// Code generated by ent, DO NOT EDIT.

package ent

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

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

// MsgUpdate is the builder for updating Msg entities.
type MsgUpdate struct {
	config
	hooks    []Hook
	mutation *MsgMutation
}

// Where appends a list predicates to the MsgUpdate builder.
func (mu *MsgUpdate) Where(ps ...predicate.Msg) *MsgUpdate {
	mu.mutation.Where(ps...)
	return mu
}

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

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

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

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

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

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

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

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

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

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

// ClearFromwxid clears the value of the "fromwxid" field.
func (mu *MsgUpdate) ClearFromwxid() *MsgUpdate {
	mu.mutation.ClearFromwxid()
	return mu
}

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

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

// ClearToid clears the value of the "toid" field.
func (mu *MsgUpdate) ClearToid() *MsgUpdate {
	mu.mutation.ClearToid()
	return mu
}

// SetMsgtype sets the "msgtype" field.
func (mu *MsgUpdate) SetMsgtype(i int32) *MsgUpdate {
	mu.mutation.ResetMsgtype()
	mu.mutation.SetMsgtype(i)
	return mu
}

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

// AddMsgtype adds i to the "msgtype" field.
func (mu *MsgUpdate) AddMsgtype(i int32) *MsgUpdate {
	mu.mutation.AddMsgtype(i)
	return mu
}

// ClearMsgtype clears the value of the "msgtype" field.
func (mu *MsgUpdate) ClearMsgtype() *MsgUpdate {
	mu.mutation.ClearMsgtype()
	return mu
}

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

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

// ClearMsg clears the value of the "msg" field.
func (mu *MsgUpdate) ClearMsg() *MsgUpdate {
	mu.mutation.ClearMsg()
	return mu
}

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

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

// ClearBatchNo clears the value of the "batch_no" field.
func (mu *MsgUpdate) ClearBatchNo() *MsgUpdate {
	mu.mutation.ClearBatchNo()
	return mu
}

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

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

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

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

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

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

func (mu *MsgUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(msg.Table, msg.Columns, sqlgraph.NewFieldSpec(msg.FieldID, field.TypeUint64))
	if ps := mu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := mu.mutation.UpdatedAt(); ok {
		_spec.SetField(msg.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := mu.mutation.DeletedAt(); ok {
		_spec.SetField(msg.FieldDeletedAt, field.TypeTime, value)
	}
	if mu.mutation.DeletedAtCleared() {
		_spec.ClearField(msg.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := mu.mutation.Status(); ok {
		_spec.SetField(msg.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := mu.mutation.AddedStatus(); ok {
		_spec.AddField(msg.FieldStatus, field.TypeUint8, value)
	}
	if mu.mutation.StatusCleared() {
		_spec.ClearField(msg.FieldStatus, field.TypeUint8)
	}
	if value, ok := mu.mutation.Fromwxid(); ok {
		_spec.SetField(msg.FieldFromwxid, field.TypeString, value)
	}
	if mu.mutation.FromwxidCleared() {
		_spec.ClearField(msg.FieldFromwxid, field.TypeString)
	}
	if value, ok := mu.mutation.Toid(); ok {
		_spec.SetField(msg.FieldToid, field.TypeString, value)
	}
	if mu.mutation.ToidCleared() {
		_spec.ClearField(msg.FieldToid, field.TypeString)
	}
	if value, ok := mu.mutation.Msgtype(); ok {
		_spec.SetField(msg.FieldMsgtype, field.TypeInt32, value)
	}
	if value, ok := mu.mutation.AddedMsgtype(); ok {
		_spec.AddField(msg.FieldMsgtype, field.TypeInt32, value)
	}
	if mu.mutation.MsgtypeCleared() {
		_spec.ClearField(msg.FieldMsgtype, field.TypeInt32)
	}
	if value, ok := mu.mutation.Msg(); ok {
		_spec.SetField(msg.FieldMsg, field.TypeString, value)
	}
	if mu.mutation.MsgCleared() {
		_spec.ClearField(msg.FieldMsg, field.TypeString)
	}
	if value, ok := mu.mutation.BatchNo(); ok {
		_spec.SetField(msg.FieldBatchNo, field.TypeString, value)
	}
	if mu.mutation.BatchNoCleared() {
		_spec.ClearField(msg.FieldBatchNo, field.TypeString)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, mu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{msg.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	mu.mutation.done = true
	return n, nil
}

// MsgUpdateOne is the builder for updating a single Msg entity.
type MsgUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *MsgMutation
}

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

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

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

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

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

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

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

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

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

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

// ClearFromwxid clears the value of the "fromwxid" field.
func (muo *MsgUpdateOne) ClearFromwxid() *MsgUpdateOne {
	muo.mutation.ClearFromwxid()
	return muo
}

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

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

// ClearToid clears the value of the "toid" field.
func (muo *MsgUpdateOne) ClearToid() *MsgUpdateOne {
	muo.mutation.ClearToid()
	return muo
}

// SetMsgtype sets the "msgtype" field.
func (muo *MsgUpdateOne) SetMsgtype(i int32) *MsgUpdateOne {
	muo.mutation.ResetMsgtype()
	muo.mutation.SetMsgtype(i)
	return muo
}

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

// AddMsgtype adds i to the "msgtype" field.
func (muo *MsgUpdateOne) AddMsgtype(i int32) *MsgUpdateOne {
	muo.mutation.AddMsgtype(i)
	return muo
}

// ClearMsgtype clears the value of the "msgtype" field.
func (muo *MsgUpdateOne) ClearMsgtype() *MsgUpdateOne {
	muo.mutation.ClearMsgtype()
	return muo
}

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

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

// ClearMsg clears the value of the "msg" field.
func (muo *MsgUpdateOne) ClearMsg() *MsgUpdateOne {
	muo.mutation.ClearMsg()
	return muo
}

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

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

// ClearBatchNo clears the value of the "batch_no" field.
func (muo *MsgUpdateOne) ClearBatchNo() *MsgUpdateOne {
	muo.mutation.ClearBatchNo()
	return muo
}

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

// Where appends a list predicates to the MsgUpdate builder.
func (muo *MsgUpdateOne) Where(ps ...predicate.Msg) *MsgUpdateOne {
	muo.mutation.Where(ps...)
	return muo
}

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

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

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

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

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

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

func (muo *MsgUpdateOne) sqlSave(ctx context.Context) (_node *Msg, err error) {
	_spec := sqlgraph.NewUpdateSpec(msg.Table, msg.Columns, sqlgraph.NewFieldSpec(msg.FieldID, field.TypeUint64))
	id, ok := muo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Msg.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := muo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, msg.FieldID)
		for _, f := range fields {
			if !msg.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != msg.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := muo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := muo.mutation.UpdatedAt(); ok {
		_spec.SetField(msg.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := muo.mutation.DeletedAt(); ok {
		_spec.SetField(msg.FieldDeletedAt, field.TypeTime, value)
	}
	if muo.mutation.DeletedAtCleared() {
		_spec.ClearField(msg.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := muo.mutation.Status(); ok {
		_spec.SetField(msg.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := muo.mutation.AddedStatus(); ok {
		_spec.AddField(msg.FieldStatus, field.TypeUint8, value)
	}
	if muo.mutation.StatusCleared() {
		_spec.ClearField(msg.FieldStatus, field.TypeUint8)
	}
	if value, ok := muo.mutation.Fromwxid(); ok {
		_spec.SetField(msg.FieldFromwxid, field.TypeString, value)
	}
	if muo.mutation.FromwxidCleared() {
		_spec.ClearField(msg.FieldFromwxid, field.TypeString)
	}
	if value, ok := muo.mutation.Toid(); ok {
		_spec.SetField(msg.FieldToid, field.TypeString, value)
	}
	if muo.mutation.ToidCleared() {
		_spec.ClearField(msg.FieldToid, field.TypeString)
	}
	if value, ok := muo.mutation.Msgtype(); ok {
		_spec.SetField(msg.FieldMsgtype, field.TypeInt32, value)
	}
	if value, ok := muo.mutation.AddedMsgtype(); ok {
		_spec.AddField(msg.FieldMsgtype, field.TypeInt32, value)
	}
	if muo.mutation.MsgtypeCleared() {
		_spec.ClearField(msg.FieldMsgtype, field.TypeInt32)
	}
	if value, ok := muo.mutation.Msg(); ok {
		_spec.SetField(msg.FieldMsg, field.TypeString, value)
	}
	if muo.mutation.MsgCleared() {
		_spec.ClearField(msg.FieldMsg, field.TypeString)
	}
	if value, ok := muo.mutation.BatchNo(); ok {
		_spec.SetField(msg.FieldBatchNo, field.TypeString, value)
	}
	if muo.mutation.BatchNoCleared() {
		_spec.ClearField(msg.FieldBatchNo, field.TypeString)
	}
	_node = &Msg{config: muo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, muo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{msg.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	muo.mutation.done = true
	return _node, nil
}