// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ServerUpdate is the builder for updating Server entities.
type ServerUpdate struct {
	config
	hooks    []Hook
	mutation *ServerMutation
}

// Where appends a list predicates to the ServerUpdate builder.
func (su *ServerUpdate) Where(ps ...predicate.Server) *ServerUpdate {
	su.mutation.Where(ps...)
	return su
}

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

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

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

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

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

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

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

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

// SetName sets the "name" field.
func (su *ServerUpdate) SetName(s string) *ServerUpdate {
	su.mutation.SetName(s)
	return su
}

// SetNillableName sets the "name" field if the given value is not nil.
func (su *ServerUpdate) SetNillableName(s *string) *ServerUpdate {
	if s != nil {
		su.SetName(*s)
	}
	return su
}

// SetPublicIP sets the "public_ip" field.
func (su *ServerUpdate) SetPublicIP(s string) *ServerUpdate {
	su.mutation.SetPublicIP(s)
	return su
}

// SetNillablePublicIP sets the "public_ip" field if the given value is not nil.
func (su *ServerUpdate) SetNillablePublicIP(s *string) *ServerUpdate {
	if s != nil {
		su.SetPublicIP(*s)
	}
	return su
}

// SetPrivateIP sets the "private_ip" field.
func (su *ServerUpdate) SetPrivateIP(s string) *ServerUpdate {
	su.mutation.SetPrivateIP(s)
	return su
}

// SetNillablePrivateIP sets the "private_ip" field if the given value is not nil.
func (su *ServerUpdate) SetNillablePrivateIP(s *string) *ServerUpdate {
	if s != nil {
		su.SetPrivateIP(*s)
	}
	return su
}

// SetAdminPort sets the "admin_port" field.
func (su *ServerUpdate) SetAdminPort(s string) *ServerUpdate {
	su.mutation.SetAdminPort(s)
	return su
}

// SetNillableAdminPort sets the "admin_port" field if the given value is not nil.
func (su *ServerUpdate) SetNillableAdminPort(s *string) *ServerUpdate {
	if s != nil {
		su.SetAdminPort(*s)
	}
	return su
}

// AddWxIDs adds the "wxs" edge to the Wx entity by IDs.
func (su *ServerUpdate) AddWxIDs(ids ...uint64) *ServerUpdate {
	su.mutation.AddWxIDs(ids...)
	return su
}

// AddWxs adds the "wxs" edges to the Wx entity.
func (su *ServerUpdate) AddWxs(w ...*Wx) *ServerUpdate {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return su.AddWxIDs(ids...)
}

// Mutation returns the ServerMutation object of the builder.
func (su *ServerUpdate) Mutation() *ServerMutation {
	return su.mutation
}

// ClearWxs clears all "wxs" edges to the Wx entity.
func (su *ServerUpdate) ClearWxs() *ServerUpdate {
	su.mutation.ClearWxs()
	return su
}

// RemoveWxIDs removes the "wxs" edge to Wx entities by IDs.
func (su *ServerUpdate) RemoveWxIDs(ids ...uint64) *ServerUpdate {
	su.mutation.RemoveWxIDs(ids...)
	return su
}

// RemoveWxs removes "wxs" edges to Wx entities.
func (su *ServerUpdate) RemoveWxs(w ...*Wx) *ServerUpdate {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return su.RemoveWxIDs(ids...)
}

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

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

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

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

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

