39 lines
1.8 KiB
Python
39 lines
1.8 KiB
Python
from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, ForeignKey
|
|
from sqlalchemy.sql import func
|
|
from app.core.database import Base
|
|
|
|
|
|
class EnergyQuota(Base):
|
|
"""能源配额管理"""
|
|
__tablename__ = "energy_quotas"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
name = Column(String(200), nullable=False)
|
|
target_type = Column(String(50), nullable=False) # building, department, device_group
|
|
target_id = Column(Integer, nullable=False) # FK to device_groups.id
|
|
energy_type = Column(String(50), nullable=False) # electricity, heat, water, gas
|
|
period = Column(String(20), nullable=False) # monthly, yearly
|
|
quota_value = Column(Float, nullable=False) # 目标消耗值
|
|
unit = Column(String(20), default="kWh")
|
|
warning_threshold_pct = Column(Float, default=80) # 80%预警
|
|
alert_threshold_pct = Column(Float, default=95) # 95%告警
|
|
is_active = Column(Boolean, default=True)
|
|
created_by = Column(Integer, ForeignKey("users.id"))
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
|
|
|
|
|
class QuotaUsage(Base):
|
|
"""配额使用记录"""
|
|
__tablename__ = "quota_usage"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
quota_id = Column(Integer, ForeignKey("energy_quotas.id"), nullable=False)
|
|
period_start = Column(DateTime(timezone=True), nullable=False)
|
|
period_end = Column(DateTime(timezone=True), nullable=False)
|
|
actual_value = Column(Float, default=0)
|
|
quota_value = Column(Float, nullable=False)
|
|
usage_rate_pct = Column(Float, default=0)
|
|
status = Column(String(20), default="normal") # normal, warning, exceeded
|
|
calculated_at = Column(DateTime(timezone=True), server_default=func.now())
|