// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/order/rpc/internal/models/orders"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/shopspring/decimal"
)

// OrdersCreate is the builder for creating a Orders entity.
type OrdersCreate struct {
	config
	mutation *OrdersMutation
	hooks    []Hook
}

// SetConsumerID sets the "consumer_id" field.
func (_c *OrdersCreate) SetConsumerID(v int64) *OrdersCreate {
	_c.mutation.SetConsumerID(v)
	return _c
}

// SetPlayerID sets the "player_id" field.
func (_c *OrdersCreate) SetPlayerID(v int64) *OrdersCreate {
	_c.mutation.SetPlayerID(v)
	return _c
}

// SetShopID sets the "shop_id" field.
func (_c *OrdersCreate) SetShopID(v int64) *OrdersCreate {
	_c.mutation.SetShopID(v)
	return _c
}

// SetNillableShopID sets the "shop_id" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableShopID(v *int64) *OrdersCreate {
	if v != nil {
		_c.SetShopID(*v)
	}
	return _c
}

// SetServiceSnapshot sets the "service_snapshot" field.
func (_c *OrdersCreate) SetServiceSnapshot(v map[string]interface{}) *OrdersCreate {
	_c.mutation.SetServiceSnapshot(v)
	return _c
}

// SetStatus sets the "status" field.
func (_c *OrdersCreate) SetStatus(v string) *OrdersCreate {
	_c.mutation.SetStatus(v)
	return _c
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableStatus(v *string) *OrdersCreate {
	if v != nil {
		_c.SetStatus(*v)
	}
	return _c
}

// SetTotalPrice sets the "total_price" field.
func (_c *OrdersCreate) SetTotalPrice(v decimal.Decimal) *OrdersCreate {
	_c.mutation.SetTotalPrice(v)
	return _c
}

// SetNote sets the "note" field.
func (_c *OrdersCreate) SetNote(v string) *OrdersCreate {
	_c.mutation.SetNote(v)
	return _c
}

// SetNillableNote sets the "note" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableNote(v *string) *OrdersCreate {
	if v != nil {
		_c.SetNote(*v)
	}
	return _c
}

// SetVersion sets the "version" field.
func (_c *OrdersCreate) SetVersion(v int) *OrdersCreate {
	_c.mutation.SetVersion(v)
	return _c
}

// SetNillableVersion sets the "version" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableVersion(v *int) *OrdersCreate {
	if v != nil {
		_c.SetVersion(*v)
	}
	return _c
}

// SetTimeoutJobID sets the "timeout_job_id" field.
func (_c *OrdersCreate) SetTimeoutJobID(v string) *OrdersCreate {
	_c.mutation.SetTimeoutJobID(v)
	return _c
}

// SetNillableTimeoutJobID sets the "timeout_job_id" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableTimeoutJobID(v *string) *OrdersCreate {
	if v != nil {
		_c.SetTimeoutJobID(*v)
	}
	return _c
}

// SetSearchText sets the "search_text" field.
func (_c *OrdersCreate) SetSearchText(v string) *OrdersCreate {
	_c.mutation.SetSearchText(v)
	return _c
}

// SetNillableSearchText sets the "search_text" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableSearchText(v *string) *OrdersCreate {
	if v != nil {
		_c.SetSearchText(*v)
	}
	return _c
}

// SetCreatedAt sets the "created_at" field.
func (_c *OrdersCreate) SetCreatedAt(v time.Time) *OrdersCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

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

// SetAcceptedAt sets the "accepted_at" field.
func (_c *OrdersCreate) SetAcceptedAt(v time.Time) *OrdersCreate {
	_c.mutation.SetAcceptedAt(v)
	return _c
}

// SetNillableAcceptedAt sets the "accepted_at" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableAcceptedAt(v *time.Time) *OrdersCreate {
	if v != nil {
		_c.SetAcceptedAt(*v)
	}
	return _c
}

// SetClosedAt sets the "closed_at" field.
func (_c *OrdersCreate) SetClosedAt(v time.Time) *OrdersCreate {
	_c.mutation.SetClosedAt(v)
	return _c
}

// SetNillableClosedAt sets the "closed_at" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableClosedAt(v *time.Time) *OrdersCreate {
	if v != nil {
		_c.SetClosedAt(*v)
	}
	return _c
}

// SetCompletedAt sets the "completed_at" field.
func (_c *OrdersCreate) SetCompletedAt(v time.Time) *OrdersCreate {
	_c.mutation.SetCompletedAt(v)
	return _c
}

// SetNillableCompletedAt sets the "completed_at" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableCompletedAt(v *time.Time) *OrdersCreate {
	if v != nil {
		_c.SetCompletedAt(*v)
	}
	return _c
}

// SetCancelledAt sets the "cancelled_at" field.
func (_c *OrdersCreate) SetCancelledAt(v time.Time) *OrdersCreate {
	_c.mutation.SetCancelledAt(v)
	return _c
}

// SetNillableCancelledAt sets the "cancelled_at" field if the given value is not nil.
func (_c *OrdersCreate) SetNillableCancelledAt(v *time.Time) *OrdersCreate {
	if v != nil {
		_c.SetCancelledAt(*v)
	}
	return _c
}

// SetUpdatedAt sets the "updated_at" field.
func (_c *OrdersCreate) SetUpdatedAt(v time.Time) *OrdersCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

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

