# -*- coding: utf-8 -*-
from sqlalchemy import String, Column, Integer,DateTime,Text,BigInteger,Boolean,PrimaryKeyConstraint,Index,UniqueConstraint,CHAR,LargeBinary,TIMESTAMP,ForeignKey,Numeric
from sqlalchemy.dialects.mysql import TINYINT
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base
from datetime import datetime

class EmergencyPlan(Base):
    __tablename__ = 'emergency_plans'  # 表名

    # 定义字段
    id = Column(Integer, autoincrement=True, primary_key=True)  # 主键
    plan_id = Column(String(50),comment='预案uid')
    plan_number = Column(String(15), comment='预案编号')
    plan_name = Column(String(255), nullable=False, comment='预案名称')
    plan_type = Column(String(100), nullable=False, comment='预案类型')
    plan_type_desc = Column(String(100), nullable=False, comment='预案类型（中文名称）')
    publish_date = Column(DateTime, default=datetime.now, comment='发布日期')
    organizing_unit = Column(String(255), comment='编制单位')
    document_number = Column(String(100), comment='发文字号')
    create_time = Column(DateTime, default=datetime.now, comment='数据创建时间')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='数据更新时间')
    create_dept = Column(BigInteger, default=None, comment='创建部门')
    create_by = Column(BigInteger, default=None, comment='创建者')
    del_flag = Column(String(1), default='0', comment='删除标志（0代表存在 2代表删除）')

    class Config:
        orm_mode = True

# def create_new_plan_with_number(session, name):
#     new_plan = EmergencyPlan(
#         name=name
#     )
#     session.add(new_plan)
#     session.commit()
#     session.refresh(new_plan)  # 刷新实例以获取数据库分配的 id
#     new_plan.plan_number = f'YJYA{str(new_plan.id).zfill(10)}'
#     session.commit()
#     return new_plan

class EmergencyFile(Base):
    __tablename__ = 'emergency_file'

    id = Column(Integer, autoincrement=True, primary_key=True)
    file_id = Column(String(50), nullable=False, comment='文件id')
    file_name = Column(String(255), nullable=False, comment='文件名称')
    file_name_desc = Column(String(255), nullable=False, comment='文件名称原名')
    file_path = Column(String(255), nullable=False, comment='文件存储路径')
    file_size = Column(String(50), comment='文件大小')
    foreign_key = Column(String(50), comment='文件外键 --技术字段')
    from_scenario = Column(String(50), comment='对应标识 --技术字段')
    create_time = Column(DateTime, default=datetime.now, comment='数据创建时间')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='数据更新时间')
    del_flag = Column(String(1), default='0', comment='删除标志（0代表存在 2代表删除）') #更新预案信息的时候 先将原有的进行备注删除

    # 关联到 EmergencyPlan 实例


class EmergencyDrill(Base):
    __tablename__ = 'emergency_drills'

    id = Column(Integer, autoincrement=True, primary_key=True)
    drill_id = Column(String(50), comment='演练uid' )
    drill_name = Column(String(255), nullable=False, comment='演练名称')
    organizing_unit = Column(String(255), nullable=False, comment='演练单位')
    plan_id = Column(String(50), nullable=False,comment='预案uid')
    # 前端确认传输什么数据
    drill_location = Column(String(255), comment='演练地点')
    drill_lon = Column(Numeric(9, 6), comment='演练经度')
    drill_lat = Column(Numeric(9, 6), comment='演练纬度')
    planned_annual = Column(Integer, comment='计划年度')
    planned_time = Column(DateTime, comment='计划演练时间')
    create_time = Column(DateTime, default=datetime.now, comment='数据创建时间')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='数据更新时间')
    del_flag = Column(String(1), default='0', comment='删除标志（0代表存在 2代表删除）')


class EmergencyTrainingSession(Base):
    __tablename__ = 'emergency_training_sessions'

    id = Column(Integer, autoincrement=True, primary_key=True)
    training_id = Column(String(50), nullable=False,comment='培训uid')
    training_content = Column(String(255), nullable=False, comment='培训内容')
    training_unit = Column(String(255), nullable=False, comment='培训单位')
    participant_count = Column(Integer, comment='参与人数')
    training_method = Column(String(255), comment='培训方式')
    start_time = Column(DateTime, comment='培训开始时间')
    end_time = Column(DateTime, comment='培训结束时间')
    training_location = Column(String(255), comment='培训地点')
    drill_lon = Column(Numeric(9, 6), comment='培训经度')
    drill_lat = Column(Numeric(9, 6), comment='培训纬度')
    plan_id = Column(String(50), nullable=False,comment='预案uid')
    del_flag = Column(String(1), default='0', comment='删除标志（0代表存在 2代表删除）')