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

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

-- ----------------------------
-- 第二步：添加缺失字段
-- ----------------------------
-- 沟通日期
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'communication_date')
begin
    alter table biz_candidate add communication_date date null;
    execute sp_addextendedproperty 'MS_Description', '沟通日期', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'communication_date';
end
go

-- 项目组
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'project_group')
begin
    alter table biz_candidate add project_group varchar(100) null;
    execute sp_addextendedproperty 'MS_Description', '项目组', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'project_group';
end
go

-- 统招是否
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'is_unified_recruitment')
begin
    alter table biz_candidate add is_unified_recruitment char(1) null;
    execute sp_addextendedproperty 'MS_Description', '统招是否(0否 1是)', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'is_unified_recruitment';
end
go

-- 专业
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'major')
begin
    alter table biz_candidate add major varchar(100) null;
    execute sp_addextendedproperty 'MS_Description', '专业', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'major';
end
go

-- 是否愿意外派
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'willing_to_outsource')
begin
    alter table biz_candidate add willing_to_outsource char(1) null;
    execute sp_addextendedproperty 'MS_Description', '是否愿意外派(0否 1是)', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'willing_to_outsource';
end
go

-- 是否在职
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'is_employed')
begin
    alter table biz_candidate add is_employed char(1) null;
    execute sp_addextendedproperty 'MS_Description', '是否在职(0否 1是)', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'is_employed';
end
go

-- 离职周期多久
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'resignation_cycle')
begin
    alter table biz_candidate add resignation_cycle varchar(50) null;
    execute sp_addextendedproperty 'MS_Description', '离职周期多久', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'resignation_cycle';
end
go

-- 什么时候方便面试
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'interview_availability')
begin
    alter table biz_candidate add interview_availability varchar(200) null;
    execute sp_addextendedproperty 'MS_Description', '什么时候方便面试', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'interview_availability';
end
go

-- 候选人住哪里
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'residence_location')
begin
    alter table biz_candidate add residence_location varchar(200) null;
    execute sp_addextendedproperty 'MS_Description', '候选人住哪里', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'residence_location';
end
go

-- 是否加微信
if not exists (select * from sys.columns where object_id = object_id('biz_candidate') and name = 'wechat_added')
begin
    alter table biz_candidate add wechat_added char(1) null;
    execute sp_addextendedproperty 'MS_Description', '是否加微信(0否 1是)', 'SCHEMA', 'dbo', 'TABLE', 'biz_candidate', 'COLUMN', 'wechat_added';
end
go

-- ----------------------------
-- 第三步：添加唯一约束
-- ----------------------------
-- 删除可能存在的旧索引
if exists (select * from sys.indexes where name = 'uk_name' and object_id = object_id('biz_candidate'))
    drop index uk_name on biz_candidate;
if exists (select * from sys.indexes where name = 'uk_phone_number' and object_id = object_id('biz_candidate'))
    drop index uk_phone_number on biz_candidate;
if exists (select * from sys.indexes where name = 'uk_name_phone' and object_id = object_id('biz_candidate'))
    drop index uk_name_phone on biz_candidate;
go

-- 创建唯一索引：姓名唯一（过滤空值）
create unique nonclustered index uk_name on biz_candidate(name)
where name is not null;
go

-- 创建唯一索引：手机号码唯一（过滤空值）
create unique nonclustered index uk_phone_number on biz_candidate(phone_number)
where phone_number is not null;
go