// SetID sets the "id" field.
func (_c *OrdersCreate) SetID(v int64) *OrdersCreate {
	_c.mutation.SetID(v)
	return _c
}

// Mutation returns the OrdersMutation object of the builder.
func (_c *OrdersCreate) Mutation() *OrdersMutation {
	return _c.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_c *OrdersCreate) defaults() {
	if _, ok := _c.mutation.Status(); !ok {
		v := orders.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.Version(); !ok {
		v := orders.DefaultVersion
		_c.mutation.SetVersion(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := orders.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := orders.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *OrdersCreate) check() error {
	if _, ok := _c.mutation.ConsumerID(); !ok {
		return &ValidationError{Name: "consumer_id", err: errors.New(`models: missing required field "Orders.consumer_id"`)}
	}
	if _, ok := _c.mutation.PlayerID(); !ok {
		return &ValidationError{Name: "player_id", err: errors.New(`models: missing required field "Orders.player_id"`)}
	}
	if _, ok := _c.mutation.ServiceSnapshot(); !ok {
		return &ValidationError{Name: "service_snapshot", err: errors.New(`models: missing required field "Orders.service_snapshot"`)}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`models: missing required field "Orders.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := orders.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`models: validator failed for field "Orders.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.TotalPrice(); !ok {
		return &ValidationError{Name: "total_price", err: errors.New(`models: missing required field "Orders.total_price"`)}
	}
	if _, ok := _c.mutation.Version(); !ok {
		return &ValidationError{Name: "version", err: errors.New(`models: missing required field "Orders.version"`)}
	}
	if v, ok := _c.mutation.TimeoutJobID(); ok {
		if err := orders.TimeoutJobIDValidator(v); err != nil {
			return &ValidationError{Name: "timeout_job_id", err: fmt.Errorf(`models: validator failed for field "Orders.timeout_job_id": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Orders.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Orders.updated_at"`)}
	}
	return nil
}

func (_c *OrdersCreate) sqlSave(ctx context.Context) (*Orders, error) {
	if err := _c.check(); err != nil {
		return nil, err
	}
	_node, _spec := _c.createSpec()
	if err := sqlgraph.CreateNode(ctx, _c.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)
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *OrdersCreate) createSpec() (*Orders, *sqlgraph.CreateSpec) {
	var (
		_node = &Orders{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(orders.Table, sqlgraph.NewFieldSpec(orders.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.ConsumerID(); ok {
		_spec.SetField(orders.FieldConsumerID, field.TypeInt64, value)
		_node.ConsumerID = value
	}
	if value, ok := _c.mutation.PlayerID(); ok {
		_spec.SetField(orders.FieldPlayerID, field.TypeInt64, value)
		_node.PlayerID = value
	}
	if value, ok := _c.mutation.ShopID(); ok {
		_spec.SetField(orders.FieldShopID, field.TypeInt64, value)
		_node.ShopID = &value
	}
	if value, ok := _c.mutation.ServiceSnapshot(); ok {
		_spec.SetField(orders.FieldServiceSnapshot, field.TypeJSON, value)
		_node.ServiceSnapshot = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(orders.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.TotalPrice(); ok {
		_spec.SetField(orders.FieldTotalPrice, field.TypeOther, value)
		_node.TotalPrice = value
	}
	if value, ok := _c.mutation.Note(); ok {
		_spec.SetField(orders.FieldNote, field.TypeString, value)
		_node.Note = &value
	}
	if value, ok := _c.mutation.Version(); ok {
		_spec.SetField(orders.FieldVersion, field.TypeInt, value)
		_node.Version = value
	}
	if value, ok := _c.mutation.TimeoutJobID(); ok {
		_spec.SetField(orders.FieldTimeoutJobID, field.TypeString, value)
		_node.TimeoutJobID = &value
	}
	if value, ok := _c.mutation.SearchText(); ok {
		_spec.SetField(orders.FieldSearchText, field.TypeString, value)
		_node.SearchText = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(orders.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.AcceptedAt(); ok {
		_spec.SetField(orders.FieldAcceptedAt, field.TypeTime, value)
		_node.AcceptedAt = &value
	}
	if value, ok := _c.mutation.ClosedAt(); ok {
		_spec.SetField(orders.FieldClosedAt, field.TypeTime, value)
		_node.ClosedAt = &value
	}
	if value, ok := _c.mutation.CompletedAt(); ok {
		_spec.SetField(orders.FieldCompletedAt, field.TypeTime, value)
		_node.CompletedAt = &value
	}
	if value, ok := _c.mutation.CancelledAt(); ok {
		_spec.SetField(orders.FieldCancelledAt, field.TypeTime, value)
		_node.CancelledAt = &value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(orders.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// OrdersCreateBulk is the builder for creating many Orders entities in bulk.
type OrdersCreateBulk struct {
	config
	err      error
	builders []*OrdersCreate
}

// Save creates the Orders entities in the database.
func (_c *OrdersCreateBulk) Save(ctx context.Context) ([]*Orders, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Orders, len(_c.builders))
	mutators := make([]Mutator, len(_c.builders))
	for i := range _c.builders {
		func(i int, root context.Context) {
			builder := _c.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*OrdersMutation)
				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, _c.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, _c.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, _c.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

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

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

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