// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ContactFieldTemplateUpdate is the builder for updating ContactFieldTemplate entities.
type ContactFieldTemplateUpdate struct {
	config
	hooks    []Hook
	mutation *ContactFieldTemplateMutation
}

// Where appends a list predicates to the ContactFieldTemplateUpdate builder.
func (cftu *ContactFieldTemplateUpdate) Where(ps ...predicate.ContactFieldTemplate) *ContactFieldTemplateUpdate {
	cftu.mutation.Where(ps...)
	return cftu
}

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

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

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

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

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

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

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

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

// SetOrganizationID sets the "organization_id" field.
func (cftu *ContactFieldTemplateUpdate) SetOrganizationID(u uint64) *ContactFieldTemplateUpdate {
	cftu.mutation.ResetOrganizationID()
	cftu.mutation.SetOrganizationID(u)
	return cftu
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (cftu *ContactFieldTemplateUpdate) SetNillableOrganizationID(u *uint64) *ContactFieldTemplateUpdate {
	if u != nil {
		cftu.SetOrganizationID(*u)
	}
	return cftu
}

// AddOrganizationID adds u to the "organization_id" field.
func (cftu *ContactFieldTemplateUpdate) AddOrganizationID(u int64) *ContactFieldTemplateUpdate {
	cftu.mutation.AddOrganizationID(u)
	return cftu
}

// SetTemplate sets the "template" field.
func (cftu *ContactFieldTemplateUpdate) SetTemplate(ctft []custom_types.ContactFieldTemplate) *ContactFieldTemplateUpdate {
	cftu.mutation.SetTemplate(ctft)
	return cftu
}

// AppendTemplate appends ctft to the "template" field.
func (cftu *ContactFieldTemplateUpdate) AppendTemplate(ctft []custom_types.ContactFieldTemplate) *ContactFieldTemplateUpdate {
	cftu.mutation.AppendTemplate(ctft)
	return cftu
}

// Mutation returns the ContactFieldTemplateMutation object of the builder.
func (cftu *ContactFieldTemplateUpdate) Mutation() *ContactFieldTemplateMutation {
	return cftu.mutation
}

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

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

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

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

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

func (cftu *ContactFieldTemplateUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(contactfieldtemplate.Table, contactfieldtemplate.Columns, sqlgraph.NewFieldSpec(contactfieldtemplate.FieldID, field.TypeUint64))
	if ps := cftu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cftu.mutation.UpdatedAt(); ok {
		_spec.SetField(contactfieldtemplate.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cftu.mutation.Status(); ok {
		_spec.SetField(contactfieldtemplate.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := cftu.mutation.AddedStatus(); ok {
		_spec.AddField(contactfieldtemplate.FieldStatus, field.TypeUint8, value)
	}
	if cftu.mutation.StatusCleared() {
		_spec.ClearField(contactfieldtemplate.FieldStatus, field.TypeUint8)
	}
	if value, ok := cftu.mutation.DeletedAt(); ok {
		_spec.SetField(contactfieldtemplate.FieldDeletedAt, field.TypeTime, value)
	}
	if cftu.mutation.DeletedAtCleared() {
		_spec.ClearField(contactfieldtemplate.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := cftu.mutation.OrganizationID(); ok {
		_spec.SetField(contactfieldtemplate.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cftu.mutation.AddedOrganizationID(); ok {
		_spec.AddField(contactfieldtemplate.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cftu.mutation.Template(); ok {
		_spec.SetField(contactfieldtemplate.FieldTemplate, field.TypeJSON, value)
	}
	if value, ok := cftu.mutation.AppendedTemplate(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, contactfieldtemplate.FieldTemplate, value)
		})
	}
	if n, err = sqlgraph.UpdateNodes(ctx, cftu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{contactfieldtemplate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	cftu.mutation.done = true
	return n, nil
}

// ContactFieldTemplateUpdateOne is the builder for updating a single ContactFieldTemplate entity.
type ContactFieldTemplateUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ContactFieldTemplateMutation
}

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

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

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

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

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

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

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

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

// SetOrganizationID sets the "organization_id" field.
func (cftuo *ContactFieldTemplateUpdateOne) SetOrganizationID(u uint64) *ContactFieldTemplateUpdateOne {
	cftuo.mutation.ResetOrganizationID()
	cftuo.mutation.SetOrganizationID(u)
	return cftuo
}

// SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
func (cftuo *ContactFieldTemplateUpdateOne) SetNillableOrganizationID(u *uint64) *ContactFieldTemplateUpdateOne {
	if u != nil {
		cftuo.SetOrganizationID(*u)
	}
	return cftuo
}

// AddOrganizationID adds u to the "organization_id" field.
func (cftuo *ContactFieldTemplateUpdateOne) AddOrganizationID(u int64) *ContactFieldTemplateUpdateOne {
	cftuo.mutation.AddOrganizationID(u)
	return cftuo
}

// SetTemplate sets the "template" field.
func (cftuo *ContactFieldTemplateUpdateOne) SetTemplate(ctft []custom_types.ContactFieldTemplate) *ContactFieldTemplateUpdateOne {
	cftuo.mutation.SetTemplate(ctft)
	return cftuo
}

// AppendTemplate appends ctft to the "template" field.
func (cftuo *ContactFieldTemplateUpdateOne) AppendTemplate(ctft []custom_types.ContactFieldTemplate) *ContactFieldTemplateUpdateOne {
	cftuo.mutation.AppendTemplate(ctft)
	return cftuo
}

// Mutation returns the ContactFieldTemplateMutation object of the builder.
func (cftuo *ContactFieldTemplateUpdateOne) Mutation() *ContactFieldTemplateMutation {
	return cftuo.mutation
}

// Where appends a list predicates to the ContactFieldTemplateUpdate builder.
func (cftuo *ContactFieldTemplateUpdateOne) Where(ps ...predicate.ContactFieldTemplate) *ContactFieldTemplateUpdateOne {
	cftuo.mutation.Where(ps...)
	return cftuo
}

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

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

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

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

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

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

func (cftuo *ContactFieldTemplateUpdateOne) sqlSave(ctx context.Context) (_node *ContactFieldTemplate, err error) {
	_spec := sqlgraph.NewUpdateSpec(contactfieldtemplate.Table, contactfieldtemplate.Columns, sqlgraph.NewFieldSpec(contactfieldtemplate.FieldID, field.TypeUint64))
	id, ok := cftuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ContactFieldTemplate.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := cftuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, contactfieldtemplate.FieldID)
		for _, f := range fields {
			if !contactfieldtemplate.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != contactfieldtemplate.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := cftuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := cftuo.mutation.UpdatedAt(); ok {
		_spec.SetField(contactfieldtemplate.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := cftuo.mutation.Status(); ok {
		_spec.SetField(contactfieldtemplate.FieldStatus, field.TypeUint8, value)
	}
	if value, ok := cftuo.mutation.AddedStatus(); ok {
		_spec.AddField(contactfieldtemplate.FieldStatus, field.TypeUint8, value)
	}
	if cftuo.mutation.StatusCleared() {
		_spec.ClearField(contactfieldtemplate.FieldStatus, field.TypeUint8)
	}
	if value, ok := cftuo.mutation.DeletedAt(); ok {
		_spec.SetField(contactfieldtemplate.FieldDeletedAt, field.TypeTime, value)
	}
	if cftuo.mutation.DeletedAtCleared() {
		_spec.ClearField(contactfieldtemplate.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := cftuo.mutation.OrganizationID(); ok {
		_spec.SetField(contactfieldtemplate.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cftuo.mutation.AddedOrganizationID(); ok {
		_spec.AddField(contactfieldtemplate.FieldOrganizationID, field.TypeUint64, value)
	}
	if value, ok := cftuo.mutation.Template(); ok {
		_spec.SetField(contactfieldtemplate.FieldTemplate, field.TypeJSON, value)
	}
	if value, ok := cftuo.mutation.AppendedTemplate(); ok {
		_spec.AddModifier(func(u *sql.UpdateBuilder) {
			sqljson.Append(u, contactfieldtemplate.FieldTemplate, value)
		})
	}
	_node = &ContactFieldTemplate{config: cftuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, cftuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{contactfieldtemplate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	cftuo.mutation.done = true
	return _node, nil
}
