// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/predicate"
	"galileo/ent/projectmember"
	"time"

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

// ProjectMemberUpdate is the builder for updating ProjectMember entities.
type ProjectMemberUpdate struct {
	config
	hooks    []Hook
	mutation *ProjectMemberMutation
}

// Where appends a list predicates to the ProjectMemberUpdate builder.
func (pmu *ProjectMemberUpdate) Where(ps ...predicate.ProjectMember) *ProjectMemberUpdate {
	pmu.mutation.Where(ps...)
	return pmu
}

// SetProjectID sets the "project_id" field.
func (pmu *ProjectMemberUpdate) SetProjectID(i int64) *ProjectMemberUpdate {
	pmu.mutation.ResetProjectID()
	pmu.mutation.SetProjectID(i)
	return pmu
}

// AddProjectID adds i to the "project_id" field.
func (pmu *ProjectMemberUpdate) AddProjectID(i int64) *ProjectMemberUpdate {
	pmu.mutation.AddProjectID(i)
	return pmu
}

// SetUserID sets the "user_id" field.
func (pmu *ProjectMemberUpdate) SetUserID(u uint32) *ProjectMemberUpdate {
	pmu.mutation.ResetUserID()
	pmu.mutation.SetUserID(u)
	return pmu
}

// AddUserID adds u to the "user_id" field.
func (pmu *ProjectMemberUpdate) AddUserID(u int32) *ProjectMemberUpdate {
	pmu.mutation.AddUserID(u)
	return pmu
}

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

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

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

