""" ETest LIMS 扩展实体定义 包含:测试权限、测试类别详情、报告权限、测试报告、报告版本、重测记录、审核记录等 """ from datetime import datetime, date from typing import Optional, Dict, Any from sqlalchemy import Column, Integer, String, DateTime, Date, Text, JSON, ForeignKey, BigInteger, func from config.database import Base class TestPermission(Base): """测试人员权限配置表""" __tablename__ = 'test_permission' permission_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='权限ID') user_id = Column(BigInteger, nullable=False, comment='用户ID') user_name = Column(String(64), nullable=False, comment='用户名称') permission_type = Column(String(32), nullable=False, comment='权限类型: TESTER/REVIEWER/SECOND_REVIEWER/THIRD_REVIEWER/WRITER/REVIEWER_1/REVIEWER_2/REVIEWER_3') test_category_id = Column(BigInteger, nullable=True, comment='测试类别ID(可为空表示全部)') test_item_id = Column(BigInteger, nullable=True, comment='测试项目ID(可为空表示全部)') can_claim = Column(Integer, default=1, comment='是否可领取: 0=否, 1=是') is_active = Column(Integer, default=1, comment='是否有效: 0=否, 1=是') del_flag = Column(String(1), default='0', comment='删除标志: 0=正常, 1=已删除') create_by = Column(String(64), nullable=True, comment='创建者') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间') update_by = Column(String(64), nullable=True, comment='更新者') update_time = Column(DateTime, nullable=True, default=func.now(), onupdate=func.now(), comment='更新时间') remark = Column(String(500), nullable=True, comment='备注') class TestCategoryDetail(Base): """测试类别详情表 - 存储动态字段配置""" __tablename__ = 'test_category_detail' detail_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='详情ID') category_id = Column(BigInteger, nullable=False, comment='测试类别ID') condition_fields = Column(JSON, nullable=True, comment='测试条件字段定义(JSON数组)') result_fields = Column(JSON, nullable=True, comment='测试结果字段定义(JSON数组)') raw_data_fields = Column(JSON, nullable=True, comment='原始数据字段定义(JSON数组)') create_by = Column(String(64), nullable=True, comment='创建者') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间') update_by = Column(String(64), nullable=True, comment='更新者') update_time = Column(DateTime, nullable=True, default=func.now(), onupdate=func.now(), comment='更新时间') class ReportPermission(Base): """报告权限配置表""" __tablename__ = 'report_permission' permission_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='权限ID') user_id = Column(BigInteger, nullable=False, comment='用户ID') user_name = Column(String(64), nullable=False, comment='用户名称') permission_type = Column(String(32), nullable=False, comment='权限类型: WRITER/REVIEWER_1/REVIEWER_2/REVIEWER_3') test_category_id = Column(BigInteger, nullable=True, comment='测试类别ID(可为空表示全部)') test_item_id = Column(BigInteger, nullable=True, comment='测试项目ID(可为空表示全部)') is_active = Column(Integer, default=1, comment='是否有效: 0=否, 1=是') create_by = Column(String(64), nullable=True, comment='创建者') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间') update_by = Column(String(64), nullable=True, comment='更新者') update_time = Column(DateTime, nullable=True, default=func.now(), onupdate=func.now(), comment='更新时间') remark = Column(String(500), nullable=True, comment='备注') class TestReport(Base): """测试报告主表""" __tablename__ = 'test_report' report_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='报告ID') report_no = Column(String(64), nullable=False, unique=True, comment='报告编号') report_title = Column(String(200), nullable=False, comment='报告标题') report_config_type = Column(String(16), default='CATEGORY', comment='报告配置类型: CATEGORY=按类别, ITEM=按项目') test_category_id = Column(BigInteger, nullable=True, comment='测试类别ID') test_item_id = Column(BigInteger, nullable=True, comment='测试项目ID') sample_id = Column(BigInteger, nullable=False, comment='样品ID') eut_id = Column(BigInteger, nullable=True, comment='受测设备ID') # 报告内容 condition_data = Column(JSON, nullable=True, comment='测试条件数据(JSON)') result_data = Column(JSON, nullable=True, comment='测试结果数据(JSON)') raw_data = Column(JSON, nullable=True, comment='原始数据(JSON)') conclusion = Column(Text, nullable=True, comment='测试结论') # 报告状态 status = Column(String(32), default='DRAFT', comment='报告状态: DRAFT/WRITING/PENDING_REVIEW_1/PENDING_REVIEW_2/PENDING_REVIEW_3/APPROVED/REJECTED') # 人员信息 writer_id = Column(BigInteger, nullable=True, comment='编写人ID') writer_name = Column(String(64), nullable=True, comment='编写人名称') reviewer_1_id = Column(BigInteger, nullable=True, comment='一级审核人ID') reviewer_1_name = Column(String(64), nullable=True, comment='一级审核人名称') reviewer_1_time = Column(DateTime, nullable=True, comment='一级审核时间') reviewer_2_id = Column(BigInteger, nullable=True, comment='二级审核人ID') reviewer_2_name = Column(String(64), nullable=True, comment='二级审核人名称') reviewer_2_time = Column(DateTime, nullable=True, comment='二级审核时间') reviewer_3_id = Column(BigInteger, nullable=True, comment='三级审核人ID') reviewer_3_name = Column(String(64), nullable=True, comment='三级审核人名称') reviewer_3_time = Column(DateTime, nullable=True, comment='三级审核时间') # 版本控制 version = Column(Integer, default=1, comment='版本号') is_current = Column(Integer, default=1, comment='是否当前版本: 0=否, 1=是') parent_version_id = Column(BigInteger, nullable=True, comment='父版本ID(用于追溯)') # 时间戳 submit_time = Column(DateTime, nullable=True, comment='提交时间') approve_time = Column(DateTime, nullable=True, comment='批准时间') create_by = Column(String(64), nullable=True, comment='创建者') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间') update_by = Column(String(64), nullable=True, comment='更新者') update_time = Column(DateTime, nullable=True, default=func.now(), onupdate=func.now(), comment='更新时间') del_flag = Column(Integer, default=0, comment='删除标志: 0=正常, 1=已删除') remark = Column(String(500), nullable=True, comment='备注') class ReportWorkOrderRelation(Base): """报告与工单关联表""" __tablename__ = 'report_work_order_relation' relation_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='关联ID') report_id = Column(BigInteger, nullable=False, comment='报告ID') work_order_id = Column(BigInteger, nullable=False, comment='工单ID') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间') class TestReportVersion(Base): """测试报告版本历史表""" __tablename__ = 'test_report_version' version_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='版本ID') report_id = Column(BigInteger, nullable=False, comment='报告ID') version_no = Column(Integer, nullable=False, comment='版本号') version_type = Column(String(32), nullable=False, comment='版本类型: INITIAL/REWRITE/RETEST') # 版本内容快照 condition_data = Column(JSON, nullable=True, comment='测试条件数据快照') result_data = Column(JSON, nullable=True, comment='测试结果数据快照') raw_data = Column(JSON, nullable=True, comment='原始数据快照') conclusion = Column(Text, nullable=True, comment='测试结论快照') # 版本元数据 create_by = Column(String(64), nullable=True, comment='创建者') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间') change_reason = Column(String(500), nullable=True, comment='变更原因') class WorkOrderRetestRecord(Base): """工单重测记录表""" __tablename__ = 'work_order_retest_record' record_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='记录ID') original_work_order_id = Column(BigInteger, nullable=False, comment='原工单ID') retest_work_order_id = Column(BigInteger, nullable=False, comment='重测工单ID') retest_count = Column(Integer, default=1, comment='重测次数') retest_reason = Column(String(500), nullable=True, comment='重测原因') rejected_by = Column(BigInteger, nullable=True, comment='驳回人ID') rejected_time = Column(DateTime, nullable=True, comment='驳回时间') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间') class ReportReviewRecord(Base): """报告审核记录表""" __tablename__ = 'report_review_record' record_id = Column(BigInteger, primary_key=True, autoincrement=True, comment='记录ID') report_id = Column(BigInteger, nullable=False, comment='报告ID') version_id = Column(BigInteger, nullable=True, comment='版本ID') reviewer_id = Column(BigInteger, nullable=False, comment='审核人ID') reviewer_name = Column(String(64), nullable=True, comment='审核人名称') review_level = Column(Integer, nullable=False, comment='审核级别: 1/2/3') action = Column(String(32), nullable=False, comment='操作: APPROVE/REJECT_REWRITE/REJECT_RETEST') comments = Column(String(1000), nullable=True, comment='审核意见') create_time = Column(DateTime, nullable=True, default=func.now(), comment='创建时间')