
DROP TABLE IF EXISTS  `refund_management`;
CREATE TABLE `refund_management`
(
    `id`                   bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `business_number`      varchar(250)       DEFAULT NULL COMMENT '业务编号',
    `contract_no`          varchar(50)        DEFAULT NULL COMMENT '对应SOP合同编号',
    `quota_disburse_batch` int(11) DEFAULT NULL COMMENT '额度支用批次',
    `trading_platform_id`  bigint(20) DEFAULT NULL COMMENT '贸易平台Id',
    `refund_status`        varchar(32)        DEFAULT NULL COMMENT '状态',
    `user_create`          varchar(32)        DEFAULT '' COMMENT '创建人',
    `user_modified`        varchar(32)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理表';

DROP TABLE IF EXISTS  `refund_details`;
CREATE TABLE `refund_details`
(
    `id`                           bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refund_management_id`         bigint(20) DEFAULT NULL COMMENT '退款管理表Id',
    `amount`                       decimal(17, 2)     DEFAULT NULL COMMENT '金额',
    `amount_type`                  varchar(32)        DEFAULT NULL COMMENT '金额类型',
    `selected`                     tinyint(1) DEFAULT NULL COMMENT '是否选中 0:未选中、1:选中',
    `status`                       tinyint(1) DEFAULT NULL COMMENT '状态 0:提交、1:通过、2:退回',
    `ledger_fund_goods_assign_id`  bigint(20) DEFAULT NULL COMMENT '分账管理-货款分配表id',
    `label`                        tinyint(1) DEFAULT NULL COMMENT '标签 0:下游回款、1:上游回购',
    `user_create`                  varchar(32)        DEFAULT '' COMMENT '创建人',
    `user_modified`                varchar(32)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='应退明细表';

DROP TABLE IF EXISTS  `refund_management_pay_coll_plan_factoring_ref`;
CREATE TABLE `refund_management_pay_coll_plan_factoring_ref`
(
    `id`                         bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `refund_management_id`       bigint(20) DEFAULT NULL COMMENT '退款管理表Id',
    `pay_coll_plan_factoring_id` bigint(20) DEFAULT NULL COMMENT '收放款计划(保理融资款)表id(选中)',
    `status`                     tinyint(1) DEFAULT NULL COMMENT '状态 0:提交、1:通过、2:退回',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-收放款计划和退款管理关联关系表';

DROP TABLE IF EXISTS  `refunds_apply_details`;
CREATE TABLE `refunds_apply_details`
(
    `id`                        bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refund_management_id`      bigint(20) DEFAULT NULL COMMENT '退款管理表Id',
    `oa_company_entity_info_id` bigint(20) DEFAULT NULL COMMENT '付款公司主体ID',
    `payment_matter`            varchar(200)       DEFAULT NULL COMMENT '付款事项（字典）ID',
    `project_type`              varchar(200)       DEFAULT NULL COMMENT '项目类型',
    `payment_reason`            varchar(500)       DEFAULT '' COMMENT '付款事由',
    `payment_amount_total`      decimal(17, 2)     DEFAULT NULL COMMENT '付款总金额',
    `amount_capitalization`     varchar(500)       DEFAULT '' COMMENT '金额大写',
    `currency`                  varchar(20)        DEFAULT NULL COMMENT '币种',
    `payment_method`            varchar(200)       DEFAULT NULL COMMENT '付款方式',
    `payment_date`              datetime           DEFAULT NULL COMMENT '支付日期',
    `payment_remark`            varchar(500)       DEFAULT '' COMMENT '付款备注',
    `other_remark`              varchar(500)       DEFAULT '' COMMENT '要求时效或付款行等',
    `is_relation_assets`        tinyint(1) DEFAULT NULL COMMENT '是否关联资产',
    `status`                    tinyint(1) DEFAULT NULL COMMENT '状态 0:提交、1:通过、2:退回',
    `user_create`               varchar(30)        DEFAULT '' COMMENT '创建人',
    `user_modified`             varchar(30)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-申请内容表';

DROP TABLE IF EXISTS  `refunds_apply_account_info`;
CREATE TABLE `refunds_apply_account_info`
(
    `id`                       bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refunds_apply_details_id` bigint(20) DEFAULT NULL COMMENT '退款管理-申请内容表id',
    `collection_account_name`  varchar(200)       DEFAULT '' COMMENT '收款账户名称',
    `collection_account`       varchar(200)       DEFAULT '' COMMENT '收款账号',
    `collection_bank_name`     varchar(200)       DEFAULT '' COMMENT '收款账户开户行',
    `status`                   tinyint(1) DEFAULT NULL COMMENT '状态 0:提交、1:通过、2:退回',
    `user_create`              varchar(30)        DEFAULT '' COMMENT '创建人',
    `user_modified`            varchar(30)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-收方账户信息表';

DROP TABLE IF EXISTS  `refund_management_his`;
CREATE TABLE `refund_management_his`
(
    `id`                   bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refund_management_id` bigint(20) DEFAULT NULL COMMENT '退款管理表Id',
    `business_number`      varchar(250)       DEFAULT NULL COMMENT '业务编号',
    `contract_no`          varchar(50)        DEFAULT NULL COMMENT '对应SOP合同编号',
    `quota_disburse_batch` int(11) DEFAULT NULL COMMENT '额度支用批次',
    `trading_platform_id`  bigint(20) DEFAULT NULL COMMENT '贸易平台Id',
    `refund_status`        varchar(32)        DEFAULT NULL COMMENT '状态',
    `user_create`          varchar(32)        DEFAULT '' COMMENT '创建人',
    `user_modified`        varchar(32)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理历史表';

DROP TABLE IF EXISTS  `refund_details_his`;
CREATE TABLE `refund_details_his`
(
    `id`                       bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refund_management_his_id` bigint(20) DEFAULT NULL COMMENT '退款管理历史表Id',
    `amount`                   decimal(17, 2)     DEFAULT NULL COMMENT '金额',
    `amount_type`              varchar(32)        DEFAULT NULL COMMENT '金额类型',
    `selected`                 tinyint(1) DEFAULT NULL COMMENT '是否选中 0:未选中、1:选中',
    `user_create`              varchar(32)        DEFAULT '' COMMENT '创建人',
    `user_modified`            varchar(32)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='应退明细历史表';

DROP TABLE IF EXISTS  `refund_management_pay_coll_plan_factoring_ref_his`;
CREATE TABLE `refund_management_pay_coll_plan_factoring_ref_his`
(
    `id`                         bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `refund_management_his_id`   bigint(20) DEFAULT NULL COMMENT '退款管理表历史Id',
    `pay_coll_plan_factoring_id` bigint(20) DEFAULT NULL COMMENT '收放款计划(保理融资款)表id(选中)',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-收放款计划和退款管理关联关系历史表';

DROP TABLE IF EXISTS  `refunds_apply_details_his`;
CREATE TABLE `refunds_apply_details_his`
(
    `id`                        bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refund_management_his_id`  bigint(20) DEFAULT NULL COMMENT '退款管理表历史Id',
    `oa_company_entity_info_id` bigint(20) DEFAULT NULL COMMENT '付款公司主体ID',
    `payment_matter`            varchar(200) DEFAULT NULL COMMENT '付款事项code',
    `project_type`              varchar(200)       DEFAULT NULL COMMENT '项目类型',
    `payment_reason`            varchar(500)       DEFAULT '' COMMENT '付款事由',
    `payment_amount_total`      decimal(17, 2)     DEFAULT NULL COMMENT '付款总金额',
    `amount_capitalization`     varchar(500)       DEFAULT '' COMMENT '金额大写',
    `currency`                  varchar(20)        DEFAULT NULL COMMENT '币种',
    `payment_method`            varchar(200)       DEFAULT NULL COMMENT '付款方式',
    `payment_date`              datetime           DEFAULT NULL COMMENT '支付日期',
    `payment_remark`            varchar(500)       DEFAULT '' COMMENT '付款备注',
    `other_remark`              varchar(500)       DEFAULT '' COMMENT '要求时效或付款行等',
    `user_create`               varchar(30)        DEFAULT '' COMMENT '创建人',
    `user_modified`             varchar(30)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-申请内容历史表';

DROP TABLE IF EXISTS  `refunds_apply_account_info_his`;
CREATE TABLE `refunds_apply_account_info_his`
(
    `id`                           bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refunds_apply_details_his_id` bigint(20) DEFAULT NULL COMMENT '退款管理-申请内容历史表id',
    `collection_account_name`      varchar(200)       DEFAULT '' COMMENT '收款账户名称',
    `collection_account`           varchar(200)       DEFAULT '' COMMENT '收款账号',
    `collection_bank_name`         varchar(200)       DEFAULT '' COMMENT '收款账户开户行',
    `user_create`                  varchar(30)        DEFAULT '' COMMENT '创建人',
    `user_modified`                varchar(30)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-收方账户信息历史表';

INSERT INTO `t_permission`(`type`, `cname`, `ename`, `gradation`, `moduleName`)
VALUES ('menu', '退款管理', 'RefundManagement', 9, NULL);
INSERT INTO `t_permission`(`parentId`, `type`, `cname`, `ename`, `gradation`, `moduleName`, `url`, `icon`)
VALUES ((SELECT t.id FROM t_permission t WHERE t.ename = 'RefundManagement'), 'button', '查看', 'viewRefundManagement', 0,
        '退款管理', NULL, NULL);
INSERT INTO `t_permission`(`parentId`, `type`, `cname`, `ename`, `gradation`, `moduleName`, `url`, `icon`)
VALUES ((SELECT t.id FROM t_permission t WHERE t.ename = 'RefundManagement'), 'button', '数据可见-查看全员', 'viewAllRefund', 1,
        '退款管理', NULL, NULL);

ALTER TABLE refund_management
    ADD `refund_number` varchar(250) DEFAULT NULL COMMENT '退款编号';
ALTER TABLE refund_management_his
    ADD `refund_number` varchar(250) DEFAULT NULL COMMENT '退款编号';

ALTER TABLE refund_details
    ADD `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '分账管理-基础信息表id';
ALTER TABLE refund_details_his
    ADD `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '分账管理-基础信息表id';

-- 付款方式
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`)
VALUES ('OA票据支付-付款方式', NULL, 'oaBillPaymentType', NULL, 0);
INSERT INTO `t_dictionary` (NAME, parentId, CODE, VALUE, gradation)
VALUES ('转账', (SELECT t.id FROM t_dictionary t WHERE t.code = 'oaBillPaymentType'), 'oaBillTransferAccounts', NULL, 0);
INSERT INTO `t_dictionary` (NAME, parentId, CODE, VALUE, gradation)
VALUES ('票据', (SELECT t.id FROM t_dictionary t WHERE t.code = 'oaBillPaymentType'), 'oaBill', NULL, 1);
INSERT INTO `t_dictionary` (NAME, parentId, CODE, VALUE, gradation)
VALUES ('现金', (SELECT t.id FROM t_dictionary t WHERE t.code = 'oaBillPaymentType'), 'oaBillCash', NULL, 2);
INSERT INTO `t_dictionary` (NAME, parentId, CODE, VALUE, gradation)
VALUES ('其他', (SELECT t.id FROM t_dictionary t WHERE t.code = 'oaBillPaymentType'), 'oaBillOther', NULL, 3);

DROP TABLE IF EXISTS  `refund_management_approval`;
CREATE TABLE `refund_management_approval`
(
    `id`                   bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `refund_management_id` bigint(20) DEFAULT NULL COMMENT '退款管理表Id',
    `approval_conclusion`  varchar(50)        DEFAULT '' COMMENT '通过pass/退回back',
    `approval_comments`    varchar(3000)      DEFAULT '' COMMENT '审批意见',
    `user_create`          varchar(32)        DEFAULT '' COMMENT '创建人',
    `user_modified`        varchar(32)        DEFAULT '' 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`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理审批表';

DROP TABLE IF EXISTS  `refund_management_approval_apply_details_ref`;
CREATE TABLE `refund_management_approval_apply_details_ref`
(
    `id`                            bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `refund_management_approval_id` bigint(20) DEFAULT NULL COMMENT '退款管理审核表Id',
    `refunds_apply_details_id`      bigint(20) DEFAULT NULL COMMENT '退款管理-申请内容表Id',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-收放款计划和申请内容关联关系表';

DROP TABLE IF EXISTS  `refund_details_refunds_apply_details_ref`;
CREATE TABLE `refund_details_refunds_apply_details_ref`
(
    `id`                       bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `refund_details_id`        bigint(20) DEFAULT NULL COMMENT '退款管理-应退明细表Id',
    `refunds_apply_details_id` bigint(20) DEFAULT NULL COMMENT '退款管理-申请内容表Id',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-应退明细和申请内容关联关系表';

DROP TABLE IF EXISTS  `refunds_apply_details_pay_coll_plan_ref`;
CREATE TABLE `refunds_apply_details_pay_coll_plan_ref`
(
    `id`                       bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `pay_coll_plan_factoring_id` bigint(20) DEFAULT NULL COMMENT '收放款计划(保理融资款)表id',
    `refunds_apply_details_id` bigint(20) DEFAULT NULL COMMENT '退款管理-申请内容表Id',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='退款管理-申请内容和计划关联关系表';

ALTER TABLE refund_details_his
    ADD `ledger_fund_goods_assign_id` bigint(20) DEFAULT NULL COMMENT '分账管理-货款分配表id';

INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('履约保证金', (select id from t_dictionary p where p.code = 'cashFlowOutType'),
        'performanceBond', NULL, 9, NULL, NULL);

INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('质保金',
        (select id from t_dictionary p where p.code = 'cashFlowOutType'), 'retentionMoney', NULL,
        10, NULL, NULL);

ALTER TABLE refund_details_his
    ADD `label` tinyint(1) DEFAULT NULL COMMENT '标签 0:下游回款、1:上游回购';

INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`)
VALUES ('退款管理', 252, 'RefundManagement', NULL, 22);

ALTER TABLE refunds_apply_details
    ADD `apply_type` tinyint(1) DEFAULT NULL COMMENT '申请类型 0:抵扣、1:退款';
ALTER TABLE refunds_apply_details_his
    ADD `apply_type` tinyint(1) DEFAULT NULL COMMENT '申请类型 0:抵扣、1:退款';

ALTER TABLE refunds_apply_details
    ADD `user_id` bigint(20) DEFAULT NULL COMMENT '用户id';
ALTER TABLE refunds_apply_details_his
    ADD `user_id` bigint(20) DEFAULT NULL COMMENT '用户id';

ALTER TABLE refund_management ADD `stage` varchar(3000) DEFAULT '' COMMENT '当前阶段';

ALTER TABLE refund_management ADD `ledger_fund_goods_assign_id` bigint(20) DEFAULT NULL COMMENT '分账管理-货款分配表Id';
ALTER TABLE refund_management_his ADD `ledger_fund_goods_assign_id` bigint(20) DEFAULT NULL COMMENT '分账管理-货款分配表Id';

DROP TABLE IF EXISTS  `cash_flow_manage_basic_pay_coll_plan_ref`;
CREATE TABLE `cash_flow_manage_basic_pay_coll_plan_ref` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `pay_coll_plan_factoring_id` bigint(20) DEFAULT NULL COMMENT '收放款计划(保理融资款)表id',
  `cash_flow_manage_basic_info_id` bigint(20) DEFAULT NULL COMMENT '退款管理-申请内容表Id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='现金流关联-出账信息和计划关联关系表';