ALTER TABLE `enterprise`
ADD COLUMN `tax_rebate_agreement_sign_status` varchar(4) NULL COMMENT '退税服务协议签署状态，枚举：sys_sign_status' AFTER `tax_authority_area`;

ALTER TABLE `enterprise`
ADD COLUMN `tax_rebate_agreement_sign_date` datetime NULL COMMENT '退税服务协议签署日期' AFTER `tax_rebate_agreement_sign_status`;

ALTER TABLE `enterprise_change_record`
ADD COLUMN `tax_rebate_agreement_sign_status` varchar(4) NULL COMMENT '退税服务协议签署状态，枚举：sys_sign_status' AFTER `authorization_file`;

ALTER TABLE `enterprise_change_record`
ADD COLUMN `tax_rebate_agreement_sign_date` datetime(0) NULL COMMENT '退税服务协议签署日期' AFTER `tax_rebate_agreement_sign_status`;

ALTER TABLE `enterprise`
ADD COLUMN `tax_rebate_effective_start` datetime NULL COMMENT '退税服务协议生效时间（含）' AFTER `tax_rebate_agreement_sign_date`,
ADD COLUMN `tax_rebate_effective_end` datetime NULL COMMENT '退税服务协议失效时间（含）' AFTER `tax_rebate_effective_start`;

ALTER TABLE `enterprise_change_record`
MODIFY COLUMN `tax_authority_area` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '主管税务机关，枚举：tax_authority_area' AFTER `tax_rebate_agreement_sign_status`,
ADD COLUMN `tax_rebate_effective_end` datetime(0) NULL COMMENT '退税服务协议失效时间（含）' AFTER `tax_rebate_agreement_sign_date`,
ADD COLUMN `tax_rebate_effective_start` datetime(0) NULL COMMENT '退税服务协议生效时间（含）' AFTER `tax_rebate_effective_end`;

