// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/district"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/predicate"
)

// DistrictUpdate is the builder for updating District entities.
type DistrictUpdate struct {
	config
	hooks     []Hook
	mutation  *DistrictMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the DistrictUpdate builder.
func (du *DistrictUpdate) Where(ps ...predicate.District) *DistrictUpdate {
	du.mutation.Where(ps...)
	return du
}

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

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

// SetShortName sets the "short_name" field.
func (du *DistrictUpdate) SetShortName(s string) *DistrictUpdate {
	du.mutation.SetShortName(s)
	return du
}

// SetNillableShortName sets the "short_name" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableShortName(s *string) *DistrictUpdate {
	if s != nil {
		du.SetShortName(*s)
	}
	return du
}

// ClearShortName clears the value of the "short_name" field.
func (du *DistrictUpdate) ClearShortName() *DistrictUpdate {
	du.mutation.ClearShortName()
	return du
}

// SetCode sets the "code" field.
func (du *DistrictUpdate) SetCode(s string) *DistrictUpdate {
	du.mutation.SetCode(s)
	return du
}

// SetProvince sets the "province" field.
func (du *DistrictUpdate) SetProvince(s string) *DistrictUpdate {
	du.mutation.SetProvince(s)
	return du
}

// SetNillableProvince sets the "province" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableProvince(s *string) *DistrictUpdate {
	if s != nil {
		du.SetProvince(*s)
	}
	return du
}

// SetCity sets the "city" field.
func (du *DistrictUpdate) SetCity(s string) *DistrictUpdate {
	du.mutation.SetCity(s)
	return du
}

// SetNillableCity sets the "city" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableCity(s *string) *DistrictUpdate {
	if s != nil {
		du.SetCity(*s)
	}
	return du
}

// ClearCity clears the value of the "city" field.
func (du *DistrictUpdate) ClearCity() *DistrictUpdate {
	du.mutation.ClearCity()
	return du
}

// SetArea sets the "area" field.
func (du *DistrictUpdate) SetArea(s string) *DistrictUpdate {
	du.mutation.SetArea(s)
	return du
}

// SetNillableArea sets the "area" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableArea(s *string) *DistrictUpdate {
	if s != nil {
		du.SetArea(*s)
	}
	return du
}

// ClearArea clears the value of the "area" field.
func (du *DistrictUpdate) ClearArea() *DistrictUpdate {
	du.mutation.ClearArea()
	return du
}

// SetStreet sets the "street" field.
func (du *DistrictUpdate) SetStreet(s string) *DistrictUpdate {
	du.mutation.SetStreet(s)
	return du
}

// SetNillableStreet sets the "street" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableStreet(s *string) *DistrictUpdate {
	if s != nil {
		du.SetStreet(*s)
	}
	return du
}

// ClearStreet clears the value of the "street" field.
func (du *DistrictUpdate) ClearStreet() *DistrictUpdate {
	du.mutation.ClearStreet()
	return du
}

// SetLevel sets the "level" field.
func (du *DistrictUpdate) SetLevel(u uint32) *DistrictUpdate {
	du.mutation.ResetLevel()
	du.mutation.SetLevel(u)
	return du
}

// SetNillableLevel sets the "level" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableLevel(u *uint32) *DistrictUpdate {
	if u != nil {
		du.SetLevel(*u)
	}
	return du
}

// AddLevel adds u to the "level" field.
func (du *DistrictUpdate) AddLevel(u int32) *DistrictUpdate {
	du.mutation.AddLevel(u)
	return du
}

// SetLatitude sets the "latitude" field.
func (du *DistrictUpdate) SetLatitude(f float64) *DistrictUpdate {
	du.mutation.ResetLatitude()
	du.mutation.SetLatitude(f)
	return du
}

// SetNillableLatitude sets the "latitude" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableLatitude(f *float64) *DistrictUpdate {
	if f != nil {
		du.SetLatitude(*f)
	}
	return du
}

