72 lines
3.3 KiB
Python
72 lines
3.3 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
from sqlalchemy import String, DateTime, Enum as SAEnum, ForeignKey, Integer, Boolean, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
|
from app.database import Base
|
|
import enum
|
|
|
|
|
|
class ItemStatus(str, enum.Enum):
|
|
pending = "pending"
|
|
approved = "approved"
|
|
rejected = "rejected"
|
|
|
|
|
|
class AIValidationStatus(str, enum.Enum):
|
|
not_started = "not_started"
|
|
pending = "pending"
|
|
completed = "completed"
|
|
failed = "failed"
|
|
|
|
|
|
class OrderItem(Base):
|
|
__tablename__ = "order_items"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
order_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("orders.id"), nullable=False)
|
|
row_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
|
|
# 11 Standard fields (columns 0-10, skip col 5)
|
|
ebene1: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
ebene2: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
baureihe: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
pim_id: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
produkt_baureihe: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
# col 5 is skipped (separator)
|
|
gewaehltes_produkt: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
name_cad_modell: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
gewuenschte_bildnummer: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
lagertyp: Mapped[str] = mapped_column(String(500), nullable=True)
|
|
medias_rendering: Mapped[bool] = mapped_column(Boolean, nullable=True)
|
|
|
|
# Component pairs (cols 11+): [{part_name, material, component_type, column_index}]
|
|
components: Mapped[list] = mapped_column(JSONB, nullable=False, default=list)
|
|
|
|
# CAD linkage
|
|
cad_file_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("cad_files.id"), nullable=True)
|
|
thumbnail_path: Mapped[str] = mapped_column(String(1000), nullable=True)
|
|
# Material assignments per CAD part: [{part_name, material}]
|
|
cad_part_materials: Mapped[list] = mapped_column(JSONB, nullable=False, default=list)
|
|
|
|
# AI validation
|
|
ai_validation_status: Mapped[AIValidationStatus] = mapped_column(
|
|
SAEnum(AIValidationStatus), default=AIValidationStatus.not_started, nullable=False
|
|
)
|
|
ai_validation_result: Mapped[dict] = mapped_column(JSONB, nullable=True)
|
|
|
|
item_status: Mapped[ItemStatus] = mapped_column(SAEnum(ItemStatus), default=ItemStatus.pending, nullable=False)
|
|
notes: Mapped[str] = mapped_column(Text, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
|
|
|
order: Mapped["Order"] = relationship("Order", back_populates="items")
|
|
cad_file: Mapped["CadFile"] = relationship("CadFile", back_populates="order_items")
|
|
|
|
@property
|
|
def cad_parsed_objects(self) -> list[str] | None:
|
|
"""Part names extracted from the linked STEP file, for Pydantic serialization."""
|
|
if self.cad_file and self.cad_file.parsed_objects:
|
|
return self.cad_file.parsed_objects.get("objects") or []
|
|
return None
|