from sqlalchemy import Column, Integer, String, ForeignKey, Double, asc from sqlalchemy.orm import relationship, mapped_column, Mapped import enums.service from models import BaseModel class Service(BaseModel): __tablename__ = 'services' id: Mapped[int] = mapped_column(Integer, autoincrement=True, primary_key=True, index=True) name = Column(String, nullable=False, comment='Название услуги') category_id = Column(Integer, ForeignKey('service_categories.id'), nullable=False, comment='ID категории услуги') category = relationship('ServiceCategory', lazy='joined') price = Column(Double, nullable=False, comment='Стоимость услуги') service_type = Column(Integer, server_default=f'{enums.service.ServiceType.DEAL_SERVICE}', nullable=False, comment='Тип услуги') price_ranges = relationship('ServicePriceRange', back_populates='service', lazy='selectin', order_by="asc(ServicePriceRange.from_quantity)", cascade="all, delete-orphan") class ServicePriceRange(BaseModel): __tablename__ = 'service_price_ranges' id = Column(Integer, autoincrement=True, primary_key=True, index=True) service_id = Column(Integer, ForeignKey('services.id'), nullable=False, comment='ID услуги') service = relationship('Service', back_populates='price_ranges') from_quantity = Column(Integer, nullable=False, comment='От количества') to_quantity = Column(Integer, nullable=False, comment='До количества') price = Column(Double, nullable=False, comment='Цена') class ServiceCategory(BaseModel): __tablename__ = 'service_categories' id = Column(Integer, autoincrement=True, primary_key=True, index=True) name = Column(String, nullable=False)