// AddLatitude adds f to the "latitude" field.
func (du *DistrictUpdate) AddLatitude(f float64) *DistrictUpdate {
	du.mutation.AddLatitude(f)
	return du
}

// SetLongitude sets the "longitude" field.
func (du *DistrictUpdate) SetLongitude(f float64) *DistrictUpdate {
	du.mutation.ResetLongitude()
	du.mutation.SetLongitude(f)
	return du
}

// SetNillableLongitude sets the "longitude" field if the given value is not nil.
func (du *DistrictUpdate) SetNillableLongitude(f *float64) *DistrictUpdate {
	if f != nil {
		du.SetLongitude(*f)
	}
	return du
}

// AddLongitude adds f to the "longitude" field.
func (du *DistrictUpdate) AddLongitude(f float64) *DistrictUpdate {
	du.mutation.AddLongitude(f)
	return du
}

// Mutation returns the DistrictMutation object of the builder.
func (du *DistrictUpdate) Mutation() *DistrictMutation {
	return du.mutation
}

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

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

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

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

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

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (du *DistrictUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *DistrictUpdate {
	du.modifiers = append(du.modifiers, modifiers...)
	return du
}

func (du *DistrictUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(district.Table, district.Columns, sqlgraph.NewFieldSpec(district.FieldID, field.TypeInt64))
	if ps := du.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := du.mutation.UpdatedAt(); ok {
		_spec.SetField(district.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := du.mutation.Name(); ok {
		_spec.SetField(district.FieldName, field.TypeString, value)
	}
	if value, ok := du.mutation.ShortName(); ok {
		_spec.SetField(district.FieldShortName, field.TypeString, value)
	}
	if du.mutation.ShortNameCleared() {
		_spec.ClearField(district.FieldShortName, field.TypeString)
	}
	if value, ok := du.mutation.Code(); ok {
		_spec.SetField(district.FieldCode, field.TypeString, value)
	}
	if value, ok := du.mutation.Province(); ok {
		_spec.SetField(district.FieldProvince, field.TypeString, value)
	}
	if value, ok := du.mutation.City(); ok {
		_spec.SetField(district.FieldCity, field.TypeString, value)
	}
	if du.mutation.CityCleared() {
		_spec.ClearField(district.FieldCity, field.TypeString)
	}
	if value, ok := du.mutation.Area(); ok {
		_spec.SetField(district.FieldArea, field.TypeString, value)
	}
	if du.mutation.AreaCleared() {
		_spec.ClearField(district.FieldArea, field.TypeString)
	}
	if value, ok := du.mutation.Street(); ok {
		_spec.SetField(district.FieldStreet, field.TypeString, value)
	}
	if du.mutation.StreetCleared() {
		_spec.ClearField(district.FieldStreet, field.TypeString)
	}
	if value, ok := du.mutation.Level(); ok {
		_spec.SetField(district.FieldLevel, field.TypeUint32, value)
	}
	if value, ok := du.mutation.AddedLevel(); ok {
		_spec.AddField(district.FieldLevel, field.TypeUint32, value)
	}
	if value, ok := du.mutation.Latitude(); ok {
		_spec.SetField(district.FieldLatitude, field.TypeFloat64, value)
	}
	if value, ok := du.mutation.AddedLatitude(); ok {
		_spec.AddField(district.FieldLatitude, field.TypeFloat64, value)
	}
	if value, ok := du.mutation.Longitude(); ok {
		_spec.SetField(district.FieldLongitude, field.TypeFloat64, value)
	}
	if value, ok := du.mutation.AddedLongitude(); ok {
		_spec.AddField(district.FieldLongitude, field.TypeFloat64, value)
	}
	_spec.AddModifiers(du.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, du.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{district.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	du.mutation.done = true
	return n, nil
}

// DistrictUpdateOne is the builder for updating a single District entity.
type DistrictUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *DistrictMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

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

// SetShortName sets the "short_name" field.
func (duo *DistrictUpdateOne) SetShortName(s string) *DistrictUpdateOne {
	duo.mutation.SetShortName(s)
	return duo
}

// SetNillableShortName sets the "short_name" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableShortName(s *string) *DistrictUpdateOne {
	if s != nil {
		duo.SetShortName(*s)
	}
	return duo
}

// ClearShortName clears the value of the "short_name" field.
func (duo *DistrictUpdateOne) ClearShortName() *DistrictUpdateOne {
	duo.mutation.ClearShortName()
	return duo
}

// SetCode sets the "code" field.
func (duo *DistrictUpdateOne) SetCode(s string) *DistrictUpdateOne {
	duo.mutation.SetCode(s)
	return duo
}

// SetProvince sets the "province" field.
func (duo *DistrictUpdateOne) SetProvince(s string) *DistrictUpdateOne {
	duo.mutation.SetProvince(s)
	return duo
}

// SetNillableProvince sets the "province" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableProvince(s *string) *DistrictUpdateOne {
	if s != nil {
		duo.SetProvince(*s)
	}
	return duo
}

// SetCity sets the "city" field.
func (duo *DistrictUpdateOne) SetCity(s string) *DistrictUpdateOne {
	duo.mutation.SetCity(s)
	return duo
}

// SetNillableCity sets the "city" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableCity(s *string) *DistrictUpdateOne {
	if s != nil {
		duo.SetCity(*s)
	}
	return duo
}

// ClearCity clears the value of the "city" field.
func (duo *DistrictUpdateOne) ClearCity() *DistrictUpdateOne {
	duo.mutation.ClearCity()
	return duo
}

// SetArea sets the "area" field.
func (duo *DistrictUpdateOne) SetArea(s string) *DistrictUpdateOne {
	duo.mutation.SetArea(s)
	return duo
}

// SetNillableArea sets the "area" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableArea(s *string) *DistrictUpdateOne {
	if s != nil {
		duo.SetArea(*s)
	}
	return duo
}

// ClearArea clears the value of the "area" field.
func (duo *DistrictUpdateOne) ClearArea() *DistrictUpdateOne {
	duo.mutation.ClearArea()
	return duo
}

// SetStreet sets the "street" field.
func (duo *DistrictUpdateOne) SetStreet(s string) *DistrictUpdateOne {
	duo.mutation.SetStreet(s)
	return duo
}

// SetNillableStreet sets the "street" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableStreet(s *string) *DistrictUpdateOne {
	if s != nil {
		duo.SetStreet(*s)
	}
	return duo
}

