// Code generated by ent, DO NOT EDIT.

package ent

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

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

// WpChatroomUpdate is the builder for updating WpChatroom entities.
type WpChatroomUpdate struct {
	config
	hooks    []Hook
	mutation *WpChatroomMutation
}

// Where appends a list predicates to the WpChatroomUpdate builder.
func (wcu *WpChatroomUpdate) Where(ps ...predicate.WpChatroom) *WpChatroomUpdate {
	wcu.mutation.Where(ps...)
	return wcu
}

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

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

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

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

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

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

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

// SetChatroomID sets the "chatroom_id" field.
func (wcu *WpChatroomUpdate) SetChatroomID(s string) *WpChatroomUpdate {
	wcu.mutation.SetChatroomID(s)
	return wcu
}

// SetNillableChatroomID sets the "chatroom_id" field if the given value is not nil.
func (wcu *WpChatroomUpdate) SetNillableChatroomID(s *string) *WpChatroomUpdate {
	if s != nil {
		wcu.SetChatroomID(*s)
	}
	return wcu
}

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

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

// SetOwner sets the "owner" field.
func (wcu *WpChatroomUpdate) SetOwner(s string) *WpChatroomUpdate {
	wcu.mutation.SetOwner(s)
	return wcu
}

// SetNillableOwner sets the "owner" field if the given value is not nil.
func (wcu *WpChatroomUpdate) SetNillableOwner(s *string) *WpChatroomUpdate {
	if s != nil {
		wcu.SetOwner(*s)
	}
	return wcu
}

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

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

// SetMemberList sets the "member_list" field.
func (wcu *WpChatroomUpdate) SetMemberList(s []string) *WpChatroomUpdate {
	wcu.mutation.SetMemberList(s)
	return wcu
}

// AppendMemberList appends s to the "member_list" field.
func (wcu *WpChatroomUpdate) AppendMemberList(s []string) *WpChatroomUpdate {
	wcu.mutation.AppendMemberList(s)
	return wcu
}

// Mutation returns the WpChatroomMutation object of the builder.
func (wcu *WpChatroomUpdate) Mutation() *WpChatroomMutation {
	return wcu.mutation
}

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

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

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

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

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

func (wcu *WpChatroomUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(wpchatroom.Table, wpchatroom.Columns, sqlgraph.NewFieldSpec(wpchatroom.FieldID, field.TypeUint64))
	if ps := wcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := wcu.mutation.UpdatedAt(); ok {
		_spec.SetField(wpchatroom.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := wcu.mutation.Status(); ok {
		_spec.SetField(wpchatroom.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := wcu.mutation.AddedStatus(); ok {
		_spec.AddField(wpchatroom.FieldStatus, field.TypeUint8, value)
	}
	if wcu.mutation.StatusCleared() {
		_spec.ClearField(wpchatroom.FieldStatus, field.TypeUint8)
	}
	if value, ok := wcu.mutation.WxWxid(); ok {
		_spec.SetField(wpchatroom.FieldWxWxid, field.TypeString, value)
	}
	if value, ok := wcu.mutation.ChatroomID(); ok {
		_spec.SetField(wpchatroom.FieldChatroomID, field.TypeString, value)
	}
	if value, ok := wcu.mutation.Nickname(); ok {
		_spec.SetField(wpchatroom.FieldNickname, field.TypeString, value)
	}
	if value, ok := wcu.mutation.Owner(); ok {
		_spec.SetField(wpchatroom.FieldOwner, field.TypeString, value)
	}
	if value, ok := wcu.mutation.Avatar(); ok {
		_spec.SetField(wpchatroom.FieldAvatar, field.TypeString, value)
	}
	if value, ok := wcu.mutation.MemberList(); ok {
		_spec.SetField(wpchatroom.FieldMemberList, field.TypeJSON, value)
	}
	if value, ok := wcu.mutation.AppendedMemberList(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, wpchatroom.FieldMemberList, value)
		})
	}
	if n, err = sqlgraph.UpdateNodes(ctx, wcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{wpchatroom.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	wcu.mutation.done = true
	return n, nil
}

// WpChatroomUpdateOne is the builder for updating a single WpChatroom entity.
type WpChatroomUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *WpChatroomMutation
}

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

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

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

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

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

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

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

// SetChatroomID sets the "chatroom_id" field.
func (wcuo *WpChatroomUpdateOne) SetChatroomID(s string) *WpChatroomUpdateOne {
	wcuo.mutation.SetChatroomID(s)
	return wcuo
}

// SetNillableChatroomID sets the "chatroom_id" field if the given value is not nil.
func (wcuo *WpChatroomUpdateOne) SetNillableChatroomID(s *string) *WpChatroomUpdateOne {
	if s != nil {
		wcuo.SetChatroomID(*s)
	}
	return wcuo
}

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

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

// SetOwner sets the "owner" field.
func (wcuo *WpChatroomUpdateOne) SetOwner(s string) *WpChatroomUpdateOne {
	wcuo.mutation.SetOwner(s)
	return wcuo
}

// SetNillableOwner sets the "owner" field if the given value is not nil.
func (wcuo *WpChatroomUpdateOne) SetNillableOwner(s *string) *WpChatroomUpdateOne {
	if s != nil {
		wcuo.SetOwner(*s)
	}
	return wcuo
}

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

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

// SetMemberList sets the "member_list" field.
func (wcuo *WpChatroomUpdateOne) SetMemberList(s []string) *WpChatroomUpdateOne {
	wcuo.mutation.SetMemberList(s)
	return wcuo
}

// AppendMemberList appends s to the "member_list" field.
func (wcuo *WpChatroomUpdateOne) AppendMemberList(s []string) *WpChatroomUpdateOne {
	wcuo.mutation.AppendMemberList(s)
	return wcuo
}

// Mutation returns the WpChatroomMutation object of the builder.
func (wcuo *WpChatroomUpdateOne) Mutation() *WpChatroomMutation {
	return wcuo.mutation
}

// Where appends a list predicates to the WpChatroomUpdate builder.
func (wcuo *WpChatroomUpdateOne) Where(ps ...predicate.WpChatroom) *WpChatroomUpdateOne {
	wcuo.mutation.Where(ps...)
	return wcuo
}

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

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

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

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

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

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

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