423baff73b
- Docker bridge 网络隔离(8000 端口封死) - Gunicorn 4 Worker 多进程 - Alembic 数据库迁移基线 - 日志轮转 20m×3 - JWT 密钥 + DB 密码 + CORS 收紧 - 3-2-1 备份链路(NAS + R740-B 冷备) - 连接池 pool_pre_ping + pool_recycle=3600
89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
"""
|
|
ERP 交易域 ORM 模型
|
|
映射: erp_orders / erp_order_items
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import date, datetime
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
Date,
|
|
DateTime,
|
|
ForeignKey,
|
|
Numeric,
|
|
String,
|
|
Text,
|
|
func,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.models.base import Base
|
|
|
|
|
|
class ErpOrder(Base):
|
|
__tablename__ = "erp_orders"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
order_no: Mapped[str] = mapped_column(String(30), unique=True, nullable=False)
|
|
customer_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("crm_customers.id"), nullable=False
|
|
)
|
|
salesperson_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("sys_users.id"), nullable=True
|
|
)
|
|
total_amount: Mapped[float] = mapped_column(Numeric(14, 2), default=0)
|
|
shipping_state: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="pending"
|
|
)
|
|
payment_state: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="unpaid"
|
|
)
|
|
paid_amount: Mapped[float] = mapped_column(Numeric(14, 2), default=0)
|
|
remark: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
order_date: Mapped[date] = mapped_column(Date, default=date.today)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, server_default=func.now(), onupdate=func.now()
|
|
)
|
|
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
|
|
# 关系
|
|
customer: Mapped["CrmCustomer"] = relationship("CrmCustomer", lazy="selectin") # noqa: F821
|
|
salesperson: Mapped["SysUser | None"] = relationship("SysUser", lazy="selectin") # noqa: F821
|
|
items: Mapped[list[ErpOrderItem]] = relationship(
|
|
"ErpOrderItem", back_populates="order", lazy="selectin"
|
|
)
|
|
|
|
|
|
class ErpOrderItem(Base):
|
|
__tablename__ = "erp_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("erp_orders.id"), nullable=False
|
|
)
|
|
sku_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("erp_product_skus.id"), nullable=False
|
|
)
|
|
qty: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False)
|
|
unit_price: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False)
|
|
sub_total: Mapped[float] = mapped_column(Numeric(14, 2), nullable=False)
|
|
shipped_qty: Mapped[float] = mapped_column(Numeric(12, 2), default=0)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, server_default=func.now(), onupdate=func.now()
|
|
)
|
|
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
|
|
# 关系
|
|
order: Mapped[ErpOrder] = relationship("ErpOrder", back_populates="items")
|
|
sku: Mapped["ProductSku"] = relationship("ProductSku", lazy="selectin") # noqa: F821
|