// ClearStreet clears the value of the "street" field.
func (duo *DistrictUpdateOne) ClearStreet() *DistrictUpdateOne {
	duo.mutation.ClearStreet()
	return duo
}

// SetLevel sets the "level" field.
func (duo *DistrictUpdateOne) SetLevel(u uint32) *DistrictUpdateOne {
	duo.mutation.ResetLevel()
	duo.mutation.SetLevel(u)
	return duo
}

// SetNillableLevel sets the "level" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableLevel(u *uint32) *DistrictUpdateOne {
	if u != nil {
		duo.SetLevel(*u)
	}
	return duo
}

// AddLevel adds u to the "level" field.
func (duo *DistrictUpdateOne) AddLevel(u int32) *DistrictUpdateOne {
	duo.mutation.AddLevel(u)
	return duo
}

// SetLatitude sets the "latitude" field.
func (duo *DistrictUpdateOne) SetLatitude(f float64) *DistrictUpdateOne {
	duo.mutation.ResetLatitude()
	duo.mutation.SetLatitude(f)
	return duo
}

// SetNillableLatitude sets the "latitude" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableLatitude(f *float64) *DistrictUpdateOne {
	if f != nil {
		duo.SetLatitude(*f)
	}
	return duo
}

// AddLatitude adds f to the "latitude" field.
func (duo *DistrictUpdateOne) AddLatitude(f float64) *DistrictUpdateOne {
	duo.mutation.AddLatitude(f)
	return duo
}

// SetLongitude sets the "longitude" field.
func (duo *DistrictUpdateOne) SetLongitude(f float64) *DistrictUpdateOne {
	duo.mutation.ResetLongitude()
	duo.mutation.SetLongitude(f)
	return duo
}

