from sqlalchemy import ForeignKey, Table, Column from sqlalchemy.orm import Mapped, mapped_column, relationship from models.base import BaseModel from models.mixins import IdMixin, SoftDeleteMixin, CostMixin, PriceMixin from modules.fulfillment_base import enums services_kit_services = Table( "fulfillment_base_services_kit_services", BaseModel.metadata, Column("services_kit_id", ForeignKey("fulfillment_base_services_kits.id")), Column("service_id", ForeignKey("fulfillment_base_services.id")), ) class Service(BaseModel, IdMixin, SoftDeleteMixin, PriceMixin, CostMixin): __tablename__ = "fulfillment_base_services" name: Mapped[str] = mapped_column(index=True) price_ranges: Mapped[list["ServicePriceRange"]] = relationship( back_populates="service", lazy="selectin", order_by="asc(ServicePriceRange.from_quantity)", cascade="all, delete-orphan", ) category_id: Mapped[int] = mapped_column( ForeignKey("fulfillment_base_service_categories.id"), comment="ID категории услуги", ) category: Mapped["ServiceCategory"] = relationship("ServiceCategory", lazy="joined") service_type: Mapped[int] = mapped_column( server_default=f"{enums.service.ServiceType.DEAL_SERVICE}", comment="Тип услуги", ) lexorank: Mapped[str] = mapped_column(comment="Ранг услуги") class ServiceCategory(BaseModel, IdMixin): __tablename__ = "fulfillment_base_service_categories" name: Mapped[str] = mapped_column() is_deleted: Mapped[bool] = mapped_column( default=False, comment="Удалена ли категория" ) deal_service_rank: Mapped[str] = mapped_column(comment="Ранг услуги для сделки") product_service_rank: Mapped[str] = mapped_column(comment="Ранг услуги для товара") class ServicesKit(BaseModel, IdMixin): __tablename__ = "fulfillment_base_services_kits" name: Mapped[str] = mapped_column() service_type: Mapped[int] = mapped_column( server_default=f"{enums.ServiceType.DEAL_SERVICE}", comment="Тип услуги", ) services: Mapped[list["Service"]] = relationship( secondary=services_kit_services, lazy="selectin" ) class ServicePriceRange(BaseModel, IdMixin, PriceMixin): __tablename__ = "fulfillment_base_service_price_ranges" service_id: Mapped[int] = mapped_column(ForeignKey("fulfillment_base_services.id")) service: Mapped[Service] = relationship(back_populates="price_ranges") from_quantity: Mapped[int] = mapped_column(comment="От количества") to_quantity: Mapped[int] = mapped_column(comment="До количества")