// Code generated by ent, DO NOT EDIT.

package serversitemicros

import (
	"context"
	"convert-server/domain/entity/serversitemicros/docservergetproject"
	"convert-server/domain/entity/serversitemicros/predicate"
	"errors"
	"fmt"
	"time"

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

// DocServerGetProjectUpdate is the builder for updating DocServerGetProject entities.
type DocServerGetProjectUpdate struct {
	config
	hooks    []Hook
	mutation *DocServerGetProjectMutation
}

// Where appends a list predicates to the DocServerGetProjectUpdate builder.
func (dsgpu *DocServerGetProjectUpdate) Where(ps ...predicate.DocServerGetProject) *DocServerGetProjectUpdate {
	dsgpu.mutation.Where(ps...)
	return dsgpu
}

// SetServerProjectID sets the "server_project_id" field.
func (dsgpu *DocServerGetProjectUpdate) SetServerProjectID(i int32) *DocServerGetProjectUpdate {
	dsgpu.mutation.ResetServerProjectID()
	dsgpu.mutation.SetServerProjectID(i)
	return dsgpu
}

// SetNillableServerProjectID sets the "server_project_id" field if the given value is not nil.
func (dsgpu *DocServerGetProjectUpdate) SetNillableServerProjectID(i *int32) *DocServerGetProjectUpdate {
	if i != nil {
		dsgpu.SetServerProjectID(*i)
	}
	return dsgpu
}

// AddServerProjectID adds i to the "server_project_id" field.
func (dsgpu *DocServerGetProjectUpdate) AddServerProjectID(i int32) *DocServerGetProjectUpdate {
	dsgpu.mutation.AddServerProjectID(i)
	return dsgpu
}

// SetCurrNum sets the "curr_num" field.
func (dsgpu *DocServerGetProjectUpdate) SetCurrNum(i int32) *DocServerGetProjectUpdate {
	dsgpu.mutation.ResetCurrNum()
	dsgpu.mutation.SetCurrNum(i)
	return dsgpu
}

// SetNillableCurrNum sets the "curr_num" field if the given value is not nil.
func (dsgpu *DocServerGetProjectUpdate) SetNillableCurrNum(i *int32) *DocServerGetProjectUpdate {
	if i != nil {
		dsgpu.SetCurrNum(*i)
	}
	return dsgpu
}

// AddCurrNum adds i to the "curr_num" field.
func (dsgpu *DocServerGetProjectUpdate) AddCurrNum(i int32) *DocServerGetProjectUpdate {
	dsgpu.mutation.AddCurrNum(i)
	return dsgpu
}

// SetStatus sets the "status" field.
func (dsgpu *DocServerGetProjectUpdate) SetStatus(i int8) *DocServerGetProjectUpdate {
	dsgpu.mutation.ResetStatus()
	dsgpu.mutation.SetStatus(i)
	return dsgpu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (dsgpu *DocServerGetProjectUpdate) SetNillableStatus(i *int8) *DocServerGetProjectUpdate {
	if i != nil {
		dsgpu.SetStatus(*i)
	}
	return dsgpu
}

// AddStatus adds i to the "status" field.
func (dsgpu *DocServerGetProjectUpdate) AddStatus(i int8) *DocServerGetProjectUpdate {
	dsgpu.mutation.AddStatus(i)
	return dsgpu
}

// SetAliResult sets the "ali_result" field.
func (dsgpu *DocServerGetProjectUpdate) SetAliResult(s string) *DocServerGetProjectUpdate {
	dsgpu.mutation.SetAliResult(s)
	return dsgpu
}

// SetNillableAliResult sets the "ali_result" field if the given value is not nil.
func (dsgpu *DocServerGetProjectUpdate) SetNillableAliResult(s *string) *DocServerGetProjectUpdate {
	if s != nil {
		dsgpu.SetAliResult(*s)
	}
	return dsgpu
}

// ClearAliResult clears the value of the "ali_result" field.
func (dsgpu *DocServerGetProjectUpdate) ClearAliResult() *DocServerGetProjectUpdate {
	dsgpu.mutation.ClearAliResult()
	return dsgpu
}

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

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

// ClearCreatedAt clears the value of the "created_at" field.
func (dsgpu *DocServerGetProjectUpdate) ClearCreatedAt() *DocServerGetProjectUpdate {
	dsgpu.mutation.ClearCreatedAt()
	return dsgpu
}

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

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

// ClearUpdatedAt clears the value of the "updated_at" field.
func (dsgpu *DocServerGetProjectUpdate) ClearUpdatedAt() *DocServerGetProjectUpdate {
	dsgpu.mutation.ClearUpdatedAt()
	return dsgpu
}

// Mutation returns the DocServerGetProjectMutation object of the builder.
func (dsgpu *DocServerGetProjectUpdate) Mutation() *DocServerGetProjectMutation {
	return dsgpu.mutation
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dsgpu *DocServerGetProjectUpdate) check() error {
	if v, ok := dsgpu.mutation.AliResult(); ok {
		if err := docservergetproject.AliResultValidator(v); err != nil {
			return &ValidationError{Name: "ali_result", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerGetProject.ali_result": %w`, err)}
		}
	}
	return nil
}

func (dsgpu *DocServerGetProjectUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := dsgpu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(docservergetproject.Table, docservergetproject.Columns, sqlgraph.NewFieldSpec(docservergetproject.FieldID, field.TypeInt32))
	if ps := dsgpu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dsgpu.mutation.ServerProjectID(); ok {
		_spec.SetField(docservergetproject.FieldServerProjectID, field.TypeInt32, value)
	}
	if value, ok := dsgpu.mutation.AddedServerProjectID(); ok {
		_spec.AddField(docservergetproject.FieldServerProjectID, field.TypeInt32, value)
	}
	if value, ok := dsgpu.mutation.CurrNum(); ok {
		_spec.SetField(docservergetproject.FieldCurrNum, field.TypeInt32, value)
	}
	if value, ok := dsgpu.mutation.AddedCurrNum(); ok {
		_spec.AddField(docservergetproject.FieldCurrNum, field.TypeInt32, value)
	}
	if value, ok := dsgpu.mutation.Status(); ok {
		_spec.SetField(docservergetproject.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := dsgpu.mutation.AddedStatus(); ok {
		_spec.AddField(docservergetproject.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := dsgpu.mutation.AliResult(); ok {
		_spec.SetField(docservergetproject.FieldAliResult, field.TypeString, value)
	}
	if dsgpu.mutation.AliResultCleared() {
		_spec.ClearField(docservergetproject.FieldAliResult, field.TypeString)
	}
	if value, ok := dsgpu.mutation.CreatedAt(); ok {
		_spec.SetField(docservergetproject.FieldCreatedAt, field.TypeTime, value)
	}
	if dsgpu.mutation.CreatedAtCleared() {
		_spec.ClearField(docservergetproject.FieldCreatedAt, field.TypeTime)
	}
	if value, ok := dsgpu.mutation.UpdatedAt(); ok {
		_spec.SetField(docservergetproject.FieldUpdatedAt, field.TypeTime, value)
	}
	if dsgpu.mutation.UpdatedAtCleared() {
		_spec.ClearField(docservergetproject.FieldUpdatedAt, field.TypeTime)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, dsgpu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{docservergetproject.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	dsgpu.mutation.done = true
	return n, nil
}

// DocServerGetProjectUpdateOne is the builder for updating a single DocServerGetProject entity.
type DocServerGetProjectUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DocServerGetProjectMutation
}

// SetServerProjectID sets the "server_project_id" field.
func (dsgpuo *DocServerGetProjectUpdateOne) SetServerProjectID(i int32) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.ResetServerProjectID()
	dsgpuo.mutation.SetServerProjectID(i)
	return dsgpuo
}

// SetNillableServerProjectID sets the "server_project_id" field if the given value is not nil.
func (dsgpuo *DocServerGetProjectUpdateOne) SetNillableServerProjectID(i *int32) *DocServerGetProjectUpdateOne {
	if i != nil {
		dsgpuo.SetServerProjectID(*i)
	}
	return dsgpuo
}

// AddServerProjectID adds i to the "server_project_id" field.
func (dsgpuo *DocServerGetProjectUpdateOne) AddServerProjectID(i int32) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.AddServerProjectID(i)
	return dsgpuo
}

// SetCurrNum sets the "curr_num" field.
func (dsgpuo *DocServerGetProjectUpdateOne) SetCurrNum(i int32) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.ResetCurrNum()
	dsgpuo.mutation.SetCurrNum(i)
	return dsgpuo
}

// SetNillableCurrNum sets the "curr_num" field if the given value is not nil.
func (dsgpuo *DocServerGetProjectUpdateOne) SetNillableCurrNum(i *int32) *DocServerGetProjectUpdateOne {
	if i != nil {
		dsgpuo.SetCurrNum(*i)
	}
	return dsgpuo
}

// AddCurrNum adds i to the "curr_num" field.
func (dsgpuo *DocServerGetProjectUpdateOne) AddCurrNum(i int32) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.AddCurrNum(i)
	return dsgpuo
}

// SetStatus sets the "status" field.
func (dsgpuo *DocServerGetProjectUpdateOne) SetStatus(i int8) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.ResetStatus()
	dsgpuo.mutation.SetStatus(i)
	return dsgpuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (dsgpuo *DocServerGetProjectUpdateOne) SetNillableStatus(i *int8) *DocServerGetProjectUpdateOne {
	if i != nil {
		dsgpuo.SetStatus(*i)
	}
	return dsgpuo
}

// AddStatus adds i to the "status" field.
func (dsgpuo *DocServerGetProjectUpdateOne) AddStatus(i int8) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.AddStatus(i)
	return dsgpuo
}

// SetAliResult sets the "ali_result" field.
func (dsgpuo *DocServerGetProjectUpdateOne) SetAliResult(s string) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.SetAliResult(s)
	return dsgpuo
}

// SetNillableAliResult sets the "ali_result" field if the given value is not nil.
func (dsgpuo *DocServerGetProjectUpdateOne) SetNillableAliResult(s *string) *DocServerGetProjectUpdateOne {
	if s != nil {
		dsgpuo.SetAliResult(*s)
	}
	return dsgpuo
}

// ClearAliResult clears the value of the "ali_result" field.
func (dsgpuo *DocServerGetProjectUpdateOne) ClearAliResult() *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.ClearAliResult()
	return dsgpuo
}

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

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

// ClearCreatedAt clears the value of the "created_at" field.
func (dsgpuo *DocServerGetProjectUpdateOne) ClearCreatedAt() *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.ClearCreatedAt()
	return dsgpuo
}

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

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

