-- 平台共享字典（专家职称/医院）+ 专家表字典编码字段

CREATE TABLE IF NOT EXISTS platform_dictionary_item (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID',
    dict_type VARCHAR(64) NOT NULL COMMENT '字典类型：EXPERT_TITLE/EXPERT_HOSPITAL',
    dict_code VARCHAR(64) NOT NULL COMMENT '字典编码',
    dict_name VARCHAR(128) NOT NULL COMMENT '字典名称',
    sort_no INT NOT NULL DEFAULT 100 COMMENT '排序',
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED' COMMENT '状态：ENABLED/DISABLED',
    remark VARCHAR(500) DEFAULT NULL COMMENT '备注',
    is_deleted TINYINT(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除',
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建人',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_by BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新人',
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    UNIQUE KEY uk_dict_type_code (dict_type, dict_code),
    KEY idx_dict_type_status_sort (dict_type, status, sort_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='平台共享字典项';

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'expert' AND COLUMN_NAME = 'title_code');
SET @sql := IF(@c = 0, 'ALTER TABLE expert ADD COLUMN title_code VARCHAR(64) NULL AFTER phone', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'expert' AND COLUMN_NAME = 'hospital_code');
SET @sql := IF(@c = 0, 'ALTER TABLE expert ADD COLUMN hospital_code VARCHAR(64) NULL AFTER title', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @idx := (SELECT COUNT(1) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'expert' AND INDEX_NAME = 'idx_expert_title_code');
SET @sql := IF(@idx = 0, 'ALTER TABLE expert ADD KEY idx_expert_title_code (title_code)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @idx := (SELECT COUNT(1) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'expert' AND INDEX_NAME = 'idx_expert_hospital_code');
SET @sql := IF(@idx = 0, 'ALTER TABLE expert ADD KEY idx_expert_hospital_code (hospital_code)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

ALTER TABLE expert
    COMMENT = '平台共享专家主数据（tenant_id=0）';

ALTER TABLE expert
    MODIFY COLUMN expert_name VARCHAR(128) NOT NULL COMMENT '专家姓名',
    MODIFY COLUMN id_no VARCHAR(64) NOT NULL COMMENT '身份证号（敏感）',
    MODIFY COLUMN phone VARCHAR(32) NOT NULL COMMENT '手机号（敏感）',
    MODIFY COLUMN title_code VARCHAR(64) NULL COMMENT '职称字典编码',
    MODIFY COLUMN title VARCHAR(128) DEFAULT NULL COMMENT '职称名称快照',
    MODIFY COLUMN hospital_code VARCHAR(64) NULL COMMENT '医院字典编码',
    MODIFY COLUMN organization VARCHAR(255) DEFAULT NULL COMMENT '医院名称快照',
    MODIFY COLUMN status VARCHAR(32) NOT NULL DEFAULT 'ENABLED' COMMENT '状态：DRAFT/ENABLED/DISABLED/BLACKLISTED';

ALTER TABLE expert_bank_card
    COMMENT = '专家银行卡信息';

ALTER TABLE expert_bank_card
    MODIFY COLUMN bank_name VARCHAR(128) NOT NULL COMMENT '开户银行',
    MODIFY COLUMN bank_card_no VARCHAR(128) NOT NULL COMMENT '银行卡号（敏感）',
    MODIFY COLUMN account_name VARCHAR(128) NOT NULL COMMENT '开户名';

INSERT INTO platform_permission (id, permission_code, permission_name, module)
VALUES
    (10, 'platform.dictionary.read', '平台字典查看', 'platform'),
    (11, 'platform.dictionary.manage', '平台字典管理', 'platform')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO platform_role_permission (id, role_id, permission_id)
VALUES
    (10, 1, 10),
    (11, 1, 11)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);

INSERT INTO platform_menu (id, menu_code, menu_name, route_path, permission_code, sort_no, status, is_deleted, created_by, updated_by)
VALUES
    (8, 'platform_dictionary_manage', '平台字典管理', '/platform/dictionaries', 'platform.dictionary.read', 19, 'ENABLED', 0, 0, 0)
ON DUPLICATE KEY UPDATE
    menu_name = VALUES(menu_name),
    route_path = VALUES(route_path),
    permission_code = VALUES(permission_code),
    sort_no = VALUES(sort_no),
    status = VALUES(status),
    is_deleted = VALUES(is_deleted),
    updated_at = CURRENT_TIMESTAMP;

INSERT INTO platform_role_menu (id, role_id, menu_id)
VALUES
    (8, 1, 8)
ON DUPLICATE KEY UPDATE menu_id = VALUES(menu_id);

INSERT INTO platform_dictionary_item (dict_type, dict_code, dict_name, sort_no, status, remark, created_by, updated_by)
VALUES
    ('EXPERT_TITLE', 'CHIEF_PHYSICIAN', '主任医师', 10, 'ENABLED', '默认种子', 0, 0),
    ('EXPERT_TITLE', 'ASSOCIATE_CHIEF_PHYSICIAN', '副主任医师', 20, 'ENABLED', '默认种子', 0, 0),
    ('EXPERT_TITLE', 'ATTENDING_PHYSICIAN', '主治医师', 30, 'ENABLED', '默认种子', 0, 0),
    ('EXPERT_HOSPITAL', 'PUMCH', '北京协和医院', 10, 'ENABLED', '默认种子', 0, 0),
    ('EXPERT_HOSPITAL', 'PKUH', '北京大学第一医院', 20, 'ENABLED', '默认种子', 0, 0)
ON DUPLICATE KEY UPDATE
    dict_name = VALUES(dict_name),
    sort_no = VALUES(sort_no),
    status = VALUES(status),
    remark = VALUES(remark),
    updated_at = CURRENT_TIMESTAMP;
