-- ----------------------------
-- 合同表结构更新脚本 (SQL Server)
-- 说明：此脚本用于将旧的合同表结构更新为新的结构
-- 请根据实际情况谨慎执行，建议先备份数据
-- ----------------------------

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

-- ----------------------------
-- 第二步：添加新字段（在删除旧字段之前）
-- SQL Server 不支持 if not exists，需要先检查列是否存在
-- ----------------------------
if not exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'party_a_name')
begin
    alter table biz_contract add party_a_name varchar(200) null;
end

if not exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'party_b_name')
begin
    alter table biz_contract add party_b_name varchar(200) null;
end

-- ----------------------------
-- 第三步：迁移数据（如果需要）
-- 如果需要从旧字段迁移数据到新字段，在这里写UPDATE语句
-- ----------------------------
-- update biz_contract set party_a_name = customer_name where party_a_name is null;
-- update biz_contract set party_b_name = signing_entity where party_b_name is null;

-- ----------------------------
-- 第四步：设置新字段为非空
-- 注意：需要先确保所有行都有值
-- ----------------------------
-- alter table biz_contract alter column party_a_name varchar(200) not null;
-- alter table biz_contract alter column party_b_name varchar(200) not null;

-- ----------------------------
-- 第五步：删除旧字段
-- ----------------------------
if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'delivery_dept_id')
begin
    alter table biz_contract drop column delivery_dept_id;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'sales_user_id')
begin
    alter table biz_contract drop column sales_user_id;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'accounting_type')
begin
    alter table biz_contract drop column accounting_type;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'customer_name')
begin
    alter table biz_contract drop column customer_name;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'signing_entity')
begin
    alter table biz_contract drop column signing_entity;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'target_profit_rate')
begin
    alter table biz_contract drop column target_profit_rate;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'business_tags')
begin
    alter table biz_contract drop column business_tags;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'paid_amount')
begin
    alter table biz_contract drop column paid_amount;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'unpaid_amount')
begin
    alter table biz_contract drop column unpaid_amount;
end

if exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'remark')
begin
    alter table biz_contract drop column remark;
end

-- ----------------------------
-- 第六步：添加转包相关字段
-- ----------------------------
if not exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'is_subcontract')
begin
    alter table biz_contract add is_subcontract varchar(1) null default '0';
    execute sp_addextendedproperty 'MS_Description', '是否转包(0否 1是)', 'SCHEMA', 'dbo', 'TABLE', 'biz_contract', 'COLUMN', 'is_subcontract';
end
go

if not exists (select * from sys.columns where object_id = object_id('biz_contract') and name = 'subcontract_from_id')
begin
    alter table biz_contract add subcontract_from_id bigint null;
    execute sp_addextendedproperty 'MS_Description', '转包来源合同ID', 'SCHEMA', 'dbo', 'TABLE', 'biz_contract', 'COLUMN', 'subcontract_from_id';
end
go

-- 添加外键约束（可选）
-- if not exists (select * from sys.foreign_keys where name = 'fk_contract_subcontract_from')
-- begin
--     alter table biz_contract add constraint fk_contract_subcontract_from foreign key (subcontract_from_id) references biz_contract(contract_id);
-- end
-- go
