-- c端文件表
DROP TABLE IF EXISTS `c_sys_file`;
CREATE TABLE `c_sys_file` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        `storage_type` varchar(50)  DEFAULT NULL COMMENT 'disk/oss',
        `original_name` varchar(255) DEFAULT NULL COMMENT '文件原始名称',
        `path` varchar(255) DEFAULT NULL COMMENT '访问路径',
        `absolute_path` varchar(255) DEFAULT NULL COMMENT '绝对路径',
        `type` varchar(255) DEFAULT NULL COMMENT '文件类型',
        `size` bigint DEFAULT NULL COMMENT '大小(kb)',
        `business_id` bigint DEFAULT NULL COMMENT '绑定的业务id',
        `business_type` varchar(255) 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 DEFAULT CHARSET=utf8 COMMENT='c端文件表';

-- c端用户表
DROP TABLE IF EXISTS `c_sys_user`;
CREATE TABLE `c_sys_user` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `login_name` varchar(50)  DEFAULT NULL COMMENT '登录名',
        `user_name` varchar(64)  DEFAULT NULL COMMENT '用户名',
        `password` varchar(50)  DEFAULT NULL COMMENT '密码',
        `mobile` varchar(20)  DEFAULT NULL COMMENT '手机号',
        `email` varchar(50)  DEFAULT NULL COMMENT '企业邮箱',
        `head_img_url` varchar(500)  DEFAULT NULL COMMENT '头像图片链接',
        `enterprise_address` varchar(50) DEFAULT NULL COMMENT '企业地址',
        `province_id` bigint(20) DEFAULT NULL COMMENT '省份id',
        `city_id` bigint(20) DEFAULT NULL COMMENT '城市id',
        `referral_code` varchar(50) DEFAULT NULL COMMENT '推荐码',
        `project_manager` bigint(20) DEFAULT NULL COMMENT '项目经理id',
        `risk_manager` bigint(20) DEFAULT NULL COMMENT '风控经理id',
        `org_id` bigint(20) DEFAULT NULL,
        `user_create` varchar(50) DEFAULT NULL COMMENT '创建人',
        `user_modified` varchar(50) 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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='c端用户表';

-- 融资申请-基础信息表
DROP TABLE IF EXISTS `c_financing_basic_info`;
CREATE TABLE `c_financing_basic_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `sys_user_id` bigint(20) NOT NULL COMMENT '用户Id',
        `project_name` varchar(50) DEFAULT NULL COMMENT '项目名称',
        `business_number` varchar(50) DEFAULT NULL COMMENT '业务编号',
        `supplier_id` bigint(20) DEFAULT NULL COMMENT '供应商Id',
        `enterprise_customer_info_id` bigint(20) DEFAULT NULL COMMENT '核心企业Id',
        `trading_platform_id` bigint(20) DEFAULT NULL COMMENT '贸易平台Id',
        `contract_name` varchar(50) DEFAULT NULL COMMENT '合同名称（采购）',
        `contract_number` varchar(50) DEFAULT NULL COMMENT '合同编号（采购）',
        `financing_amount` decimal(17,4) DEFAULT NULL COMMENT '融资金额',
        `financing_status` varchar(50) DEFAULT NULL COMMENT '融资状态: 处理中、放款执行中、还款中、已完成、已终止、已逾期',
        `current_stage`  varchar(50) DEFAULT NULL COMMENT '当前阶段',
        `sop_accounts_receivable_id` bigint(20) DEFAULT NULL COMMENT 'sop底层应收账款Id',
        `visible` tinyint(1) DEFAULT NULL COMMENT '提交/暂存',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-基础信息表';

-- 融资申请-货物信息表
DROP TABLE IF EXISTS `c_financing_goods_info`;
CREATE TABLE `c_financing_goods_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `c_financing_basic_info_id` bigint(20) NOT NULL COMMENT '融资申请基础信息表ID',
        `acceptance_basic_info_id` bigint(20) DEFAULT NULL COMMENT '验收单基础信息表ID',
        `acceptance_goods_info_id` bigint(20) DEFAULT NULL COMMENT '验收单货物信息表ID',
        `acceptance_goods_number` varchar(50) DEFAULT '' COMMENT '验收单编号',
        `sales_price` decimal(17,4) DEFAULT NULL COMMENT '销售单价',
        `sales_total_price` decimal(17,4) DEFAULT NULL COMMENT '销售总价',
        `sales_guarantee_money` decimal(17,4) DEFAULT NULL COMMENT '销售质保金',
        `purchase_price` decimal(17,4) DEFAULT NULL COMMENT '采购单价',
        `purchase_total_price` decimal(17,4) DEFAULT NULL COMMENT '采购总价',
        `purchase_guarantee_money` decimal(17,4) DEFAULT NULL COMMENT '采购质保金',
        `reference_price` decimal(17, 4) DEFAULT NULL COMMENT '参考价格',
        `floating_price` decimal(17, 4) DEFAULT NULL COMMENT '浮动价格',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-货物信息表';