// ClearUpdatedAt clears the value of the "updated_at" field.
func (dsgpuo *DocServerGetProjectUpdateOne) ClearUpdatedAt() *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.ClearUpdatedAt()
	return dsgpuo
}

// Mutation returns the DocServerGetProjectMutation object of the builder.
func (dsgpuo *DocServerGetProjectUpdateOne) Mutation() *DocServerGetProjectMutation {
	return dsgpuo.mutation
}

// Where appends a list predicates to the DocServerGetProjectUpdate builder.
func (dsgpuo *DocServerGetProjectUpdateOne) Where(ps ...predicate.DocServerGetProject) *DocServerGetProjectUpdateOne {
	dsgpuo.mutation.Where(ps...)
	return dsgpuo
}

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

// Save executes the query and returns the updated DocServerGetProject entity.
func (dsgpuo *DocServerGetProjectUpdateOne) Save(ctx context.Context) (*DocServerGetProject, error) {
	return withHooks(ctx, dsgpuo.sqlSave, dsgpuo.mutation, dsgpuo.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dsgpuo *DocServerGetProjectUpdateOne) check() error {
	if v, ok := dsgpuo.mutation.AliResult(); ok {
		if err := docservergetproject.AliResultValidator(v); err != nil {
			return &ValidationError{Name: "ali_result", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerGetProject.ali_result": %w`, err)}
		}
	}
	return nil
}

func (dsgpuo *DocServerGetProjectUpdateOne) sqlSave(ctx context.Context) (_node *DocServerGetProject, err error) {
	if err := dsgpuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(docservergetproject.Table, docservergetproject.Columns, sqlgraph.NewFieldSpec(docservergetproject.FieldID, field.TypeInt32))
	id, ok := dsgpuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`serversitemicros: missing "DocServerGetProject.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := dsgpuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, docservergetproject.FieldID)
		for _, f := range fields {
			if !docservergetproject.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("serversitemicros: invalid field %q for query", f)}
			}
			if f != docservergetproject.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := dsgpuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dsgpuo.mutation.ServerProjectID(); ok {
		_spec.SetField(docservergetproject.FieldServerProjectID, field.TypeInt32, value)
	}
	if value, ok := dsgpuo.mutation.AddedServerProjectID(); ok {
		_spec.AddField(docservergetproject.FieldServerProjectID, field.TypeInt32, value)
	}
	if value, ok := dsgpuo.mutation.CurrNum(); ok {
		_spec.SetField(docservergetproject.FieldCurrNum, field.TypeInt32, value)
	}
	if value, ok := dsgpuo.mutation.AddedCurrNum(); ok {
		_spec.AddField(docservergetproject.FieldCurrNum, field.TypeInt32, value)
	}
	if value, ok := dsgpuo.mutation.Status(); ok {
		_spec.SetField(docservergetproject.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := dsgpuo.mutation.AddedStatus(); ok {
		_spec.AddField(docservergetproject.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := dsgpuo.mutation.AliResult(); ok {
		_spec.SetField(docservergetproject.FieldAliResult, field.TypeString, value)
	}
	if dsgpuo.mutation.AliResultCleared() {
		_spec.ClearField(docservergetproject.FieldAliResult, field.TypeString)
	}
	if value, ok := dsgpuo.mutation.CreatedAt(); ok {
		_spec.SetField(docservergetproject.FieldCreatedAt, field.TypeTime, value)
	}
	if dsgpuo.mutation.CreatedAtCleared() {
		_spec.ClearField(docservergetproject.FieldCreatedAt, field.TypeTime)
	}
	if value, ok := dsgpuo.mutation.UpdatedAt(); ok {
		_spec.SetField(docservergetproject.FieldUpdatedAt, field.TypeTime, value)
	}
	if dsgpuo.mutation.UpdatedAtCleared() {
		_spec.ClearField(docservergetproject.FieldUpdatedAt, field.TypeTime)
	}
	_node = &DocServerGetProject{config: dsgpuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, dsgpuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{docservergetproject.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	dsgpuo.mutation.done = true
	return _node, nil
}