func (su *ServerUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(server.Table, server.Columns, sqlgraph.NewFieldSpec(server.FieldID, field.TypeUint64))
	if ps := su.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := su.mutation.UpdatedAt(); ok {
		_spec.SetField(server.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := su.mutation.Status(); ok {
		_spec.SetField(server.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := su.mutation.AddedStatus(); ok {
		_spec.AddField(server.FieldStatus, field.TypeUint8, value)
	}
	if su.mutation.StatusCleared() {
		_spec.ClearField(server.FieldStatus, field.TypeUint8)
	}
	if value, ok := su.mutation.DeletedAt(); ok {
		_spec.SetField(server.FieldDeletedAt, field.TypeTime, value)
	}
	if su.mutation.DeletedAtCleared() {
		_spec.ClearField(server.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := su.mutation.Name(); ok {
		_spec.SetField(server.FieldName, field.TypeString, value)
	}
	if value, ok := su.mutation.PublicIP(); ok {
		_spec.SetField(server.FieldPublicIP, field.TypeString, value)
	}
	if value, ok := su.mutation.PrivateIP(); ok {
		_spec.SetField(server.FieldPrivateIP, field.TypeString, value)
	}
	if value, ok := su.mutation.AdminPort(); ok {
		_spec.SetField(server.FieldAdminPort, field.TypeString, value)
	}
	if su.mutation.WxsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   server.WxsTable,
			Columns: []string{server.WxsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := su.mutation.RemovedWxsIDs(); len(nodes) > 0 && !su.mutation.WxsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   server.WxsTable,
			Columns: []string{server.WxsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := su.mutation.WxsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   server.WxsTable,
			Columns: []string{server.WxsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.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, su.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{server.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	su.mutation.done = true
	return n, nil
}

// ServerUpdateOne is the builder for updating a single Server entity.
type ServerUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ServerMutation
}

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

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

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

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

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

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

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

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

// SetName sets the "name" field.
func (suo *ServerUpdateOne) SetName(s string) *ServerUpdateOne {
	suo.mutation.SetName(s)
	return suo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (suo *ServerUpdateOne) SetNillableName(s *string) *ServerUpdateOne {
	if s != nil {
		suo.SetName(*s)
	}
	return suo
}

// SetPublicIP sets the "public_ip" field.
func (suo *ServerUpdateOne) SetPublicIP(s string) *ServerUpdateOne {
	suo.mutation.SetPublicIP(s)
	return suo
}

// SetNillablePublicIP sets the "public_ip" field if the given value is not nil.
func (suo *ServerUpdateOne) SetNillablePublicIP(s *string) *ServerUpdateOne {
	if s != nil {
		suo.SetPublicIP(*s)
	}
	return suo
}

// SetPrivateIP sets the "private_ip" field.
func (suo *ServerUpdateOne) SetPrivateIP(s string) *ServerUpdateOne {
	suo.mutation.SetPrivateIP(s)
	return suo
}

// SetNillablePrivateIP sets the "private_ip" field if the given value is not nil.
func (suo *ServerUpdateOne) SetNillablePrivateIP(s *string) *ServerUpdateOne {
	if s != nil {
		suo.SetPrivateIP(*s)
	}
	return suo
}

// SetAdminPort sets the "admin_port" field.
func (suo *ServerUpdateOne) SetAdminPort(s string) *ServerUpdateOne {
	suo.mutation.SetAdminPort(s)
	return suo
}

// SetNillableAdminPort sets the "admin_port" field if the given value is not nil.
func (suo *ServerUpdateOne) SetNillableAdminPort(s *string) *ServerUpdateOne {
	if s != nil {
		suo.SetAdminPort(*s)
	}
	return suo
}

// AddWxIDs adds the "wxs" edge to the Wx entity by IDs.
func (suo *ServerUpdateOne) AddWxIDs(ids ...uint64) *ServerUpdateOne {
	suo.mutation.AddWxIDs(ids...)
	return suo
}

// AddWxs adds the "wxs" edges to the Wx entity.
func (suo *ServerUpdateOne) AddWxs(w ...*Wx) *ServerUpdateOne {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return suo.AddWxIDs(ids...)
}

// Mutation returns the ServerMutation object of the builder.
func (suo *ServerUpdateOne) Mutation() *ServerMutation {
	return suo.mutation
}

// ClearWxs clears all "wxs" edges to the Wx entity.
func (suo *ServerUpdateOne) ClearWxs() *ServerUpdateOne {
	suo.mutation.ClearWxs()
	return suo
}

// RemoveWxIDs removes the "wxs" edge to Wx entities by IDs.
func (suo *ServerUpdateOne) RemoveWxIDs(ids ...uint64) *ServerUpdateOne {
	suo.mutation.RemoveWxIDs(ids...)
	return suo
}

// RemoveWxs removes "wxs" edges to Wx entities.
func (suo *ServerUpdateOne) RemoveWxs(w ...*Wx) *ServerUpdateOne {
	ids := make([]uint64, len(w))
	for i := range w {
		ids[i] = w[i].ID
	}
	return suo.RemoveWxIDs(ids...)
}

// Where appends a list predicates to the ServerUpdate builder.
func (suo *ServerUpdateOne) Where(ps ...predicate.Server) *ServerUpdateOne {
	suo.mutation.Where(ps...)
	return suo
}

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

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

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

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

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

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

func (suo *ServerUpdateOne) sqlSave(ctx context.Context) (_node *Server, err error) {
	_spec := sqlgraph.NewUpdateSpec(server.Table, server.Columns, sqlgraph.NewFieldSpec(server.FieldID, field.TypeUint64))
	id, ok := suo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Server.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := suo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, server.FieldID)
		for _, f := range fields {
			if !server.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != server.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := suo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := suo.mutation.UpdatedAt(); ok {
		_spec.SetField(server.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := suo.mutation.Status(); ok {
		_spec.SetField(server.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := suo.mutation.AddedStatus(); ok {
		_spec.AddField(server.FieldStatus, field.TypeUint8, value)
	}
	if suo.mutation.StatusCleared() {
		_spec.ClearField(server.FieldStatus, field.TypeUint8)
	}
	if value, ok := suo.mutation.DeletedAt(); ok {
		_spec.SetField(server.FieldDeletedAt, field.TypeTime, value)
	}
	if suo.mutation.DeletedAtCleared() {
		_spec.ClearField(server.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := suo.mutation.Name(); ok {
		_spec.SetField(server.FieldName, field.TypeString, value)
	}
	if value, ok := suo.mutation.PublicIP(); ok {
		_spec.SetField(server.FieldPublicIP, field.TypeString, value)
	}
	if value, ok := suo.mutation.PrivateIP(); ok {
		_spec.SetField(server.FieldPrivateIP, field.TypeString, value)
	}
	if value, ok := suo.mutation.AdminPort(); ok {
		_spec.SetField(server.FieldAdminPort, field.TypeString, value)
	}
	if suo.mutation.WxsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   server.WxsTable,
			Columns: []string{server.WxsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := suo.mutation.RemovedWxsIDs(); len(nodes) > 0 && !suo.mutation.WxsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   server.WxsTable,
			Columns: []string{server.WxsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := suo.mutation.WxsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   server.WxsTable,
			Columns: []string{server.WxsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Server{config: suo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, suo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{server.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	suo.mutation.done = true
	return _node, nil
}