-- 融资申请-底层信息表
DROP TABLE IF EXISTS `c_financing_accounts_receivable_info`;
CREATE TABLE `c_financing_accounts_receivable_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `c_financing_basic_info_id` bigint(20) NOT NULL COMMENT '融资申请基础信息表ID',
        `acceptance_basic_info_id` bigint(20) DEFAULT NULL COMMENT '验收单基础信息表ID',
        `acceptance_goods_info_id` bigint(20) DEFAULT NULL COMMENT '验收单货物信息表ID',
        `acceptance_goods_number` varchar(50) DEFAULT '' COMMENT '验收单编号',
        `expected_settlement_date` datetime DEFAULT NULL COMMENT '预计结算日期 ',
        `acceptance_date` datetime DEFAULT NULL COMMENT '验收日期',
        `goods_name` varchar(250) DEFAULT NULL COMMENT '货物名称',
        `goods_factory` varchar(250) DEFAULT NULL COMMENT '厂家',
        `goods_type` varchar(250) DEFAULT NULL COMMENT '型号',
        `goods_unit` varchar(250) DEFAULT NULL COMMENT '单位',
        `goods_quantity` decimal(17,4) DEFAULT NULL COMMENT '数量',
        `accounts_receivable_amount` decimal(17,4) DEFAULT NULL COMMENT '应收账款金额',
        `net_accounts_receivable_amount` decimal(17,4) DEFAULT NULL COMMENT '应收账款净额',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-底层信息表';

-- 融资申请-申请人信息表
DROP TABLE IF EXISTS `c_financing_applicant_info`;
CREATE TABLE `c_financing_applicant_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `c_financing_basic_info_id` bigint(20) NOT NULL COMMENT '融资申请基础信息表ID',
        `business_number` varchar(50) NOT NULL COMMENT '业务编号',
        `supplier_id` bigint(20) DEFAULT NULL COMMENT '供应商ID',
        `applicant_contact` varchar(50) DEFAULT NULL COMMENT '保理申请人联系人',
        `applicant_contact_number` varchar(50) DEFAULT NULL COMMENT '保理申请人联系电话',
        `applicant_contact_email` varchar(50) DEFAULT NULL COMMENT '保理申请人联系人邮箱',
        `applicant_designated_account_name` varchar(50) DEFAULT NULL COMMENT '保理申请人指定账户户名',
        `applicant_designated_account` varchar(50) DEFAULT NULL COMMENT '保理申请人指定账户',
        `applicant_designated_bank` varchar(50) DEFAULT NULL COMMENT '保理申请人指定账户',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-申请人信息表';

-- 融资申请-历史信息表
DROP TABLE IF EXISTS `c_financing_his_info`;
CREATE TABLE `c_financing_his_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `c_financing_basic_info_id` bigint(20) NOT NULL COMMENT '融资申请基础信息Id',
        `sys_user_id` bigint(20) NOT NULL COMMENT '用户Id',
        `project_name` varchar(50) DEFAULT NULL COMMENT '项目名称',
        `business_number` varchar(50) DEFAULT NULL COMMENT '业务编号',
        `supplier_id` bigint(20) DEFAULT NULL COMMENT '供应商Id',
        `enterprise_customer_info_id` bigint(20) DEFAULT NULL COMMENT '核心企业Id',
        `trading_platform_id` bigint(20) DEFAULT NULL COMMENT '贸易平台Id',
        `contract_name` varchar(50) DEFAULT NULL COMMENT '合同名称（采购）',
        `contract_number` varchar(50) DEFAULT NULL COMMENT '合同编号（采购）',
        `financing_amount` decimal(17,4) DEFAULT NULL COMMENT '融资金额',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-历史信息表';

-- 融资申请-货物历史信息表
DROP TABLE IF EXISTS `c_financing_his_goods_info`;
CREATE TABLE `c_financing_his_goods_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `c_financing_his_info_id` bigint(20) NOT NULL COMMENT '融资申请历史信息表ID',
        `acceptance_basic_info_id` bigint(20) DEFAULT NULL COMMENT '验收单基础信息表ID',
        `acceptance_goods_info_id` bigint(20) DEFAULT NULL COMMENT '验收单货物信息表ID',
        `acceptance_goods_number` varchar(50) DEFAULT '' COMMENT '验收单编号',
        `sales_price` decimal(17,4) DEFAULT NULL COMMENT '销售单价',
        `sales_total_price` decimal(17,4) DEFAULT NULL COMMENT '销售总价',
        `sales_guarantee_money` decimal(17,4) DEFAULT NULL COMMENT '销售质保金',
        `purchase_price` decimal(17,4) DEFAULT NULL COMMENT '采购单价',
        `purchase_total_price` decimal(17,4) DEFAULT NULL COMMENT '采购总价',
        `purchase_guarantee_money` decimal(17,4) DEFAULT NULL COMMENT '采购质保金',
        `reference_price` decimal(17, 4) DEFAULT NULL COMMENT '参考价格',
        `floating_price` decimal(17, 4) DEFAULT NULL COMMENT '浮动价格',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-货物历史信息表';

