-- ----------------------------
-- 候选人表结构更新脚本 (Oracle)
-- 说明：此脚本用于为候选人表添加缺失字段
-- 请根据实际情况谨慎执行，建议先备份数据
-- ----------------------------

-- ----------------------------
-- 第一步：备份原有数据（可选，建议执行）
-- ----------------------------
-- create table biz_candidate_backup as select * from biz_candidate;

-- ----------------------------
-- 第二步：添加缺失字段
-- ----------------------------
declare
  v_column_exists number;
begin
  -- 沟通日期
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'COMMUNICATION_DATE';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add COMMUNICATION_DATE date';
    execute immediate 'comment on column BIZ_CANDIDATE.COMMUNICATION_DATE is ''沟通日期''';
  end if;

  -- 项目组
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'PROJECT_GROUP';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add PROJECT_GROUP varchar2(100)';
    execute immediate 'comment on column BIZ_CANDIDATE.PROJECT_GROUP is ''项目组''';
  end if;

  -- 统招是否
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'IS_UNIFIED_RECRUITMENT';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add IS_UNIFIED_RECRUITMENT char(1)';
    execute immediate 'comment on column BIZ_CANDIDATE.IS_UNIFIED_RECRUITMENT is ''统招是否(0否 1是)''';
  end if;

  -- 专业
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'MAJOR';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add MAJOR varchar2(100)';
    execute immediate 'comment on column BIZ_CANDIDATE.MAJOR is ''专业''';
  end if;

  -- 是否愿意外派
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'WILLING_TO_OUTSOURCE';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add WILLING_TO_OUTSOURCE char(1)';
    execute immediate 'comment on column BIZ_CANDIDATE.WILLING_TO_OUTSOURCE is ''是否愿意外派(0否 1是)''';
  end if;

  -- 是否在职
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'IS_EMPLOYED';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add IS_EMPLOYED char(1)';
    execute immediate 'comment on column BIZ_CANDIDATE.IS_EMPLOYED is ''是否在职(0否 1是)''';
  end if;

  -- 离职周期多久
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'RESIGNATION_CYCLE';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add RESIGNATION_CYCLE varchar2(50)';
    execute immediate 'comment on column BIZ_CANDIDATE.RESIGNATION_CYCLE is ''离职周期多久''';
  end if;

  -- 什么时候方便面试
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'INTERVIEW_AVAILABILITY';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add INTERVIEW_AVAILABILITY varchar2(200)';
    execute immediate 'comment on column BIZ_CANDIDATE.INTERVIEW_AVAILABILITY is ''什么时候方便面试''';
  end if;

  -- 候选人住哪里
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'RESIDENCE_LOCATION';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add RESIDENCE_LOCATION varchar2(200)';
    execute immediate 'comment on column BIZ_CANDIDATE.RESIDENCE_LOCATION is ''候选人住哪里''';
  end if;

  -- 是否加微信
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_CANDIDATE'
    and column_name = 'WECHAT_ADDED';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_CANDIDATE add WECHAT_ADDED char(1)';
    execute immediate 'comment on column BIZ_CANDIDATE.WECHAT_ADDED is ''是否加微信(0否 1是)''';
  end if;

  -- 添加唯一约束
  -- 删除可能存在的旧索引
  execute immediate 'begin
    execute immediate ''drop index BIZ_CANDIDATE.UK_NAME'';
  exception
    when others then
      if sqlcode != -1418 then
        raise;
      end if;
  end;';

  execute immediate 'begin
    execute immediate ''drop index BIZ_CANDIDATE.UK_PHONE_NUMBER'';
  exception
    when others then
      if sqlcode != -1418 then
        raise;
      end if;
  end;';

  execute immediate 'begin
    execute immediate ''drop index BIZ_CANDIDATE.UK_NAME_PHONE'';
  exception
    when others then
      if sqlcode != -1418 then
        raise;
      end if;
  end;';

  -- 创建唯一索引：姓名唯一
  execute immediate 'create unique index UK_NAME on BIZ_CANDIDATE(NAME)';

  -- 创建唯一索引：手机号码唯一
  execute immediate 'create unique index UK_PHONE_NUMBER on BIZ_CANDIDATE(PHONE_NUMBER)';
end;
/
