-- ----------------------------
-- 发票表结构更新脚本 (Oracle)
-- 说明：此脚本用于为发票表添加发票号码、结算关联和税率字段
-- 请根据实际情况谨慎执行，建议先备份数据
-- ----------------------------

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

-- ----------------------------
-- 第二步：添加发票号码字段
-- ----------------------------
declare
  v_column_exists number;
begin
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_INVOICE'
    and column_name = 'INVOICE_NO';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_INVOICE add INVOICE_NO varchar2(100)';
    execute immediate 'comment on column BIZ_INVOICE.INVOICE_NO is ''发票号码''';
  end if;
end;
/

-- ----------------------------
-- 第三步：添加结算关联字段
-- ----------------------------
declare
  v_column_exists number;
begin
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_INVOICE'
    and column_name = 'SETTLEMENT_ID';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_INVOICE add SETTLEMENT_ID number(20)';
    execute immediate 'comment on column BIZ_INVOICE.SETTLEMENT_ID is ''结算ID''';
  end if;
end;
/

-- ----------------------------
-- 第四步：添加税率字段
-- ----------------------------
declare
  v_column_exists number;
begin
  select count(*) into v_column_exists
  from user_tab_columns
  where table_name = 'BIZ_INVOICE'
    and column_name = 'TAX_RATE';

  if v_column_exists = 0 then
    execute immediate 'alter table BIZ_INVOICE add TAX_RATE varchar2(10)';
    execute immediate 'comment on column BIZ_INVOICE.TAX_RATE is ''税率(1:6%+2% 2:10%+2% 3:13%+2%)''';
  end if;
end;
/