// SetNillableLongitude sets the "longitude" field if the given value is not nil.
func (duo *DistrictUpdateOne) SetNillableLongitude(f *float64) *DistrictUpdateOne {
	if f != nil {
		duo.SetLongitude(*f)
	}
	return duo
}

// AddLongitude adds f to the "longitude" field.
func (duo *DistrictUpdateOne) AddLongitude(f float64) *DistrictUpdateOne {
	duo.mutation.AddLongitude(f)
	return duo
}

// Mutation returns the DistrictMutation object of the builder.
func (duo *DistrictUpdateOne) Mutation() *DistrictMutation {
	return duo.mutation
}

// Where appends a list predicates to the DistrictUpdate builder.
func (duo *DistrictUpdateOne) Where(ps ...predicate.District) *DistrictUpdateOne {
	duo.mutation.Where(ps...)
	return duo
}

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

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

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

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

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

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

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (duo *DistrictUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *DistrictUpdateOne {
	duo.modifiers = append(duo.modifiers, modifiers...)
	return duo
}

func (duo *DistrictUpdateOne) sqlSave(ctx context.Context) (_node *District, err error) {
	_spec := sqlgraph.NewUpdateSpec(district.Table, district.Columns, sqlgraph.NewFieldSpec(district.FieldID, field.TypeInt64))
	id, ok := duo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "District.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := duo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, district.FieldID)
		for _, f := range fields {
			if !district.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != district.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := duo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := duo.mutation.UpdatedAt(); ok {
		_spec.SetField(district.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := duo.mutation.Name(); ok {
		_spec.SetField(district.FieldName, field.TypeString, value)
	}
	if value, ok := duo.mutation.ShortName(); ok {
		_spec.SetField(district.FieldShortName, field.TypeString, value)
	}
	if duo.mutation.ShortNameCleared() {
		_spec.ClearField(district.FieldShortName, field.TypeString)
	}
	if value, ok := duo.mutation.Code(); ok {
		_spec.SetField(district.FieldCode, field.TypeString, value)
	}
	if value, ok := duo.mutation.Province(); ok {
		_spec.SetField(district.FieldProvince, field.TypeString, value)
	}
	if value, ok := duo.mutation.City(); ok {
		_spec.SetField(district.FieldCity, field.TypeString, value)
	}
	if duo.mutation.CityCleared() {
		_spec.ClearField(district.FieldCity, field.TypeString)
	}
	if value, ok := duo.mutation.Area(); ok {
		_spec.SetField(district.FieldArea, field.TypeString, value)
	}
	if duo.mutation.AreaCleared() {
		_spec.ClearField(district.FieldArea, field.TypeString)
	}
	if value, ok := duo.mutation.Street(); ok {
		_spec.SetField(district.FieldStreet, field.TypeString, value)
	}
	if duo.mutation.StreetCleared() {
		_spec.ClearField(district.FieldStreet, field.TypeString)
	}
	if value, ok := duo.mutation.Level(); ok {
		_spec.SetField(district.FieldLevel, field.TypeUint32, value)
	}
	if value, ok := duo.mutation.AddedLevel(); ok {
		_spec.AddField(district.FieldLevel, field.TypeUint32, value)
	}
	if value, ok := duo.mutation.Latitude(); ok {
		_spec.SetField(district.FieldLatitude, field.TypeFloat64, value)
	}
	if value, ok := duo.mutation.AddedLatitude(); ok {
		_spec.AddField(district.FieldLatitude, field.TypeFloat64, value)
	}
	if value, ok := duo.mutation.Longitude(); ok {
		_spec.SetField(district.FieldLongitude, field.TypeFloat64, value)
	}
	if value, ok := duo.mutation.AddedLongitude(); ok {
		_spec.AddField(district.FieldLongitude, field.TypeFloat64, value)
	}
	_spec.AddModifiers(duo.modifiers...)
	_node = &District{config: duo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, duo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{district.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	duo.mutation.done = true
	return _node, nil
}
