118 lines
4.7 KiB
Python
118 lines
4.7 KiB
Python
import uuid
|
|
from sqlalchemy import Column, String, Integer, ForeignKey, Text, DateTime, Float, Boolean, text, func, Numeric, Index
|
|
from sqlalchemy.dialects.postgresql import UUID as PG_UUID, JSONB
|
|
from sqlalchemy.orm import relationship, foreign
|
|
from app.db.base_class import Base
|
|
|
|
class GeoPostalCode(Base):
|
|
"""Irányítószám alapú földrajzi kereső tábla."""
|
|
__tablename__ = "geo_postal_codes"
|
|
__table_args__ = {"schema": "data"}
|
|
id = Column(Integer, primary_key=True)
|
|
country_code = Column(String(5), default="HU")
|
|
zip_code = Column(String(10), nullable=False)
|
|
city = Column(String(100), nullable=False)
|
|
|
|
class GeoStreet(Base):
|
|
"""Utcajegyzék tábla."""
|
|
__tablename__ = "geo_streets"
|
|
__table_args__ = {"schema": "data"}
|
|
id = Column(Integer, primary_key=True)
|
|
postal_code_id = Column(Integer, ForeignKey("data.geo_postal_codes.id"))
|
|
name = Column(String(200), nullable=False)
|
|
|
|
class GeoStreetType(Base):
|
|
"""Közterület jellege (utca, út, köz stb.)."""
|
|
__tablename__ = "geo_street_types"
|
|
__table_args__ = {"schema": "data"}
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String(50), unique=True, nullable=False)
|
|
|
|
class Address(Base):
|
|
"""Univerzális cím entitás GPS adatokkal kiegészítve."""
|
|
__tablename__ = "addresses"
|
|
__table_args__ = {"schema": "data"}
|
|
|
|
id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
postal_code_id = Column(Integer, ForeignKey("data.geo_postal_codes.id"))
|
|
street_name = Column(String(200), nullable=False)
|
|
street_type = Column(String(50), nullable=False)
|
|
house_number = Column(String(50), nullable=False)
|
|
stairwell = Column(String(20))
|
|
floor = Column(String(20))
|
|
door = Column(String(20))
|
|
parcel_id = Column(String(50))
|
|
full_address_text = Column(Text)
|
|
|
|
# Robot és térképes funkciók számára
|
|
latitude = Column(Float)
|
|
longitude = Column(Float)
|
|
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
class Branch(Base):
|
|
"""
|
|
Telephely entitás. A fizikai helyszín, ahol a szolgáltatás vagy flotta-kezelés zajlik.
|
|
Minden cégnek van legalább egy 'Main' telephelye.
|
|
"""
|
|
__tablename__ = "branches"
|
|
__table_args__ = {"schema": "data"}
|
|
|
|
id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
organization_id = Column(Integer, ForeignKey("data.organizations.id"), nullable=False)
|
|
address_id = Column(PG_UUID(as_uuid=True), ForeignKey("data.addresses.id"), nullable=True)
|
|
|
|
name = Column(String(100), nullable=False)
|
|
is_main = Column(Boolean, default=False)
|
|
|
|
# Részletes címadatok (Denormalizált a gyors kereséshez)
|
|
postal_code = Column(String(10), index=True)
|
|
city = Column(String(100), index=True)
|
|
street_name = Column(String(150))
|
|
street_type = Column(String(50))
|
|
house_number = Column(String(20))
|
|
stairwell = Column(String(20))
|
|
floor = Column(String(20))
|
|
door = Column(String(20))
|
|
hrsz = Column(String(50))
|
|
|
|
opening_hours = Column(JSONB, server_default=text("'{}'::jsonb"))
|
|
branch_rating = Column(Float, default=0.0)
|
|
|
|
status = Column(String(30), default="active")
|
|
is_deleted = Column(Boolean, default=False)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
organization = relationship("Organization", back_populates="branches")
|
|
address = relationship("Address")
|
|
|
|
# JAVÍTOTT KAPCSOLAT: target_branch_id használata target_id helyett
|
|
reviews = relationship(
|
|
"Rating",
|
|
primaryjoin="and_(Branch.id==foreign(Rating.target_branch_id))"
|
|
)
|
|
|
|
class Rating(Base):
|
|
"""Univerzális értékelési rendszer - v1.3.1"""
|
|
__tablename__ = "ratings"
|
|
__table_args__ = (
|
|
Index('idx_rating_org', 'target_organization_id'),
|
|
Index('idx_rating_user', 'target_user_id'),
|
|
Index('idx_rating_branch', 'target_branch_id'),
|
|
{"schema": "data"}
|
|
)
|
|
# Az ID most már Integer, ahogy kérted a statisztikákhoz
|
|
id = Column(Integer, primary_key=True)
|
|
author_id = Column(Integer, ForeignKey("data.users.id"), nullable=False)
|
|
|
|
# Explicit célpontok a típusbiztonság és gyorsaság érdekében
|
|
target_organization_id = Column(Integer, ForeignKey("data.organizations.id"), nullable=True)
|
|
target_user_id = Column(Integer, ForeignKey("data.users.id"), nullable=True)
|
|
target_branch_id = Column(PG_UUID(as_uuid=True), ForeignKey("data.branches.id"), nullable=True)
|
|
|
|
score = Column(Numeric(3, 2), nullable=False) # 1.00 - 5.00
|
|
comment = Column(Text)
|
|
images = Column(JSONB, server_default=text("'[]'::jsonb"))
|
|
|
|
is_verified = Column(Boolean, default=False)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now()) |