"""设备模型 管理设备基础信息和折旧计算 """ from typing import Optional from datetime import date from sqlalchemy import String, Boolean, Integer, Date, DECIMAL from sqlalchemy.orm import Mapped, mapped_column from app.models.base import BaseModel class Equipment(BaseModel): """设备表""" __tablename__ = "equipments" equipment_code: Mapped[str] = mapped_column( String(50), unique=True, nullable=False, index=True, comment="设备编码" ) equipment_name: Mapped[str] = mapped_column( String(100), nullable=False, comment="设备名称" ) original_value: Mapped[float] = mapped_column( DECIMAL(12, 2), nullable=False, comment="设备原值" ) residual_rate: Mapped[float] = mapped_column( DECIMAL(5, 2), nullable=False, default=5.00, comment="残值率(%)" ) service_years: Mapped[int] = mapped_column( Integer, nullable=False, comment="预计使用年限" ) estimated_uses: Mapped[int] = mapped_column( Integer, nullable=False, comment="预计使用次数" ) depreciation_per_use: Mapped[float] = mapped_column( DECIMAL(12, 4), nullable=False, comment="单次折旧成本 = (原值 - 残值) / 总次数" ) purchase_date: Mapped[Optional[date]] = mapped_column( Date, nullable=True, comment="购入日期" ) is_active: Mapped[bool] = mapped_column( Boolean, nullable=False, default=True, comment="是否启用" ) def calculate_depreciation(self) -> float: """计算单次折旧成本""" if self.estimated_uses <= 0: return 0 residual_value = float(self.original_value) * float(self.residual_rate) / 100 return (float(self.original_value) - residual_value) / self.estimated_uses