// Code generated by ent, DO NOT EDIT.

package ent

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

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

// WpChatroomMemberUpdate is the builder for updating WpChatroomMember entities.
type WpChatroomMemberUpdate struct {
	config
	hooks    []Hook
	mutation *WpChatroomMemberMutation
}

// Where appends a list predicates to the WpChatroomMemberUpdate builder.
func (wcmu *WpChatroomMemberUpdate) Where(ps ...predicate.WpChatroomMember) *WpChatroomMemberUpdate {
	wcmu.mutation.Where(ps...)
	return wcmu
}

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

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

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

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

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

// SetWxWxid sets the "wx_wxid" field.
func (wcmu *WpChatroomMemberUpdate) SetWxWxid(s string) *WpChatroomMemberUpdate {
	wcmu.mutation.SetWxWxid(s)
	return wcmu
}

// SetNillableWxWxid sets the "wx_wxid" field if the given value is not nil.
func (wcmu *WpChatroomMemberUpdate) SetNillableWxWxid(s *string) *WpChatroomMemberUpdate {
	if s != nil {
		wcmu.SetWxWxid(*s)
	}
	return wcmu
}

// SetWxid sets the "wxid" field.
func (wcmu *WpChatroomMemberUpdate) SetWxid(s string) *WpChatroomMemberUpdate {
	wcmu.mutation.SetWxid(s)
	return wcmu
}

// SetNillableWxid sets the "wxid" field if the given value is not nil.
func (wcmu *WpChatroomMemberUpdate) SetNillableWxid(s *string) *WpChatroomMemberUpdate {
	if s != nil {
		wcmu.SetWxid(*s)
	}
	return wcmu
}

// SetNickname sets the "nickname" field.
func (wcmu *WpChatroomMemberUpdate) SetNickname(s string) *WpChatroomMemberUpdate {
	wcmu.mutation.SetNickname(s)
	return wcmu
}

// SetNillableNickname sets the "nickname" field if the given value is not nil.
func (wcmu *WpChatroomMemberUpdate) SetNillableNickname(s *string) *WpChatroomMemberUpdate {
	if s != nil {
		wcmu.SetNickname(*s)
	}
	return wcmu
}

// SetAvatar sets the "avatar" field.
func (wcmu *WpChatroomMemberUpdate) SetAvatar(s string) *WpChatroomMemberUpdate {
	wcmu.mutation.SetAvatar(s)
	return wcmu
}

// SetNillableAvatar sets the "avatar" field if the given value is not nil.
func (wcmu *WpChatroomMemberUpdate) SetNillableAvatar(s *string) *WpChatroomMemberUpdate {
	if s != nil {
		wcmu.SetAvatar(*s)
	}
	return wcmu
}

// Mutation returns the WpChatroomMemberMutation object of the builder.
func (wcmu *WpChatroomMemberUpdate) Mutation() *WpChatroomMemberMutation {
	return wcmu.mutation
}

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

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

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

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

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

