-- ----------------------------
-- 合同表添加合同状态字段 (SQL Server)
-- 说明：此脚本用于添加合同状态字段和相关字典数据
-- 请根据实际情况谨慎执行，建议先备份数据
-- ----------------------------

-- ----------------------------
-- 第一步：添加合同状态字段
-- ----------------------------
if not exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'contract_status')
begin
    alter table biz_contract add contract_status varchar(10) null default '0';
    execute sp_addextendedproperty 'MS_Description', '合同状态(0草稿 1生效中 2已完成 3已终止)', 'SCHEMA', 'dbo', 'TABLE', 'biz_contract', 'COLUMN', 'contract_status';
end
go

-- ----------------------------
-- 第二步：更新现有数据为生效中
-- ----------------------------
update biz_contract set contract_status = '1' where contract_status is null or contract_status = '';
go

-- ----------------------------
-- 第三步：添加合同状态字典类型
-- ----------------------------
if not exists (select * from sys_dict_type where dict_type = 'biz_contract_status')
begin
    insert into sys_dict_type (dict_id, tenant_id, dict_name, dict_type, status, del_flag, create_dept, create_time, remark)
    values (110, '000000', '合同状态', 'biz_contract_status', '0', '0', 103, getdate(), '合同状态列表');
end
go

-- ----------------------------
-- 第四步：添加合同状态字典数据
-- ----------------------------
if not exists (select * from sys_dict_data where dict_value = '0' and dict_type = 'biz_contract_status')
begin
    insert into sys_dict_data (dict_code, tenant_id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, del_flag, create_dept, create_time, remark)
    values (1100, '000000', 1, '草稿', '0', 'biz_contract_status', '', 'info', 'N', '0', '0', 103, getdate(), '草稿');
end
go

if not exists (select * from sys_dict_data where dict_value = '1' and dict_type = 'biz_contract_status')
begin
    insert into sys_dict_data (dict_code, tenant_id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, del_flag, create_dept, create_time, remark)
    values (1101, '000000', 2, '生效中', '1', 'biz_contract_status', '', 'primary', 'N', '0', '0', 103, getdate(), '生效中');
end
go

if not exists (select * from sys_dict_data where dict_value = '2' and dict_type = 'biz_contract_status')
begin
    insert into sys_dict_data (dict_code, tenant_id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, del_flag, create_dept, create_time, remark)
    values (1102, '000000', 3, '已完成', '2', 'biz_contract_status', '', 'success', 'N', '0', '0', 103, getdate(), '已完成');
end
go

if not exists (select * from sys_dict_data where dict_value = '3' and dict_type = 'biz_contract_status')
begin
    insert into sys_dict_data (dict_code, tenant_id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, del_flag, create_dept, create_time, remark)
    values (1103, '000000', 4, '已终止', '3', 'biz_contract_status', '', 'danger', 'N', '0', '0', 103, getdate(), '已终止');
end
go
