""" 财务核算模型模块 """ from datetime import datetime from typing import Optional, List from sqlalchemy import ( String, Text, Integer, Numeric, Boolean, DateTime, ForeignKey, Enum as SQLEnum, Index, CheckConstraint ) from sqlalchemy.orm import Mapped, mapped_column, relationship from enum import Enum from app.core.database import Base class RevenueCategory(str, Enum): """收入类别""" EXAMINATION = "examination" # 检查费 LAB_TEST = "lab_test" # 检验费 RADIOLOGY = "radiology" # 放射费 BED = "bed" # 床位费 NURSING = "nursing" # 护理费 TREATMENT = "treatment" # 治疗费 SURGERY = "surgery" # 手术费 INJECTION = "injection" # 注射费 OXYGEN = "oxygen" # 吸氧费 OTHER = "other" # 其他 class ExpenseCategory(str, Enum): """支出类别""" MATERIAL = "material" # 材料费 PERSONNEL = "personnel" # 人员支出 MAINTENANCE = "maintenance" # 维修费 UTILITY = "utility" # 水电费 OTHER = "other" # 其他 class FinanceType(str, Enum): """财务类型""" REVENUE = "revenue" # 收入 EXPENSE = "expense" # 支出 class DepartmentFinance(Base): """科室财务记录""" __tablename__ = "department_finances" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) department_id: Mapped[int] = mapped_column(ForeignKey("departments.id"), nullable=False, comment="科室ID") period_year: Mapped[int] = mapped_column(Integer, nullable=False, comment="年度") period_month: Mapped[int] = mapped_column(Integer, nullable=False, comment="月份") finance_type: Mapped[FinanceType] = mapped_column( SQLEnum(FinanceType, values_callable=lambda x: [e.value for e in x]), nullable=False, comment="财务类型" ) category: Mapped[str] = mapped_column(String(50), nullable=False, comment="类别") amount: Mapped[float] = mapped_column(Numeric(12, 2), default=0, comment="金额") source: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, comment="数据来源") remark: Mapped[Optional[str]] = mapped_column(Text, nullable=True, comment="备注") created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, comment="创建时间") updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间") # 关系 department: Mapped["Department"] = relationship("Department", backref="finances") __table_args__ = ( Index("idx_finance_dept", "department_id"), Index("idx_finance_period", "period_year", "period_month"), Index("idx_finance_type", "finance_type"), Index("idx_finance_category", "category"), CheckConstraint("amount >= 0", name="ck_finance_amount"), ) # 为了避免循环导入,在models.py中导入时使用 from app.models.models import Department # noqa