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

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

-- ----------------------------
-- 第二步：删除旧字段
-- ----------------------------
alter table biz_contract drop column if exists delivery_dept_id;
alter table biz_contract drop column if exists sales_user_id;
alter table biz_contract drop column if exists accounting_type;
alter table biz_contract drop column if exists customer_name;
alter table biz_contract drop column if exists signing_entity;
alter table biz_contract drop column if exists target_profit_rate;
alter table biz_contract drop column if exists business_tags;
alter table biz_contract drop column if exists paid_amount;
alter table biz_contract drop column if exists unpaid_amount;
alter table biz_contract drop column if exists remark;

-- ----------------------------
-- 第三步：添加新字段
-- ----------------------------
alter table biz_contract add column if not exists party_a_name varchar(200) not null default '';
alter table biz_contract add column if not exists party_b_name varchar(200) not null default '';

-- ----------------------------
-- 第四步：修改字段注释
-- ----------------------------
comment on column biz_contract.contract_type is '合同类型(1人月合同 2实施合同 3销售合同)';
comment on column biz_contract.start_date is '合同开始日期';
comment on column biz_contract.end_date is '合同结束日期';
comment on column biz_contract.contract_amount is '合同金额';
comment on column biz_contract.attachment_oss_id is '附件ossId';
comment on column biz_contract.party_a_name is '甲方名称';
comment on column biz_contract.party_b_name is '乙方名称';

-- ----------------------------
-- 第五步：添加转包相关字段
-- ----------------------------
alter table biz_contract add column if not exists is_subcontract varchar(1) default '0';
alter table biz_contract add column if not exists subcontract_from_id bigint(20) default null;

comment on column biz_contract.is_subcontract is '是否转包(0否 1是)';
comment on column biz_contract.subcontract_from_id is '转包来源合同ID';

-- 添加外键约束（可选）
-- alter table biz_contract add constraint fk_contract_subcontract_from foreign key (subcontract_from_id) references biz_contract(contract_id);
