from sqlalchemy import Column, Integer, String, Float, DateTime, Text, ForeignKey, func
from app.database import Base


class ProductionRecord(Base):
    """生产实绩"""
    __tablename__ = "production_records"

    id = Column(Integer, primary_key=True, index=True)
    coil_no = Column(String(30), nullable=False, index=True)  # 兼容旧字段，等同于 sub_coil_no
    sub_coil_no = Column(String(30), index=True, comment="子卷号")
    hot_coil_no = Column(String(30), index=True, comment="热卷号")
    plan_id = Column(Integer, ForeignKey("production_plans.id"), nullable=True)
    shift = Column(String(10), comment="班")
    team = Column(String(10), comment="组")
    steel_grade = Column(String(30), comment="钢种")
    incoming_thickness = Column(Float, comment="来料厚度 mm")
    outlet_thickness = Column(Float, comment="出口厚度 mm")
    deviation_upper = Column(Float, comment="偏差上限")
    deviation_lower = Column(Float, comment="偏差下限")
    incoming_width = Column(Float, comment="来料宽度 mm")
    outlet_width = Column(Float, comment="出口宽度 mm")
    incoming_weight = Column(Float, comment="来料重量 t")
    weighed_weight = Column(Float, comment="称重重量 t")
    packaging_req = Column(String(30), comment="包装要求")
    trim_req = Column(String(30), comment="切边要求")
    surface_quality = Column(String(30), comment="表面质量")
    product_quality = Column(Float, comment="成品质量 %")
    product_length = Column(Float, comment="成品长度 m")
    length_per_ton = Column(Float, comment="吨钢长度 m/t")
    offline_time = Column(DateTime, comment="下线时间")
    status = Column(String(20), default="UNWEIGH", comment="状态: UNWEIGH/PRODUCT")

    # 兼容历史字段
    shift_date = Column(DateTime)
    start_time = Column(DateTime)
    end_time = Column(DateTime)
    process_length = Column(Float)
    process_weight = Column(Float)
    avg_speed = Column(Float)
    max_speed = Column(Float)
    acid_consumption = Column(Float)
    inlet_thickness = Column(Float)
    inlet_width = Column(Float)
    quality_grade = Column(String(10))
    operator = Column(String(50))
    remark = Column(Text)
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
