-- ============================================
-- 科室预约工作时间维护表结构
-- 创建日期: 2025-01-05
-- ============================================

-- 如果表不存在，创建表
CREATE TABLE IF NOT EXISTS dept_appointment_hours (
    id BIGSERIAL PRIMARY KEY,
    institution_name VARCHAR(100) DEFAULT NULL,
    dept_name VARCHAR(100) DEFAULT NULL,
    morning_start VARCHAR(20) DEFAULT NULL,
    morning_end VARCHAR(20) DEFAULT NULL,
    afternoon_start VARCHAR(20) DEFAULT NULL,
    afternoon_end VARCHAR(20) DEFAULT NULL,
    limit_count INTEGER DEFAULT NULL,
    create_by VARCHAR(50) DEFAULT NULL,
    create_time TIMESTAMP DEFAULT NULL,
    update_time TIMESTAMP DEFAULT NULL
);

-- 如果表已存在但缺少列，添加缺失的列
-- 添加 institution_name 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'institution_name'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN institution_name VARCHAR(100) DEFAULT NULL;
    END IF;
END $$;

-- 添加 dept_name 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'dept_name'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN dept_name VARCHAR(100) DEFAULT NULL;
    END IF;
END $$;

-- 添加 morning_start 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'morning_start'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN morning_start VARCHAR(20) DEFAULT NULL;
    END IF;
END $$;

-- 添加 morning_end 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'morning_end'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN morning_end VARCHAR(20) DEFAULT NULL;
    END IF;
END $$;

-- 添加 afternoon_start 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'afternoon_start'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN afternoon_start VARCHAR(20) DEFAULT NULL;
    END IF;
END $$;

-- 添加 afternoon_end 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'afternoon_end'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN afternoon_end VARCHAR(20) DEFAULT NULL;
    END IF;
END $$;

-- 添加 limit_count 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'limit_count'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN limit_count INTEGER DEFAULT NULL;
    END IF;
END $$;

-- 添加 create_by 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'create_by'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN create_by VARCHAR(50) DEFAULT NULL;
    END IF;
END $$;

-- 添加 create_time 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'create_time'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN create_time TIMESTAMP DEFAULT NULL;
    END IF;
END $$;

-- 添加 update_time 列
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'dept_appointment_hours'
        AND column_name = 'update_time'
    ) THEN
        ALTER TABLE dept_appointment_hours ADD COLUMN update_time TIMESTAMP DEFAULT NULL;
    END IF;
END $$;

-- 添加注释
COMMENT ON TABLE dept_appointment_hours IS '科室预约工作时间维护表';
COMMENT ON COLUMN dept_appointment_hours.institution_name IS '所属机构名称';
COMMENT ON COLUMN dept_appointment_hours.dept_name IS '科室名称';
COMMENT ON COLUMN dept_appointment_hours.morning_start IS '上午开始时间';
COMMENT ON COLUMN dept_appointment_hours.morning_end IS '上午结束时间';
COMMENT ON COLUMN dept_appointment_hours.afternoon_start IS '下午开始时间';
COMMENT ON COLUMN dept_appointment_hours.afternoon_end IS '下午结束时间';
COMMENT ON COLUMN dept_appointment_hours.limit_count IS '限号数量';
COMMENT ON COLUMN dept_appointment_hours.create_by IS '操作人';
COMMENT ON COLUMN dept_appointment_hours.create_time IS '创建时间';
COMMENT ON COLUMN dept_appointment_hours.update_time IS '更新时间';

-- 验证表结构
SELECT
    column_name,
    data_type,
    is_nullable,
    column_default
FROM information_schema.columns
WHERE table_name = 'dept_appointment_hours'
ORDER BY ordinal_position;