// SetDeletedBy sets the "deleted_by" field.
func (pmu *ProjectMemberUpdate) SetDeletedBy(u uint32) *ProjectMemberUpdate {
	pmu.mutation.ResetDeletedBy()
	pmu.mutation.SetDeletedBy(u)
	return pmu
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (pmu *ProjectMemberUpdate) SetNillableDeletedBy(u *uint32) *ProjectMemberUpdate {
	if u != nil {
		pmu.SetDeletedBy(*u)
	}
	return pmu
}

// AddDeletedBy adds u to the "deleted_by" field.
func (pmu *ProjectMemberUpdate) AddDeletedBy(u int32) *ProjectMemberUpdate {
	pmu.mutation.AddDeletedBy(u)
	return pmu
}

// ClearDeletedBy clears the value of the "deleted_by" field.
func (pmu *ProjectMemberUpdate) ClearDeletedBy() *ProjectMemberUpdate {
	pmu.mutation.ClearDeletedBy()
	return pmu
}

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

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

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

// SetDescription sets the "description" field.
func (pmu *ProjectMemberUpdate) SetDescription(s string) *ProjectMemberUpdate {
	pmu.mutation.SetDescription(s)
	return pmu
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (pmu *ProjectMemberUpdate) SetNillableDescription(s *string) *ProjectMemberUpdate {
	if s != nil {
		pmu.SetDescription(*s)
	}
	return pmu
}

// ClearDescription clears the value of the "description" field.
func (pmu *ProjectMemberUpdate) ClearDescription() *ProjectMemberUpdate {
	pmu.mutation.ClearDescription()
	return pmu
}

// SetRemark sets the "remark" field.
func (pmu *ProjectMemberUpdate) SetRemark(s string) *ProjectMemberUpdate {
	pmu.mutation.SetRemark(s)
	return pmu
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (pmu *ProjectMemberUpdate) SetNillableRemark(s *string) *ProjectMemberUpdate {
	if s != nil {
		pmu.SetRemark(*s)
	}
	return pmu
}

// ClearRemark clears the value of the "remark" field.
func (pmu *ProjectMemberUpdate) ClearRemark() *ProjectMemberUpdate {
	pmu.mutation.ClearRemark()
	return pmu
}

// SetRole sets the "role" field.
func (pmu *ProjectMemberUpdate) SetRole(u uint8) *ProjectMemberUpdate {
	pmu.mutation.ResetRole()
	pmu.mutation.SetRole(u)
	return pmu
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (pmu *ProjectMemberUpdate) SetNillableRole(u *uint8) *ProjectMemberUpdate {
	if u != nil {
		pmu.SetRole(*u)
	}
	return pmu
}

// AddRole adds u to the "role" field.
func (pmu *ProjectMemberUpdate) AddRole(u int8) *ProjectMemberUpdate {
	pmu.mutation.AddRole(u)
	return pmu
}

// Mutation returns the ProjectMemberMutation object of the builder.
func (pmu *ProjectMemberUpdate) Mutation() *ProjectMemberMutation {
	return pmu.mutation
}

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

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

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

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

func (pmu *ProjectMemberUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(projectmember.Table, projectmember.Columns, sqlgraph.NewFieldSpec(projectmember.FieldID, field.TypeInt))
	if ps := pmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := pmu.mutation.ProjectID(); ok {
		_spec.SetField(projectmember.FieldProjectID, field.TypeInt64, value)
	}
	if value, ok := pmu.mutation.AddedProjectID(); ok {
		_spec.AddField(projectmember.FieldProjectID, field.TypeInt64, value)
	}
	if value, ok := pmu.mutation.UserID(); ok {
		_spec.SetField(projectmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := pmu.mutation.AddedUserID(); ok {
		_spec.AddField(projectmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := pmu.mutation.DeletedAt(); ok {
		_spec.SetField(projectmember.FieldDeletedAt, field.TypeTime, value)
	}
	if pmu.mutation.DeletedAtCleared() {
		_spec.ClearField(projectmember.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := pmu.mutation.DeletedBy(); ok {
		_spec.SetField(projectmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if value, ok := pmu.mutation.AddedDeletedBy(); ok {
		_spec.AddField(projectmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if pmu.mutation.DeletedByCleared() {
		_spec.ClearField(projectmember.FieldDeletedBy, field.TypeUint32)
	}
	if value, ok := pmu.mutation.Status(); ok {
		_spec.SetField(projectmember.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := pmu.mutation.AddedStatus(); ok {
		_spec.AddField(projectmember.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := pmu.mutation.Description(); ok {
		_spec.SetField(projectmember.FieldDescription, field.TypeString, value)
	}
	if pmu.mutation.DescriptionCleared() {
		_spec.ClearField(projectmember.FieldDescription, field.TypeString)
	}
	if value, ok := pmu.mutation.Remark(); ok {
		_spec.SetField(projectmember.FieldRemark, field.TypeString, value)
	}
	if pmu.mutation.RemarkCleared() {
		_spec.ClearField(projectmember.FieldRemark, field.TypeString)
	}
	if value, ok := pmu.mutation.Role(); ok {
		_spec.SetField(projectmember.FieldRole, field.TypeUint8, value)
	}
	if value, ok := pmu.mutation.AddedRole(); ok {
		_spec.AddField(projectmember.FieldRole, field.TypeUint8, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, pmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{projectmember.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	pmu.mutation.done = true
	return n, nil
}

// ProjectMemberUpdateOne is the builder for updating a single ProjectMember entity.
type ProjectMemberUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ProjectMemberMutation
}

// SetProjectID sets the "project_id" field.
func (pmuo *ProjectMemberUpdateOne) SetProjectID(i int64) *ProjectMemberUpdateOne {
	pmuo.mutation.ResetProjectID()
	pmuo.mutation.SetProjectID(i)
	return pmuo
}

// AddProjectID adds i to the "project_id" field.
func (pmuo *ProjectMemberUpdateOne) AddProjectID(i int64) *ProjectMemberUpdateOne {
	pmuo.mutation.AddProjectID(i)
	return pmuo
}

// SetUserID sets the "user_id" field.
func (pmuo *ProjectMemberUpdateOne) SetUserID(u uint32) *ProjectMemberUpdateOne {
	pmuo.mutation.ResetUserID()
	pmuo.mutation.SetUserID(u)
	return pmuo
}

// AddUserID adds u to the "user_id" field.
func (pmuo *ProjectMemberUpdateOne) AddUserID(u int32) *ProjectMemberUpdateOne {
	pmuo.mutation.AddUserID(u)
	return pmuo
}

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

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

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

// SetDeletedBy sets the "deleted_by" field.
func (pmuo *ProjectMemberUpdateOne) SetDeletedBy(u uint32) *ProjectMemberUpdateOne {
	pmuo.mutation.ResetDeletedBy()
	pmuo.mutation.SetDeletedBy(u)
	return pmuo
}

// SetNillableDeletedBy sets the "deleted_by" field if the given value is not nil.
func (pmuo *ProjectMemberUpdateOne) SetNillableDeletedBy(u *uint32) *ProjectMemberUpdateOne {
	if u != nil {
		pmuo.SetDeletedBy(*u)
	}
	return pmuo
}

// AddDeletedBy adds u to the "deleted_by" field.
func (pmuo *ProjectMemberUpdateOne) AddDeletedBy(u int32) *ProjectMemberUpdateOne {
	pmuo.mutation.AddDeletedBy(u)
	return pmuo
}

// ClearDeletedBy clears the value of the "deleted_by" field.
func (pmuo *ProjectMemberUpdateOne) ClearDeletedBy() *ProjectMemberUpdateOne {
	pmuo.mutation.ClearDeletedBy()
	return pmuo
}

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

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

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

// SetDescription sets the "description" field.
func (pmuo *ProjectMemberUpdateOne) SetDescription(s string) *ProjectMemberUpdateOne {
	pmuo.mutation.SetDescription(s)
	return pmuo
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (pmuo *ProjectMemberUpdateOne) SetNillableDescription(s *string) *ProjectMemberUpdateOne {
	if s != nil {
		pmuo.SetDescription(*s)
	}
	return pmuo
}

// ClearDescription clears the value of the "description" field.
func (pmuo *ProjectMemberUpdateOne) ClearDescription() *ProjectMemberUpdateOne {
	pmuo.mutation.ClearDescription()
	return pmuo
}

// SetRemark sets the "remark" field.
func (pmuo *ProjectMemberUpdateOne) SetRemark(s string) *ProjectMemberUpdateOne {
	pmuo.mutation.SetRemark(s)
	return pmuo
}

// SetNillableRemark sets the "remark" field if the given value is not nil.
func (pmuo *ProjectMemberUpdateOne) SetNillableRemark(s *string) *ProjectMemberUpdateOne {
	if s != nil {
		pmuo.SetRemark(*s)
	}
	return pmuo
}

// ClearRemark clears the value of the "remark" field.
func (pmuo *ProjectMemberUpdateOne) ClearRemark() *ProjectMemberUpdateOne {
	pmuo.mutation.ClearRemark()
	return pmuo
}

// SetRole sets the "role" field.
func (pmuo *ProjectMemberUpdateOne) SetRole(u uint8) *ProjectMemberUpdateOne {
	pmuo.mutation.ResetRole()
	pmuo.mutation.SetRole(u)
	return pmuo
}

// SetNillableRole sets the "role" field if the given value is not nil.
func (pmuo *ProjectMemberUpdateOne) SetNillableRole(u *uint8) *ProjectMemberUpdateOne {
	if u != nil {
		pmuo.SetRole(*u)
	}
	return pmuo
}

// AddRole adds u to the "role" field.
func (pmuo *ProjectMemberUpdateOne) AddRole(u int8) *ProjectMemberUpdateOne {
	pmuo.mutation.AddRole(u)
	return pmuo
}

// Mutation returns the ProjectMemberMutation object of the builder.
func (pmuo *ProjectMemberUpdateOne) Mutation() *ProjectMemberMutation {
	return pmuo.mutation
}

// Where appends a list predicates to the ProjectMemberUpdate builder.
func (pmuo *ProjectMemberUpdateOne) Where(ps ...predicate.ProjectMember) *ProjectMemberUpdateOne {
	pmuo.mutation.Where(ps...)
	return pmuo
}

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

// Save executes the query and returns the updated ProjectMember entity.
func (pmuo *ProjectMemberUpdateOne) Save(ctx context.Context) (*ProjectMember, error) {
	return withHooks[*ProjectMember, ProjectMemberMutation](ctx, pmuo.sqlSave, pmuo.mutation, pmuo.hooks)
}

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

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

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

func (pmuo *ProjectMemberUpdateOne) sqlSave(ctx context.Context) (_node *ProjectMember, err error) {
	_spec := sqlgraph.NewUpdateSpec(projectmember.Table, projectmember.Columns, sqlgraph.NewFieldSpec(projectmember.FieldID, field.TypeInt))
	id, ok := pmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ProjectMember.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := pmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, projectmember.FieldID)
		for _, f := range fields {
			if !projectmember.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != projectmember.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := pmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := pmuo.mutation.ProjectID(); ok {
		_spec.SetField(projectmember.FieldProjectID, field.TypeInt64, value)
	}
	if value, ok := pmuo.mutation.AddedProjectID(); ok {
		_spec.AddField(projectmember.FieldProjectID, field.TypeInt64, value)
	}
	if value, ok := pmuo.mutation.UserID(); ok {
		_spec.SetField(projectmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := pmuo.mutation.AddedUserID(); ok {
		_spec.AddField(projectmember.FieldUserID, field.TypeUint32, value)
	}
	if value, ok := pmuo.mutation.DeletedAt(); ok {
		_spec.SetField(projectmember.FieldDeletedAt, field.TypeTime, value)
	}
	if pmuo.mutation.DeletedAtCleared() {
		_spec.ClearField(projectmember.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := pmuo.mutation.DeletedBy(); ok {
		_spec.SetField(projectmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if value, ok := pmuo.mutation.AddedDeletedBy(); ok {
		_spec.AddField(projectmember.FieldDeletedBy, field.TypeUint32, value)
	}
	if pmuo.mutation.DeletedByCleared() {
		_spec.ClearField(projectmember.FieldDeletedBy, field.TypeUint32)
	}
	if value, ok := pmuo.mutation.Status(); ok {
		_spec.SetField(projectmember.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := pmuo.mutation.AddedStatus(); ok {
		_spec.AddField(projectmember.FieldStatus, field.TypeInt8, value)
	}
	if value, ok := pmuo.mutation.Description(); ok {
		_spec.SetField(projectmember.FieldDescription, field.TypeString, value)
	}
	if pmuo.mutation.DescriptionCleared() {
		_spec.ClearField(projectmember.FieldDescription, field.TypeString)
	}
	if value, ok := pmuo.mutation.Remark(); ok {
		_spec.SetField(projectmember.FieldRemark, field.TypeString, value)
	}
	if pmuo.mutation.RemarkCleared() {
		_spec.ClearField(projectmember.FieldRemark, field.TypeString)
	}
	if value, ok := pmuo.mutation.Role(); ok {
		_spec.SetField(projectmember.FieldRole, field.TypeUint8, value)
	}
	if value, ok := pmuo.mutation.AddedRole(); ok {
		_spec.AddField(projectmember.FieldRole, field.TypeUint8, value)
	}
	_node = &ProjectMember{config: pmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, pmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{projectmember.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	pmuo.mutation.done = true
	return _node, nil
}