func (wcmu *WpChatroomMemberUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(wpchatroommember.Table, wpchatroommember.Columns, sqlgraph.NewFieldSpec(wpchatroommember.FieldID, field.TypeUint64))
	if ps := wcmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcmu.mutation.UpdatedAt(); ok {
		_spec.SetField(wpchatroommember.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcmu.mutation.Status(); ok {
		_spec.SetField(wpchatroommember.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := wcmu.mutation.AddedStatus(); ok {
		_spec.AddField(wpchatroommember.FieldStatus, field.TypeUint8, value)
	}
	if wcmu.mutation.StatusCleared() {
		_spec.ClearField(wpchatroommember.FieldStatus, field.TypeUint8)
	}
	if value, ok := wcmu.mutation.WxWxid(); ok {
		_spec.SetField(wpchatroommember.FieldWxWxid, field.TypeString, value)
	}
	if value, ok := wcmu.mutation.Wxid(); ok {
		_spec.SetField(wpchatroommember.FieldWxid, field.TypeString, value)
	}
	if value, ok := wcmu.mutation.Nickname(); ok {
		_spec.SetField(wpchatroommember.FieldNickname, field.TypeString, value)
	}
	if value, ok := wcmu.mutation.Avatar(); ok {
		_spec.SetField(wpchatroommember.FieldAvatar, field.TypeString, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, wcmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wpchatroommember.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	wcmu.mutation.done = true
	return n, nil
}

// WpChatroomMemberUpdateOne is the builder for updating a single WpChatroomMember entity.
type WpChatroomMemberUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WpChatroomMemberMutation
}

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

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

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

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

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

// SetWxWxid sets the "wx_wxid" field.
func (wcmuo *WpChatroomMemberUpdateOne) SetWxWxid(s string) *WpChatroomMemberUpdateOne {
	wcmuo.mutation.SetWxWxid(s)
	return wcmuo
}

// SetNillableWxWxid sets the "wx_wxid" field if the given value is not nil.
func (wcmuo *WpChatroomMemberUpdateOne) SetNillableWxWxid(s *string) *WpChatroomMemberUpdateOne {
	if s != nil {
		wcmuo.SetWxWxid(*s)
	}
	return wcmuo
}

// SetWxid sets the "wxid" field.
func (wcmuo *WpChatroomMemberUpdateOne) SetWxid(s string) *WpChatroomMemberUpdateOne {
	wcmuo.mutation.SetWxid(s)
	return wcmuo
}

// SetNillableWxid sets the "wxid" field if the given value is not nil.
func (wcmuo *WpChatroomMemberUpdateOne) SetNillableWxid(s *string) *WpChatroomMemberUpdateOne {
	if s != nil {
		wcmuo.SetWxid(*s)
	}
	return wcmuo
}

// SetNickname sets the "nickname" field.
func (wcmuo *WpChatroomMemberUpdateOne) SetNickname(s string) *WpChatroomMemberUpdateOne {
	wcmuo.mutation.SetNickname(s)
	return wcmuo
}

// SetNillableNickname sets the "nickname" field if the given value is not nil.
func (wcmuo *WpChatroomMemberUpdateOne) SetNillableNickname(s *string) *WpChatroomMemberUpdateOne {
	if s != nil {
		wcmuo.SetNickname(*s)
	}
	return wcmuo
}

// SetAvatar sets the "avatar" field.
func (wcmuo *WpChatroomMemberUpdateOne) SetAvatar(s string) *WpChatroomMemberUpdateOne {
	wcmuo.mutation.SetAvatar(s)
	return wcmuo
}

// SetNillableAvatar sets the "avatar" field if the given value is not nil.
func (wcmuo *WpChatroomMemberUpdateOne) SetNillableAvatar(s *string) *WpChatroomMemberUpdateOne {
	if s != nil {
		wcmuo.SetAvatar(*s)
	}
	return wcmuo
}

// Mutation returns the WpChatroomMemberMutation object of the builder.
func (wcmuo *WpChatroomMemberUpdateOne) Mutation() *WpChatroomMemberMutation {
	return wcmuo.mutation
}

// Where appends a list predicates to the WpChatroomMemberUpdate builder.
func (wcmuo *WpChatroomMemberUpdateOne) Where(ps ...predicate.WpChatroomMember) *WpChatroomMemberUpdateOne {
	wcmuo.mutation.Where(ps...)
	return wcmuo
}

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

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

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

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

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

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

func (wcmuo *WpChatroomMemberUpdateOne) sqlSave(ctx context.Context) (_node *WpChatroomMember, err error) {
	_spec := sqlgraph.NewUpdateSpec(wpchatroommember.Table, wpchatroommember.Columns, sqlgraph.NewFieldSpec(wpchatroommember.FieldID, field.TypeUint64))
	id, ok := wcmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "WpChatroomMember.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := wcmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, wpchatroommember.FieldID)
		for _, f := range fields {
			if !wpchatroommember.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != wpchatroommember.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := wcmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcmuo.mutation.UpdatedAt(); ok {
		_spec.SetField(wpchatroommember.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcmuo.mutation.Status(); ok {
		_spec.SetField(wpchatroommember.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := wcmuo.mutation.AddedStatus(); ok {
		_spec.AddField(wpchatroommember.FieldStatus, field.TypeUint8, value)
	}
	if wcmuo.mutation.StatusCleared() {
		_spec.ClearField(wpchatroommember.FieldStatus, field.TypeUint8)
	}
	if value, ok := wcmuo.mutation.WxWxid(); ok {
		_spec.SetField(wpchatroommember.FieldWxWxid, field.TypeString, value)
	}
	if value, ok := wcmuo.mutation.Wxid(); ok {
		_spec.SetField(wpchatroommember.FieldWxid, field.TypeString, value)
	}
	if value, ok := wcmuo.mutation.Nickname(); ok {
		_spec.SetField(wpchatroommember.FieldNickname, field.TypeString, value)
	}
	if value, ok := wcmuo.mutation.Avatar(); ok {
		_spec.SetField(wpchatroommember.FieldAvatar, field.TypeString, value)
	}
	_node = &WpChatroomMember{config: wcmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, wcmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wpchatroommember.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	wcmuo.mutation.done = true
	return _node, nil
}
