// 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"
)

// DistrictCreate is the builder for creating a District entity.
type DistrictCreate struct {
	config
	mutation *DistrictMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// SetLevel sets the "level" field.
func (dc *DistrictCreate) SetLevel(u uint32) *DistrictCreate {
	dc.mutation.SetLevel(u)
	return dc
}

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

// SetLatitude sets the "latitude" field.
func (dc *DistrictCreate) SetLatitude(f float64) *DistrictCreate {
	dc.mutation.SetLatitude(f)
	return dc
}

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

// SetLongitude sets the "longitude" field.
func (dc *DistrictCreate) SetLongitude(f float64) *DistrictCreate {
	dc.mutation.SetLongitude(f)
	return dc
}

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

// SetID sets the "id" field.
func (dc *DistrictCreate) SetID(i int64) *DistrictCreate {
	dc.mutation.SetID(i)
	return dc
}

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

// Save creates the District in the database.
func (dc *DistrictCreate) Save(ctx context.Context) (*District, error) {
	dc.defaults()
	return withHooks(ctx, dc.sqlSave, dc.mutation, dc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (dc *DistrictCreate) SaveX(ctx context.Context) *District {
	v, err := dc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// defaults sets the default values of the builder before save.
func (dc *DistrictCreate) defaults() {
	if _, ok := dc.mutation.CreatedAt(); !ok {
		v := district.DefaultCreatedAt()
		dc.mutation.SetCreatedAt(v)
	}
	if _, ok := dc.mutation.UpdatedAt(); !ok {
		v := district.DefaultUpdatedAt()
		dc.mutation.SetUpdatedAt(v)
	}
	if _, ok := dc.mutation.Province(); !ok {
		v := district.DefaultProvince
		dc.mutation.SetProvince(v)
	}
	if _, ok := dc.mutation.Level(); !ok {
		v := district.DefaultLevel
		dc.mutation.SetLevel(v)
	}
	if _, ok := dc.mutation.Latitude(); !ok {
		v := district.DefaultLatitude
		dc.mutation.SetLatitude(v)
	}
	if _, ok := dc.mutation.Longitude(); !ok {
		v := district.DefaultLongitude
		dc.mutation.SetLongitude(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (dc *DistrictCreate) check() error {
	if _, ok := dc.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "District.created_at"`)}
	}
	if _, ok := dc.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "District.updated_at"`)}
	}
	if _, ok := dc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "District.name"`)}
	}
	if _, ok := dc.mutation.Code(); !ok {
		return &ValidationError{Name: "code", err: errors.New(`ent: missing required field "District.code"`)}
	}
	if _, ok := dc.mutation.Province(); !ok {
		return &ValidationError{Name: "province", err: errors.New(`ent: missing required field "District.province"`)}
	}
	if _, ok := dc.mutation.Level(); !ok {
		return &ValidationError{Name: "level", err: errors.New(`ent: missing required field "District.level"`)}
	}
	if _, ok := dc.mutation.Latitude(); !ok {
		return &ValidationError{Name: "latitude", err: errors.New(`ent: missing required field "District.latitude"`)}
	}
	if _, ok := dc.mutation.Longitude(); !ok {
		return &ValidationError{Name: "longitude", err: errors.New(`ent: missing required field "District.longitude"`)}
	}
	return nil
}

func (dc *DistrictCreate) sqlSave(ctx context.Context) (*District, error) {
	if err := dc.check(); err != nil {
		return nil, err
	}
	_node, _spec := dc.createSpec()
	if err := sqlgraph.CreateNode(ctx, dc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = int64(id)
	}
	dc.mutation.id = &_node.ID
	dc.mutation.done = true
	return _node, nil
}

func (dc *DistrictCreate) createSpec() (*District, *sqlgraph.CreateSpec) {
	var (
		_node = &District{config: dc.config}
		_spec = sqlgraph.NewCreateSpec(district.Table, sqlgraph.NewFieldSpec(district.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = dc.conflict
	if id, ok := dc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := dc.mutation.CreatedAt(); ok {
		_spec.SetField(district.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := dc.mutation.UpdatedAt(); ok {
		_spec.SetField(district.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := dc.mutation.Name(); ok {
		_spec.SetField(district.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := dc.mutation.ShortName(); ok {
		_spec.SetField(district.FieldShortName, field.TypeString, value)
		_node.ShortName = value
	}
	if value, ok := dc.mutation.Code(); ok {
		_spec.SetField(district.FieldCode, field.TypeString, value)
		_node.Code = value
	}
	if value, ok := dc.mutation.Province(); ok {
		_spec.SetField(district.FieldProvince, field.TypeString, value)
		_node.Province = value
	}
	if value, ok := dc.mutation.City(); ok {
		_spec.SetField(district.FieldCity, field.TypeString, value)
		_node.City = value
	}
	if value, ok := dc.mutation.Area(); ok {
		_spec.SetField(district.FieldArea, field.TypeString, value)
		_node.Area = value
	}
	if value, ok := dc.mutation.Street(); ok {
		_spec.SetField(district.FieldStreet, field.TypeString, value)
		_node.Street = value
	}
	if value, ok := dc.mutation.Level(); ok {
		_spec.SetField(district.FieldLevel, field.TypeUint32, value)
		_node.Level = value
	}
	if value, ok := dc.mutation.Latitude(); ok {
		_spec.SetField(district.FieldLatitude, field.TypeFloat64, value)
		_node.Latitude = value
	}
	if value, ok := dc.mutation.Longitude(); ok {
		_spec.SetField(district.FieldLongitude, field.TypeFloat64, value)
		_node.Longitude = value
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.District.Create().
//		SetCreatedAt(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.DistrictUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (dc *DistrictCreate) OnConflict(opts ...sql.ConflictOption) *DistrictUpsertOne {
	dc.conflict = opts
	return &DistrictUpsertOne{
		create: dc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.District.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (dc *DistrictCreate) OnConflictColumns(columns ...string) *DistrictUpsertOne {
	dc.conflict = append(dc.conflict, sql.ConflictColumns(columns...))
	return &DistrictUpsertOne{
		create: dc,
	}
}

type (
	// DistrictUpsertOne is the builder for "upsert"-ing
	//  one District node.
	DistrictUpsertOne struct {
		create *DistrictCreate
	}

	// DistrictUpsert is the "OnConflict" setter.
	DistrictUpsert struct {
		*sql.UpdateSet
	}
)

// SetUpdatedAt sets the "updated_at" field.
func (u *DistrictUpsert) SetUpdatedAt(v time.Time) *DistrictUpsert {
	u.Set(district.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateUpdatedAt() *DistrictUpsert {
	u.SetExcluded(district.FieldUpdatedAt)
	return u
}

// SetName sets the "name" field.
func (u *DistrictUpsert) SetName(v string) *DistrictUpsert {
	u.Set(district.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateName() *DistrictUpsert {
	u.SetExcluded(district.FieldName)
	return u
}

// SetShortName sets the "short_name" field.
func (u *DistrictUpsert) SetShortName(v string) *DistrictUpsert {
	u.Set(district.FieldShortName, v)
	return u
}

// UpdateShortName sets the "short_name" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateShortName() *DistrictUpsert {
	u.SetExcluded(district.FieldShortName)
	return u
}

// ClearShortName clears the value of the "short_name" field.
func (u *DistrictUpsert) ClearShortName() *DistrictUpsert {
	u.SetNull(district.FieldShortName)
	return u
}

// SetCode sets the "code" field.
func (u *DistrictUpsert) SetCode(v string) *DistrictUpsert {
	u.Set(district.FieldCode, v)
	return u
}

// UpdateCode sets the "code" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateCode() *DistrictUpsert {
	u.SetExcluded(district.FieldCode)
	return u
}

// SetProvince sets the "province" field.
func (u *DistrictUpsert) SetProvince(v string) *DistrictUpsert {
	u.Set(district.FieldProvince, v)
	return u
}

// UpdateProvince sets the "province" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateProvince() *DistrictUpsert {
	u.SetExcluded(district.FieldProvince)
	return u
}

// SetCity sets the "city" field.
func (u *DistrictUpsert) SetCity(v string) *DistrictUpsert {
	u.Set(district.FieldCity, v)
	return u
}

// UpdateCity sets the "city" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateCity() *DistrictUpsert {
	u.SetExcluded(district.FieldCity)
	return u
}

// ClearCity clears the value of the "city" field.
func (u *DistrictUpsert) ClearCity() *DistrictUpsert {
	u.SetNull(district.FieldCity)
	return u
}

// SetArea sets the "area" field.
func (u *DistrictUpsert) SetArea(v string) *DistrictUpsert {
	u.Set(district.FieldArea, v)
	return u
}

// UpdateArea sets the "area" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateArea() *DistrictUpsert {
	u.SetExcluded(district.FieldArea)
	return u
}

// ClearArea clears the value of the "area" field.
func (u *DistrictUpsert) ClearArea() *DistrictUpsert {
	u.SetNull(district.FieldArea)
	return u
}

// SetStreet sets the "street" field.
func (u *DistrictUpsert) SetStreet(v string) *DistrictUpsert {
	u.Set(district.FieldStreet, v)
	return u
}

// UpdateStreet sets the "street" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateStreet() *DistrictUpsert {
	u.SetExcluded(district.FieldStreet)
	return u
}

// ClearStreet clears the value of the "street" field.
func (u *DistrictUpsert) ClearStreet() *DistrictUpsert {
	u.SetNull(district.FieldStreet)
	return u
}

// SetLevel sets the "level" field.
func (u *DistrictUpsert) SetLevel(v uint32) *DistrictUpsert {
	u.Set(district.FieldLevel, v)
	return u
}

// UpdateLevel sets the "level" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateLevel() *DistrictUpsert {
	u.SetExcluded(district.FieldLevel)
	return u
}

// AddLevel adds v to the "level" field.
func (u *DistrictUpsert) AddLevel(v uint32) *DistrictUpsert {
	u.Add(district.FieldLevel, v)
	return u
}

// SetLatitude sets the "latitude" field.
func (u *DistrictUpsert) SetLatitude(v float64) *DistrictUpsert {
	u.Set(district.FieldLatitude, v)
	return u
}

// UpdateLatitude sets the "latitude" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateLatitude() *DistrictUpsert {
	u.SetExcluded(district.FieldLatitude)
	return u
}

// AddLatitude adds v to the "latitude" field.
func (u *DistrictUpsert) AddLatitude(v float64) *DistrictUpsert {
	u.Add(district.FieldLatitude, v)
	return u
}

// SetLongitude sets the "longitude" field.
func (u *DistrictUpsert) SetLongitude(v float64) *DistrictUpsert {
	u.Set(district.FieldLongitude, v)
	return u
}

// UpdateLongitude sets the "longitude" field to the value that was provided on create.
func (u *DistrictUpsert) UpdateLongitude() *DistrictUpsert {
	u.SetExcluded(district.FieldLongitude)
	return u
}

// AddLongitude adds v to the "longitude" field.
func (u *DistrictUpsert) AddLongitude(v float64) *DistrictUpsert {
	u.Add(district.FieldLongitude, v)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.District.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(district.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *DistrictUpsertOne) UpdateNewValues() *DistrictUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(district.FieldID)
		}
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(district.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.District.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *DistrictUpsertOne) Ignore() *DistrictUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *DistrictUpsertOne) DoNothing() *DistrictUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the DistrictCreate.OnConflict
// documentation for more info.
func (u *DistrictUpsertOne) Update(set func(*DistrictUpsert)) *DistrictUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&DistrictUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *DistrictUpsertOne) SetUpdatedAt(v time.Time) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateUpdatedAt() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetName sets the "name" field.
func (u *DistrictUpsertOne) SetName(v string) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateName() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateName()
	})
}

// SetShortName sets the "short_name" field.
func (u *DistrictUpsertOne) SetShortName(v string) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetShortName(v)
	})
}

// UpdateShortName sets the "short_name" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateShortName() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateShortName()
	})
}

// ClearShortName clears the value of the "short_name" field.
func (u *DistrictUpsertOne) ClearShortName() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearShortName()
	})
}

// SetCode sets the "code" field.
func (u *DistrictUpsertOne) SetCode(v string) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetCode(v)
	})
}

// UpdateCode sets the "code" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateCode() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateCode()
	})
}

// SetProvince sets the "province" field.
func (u *DistrictUpsertOne) SetProvince(v string) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetProvince(v)
	})
}

// UpdateProvince sets the "province" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateProvince() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateProvince()
	})
}

// SetCity sets the "city" field.
func (u *DistrictUpsertOne) SetCity(v string) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetCity(v)
	})
}

// UpdateCity sets the "city" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateCity() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateCity()
	})
}

// ClearCity clears the value of the "city" field.
func (u *DistrictUpsertOne) ClearCity() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearCity()
	})
}

// SetArea sets the "area" field.
func (u *DistrictUpsertOne) SetArea(v string) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetArea(v)
	})
}

// UpdateArea sets the "area" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateArea() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateArea()
	})
}

// ClearArea clears the value of the "area" field.
func (u *DistrictUpsertOne) ClearArea() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearArea()
	})
}

// SetStreet sets the "street" field.
func (u *DistrictUpsertOne) SetStreet(v string) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetStreet(v)
	})
}

// UpdateStreet sets the "street" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateStreet() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateStreet()
	})
}

// ClearStreet clears the value of the "street" field.
func (u *DistrictUpsertOne) ClearStreet() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearStreet()
	})
}

// SetLevel sets the "level" field.
func (u *DistrictUpsertOne) SetLevel(v uint32) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetLevel(v)
	})
}

// AddLevel adds v to the "level" field.
func (u *DistrictUpsertOne) AddLevel(v uint32) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.AddLevel(v)
	})
}

// UpdateLevel sets the "level" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateLevel() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateLevel()
	})
}

// SetLatitude sets the "latitude" field.
func (u *DistrictUpsertOne) SetLatitude(v float64) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetLatitude(v)
	})
}

// AddLatitude adds v to the "latitude" field.
func (u *DistrictUpsertOne) AddLatitude(v float64) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.AddLatitude(v)
	})
}

// UpdateLatitude sets the "latitude" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateLatitude() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateLatitude()
	})
}

// SetLongitude sets the "longitude" field.
func (u *DistrictUpsertOne) SetLongitude(v float64) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.SetLongitude(v)
	})
}

// AddLongitude adds v to the "longitude" field.
func (u *DistrictUpsertOne) AddLongitude(v float64) *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.AddLongitude(v)
	})
}

// UpdateLongitude sets the "longitude" field to the value that was provided on create.
func (u *DistrictUpsertOne) UpdateLongitude() *DistrictUpsertOne {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateLongitude()
	})
}

// Exec executes the query.
func (u *DistrictUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for DistrictCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

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

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *DistrictUpsertOne) ID(ctx context.Context) (id int64, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *DistrictUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// DistrictCreateBulk is the builder for creating many District entities in bulk.
type DistrictCreateBulk struct {
	config
	builders []*DistrictCreate
	conflict []sql.ConflictOption
}

// Save creates the District entities in the database.
func (dcb *DistrictCreateBulk) Save(ctx context.Context) ([]*District, error) {
	specs := make([]*sqlgraph.CreateSpec, len(dcb.builders))
	nodes := make([]*District, len(dcb.builders))
	mutators := make([]Mutator, len(dcb.builders))
	for i := range dcb.builders {
		func(i int, root context.Context) {
			builder := dcb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*DistrictMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, dcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = dcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, dcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, dcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (dcb *DistrictCreateBulk) SaveX(ctx context.Context) []*District {
	v, err := dcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.District.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.DistrictUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (dcb *DistrictCreateBulk) OnConflict(opts ...sql.ConflictOption) *DistrictUpsertBulk {
	dcb.conflict = opts
	return &DistrictUpsertBulk{
		create: dcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.District.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (dcb *DistrictCreateBulk) OnConflictColumns(columns ...string) *DistrictUpsertBulk {
	dcb.conflict = append(dcb.conflict, sql.ConflictColumns(columns...))
	return &DistrictUpsertBulk{
		create: dcb,
	}
}

// DistrictUpsertBulk is the builder for "upsert"-ing
// a bulk of District nodes.
type DistrictUpsertBulk struct {
	create *DistrictCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.District.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(district.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *DistrictUpsertBulk) UpdateNewValues() *DistrictUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(district.FieldID)
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(district.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.District.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *DistrictUpsertBulk) Ignore() *DistrictUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *DistrictUpsertBulk) DoNothing() *DistrictUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the DistrictCreateBulk.OnConflict
// documentation for more info.
func (u *DistrictUpsertBulk) Update(set func(*DistrictUpsert)) *DistrictUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&DistrictUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *DistrictUpsertBulk) SetUpdatedAt(v time.Time) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateUpdatedAt() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetName sets the "name" field.
func (u *DistrictUpsertBulk) SetName(v string) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateName() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateName()
	})
}

// SetShortName sets the "short_name" field.
func (u *DistrictUpsertBulk) SetShortName(v string) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetShortName(v)
	})
}

// UpdateShortName sets the "short_name" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateShortName() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateShortName()
	})
}

// ClearShortName clears the value of the "short_name" field.
func (u *DistrictUpsertBulk) ClearShortName() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearShortName()
	})
}

// SetCode sets the "code" field.
func (u *DistrictUpsertBulk) SetCode(v string) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetCode(v)
	})
}

// UpdateCode sets the "code" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateCode() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateCode()
	})
}

// SetProvince sets the "province" field.
func (u *DistrictUpsertBulk) SetProvince(v string) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetProvince(v)
	})
}

// UpdateProvince sets the "province" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateProvince() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateProvince()
	})
}

// SetCity sets the "city" field.
func (u *DistrictUpsertBulk) SetCity(v string) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetCity(v)
	})
}

// UpdateCity sets the "city" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateCity() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateCity()
	})
}

// ClearCity clears the value of the "city" field.
func (u *DistrictUpsertBulk) ClearCity() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearCity()
	})
}

// SetArea sets the "area" field.
func (u *DistrictUpsertBulk) SetArea(v string) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetArea(v)
	})
}

// UpdateArea sets the "area" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateArea() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateArea()
	})
}

// ClearArea clears the value of the "area" field.
func (u *DistrictUpsertBulk) ClearArea() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearArea()
	})
}

// SetStreet sets the "street" field.
func (u *DistrictUpsertBulk) SetStreet(v string) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetStreet(v)
	})
}

// UpdateStreet sets the "street" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateStreet() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateStreet()
	})
}

// ClearStreet clears the value of the "street" field.
func (u *DistrictUpsertBulk) ClearStreet() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.ClearStreet()
	})
}

// SetLevel sets the "level" field.
func (u *DistrictUpsertBulk) SetLevel(v uint32) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetLevel(v)
	})
}

// AddLevel adds v to the "level" field.
func (u *DistrictUpsertBulk) AddLevel(v uint32) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.AddLevel(v)
	})
}

// UpdateLevel sets the "level" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateLevel() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateLevel()
	})
}

// SetLatitude sets the "latitude" field.
func (u *DistrictUpsertBulk) SetLatitude(v float64) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetLatitude(v)
	})
}

// AddLatitude adds v to the "latitude" field.
func (u *DistrictUpsertBulk) AddLatitude(v float64) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.AddLatitude(v)
	})
}

// UpdateLatitude sets the "latitude" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateLatitude() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateLatitude()
	})
}

// SetLongitude sets the "longitude" field.
func (u *DistrictUpsertBulk) SetLongitude(v float64) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.SetLongitude(v)
	})
}

// AddLongitude adds v to the "longitude" field.
func (u *DistrictUpsertBulk) AddLongitude(v float64) *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.AddLongitude(v)
	})
}

// UpdateLongitude sets the "longitude" field to the value that was provided on create.
func (u *DistrictUpsertBulk) UpdateLongitude() *DistrictUpsertBulk {
	return u.Update(func(s *DistrictUpsert) {
		s.UpdateLongitude()
	})
}

// Exec executes the query.
func (u *DistrictUpsertBulk) Exec(ctx context.Context) error {
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the DistrictCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for DistrictCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

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