-- 为现有的consultation_request表添加缺失的字段
DO $$
BEGIN
    -- 检查并添加缺失的字段
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'consultation_request' AND column_name = 'confirming_physician_name') THEN
        ALTER TABLE consultation_request ADD COLUMN confirming_physician_name VARCHAR(100);
        COMMENT ON COLUMN consultation_request.confirming_physician_name IS '确认会诊的医生姓名';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'consultation_request' AND column_name = 'confirming_department_name') THEN
        ALTER TABLE consultation_request ADD COLUMN confirming_department_name VARCHAR(100);
        COMMENT ON COLUMN consultation_request.confirming_department_name IS '代表科室';
    END IF;

    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'consultation_request' AND column_name = 'confirming_physician_participation') THEN
        ALTER TABLE consultation_request ADD COLUMN confirming_physician_participation VARCHAR(100);
        COMMENT ON COLUMN consultation_request.confirming_physician_participation IS '会诊确认参加医师';
    END IF;

    -- 检查并更新consultation_status字段的注释
    DROP COMMENT ON COLUMN consultation_request.consultation_status;
    COMMENT ON COLUMN consultation_request.consultation_status IS '会诊状态：0-新开，10-已提交，20-已确认，30-已签名，40-已完成，50-已取消';

    -- 检查并更新consultation_urgency字段的默认值
    ALTER TABLE consultation_request ALTER COLUMN consultation_urgency SET DEFAULT '一般';
    
    -- 检查并更新字段注释
    COMMENT ON COLUMN consultation_request.consultation_id IS '会诊申请单号：CS+年月日时分秒+4位随机数';
    COMMENT ON COLUMN consultation_request.consultation_request_date IS '会诊申请时间';
    COMMENT ON COLUMN consultation_request.consultation_date IS '会诊时间';
    COMMENT ON COLUMN consultation_request.consultation_purpose IS '简要病史及会诊目的';
    COMMENT ON COLUMN consultation_request.consultation_opinion IS '会诊意见';
    COMMENT ON COLUMN consultation_request.consultation_status IS '会诊状态：0-新开，10-已提交，20-已确认，30-已签名，40-已完成，50-已取消';
    COMMENT ON COLUMN consultation_request.consultation_urgency IS '是否紧急：一般/紧急';
    COMMENT ON COLUMN consultation_request.confirming_physician IS '提交会诊的医生姓名';
    COMMENT ON COLUMN consultation_request.confirming_physician_id IS '提交会诊的医生ID';
    COMMENT ON COLUMN consultation_request.confirming_date IS '提交会诊日期';
    COMMENT ON COLUMN consultation_request.signature IS '结束会诊医生姓名/签名医生';
    COMMENT ON COLUMN consultation_request.signature_physician_id IS '结束会诊医生ID';
    COMMENT ON COLUMN consultation_request.signature_date IS '结束会诊日期/签名时间';
    COMMENT ON COLUMN consultation_request.cancel_nature_date IS '作废会诊日期';
    COMMENT ON COLUMN consultation_request.cancel_reason IS '作废原因';
    COMMENT ON COLUMN consultation_request.consultation_activity_id IS '会诊项目ID，关联wor_activity_definition表，用于确定会诊类型和价格';
    COMMENT ON COLUMN consultation_request.consultation_activity_name IS '会诊项目名称，如：院内会诊、远程会诊等';

END $$;

-- 为现有表添加必要的索引
DO $$
BEGIN
    -- 检查并创建索引
    IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE tablename = 'consultation_request' AND indexname = 'idx_consultation_request_status_date') THEN
        CREATE INDEX idx_consultation_request_status_date ON consultation_request (consultation_status, consultation_request_date);
    END IF;

    IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE tablename = 'consultation_request' AND indexname = 'idx_consultation_request_physician') THEN
        CREATE INDEX idx_consultation_request_physician ON consultation_request (requesting_physician_id);
    END IF;

    IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE tablename = 'consultation_request' AND indexname = 'idx_consultation_request_invited_object') THEN
        CREATE INDEX idx_consultation_request_invited_object ON consultation_request (invited_object);
    END IF;
END $$;