// Code generated by ent, DO NOT EDIT.

package models

import (
	"encoding/json"
	"fmt"
	"juwan-backend/app/order/rpc/internal/models/orders"
	"strings"
	"time"

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

// Orders is the model entity for the Orders schema.
type Orders struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// ConsumerID holds the value of the "consumer_id" field.
	ConsumerID int64 `json:"consumer_id,omitempty"`
	// PlayerID holds the value of the "player_id" field.
	PlayerID int64 `json:"player_id,omitempty"`
	// ShopID holds the value of the "shop_id" field.
	ShopID *int64 `json:"shop_id,omitempty"`
	// ServiceSnapshot holds the value of the "service_snapshot" field.
	ServiceSnapshot map[string]interface{} `json:"service_snapshot,omitempty"`
	// Status holds the value of the "status" field.
	Status string `json:"status,omitempty"`
	// TotalPrice holds the value of the "total_price" field.
	TotalPrice decimal.Decimal `json:"total_price,omitempty"`
	// Note holds the value of the "note" field.
	Note *string `json:"note,omitempty"`
	// Version holds the value of the "version" field.
	Version int `json:"version,omitempty"`
	// TimeoutJobID holds the value of the "timeout_job_id" field.
	TimeoutJobID *string `json:"timeout_job_id,omitempty"`
	// SearchText holds the value of the "search_text" field.
	SearchText *string `json:"search_text,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// AcceptedAt holds the value of the "accepted_at" field.
	AcceptedAt *time.Time `json:"accepted_at,omitempty"`
	// ClosedAt holds the value of the "closed_at" field.
	ClosedAt *time.Time `json:"closed_at,omitempty"`
	// CompletedAt holds the value of the "completed_at" field.
	CompletedAt *time.Time `json:"completed_at,omitempty"`
	// CancelledAt holds the value of the "cancelled_at" field.
	CancelledAt *time.Time `json:"cancelled_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt    time.Time `json:"updated_at,omitempty"`
	selectValues sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Orders) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case orders.FieldServiceSnapshot:
			values[i] = new([]byte)
		case orders.FieldTotalPrice:
			values[i] = new(decimal.Decimal)
		case orders.FieldID, orders.FieldConsumerID, orders.FieldPlayerID, orders.FieldShopID, orders.FieldVersion:
			values[i] = new(sql.NullInt64)
		case orders.FieldStatus, orders.FieldNote, orders.FieldTimeoutJobID, orders.FieldSearchText:
			values[i] = new(sql.NullString)
		case orders.FieldCreatedAt, orders.FieldAcceptedAt, orders.FieldClosedAt, orders.FieldCompletedAt, orders.FieldCancelledAt, orders.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Orders fields.
func (_m *Orders) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case orders.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = int64(value.Int64)
		case orders.FieldConsumerID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field consumer_id", values[i])
			} else if value.Valid {
				_m.ConsumerID = value.Int64
			}
		case orders.FieldPlayerID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field player_id", values[i])
			} else if value.Valid {
				_m.PlayerID = value.Int64
			}
		case orders.FieldShopID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field shop_id", values[i])
			} else if value.Valid {
				_m.ShopID = new(int64)
				*_m.ShopID = value.Int64
			}
		case orders.FieldServiceSnapshot:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field service_snapshot", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.ServiceSnapshot); err != nil {
					return fmt.Errorf("unmarshal field service_snapshot: %w", err)
				}
			}
		case orders.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = value.String
			}
		case orders.FieldTotalPrice:
			if value, ok := values[i].(*decimal.Decimal); !ok {
				return fmt.Errorf("unexpected type %T for field total_price", values[i])
			} else if value != nil {
				_m.TotalPrice = *value
			}
		case orders.FieldNote:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field note", values[i])
			} else if value.Valid {
				_m.Note = new(string)
				*_m.Note = value.String
			}
		case orders.FieldVersion:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field version", values[i])
			} else if value.Valid {
				_m.Version = int(value.Int64)
			}
		case orders.FieldTimeoutJobID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field timeout_job_id", values[i])
			} else if value.Valid {
				_m.TimeoutJobID = new(string)
				*_m.TimeoutJobID = value.String
			}
		case orders.FieldSearchText:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field search_text", values[i])
			} else if value.Valid {
				_m.SearchText = new(string)
				*_m.SearchText = value.String
			}
		case orders.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				_m.CreatedAt = value.Time
			}
		case orders.FieldAcceptedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field accepted_at", values[i])
			} else if value.Valid {
				_m.AcceptedAt = new(time.Time)
				*_m.AcceptedAt = value.Time
			}
		case orders.FieldClosedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field closed_at", values[i])
			} else if value.Valid {
				_m.ClosedAt = new(time.Time)
				*_m.ClosedAt = value.Time
			}
		case orders.FieldCompletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field completed_at", values[i])
			} else if value.Valid {
				_m.CompletedAt = new(time.Time)
				*_m.CompletedAt = value.Time
			}
		case orders.FieldCancelledAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field cancelled_at", values[i])
			} else if value.Valid {
				_m.CancelledAt = new(time.Time)
				*_m.CancelledAt = value.Time
			}
		case orders.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				_m.UpdatedAt = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the Orders.
// This includes values selected through modifiers, order, etc.
func (_m *Orders) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// Update returns a builder for updating this Orders.
// Note that you need to call Orders.Unwrap() before calling this method if this Orders
// was returned from a transaction, and the transaction was committed or rolled back.
func (_m *Orders) Update() *OrdersUpdateOne {
	return NewOrdersClient(_m.config).UpdateOne(_m)
}

// Unwrap unwraps the Orders entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (_m *Orders) Unwrap() *Orders {
	_tx, ok := _m.config.driver.(*txDriver)
	if !ok {
		panic("models: Orders is not a transactional entity")
	}
	_m.config.driver = _tx.drv
	return _m
}

// String implements the fmt.Stringer.
func (_m *Orders) String() string {
	var builder strings.Builder
	builder.WriteString("Orders(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("consumer_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.ConsumerID))
	builder.WriteString(", ")
	builder.WriteString("player_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.PlayerID))
	builder.WriteString(", ")
	if v := _m.ShopID; v != nil {
		builder.WriteString("shop_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("service_snapshot=")
	builder.WriteString(fmt.Sprintf("%v", _m.ServiceSnapshot))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(_m.Status)
	builder.WriteString(", ")
	builder.WriteString("total_price=")
	builder.WriteString(fmt.Sprintf("%v", _m.TotalPrice))
	builder.WriteString(", ")
	if v := _m.Note; v != nil {
		builder.WriteString("note=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("version=")
	builder.WriteString(fmt.Sprintf("%v", _m.Version))
	builder.WriteString(", ")
	if v := _m.TimeoutJobID; v != nil {
		builder.WriteString("timeout_job_id=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.SearchText; v != nil {
		builder.WriteString("search_text=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := _m.AcceptedAt; v != nil {
		builder.WriteString("accepted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.ClosedAt; v != nil {
		builder.WriteString("closed_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.CompletedAt; v != nil {
		builder.WriteString("completed_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.CancelledAt; v != nil {
		builder.WriteString("cancelled_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// OrdersSlice is a parsable slice of Orders.
type OrdersSlice []*Orders