CREATE TABLE `tax_rebate_export_details`
(
    `id`                                 bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `customer_user_id`                   bigint(20)          NOT NULL COMMENT '客户id',
    `agency_cert_no`                     varchar(32)                                           DEFAULT NULL COMMENT '代理公司统一社会信用代码/注册号',
    `importer_cert_no`                   varchar(32)                                           DEFAULT NULL COMMENT '进口商统一社会信用代码/注册号',
    `sales_order_no`                     varchar(64)                                           DEFAULT NULL COMMENT '销售订单号',
    `sales_contract_id`                  bigint(20)                                            DEFAULT NULL COMMENT '销售合同id',
    `sales_contract_no`                  varchar(64)                                           DEFAULT NULL COMMENT '销售合同编号',
    `declaration_month_year`             varchar(8)                                            DEFAULT NULL COMMENT '申报年月（所属期）',
    `declaration_batch`                  varchar(16)                                           DEFAULT '0' COMMENT '申报批次',
    `declaration_serial_no`              varchar(16)                                           DEFAULT NULL COMMENT '申报序号',
    `correlation_no`                     varchar(32)                                           DEFAULT NULL COMMENT '关联号',
    `customs_declaration_no`             varchar(32)                                           DEFAULT NULL COMMENT '海关报关单号（18位）',
    `export_customs_declaration_no`      varchar(32)                                           DEFAULT NULL COMMENT '出口货物报关单号（21位）',
    `agency_export_goods_certificate_no` varchar(64)                                           DEFAULT NULL COMMENT '代理出口货物证明号',
    `proforma_invoice_no`                varchar(64)                                           DEFAULT NULL COMMENT '出口发票号（形式发票号）',
    `tax_refund_amount`                  decimal(17, 2)                                        DEFAULT NULL COMMENT '退税额（生产企业：免抵退税额）',
    `export_date`                        datetime                                              DEFAULT NULL COMMENT '出口日期',
    `hs_code`                            varchar(18)                                           DEFAULT NULL COMMENT '商品HS编码',
    `export_commodity_code`              varchar(32)                                           DEFAULT NULL COMMENT '出口商品代码',
    `export_commodity_name`              varchar(255)                                          DEFAULT NULL COMMENT '出口商品名称',
    `measurement_unit`                   varchar(64)                                           DEFAULT NULL COMMENT '计量单位',
    `declaration_commodity_code`         varchar(32)                                           DEFAULT NULL COMMENT '申报商品代码',
    `declaration_commodity_name`         varchar(300)                                          DEFAULT NULL COMMENT '申报商品名称',
    `export_quantity`                    decimal(17, 6)                                        DEFAULT NULL COMMENT '出口数量',
    `fob_price_usd`                      decimal(17, 2)                                        DEFAULT NULL COMMENT '美元离岸价',
    `tax_refund_exemption_type`          varchar(16)                                           DEFAULT NULL COMMENT '退（免）税业务类型',
    `tax_refund_exemption_type_name`     varchar(255)                                          DEFAULT NULL COMMENT '退（免）税业务类型名称',
    `domestic_shipping_method`           varchar(255)                                          DEFAULT NULL COMMENT '国内启运方式',
    `domestic_shipping_name`             varchar(255)                                          DEFAULT NULL COMMENT '国内启运方式名称',
    `cga_certified_enterprise_type`      varchar(255)                                          DEFAULT NULL COMMENT '海关总署认证企业类型',
    `cga_certified_enterprise_type_name` varchar(255)                                          DEFAULT NULL COMMENT '海关总署认证企业类型名称',
    `mark`                               varchar(512)                                          DEFAULT NULL COMMENT '备注',
    `audit_status`                       varchar(64)                                           DEFAULT NULL COMMENT '审核标志',
    `not_process_flag`                   varchar(6)                                            DEFAULT NULL COMMENT '不予办理标志',
    `not_tax_refund`                     varchar(6)                                            DEFAULT NULL COMMENT '不予退税标志',
    `deferred_process_flag`              varchar(16)                                           DEFAULT NULL COMMENT '暂不办理标志',
    `not_process_month_year`             varchar(16)                                           DEFAULT NULL COMMENT '不予办理年月',
    `not_refund_month_year`              varchar(16)                                           DEFAULT NULL COMMENT '不予退税年月',
    `suspension_month_year`              varchar(16)                                           DEFAULT NULL COMMENT '暂不办理年月',
    `lifting_suspension_approval_date`   varchar(16)                                           DEFAULT NULL COMMENT '最终解除暂不办理准予年月',
    `missing_docs_flag`                  varchar(16)                                           DEFAULT NULL COMMENT '单证不齐标志',
    `status`                             varchar(4)                                            DEFAULT NULL COMMENT '退税状态，枚举rebate_process_status',
    `payment_status`                     varchar(2)                                            DEFAULT NULL COMMENT '退税到账状态，枚举rebate_payment_status',
    `payment_date`                       datetime                                              DEFAULT NULL COMMENT '退税到账日期',
    `deleted`                            tinyint(1)                                            DEFAULT '0' COMMENT '删除标识',
    `create_by`                          varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '创建者',
    `update_by`                          varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '更新者',
    `gmt_create`                         timestamp           NOT NULL                          DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`                       timestamp           NOT NULL                          DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    KEY `sales_order_no_index` (`correlation_no`, `customer_user_id`, `sales_order_no`,
                                `declaration_commodity_code`) USING BTREE,
    KEY `declaration_idx` (`declaration_month_year`, `declaration_batch`) USING BTREE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='出口退税-出口明细表';

CREATE TABLE `tax_rebate_purchase_details`
(
    `id`                               bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `rebate_export_details_id`         bigint(20)          NOT NULL COMMENT '出口退税-出口明细表id',
    `customer_user_id`                 bigint(20)          NOT NULL COMMENT '客户id',
    `agency_cert_no`                   varchar(32)                                           DEFAULT NULL COMMENT '代理公司统一社会信用代码/注册号',
    `declaration_month_year`           varchar(8)                                            DEFAULT NULL COMMENT '申报年月',
    `declaration_batch`                varchar(16)                                           DEFAULT '0' COMMENT '申报批次',
    `declaration_serial_no`            varchar(16)                                           DEFAULT NULL COMMENT '申报序号',
    `correlation_no`                   varchar(32)                                           DEFAULT NULL COMMENT '关联号',
    `voucher_type`                     varchar(64)                                           DEFAULT NULL COMMENT '凭证种类',
    `voucher_type_name`                varchar(64)                                           DEFAULT NULL COMMENT '凭证种类名称',
    `purchase_voucher_no`              varchar(64)                                           DEFAULT NULL COMMENT '进货凭证号',
    `invoice_date`                     datetime                                              DEFAULT NULL COMMENT '开票日期',
    `vendor_tax_no`                    varchar(32)                                           DEFAULT NULL COMMENT '供货方纳税人识别号',
    `hs_code`                          varchar(18)                                           DEFAULT NULL COMMENT '商品HS编码',
    `export_commodity_code`            varchar(32)                                           DEFAULT NULL COMMENT '出口商品代码',
    `export_commodity_name`            varchar(300)                                          DEFAULT NULL COMMENT '商品名称',
    `measurement_unit`                 varchar(64)                                           DEFAULT NULL COMMENT '计量单位',
    `export_quantity`                  decimal(17, 6)                                        DEFAULT NULL COMMENT '数量',
    `tax_base_amount`                  decimal(17, 2)                                        DEFAULT NULL COMMENT '计税金额',
    `added_tax_rate`                   decimal(4, 2)                                         DEFAULT NULL COMMENT '征税率（百分制，即已乘100）',
    `export_tax_rebate_rate`           decimal(4, 2)                                         DEFAULT NULL COMMENT '退税率（百分制，即已乘100）',
    `tax_refundable_amount`            decimal(17, 2)                                        DEFAULT NULL COMMENT '可退税额',
    `audit_status`                     varchar(64)                                           DEFAULT NULL COMMENT '审核标志',
    `tax_refund_amount`                decimal(17, 2)                                        DEFAULT NULL COMMENT '退税额（根据出口明细退税额，按比例计算得）',
    `mark`                             varchar(512)                                          DEFAULT NULL COMMENT '备注',
    `not_process_flag`                 varchar(6)                                            DEFAULT NULL COMMENT '不予办理标志',
    `not_tax_refund`                   varchar(6)                                            DEFAULT NULL COMMENT '不予退税标志',
    `deferred_process_flag`            varchar(16)                                           DEFAULT NULL COMMENT '暂不办理标志',
    `not_process_month_year`           varchar(16)                                           DEFAULT NULL COMMENT '不予办理年月',
    `not_refund_month_year`            varchar(16)                                           DEFAULT NULL COMMENT '不予退税年月',
    `suspension_month_year`            varchar(16)                                           DEFAULT NULL COMMENT '暂不办理年月',
    `lifting_suspension_approval_date` varchar(16)                                           DEFAULT NULL COMMENT '最终解除暂不办理准予年月',
    `missing_docs_flag`                varchar(16)                                           DEFAULT NULL COMMENT '单证不齐标志',
    `status`                           varchar(4)                                            DEFAULT NULL COMMENT '退税状态，枚举rebate_process_status',
    `deleted`                          tinyint(1)                                            DEFAULT '0' COMMENT '删除标识',
    `create_by`                        varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '创建者',
    `update_by`                        varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '更新者',
    `gmt_create`                       timestamp           NOT NULL                          DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`                     timestamp           NOT NULL                          DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    KEY `rebate_export_details_id_idx` (`rebate_export_details_id`) USING BTREE,
    KEY `declaration_idx` (`declaration_month_year`, `declaration_batch`) USING BTREE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4 COMMENT ='出口退税-进货明细表';

INSERT INTO `sys_dict_type` ( `dict_name`, `dict_type`, `status`, `create_by`, `update_by`, `remark`)
VALUES ('退税状态', 'rebate_process_status', '0', 'ADMIN', '', '退税处理状态');

INSERT INTO `sys_dict_data` (`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `update_by`, `remark`)
VALUES (0, '未退税', '0', 'rebate_process_status', NULL, 'default', 'Y', '0', 'admin', '', NULL);
INSERT INTO `sys_dict_data` (`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `update_by`, `remark`)
VALUES (1, '已退税', '1', 'rebate_process_status', NULL, 'default', 'N', '0', 'admin', '', NULL);

INSERT INTO `sys_dict_type` (`dict_name`, `dict_type`, `status`, `create_by`, `update_by`, `remark`)
VALUES ('退税到账状态', 'rebate_payment_status', '0', 'admin', '', NULL );

INSERT INTO `sys_dict_data` ( `dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `update_by`, `remark`)
VALUES (0, '未到账', '0', 'rebate_payment_status', NULL, 'default', 'N', '0', 'admin', '', NULL);
INSERT INTO `sys_dict_data` ( `dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `update_by`, `remark`)
VALUES (1, '审核中', '1', 'rebate_payment_status', NULL, 'default', 'N', '0', 'admin', '', NULL);
INSERT INTO `sys_dict_data` ( `dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `update_by`, `remark`)
VALUES (2, '已到账', '2', 'rebate_payment_status', NULL, 'default', 'N', '0', 'admin', '', NULL);

ALTER TABLE `customs_declaration_goods`
ADD COLUMN `tsjltbbs` tinyint(4) NULL DEFAULT 0 COMMENT '退税记录是否已同步' AFTER `sbsl1`;

ALTER TABLE `sales_contract`
ADD COLUMN `has_purchase_contract` tinyint(1) NULL DEFAULT 1 COMMENT '是否有对应采购合同（“自营出口”的“生产企业”,不生成采购合同）' AFTER `audit_status`;

CREATE TABLE `enterprise_change_record_i18n`
(
    `id`               bigint(20)                                                    NOT NULL,
    `language`         varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci   NOT NULL,
    `name`    varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    `register_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    `business_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    PRIMARY KEY (`id`, `language`) USING BTREE,
    UNIQUE INDEX `id_idx` (`id`, `language`) USING BTREE
) ENGINE = InnoDB
  CHARACTER SET = utf8mb4
  COLLATE = utf8mb4_general_ci
  ROW_FORMAT = Dynamic;

CREATE TABLE `enterprise_i18n`
(
    `id`               bigint(20)                                                    NOT NULL,
    `language`         varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
    `name`             varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    `register_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    `business_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    PRIMARY KEY (`id`, `language`) USING BTREE,
    UNIQUE INDEX `id_idx` (`id`, `language`) USING BTREE
) ENGINE = InnoDB
  CHARACTER SET = utf8mb4
  COLLATE = utf8mb4_general_ci
  ROW_FORMAT = Dynamic;

CREATE TABLE `trading_platform_i18n`
(
    `id`               bigint(20)                                                    NOT NULL,
    `language`         varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci   NOT NULL,
    `platform_name`    varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    `register_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    `business_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
    `contact_name`     varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  NULL DEFAULT NULL,
    PRIMARY KEY (`id`, `language`) USING BTREE,
    UNIQUE INDEX `id_idx` (`id`, `language`) USING BTREE
) ENGINE = InnoDB
  CHARACTER SET = utf8mb4
  COLLATE = utf8mb4_general_ci
  ROW_FORMAT = Dynamic;
