from typing import TYPE_CHECKING from sqlalchemy import Table, Column, ForeignKey, ForeignKeyConstraint, UniqueConstraint from sqlalchemy.orm import relationship, mapped_column, Mapped from models import Product from models.base import BaseModel if TYPE_CHECKING: from models import Card, Service, User card_product_service_employees = Table( 'card_product_service_employees', BaseModel.metadata, Column('card_id', primary_key=True), Column('service_id', primary_key=True), Column('product_id', primary_key=True), Column('user_id', ForeignKey('users.id'), primary_key=True), ForeignKeyConstraint( ['card_id', 'product_id', 'service_id'], ['card_product_services.card_id', 'card_product_services.product_id', 'card_product_services.service_id'] ) ) card_service_employees = Table( 'card_service_employees', BaseModel.metadata, Column('card_id', primary_key=True), Column('service_id', primary_key=True), Column('user_id', ForeignKey('users.id'), primary_key=True), ForeignKeyConstraint( ['card_id', 'service_id'], ['card_services.card_id', 'card_services.service_id'] ) ) class CardService(BaseModel): __tablename__ = 'card_services' card_id: Mapped[int] = mapped_column( ForeignKey('cards.id'), comment='ID Сделки', primary_key=True, ) card: Mapped['Card'] = relationship('Card', back_populates='services') service_id: Mapped[int] = mapped_column(ForeignKey('services.id'), nullable=False, comment='ID Услуги', primary_key=True) service: Mapped['Service'] = relationship('Service') quantity: Mapped[int] = mapped_column(nullable=False, comment='Кол-во услуги') price: Mapped[int] = mapped_column(nullable=False, server_default='0', comment='Цена услуги') is_fixed_price: Mapped[bool] = mapped_column(default=False, server_default='0', comment='Фиксированная цена') employees: Mapped[list['User']] = relationship('User', secondary=card_service_employees) __table_args__ = ( UniqueConstraint('card_id', 'service_id', name='uix_card_service'), ) class CardProductService(BaseModel): __tablename__ = 'card_product_services' card_id: Mapped[int] = mapped_column(primary_key=True, nullable=False, comment='ID Сделки') product_id: Mapped[int] = mapped_column(primary_key=True, nullable=False, comment='ID Продукта') service_id: Mapped[int] = mapped_column( ForeignKey('services.id'), primary_key=True, nullable=False, comment='ID Услуги', ) price: Mapped[int] = mapped_column(nullable=False, comment='Цена услуги') is_fixed_price: Mapped[bool] = mapped_column(default=False, server_default='0', comment='Фиксированная цена') card_product: Mapped['CardProduct'] = relationship( 'CardProduct', back_populates='services', primaryjoin="and_(CardProductService.card_id == CardProduct.card_id, " "CardProductService.product_id == CardProduct.product_id)", foreign_keys=[card_id, product_id], ) service: Mapped['Service'] = relationship( 'Service', foreign_keys=[service_id], lazy='joined', ) employees: Mapped[list['User']] = relationship( 'User', secondary=card_product_service_employees, ) __table_args__ = ( ForeignKeyConstraint( ['card_id', 'product_id'], ['card_products.card_id', 'card_products.product_id'] ), ) class CardProduct(BaseModel): __tablename__ = 'card_products' card_id: Mapped[int] = mapped_column(ForeignKey('cards.id'), primary_key=True, nullable=False, comment='ID карточки') product_id: Mapped[int] = mapped_column( ForeignKey('products.id'), primary_key=True, nullable=False, comment='ID Продукта', ) quantity: Mapped[int] = mapped_column(nullable=False, comment='Кол-во продукта') comment: Mapped[str] = mapped_column(nullable=False, server_default='', comment='Комментарий к товару') card: Mapped['Card'] = relationship( 'Card', back_populates='products', foreign_keys=[card_id], ) product: Mapped['Product'] = relationship( 'Product', lazy='joined', foreign_keys=[product_id], ) services: Mapped[list['CardProductService']] = relationship( 'CardProductService', back_populates='card_product', cascade="all, delete-orphan", primaryjoin="and_(CardProductService.card_id == CardProduct.card_id, " "CardProductService.product_id == CardProduct.product_id)", foreign_keys=[CardProductService.card_id, CardProductService.product_id], lazy='selectin', order_by="desc(CardProductService.service_id)" ) barcode_template_attribute_link = Table( 'barcode_template_attribute_links', BaseModel.metadata, Column('barcode_template_id', ForeignKey('barcode_templates.id')), Column('attribute_id', ForeignKey('barcode_template_attributes.id')) )