-- =============================================
-- 创建 lab_observation 表
-- 用途: 实验室观察记录表
-- 创建时间: 2026-03-09
-- =============================================

-- 删除已存在的表（如果存在）
DROP TABLE IF EXISTS lab_observation;

-- 创建序列（如果不存在）
DROP SEQUENCE IF EXISTS lab_observation_id_seq;
CREATE SEQUENCE lab_observation_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

-- 创建表
CREATE TABLE lab_observation (
    id BIGINT PRIMARY KEY DEFAULT nextval('lab_observation_id_seq'),
    patient_id BIGINT,
    encounter_id BIGINT,
    specimen_id BIGINT,
    observation_definition_id BIGINT,
    observation_result TEXT,
    observation_date TIMESTAMP,
    technician_id BIGINT,
    delete_flag VARCHAR(1) DEFAULT '0',
    create_by VARCHAR(64) DEFAULT '',
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_by VARCHAR(64) DEFAULT '',
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    remark VARCHAR(500)
);

-- 添加表注释
COMMENT ON TABLE lab_observation IS '实验室观察记录表';

-- 添加字段注释
COMMENT ON COLUMN lab_observation.id IS '观察记录的唯一标识符';
COMMENT ON COLUMN lab_observation.patient_id IS '患者ID';
COMMENT ON COLUMN lab_observation.encounter_id IS '就诊ID';
COMMENT ON COLUMN lab_observation.specimen_id IS '关联的样本ID';
COMMENT ON COLUMN lab_observation.observation_definition_id IS '观察定义ID';
COMMENT ON COLUMN lab_observation.observation_result IS '观察结果，可能是文本描述或数值';
COMMENT ON COLUMN lab_observation.observation_date IS '观察的日期和时间';
COMMENT ON COLUMN lab_observation.technician_id IS '执行观察的技术员ID';
COMMENT ON COLUMN lab_observation.delete_flag IS '删除状态，0-未删除，1-已删除';
COMMENT ON COLUMN lab_observation.create_by IS '创建者';
COMMENT ON COLUMN lab_observation.create_time IS '创建时间';
COMMENT ON COLUMN lab_observation.update_by IS '更新者';
COMMENT ON COLUMN lab_observation.update_time IS '更新时间';
COMMENT ON COLUMN lab_observation.remark IS '备注';

-- 创建索引
CREATE INDEX idx_lab_observation_patient_id ON lab_observation(patient_id);
CREATE INDEX idx_lab_observation_specimen_id ON lab_observation(specimen_id);
CREATE INDEX idx_lab_observation_observation_date ON lab_observation(observation_date);

-- 打印完成信息
SELECT 'lab_observation 表创建成功！' AS message;