-- 融资申请-底层历史信息表
DROP TABLE IF EXISTS `c_financing_his_accounts_receivable_info`;
CREATE TABLE `c_financing_his_accounts_receivable_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `c_financing_his_info_id` bigint(20) NOT NULL COMMENT '融资申请历史信息表ID',
        `acceptance_basic_info_id` bigint(20) DEFAULT NULL COMMENT '验收单基础信息表ID',
        `acceptance_goods_info_id` bigint(20) DEFAULT NULL COMMENT '验收单货物信息表ID',
        `acceptance_goods_number` varchar(50) DEFAULT '' COMMENT '验收单编号',
        `expected_settlement_date` datetime DEFAULT NULL COMMENT '预计结算日期 ',
        `acceptance_date` datetime DEFAULT NULL COMMENT '验收日期',
        `goods_name` varchar(250) DEFAULT NULL COMMENT '货物名称',
        `goods_factory` varchar(250) DEFAULT NULL COMMENT '厂家',
        `goods_type` varchar(250) DEFAULT NULL COMMENT '型号',
        `goods_unit` varchar(250) DEFAULT NULL COMMENT '单位',
        `goods_quantity` decimal(17,4) DEFAULT NULL COMMENT '数量',
        `accounts_receivable_amount` decimal(17,4) DEFAULT NULL COMMENT '应收账款金额',
        `net_accounts_receivable_amount` decimal(17,4) DEFAULT NULL COMMENT '应收账款净额',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-底层历史信息表';

-- 融资申请-申请人历史信息表
DROP TABLE IF EXISTS `c_financing_his_applicant_info`;
CREATE TABLE `c_financing_his_applicant_info` (
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
        `c_financing_his_info_id` bigint(20) NOT NULL COMMENT '融资申请历史信息表ID',
        `business_number` varchar(50) NOT NULL COMMENT '业务编号',
        `supplier_id` bigint(20) DEFAULT NULL COMMENT '供应商ID',
        `applicant_contact` varchar(50) DEFAULT NULL COMMENT '保理申请人联系人',
        `applicant_contact_number` varchar(50) DEFAULT NULL COMMENT '保理申请人联系电话',
        `applicant_contact_email` varchar(50) DEFAULT NULL COMMENT '保理申请人联系人邮箱',
        `applicant_designated_account_name` varchar(50) DEFAULT NULL COMMENT '保理申请人指定账户户名',
        `applicant_designated_account` varchar(50) DEFAULT NULL COMMENT '保理申请人指定账户',
        `applicant_designated_bank` varchar(50) DEFAULT NULL COMMENT '保理申请人指定账户',
        `user_create` varchar(50) DEFAULT '' COMMENT '创建人',
        `user_modified` varchar(50) 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 DEFAULT CHARSET=utf8 COMMENT='融资申请-申请人历史信息表';

-- 融资状态字典项
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('融资申请状态', NULL, 'financing_status', NULL, 1, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('处理中', (select t.id from t_dictionary t where t.code = 'financing_status'), 'financing_processing', NULL, 0, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('已完成', (select t.id from t_dictionary t where t.code = 'financing_status'), 'financing_completed', NULL, 1, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('放款执行中', (select t.id from t_dictionary t where t.code = 'financing_status'), 'financing_payment_executing', NULL, 2, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('还款中', (select t.id from t_dictionary t where t.code = 'financing_status'), 'financing_repayment_executing', NULL, 3, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('已逾期', (select t.id from t_dictionary t where t.code = 'financing_status'), 'financing_overdue', NULL, 4, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('已终止', (select t.id from t_dictionary t where t.code = 'financing_status'), 'financing_terminated', NULL, 5, now(), now());

-- 融资当前阶段字典项
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('融资申请当前阶段', NULL, 'financing_current_stage', NULL, 1, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('额度支用', (select t.id from t_dictionary t where t.code = 'financing_current_stage'), 'review_quota_disburse', NULL, 0, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('支用审核', (select t.id from t_dictionary t where t.code = 'financing_current_stage'), 'review_quota_disburse_approval', NULL, 1, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('合同制作', (select t.id from t_dictionary t where t.code = 'financing_current_stage'), 'contract_making', NULL, 2, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('合同签署', (select t.id from t_dictionary t where t.code = 'financing_current_stage'), 'contract_signing', NULL, 3, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('合同审核', (select t.id from t_dictionary t where t.code = 'financing_current_stage'), 'contract_approval', NULL, 4, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('付款申请', (select t.id from t_dictionary t where t.code = 'financing_current_stage'), 'payment_apply', NULL, 5, now(), now());
INSERT INTO `t_dictionary`(`name`, `parentId`, `code`, `value`, `gradation`, `gmt_create`, `gmt_modified`)
VALUES ('付款审核', (select t.id from t_dictionary t where t.code = 'financing_current_stage'), 'payment_approval', NULL, 6, now(), now());

-- 验收单货物增加是否已融资字段
ALTER TABLE `c_acceptance_goods_info` ADD COLUMN `is_financing` tinyint(1) DEFAULT '0' COMMENT '是否已融资 0：否、1：是';

