-- 销售合同管理
DROP TABLE IF EXISTS `sales_contract_management`;
CREATE TABLE `sales_contract_management` (
                                             `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                             `business_number` varchar(50) NOT NULL COMMENT '业务编号',
                                             `contract_status` bigint(20) unsigned DEFAULT '0' COMMENT '合同状态 已创建、已录入、已审核、已签署、签署已审核、已驳回、已变更、已完成',
                                             `is_change` tinyint(1) DEFAULT '0' COMMENT '是否发生变更标识、默认为否',
                                             `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='销售合同管理';


-- 销售合同基本信息
DROP TABLE IF EXISTS `sales_contract_basic_info`;
CREATE TABLE `sales_contract_basic_info` (
                                             `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                             `contract_number` varchar(50) DEFAULT NULL COMMENT '合同编号',
                                             `contract_period` int(10) DEFAULT NULL COMMENT '合同期限',
                                             `contract_plan_amount_type` bigint(20) DEFAULT NULL COMMENT '合同约定金额类型',
                                             `contract_plan_amount` decimal(17,2) DEFAULT '0.00' COMMENT '合同约定金额',
                                             `check_operator` varchar(32) DEFAULT NULL COMMENT '验收经办人',
                                             `settlement_operator` varchar(32) DEFAULT NULL COMMENT '结算经办人',
                                             `sales_contract_management_id` bigint(20) NOT NULL COMMENT '销售合同管理Id',
                                             `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='销售合同基本信息';

-- 销售合同文件中间表
DROP TABLE IF EXISTS `sales_contract_file_ref`;
CREATE TABLE `sales_contract_file_ref` (
                                           `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                           `sales_contract_basic_info_id` bigint(20) NOT NULL COMMENT '销售合同基本信息表Id',
                                           `file_storage_id` bigint(20) unsigned DEFAULT '0' COMMENT '附件的ID',
                                           PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='销售合同文件中间表';

-- 销售合同付款条件
DROP TABLE IF EXISTS `sales_contract_payment_condition`;
CREATE TABLE `sales_contract_payment_condition` (
                                                    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                                    `serial_number` smallint(5) unsigned DEFAULT '0' COMMENT '序号',
                                                    `payment_condition` varchar(2000) DEFAULT NULL COMMENT '付款条件',
                                                    `sales_contract_basic_info_id` bigint(20) NOT NULL COMMENT '销售合同基本信息表Id',
                                                    `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='销售合同付款条件';

-- 销售合同结算信息
DROP TABLE IF EXISTS `sales_contract_settle_info`;
CREATE TABLE `sales_contract_settle_info` (
                                              `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                              `delivery_date` varchar(500) DEFAULT NULL COMMENT '发货时间',
                                              `sign_date` varchar(500) DEFAULT NULL COMMENT '签收时间',
                                              `first_settle_date` datetime DEFAULT NULL COMMENT '第一次结算时间',
                                              `settle_cycle_method` bigint(20) DEFAULT NULL COMMENT '结算周期方式',
                                              `settle_cycle` int(10) DEFAULT NULL COMMENT '结算周期',
                                              `plan_payback_days` int(10) DEFAULT NULL COMMENT '约定回款时间',
                                              `estimate_payback_days` int(10) DEFAULT NULL COMMENT '预估回款时间',
                                              `sales_contract_basic_info_id` bigint(20) NOT NULL COMMENT '销售合同基本信息表Id',
                                              `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='销售合同结算信息';

-- 销售合同质保金信息
DROP TABLE IF EXISTS `sales_contract_retention_info`;
CREATE TABLE `sales_contract_retention_info` (
                                                 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                                 `is_retention` tinyint(1) DEFAULT NULL COMMENT '是否有质保金，0:无 1:有',
                                                 `retention_percent` decimal(17,2) DEFAULT NULL COMMENT '质保金比例',
                                                 `is_limit` tinyint(1) DEFAULT NULL COMMENT '质保金是否有上限，0:无 1:有',
                                                 `retention_limit` decimal(17,2) DEFAULT NULL COMMENT '质保金上限',
                                                 `release_method` bigint(20) DEFAULT NULL COMMENT '质保金释放方式',
                                                 `release_date` datetime DEFAULT NULL COMMENT '释放日期',
                                                 `release_days` int(10) DEFAULT NULL COMMENT '释放天数',
                                                 `sales_contract_basic_info_id` bigint(20) NOT NULL COMMENT '销售合同基本信息表Id',
                                                 `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='销售合同质保金信息';

-- 销售合同货物信息
DROP TABLE IF EXISTS `sales_contract_goods_info`;
CREATE TABLE `sales_contract_goods_info` (
                                             `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                             `goods_name` varchar(50) NOT NULL COMMENT '货物名称',
                                             `goods_factory` varchar(50) NOT NULL COMMENT '厂家',
                                             `model_number` varchar(50) NOT NULL COMMENT '型号',
                                             `units` varchar(10) NOT NULL COMMENT '单位',
                                             `quantity` decimal(17,3) DEFAULT NULL COMMENT '数量',
                                             `is_clear_price` tinyint(1) DEFAULT NULL COMMENT '是否明确单价，0:否 1:是',
                                             `sales_price` varchar(50) DEFAULT NULL COMMENT '销售单价',
                                             `remark` varchar(100) DEFAULT NULL COMMENT '备注',
                                             `sales_contract_basic_info_id` bigint(20) NOT NULL COMMENT '销售合同基本信息表Id',
                                             `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='销售合同货物信息';

-- 销售合同审核表
DROP TABLE IF EXISTS `sales_contract_approval`;
CREATE TABLE `sales_contract_approval` (
                                           `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                           `sales_contract_basic_info_id` bigint(20) unsigned DEFAULT '0' COMMENT '销售合同基本信息表Id',
                                           `approval_comment` varchar(3000) COLLATE utf8_bin DEFAULT '' COMMENT '审批意见',
                                           `approval_conclusion` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '审批结论',
                                           `auditor_id` bigint(20) unsigned DEFAULT '0' COMMENT '审核人Id',
                                           `user_create` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '创建人',
                                           `user_modified` varchar(50) COLLATE utf8_bin 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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='销售合同审核表';

-- 销售合同审核文件中间表
CREATE TABLE `sales_contract_approval_file_rel` (
                                                    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                                    `sales_contract_approval_id` bigint(20) unsigned DEFAULT '0' COMMENT '销售合同审核表ID',
                                                    `file_storage_id` bigint(20) unsigned DEFAULT '0' COMMENT '附件ID',
                                                    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='销售合同审核文件中间表';

-- 销售合同签署确认表
DROP TABLE IF EXISTS `sales_contract_sign_confirm`;
CREATE TABLE `sales_contract_sign_confirm` (
                                               `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                               `sales_contract_basic_info_id` bigint(20) unsigned DEFAULT '0' COMMENT '销售合同基本信息表Id',
                                               `assignee_id` bigint(20) unsigned DEFAULT '0' COMMENT '处理人ID',
                                               `conclusion` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '结论',
                                               `veto_reason` varchar(350) COLLATE utf8_bin DEFAULT '' COMMENT '否决原因',
                                               `remark` varchar(3000) COLLATE utf8_bin DEFAULT '' COMMENT '备注',
                                               `user_create` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '创建人',
                                               `user_modified` varchar(50) COLLATE utf8_bin 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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='销售合同签署确认表';

-- 销售合同签署确认附件中间表
DROP TABLE IF EXISTS `sales_contract_sign_confirm_file_rel`;
CREATE TABLE `sales_contract_sign_confirm_file_rel` (
                                                        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                                        `sales_contract_sign_confirm_id` bigint(20) unsigned DEFAULT '0' COMMENT '销售合同签署确认表ID',
                                                        `file_storage_id` bigint(20) unsigned DEFAULT '0' COMMENT '附件ID',
                                                        PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='销售合同签署确认附件中间表';

-- 销售合同签署审核表
CREATE TABLE `sales_contract_final_approval` (
                                                 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                                 `sales_contract_basic_info_id` bigint(20) unsigned DEFAULT '0' COMMENT '销售合同基本信息表Id',
                                                 `approval_comment` varchar(3000) COLLATE utf8_bin DEFAULT '' COMMENT '审批意见',
                                                 `approval_conclusion` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '审批结论',
                                                 `auditor_id` bigint(20) unsigned DEFAULT '0' COMMENT '审核人Id',
                                                 `user_create` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '创建人',
                                                 `user_modified` varchar(50) COLLATE utf8_bin 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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='销售合同签署审核表';

-- 添加销售合同状态字典项
INSERT INTO `t_dictionary` (name, parentId, code, value, gradation, gmt_create, gmt_modified)
VALUES ('销售合同状态', null, 'salesContractStatus', 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 = 'salesContractStatus'), 'sales_contract_create', 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 = 'salesContractStatus'), 'sales_contract_input', 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 = 'salesContractStatus'), 'sales_contract_first_approval', 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 = 'salesContractStatus'), 'sales_contract_sign_confirm', 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 = 'salesContractStatus'), 'sales_contract_sign_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 = 'salesContractStatus'), 'sales_contract_overrule', 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 = 'salesContractStatus'), 'sales_contract_wait_change', null, '6', 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 = 'salesContractStatus'), 'sales_contract_change', null, '7', 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 = 'salesContractStatus'), 'sales_contract_completed', null, '8', NOW(), NOW());

-- 添加销售合同--合同约定金额类型字典项
INSERT INTO `t_dictionary` (name, parentId, code, value, gradation, gmt_create, gmt_modified)
VALUES ('销售合同约定金额类型', null, 'salesContractPlanAmountType', 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 = 'salesContractPlanAmountType'), 'salesContractPlanAmountType_fixed', 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 = 'salesContractPlanAmountType'), 'salesContractPlanAmountType_timely', null, '1', NOW(), NOW());

-- 添加销售合同--结算周期方式字典项
INSERT INTO `t_dictionary` (name, parentId, code, value, gradation, gmt_create, gmt_modified)
VALUES ('结算周期方式', null, 'salesContractSettleCycleMethod', 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 = 'salesContractSettleCycleMethod'), 'salesContractSettleCycleMethod_fixed', 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 = 'salesContractSettleCycleMethod'), 'salesContractSettleCycleMethod_natural', null, '1', NOW(), NOW());

-- 添加销售合同--质保金释放方式字典项
INSERT INTO `t_dictionary` (name, parentId, code, value, gradation, gmt_create, gmt_modified)
VALUES ('质保金释放方式', null, 'salesContractReleaseMethod', 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 = 'salesContractReleaseMethod'), 'salesContractReleaseMethod_fixed', 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 = 'salesContractReleaseMethod'), 'salesContractReleaseMethod_end', 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 = 'salesContractReleaseMethod'), 'salesContractReleaseMethod_fixed_payment', null, '2', NOW(), NOW());

-- 添加系统业务模块字典项
INSERT INTO `t_dictionary` (name, parentId, code, value, gradation, gmt_create, gmt_modified)
VALUES ('系统业务模块', null, 'systemBusinessModel', 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 = 'systemBusinessModel'), 'businessManagementManagement', 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 = 'systemBusinessModel'), 'customerManagement', 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 = 'systemBusinessModel'), 'platformManagement', 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 = 'systemBusinessModel'), 'salesContractManagement', 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 = 'systemBusinessModel'), 'salesContractChangeManagement', 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 = 'systemBusinessModel'), 'purchContractManagement', 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 = 'systemBusinessModel'), 'purchContractChangeManagement', null, '6', 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 = 'systemBusinessModel'), 'acceptanceSheetManagement', null, '7', 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 = 'systemBusinessModel'), 'billManagement', null, '8', 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 = 'systemBusinessModel'), 'billChangeManagement', null, '9', 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 = 'systemBusinessModel'), 'mortgageManagement', null, '10', 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 = 'systemBusinessModel'), 'saleInvoicecMangement', null, '11', 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 = 'systemBusinessModel'), 'puchInvoiceMangement', null, '12', 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 = 'systemBusinessModel'), 'ledgerManagement', null, '13', NOW(), NOW());


-- sys_user添加字段 enterprise_customer_id
ALTER TABLE sys_user add enterprise_customer_id bigint(20) DEFAULT null COMMENT '企业客户信息表ID';

-- 分账管理信息表
DROP TABLE IF EXISTS `ledger_account_management`;
CREATE TABLE `ledger_account_management` (
                                             `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                             `purchaser_id` bigint(20) NOT NULL COMMENT '买方名称',
                                             `business_name` varchar(50) NOT NULL COMMENT '业务名称',
                                             `business_number` varchar(50) NOT NULL COMMENT '业务编号',
                                             `ledger_number` varchar(50) NOT NULL COMMENT '分账单号',
                                             `ledger_status` bigint(20) unsigned DEFAULT NULL COMMENT '分账状态 已创建、待审核、全部分配',
                                             `currency` bigint(20) DEFAULT NULL COMMENT '币种',
                                             `ledger_amount` decimal(17,2) DEFAULT '0.00' COMMENT '分账金额',
                                             `cash_flow_manage_base_info_id` bigint(20) DEFAULT NULL COMMENT '对应现金流基础信息表(入账)Id',
                                             `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='分账管理信息表';

-- 分账管理--基本信息表
DROP TABLE IF EXISTS `ledger_account_basic_info`;
CREATE TABLE `ledger_account_basic_info` (
                                             `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                             `ledger_account_management_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理信息表Id',
                                             `is_effective` tinyint(1) DEFAULT NULL COMMENT '是否生效，0:否（审批驳回） 1:是（审批通过） 提交未审批时置为null ',
                                             `visible` tinyint(1) DEFAULT NULL COMMENT '是否正式数据 0:否 1:是',
                                             `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='分账管理基本信息表';


-- 分账管理-贴现费用
DROP TABLE IF EXISTS `ledger_discount_charge`;
CREATE TABLE `ledger_discount_charge` (
                                          `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                          `discount_charge` decimal(17,2) DEFAULT '0.00' COMMENT '贴现费用',
                                          `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理基本信息表Id',
                                          `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='分账管理-贴现费用表';

-- 分账管理-资金占用费
DROP TABLE IF EXISTS `ledger_fund_possession_cost`;
CREATE TABLE `ledger_fund_possession_cost` (
                                               `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                               `supplier_id` bigint(20) DEFAULT NULL COMMENT '供应商Id',
                                               `supplier_name` varchar(50) DEFAULT NULL COMMENT '供应商名称',
                                               `purch_contract_number` varchar(50) DEFAULT NULL COMMENT '采购合同编号',
                                               `fund_possession_cost` decimal(17,2) DEFAULT '0.00' COMMENT '资金占用费',
                                               `assign_reason` varchar(200) DEFAULT NULL COMMENT '分配原因',
                                               `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理基本信息表Id',
                                               `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='分账管理-资金占用费表';

-- 分账管理-货款分配
DROP TABLE IF EXISTS `ledger_fund_goods_assign`;
CREATE TABLE `ledger_fund_goods_assign` (
                                            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                            `supplier_id` bigint(20) DEFAULT NULL COMMENT '供应商Id',
                                            `supplier_name` varchar(50) DEFAULT NULL COMMENT '供应商名称',
                                            `purch_contract_number` varchar(50) DEFAULT NULL COMMENT '采购合同编号',
                                            `acceptance_business_goods_id` bigint(20) NOT NULL COMMENT '对应验收单货物信息Id',
                                            `bill_goods_information_id` bigint(20) NOT NULL COMMENT '对应结算单货物信息Id',
                                            `mortgage_information_id` bigint(20) DEFAULT NULL COMMENT '对应底层信息Id',
                                            `financing_interest` decimal(17,2) DEFAULT '0.00' COMMENT '融资利息',
                                            `financing_principal` decimal(17,2) DEFAULT '0.00' COMMENT '融资本金',
                                            `platform_profit` decimal(17,2) DEFAULT '0.00' COMMENT '平台利润',
                                            `discount_amount` decimal(17,2) DEFAULT '0.00' COMMENT '折扣金额',
                                            `assign_amount` decimal(17,2) DEFAULT '0.00' COMMENT '分配金额',
                                            `is_purchase_price` tinyint(1) DEFAULT NULL COMMENT '是否属于质保金中的抵货款，0:否 1:是',
                                            `ledger_fund_retention_money_id` bigint(20) DEFAULT NULL COMMENT '对应质保金分配的id（只要当属于质保金抵货款时才有值）',
                                            `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理基本信息表Id',
                                            `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='分账管理-货款分配表';

-- 分账管理-质保金分配
DROP TABLE IF EXISTS `ledger_fund_retention_money`;
CREATE TABLE `ledger_fund_retention_money` (
                                               `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                               `supplier_id` bigint(20) DEFAULT NULL COMMENT '供应商Id',
                                               `supplier_name` varchar(50) DEFAULT NULL COMMENT '供应商名称',
                                               `purch_contract_number` varchar(50) DEFAULT NULL COMMENT '采购合同编号',
                                               `assign_retention_amount` decimal(17,2) DEFAULT '0.00' COMMENT '分配质保金',
                                               `cash_distribution` decimal(17,2) DEFAULT '0.00' COMMENT '现金分配',
                                               `offset_money` decimal(17,2) DEFAULT '0.00' COMMENT '抵货款',
                                               `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理基本信息表Id',
                                               `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='分账管理-质保金分配表';

-- 分账管理-审批信息表
DROP TABLE IF EXISTS `ledger_audit_info`;
CREATE TABLE `ledger_audit_info` (
                                     `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                     `approval_comment` varchar(3000) COLLATE utf8_bin DEFAULT '' COMMENT '审批意见',
                                     `approval_conclusion` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '审批结论',
                                     `auditor_id` bigint(20) unsigned DEFAULT '0' COMMENT '审核人Id',
                                     `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理基本信息表Id',
                                     `user_create` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '创建人',
                                     `user_modified` varchar(50) COLLATE utf8_bin 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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='分账管理-审批信息表';

-- 分账管理-账后收放款计划分配表
DROP TABLE IF EXISTS `ledger_plan_factoring_assign`;
CREATE TABLE `ledger_plan_factoring_assign` (
                                     `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                     `ledger_account_basic_info_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理基本信息表Id',
                                     `ledger_fund_goods_assign_id` bigint(20) DEFAULT NULL COMMENT '对应分账管理货款分配表Id',
                                     `plan_factoring_id` bigint(20) unsigned DEFAULT '0' COMMENT '账后收放款计划Id(对应sop中的账后收放款计划)',
                                     `actual_date` datetime DEFAULT NULL COMMENT '实际分配日期，精确到年月日',
                                     `plan_amount` decimal(17,2) DEFAULT '0.00' COMMENT '应分金额',
                                     `actual_amount` decimal(17,2) DEFAULT '0.00' COMMENT '实分配金额',
                                     `is_prepayment` tinyint(1) DEFAULT NULL COMMENT '是否属于提前还款，0:否 1:是',
                                     `user_create` varchar(50) COLLATE utf8_bin DEFAULT '' COMMENT '创建人',
                                     `user_modified` varchar(50) COLLATE utf8_bin 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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='分账管理-账后收放款计划分配表';


-- 添加分账状态字典项
INSERT INTO `t_dictionary` (name, parentId, code, value, gradation, gmt_create, gmt_modified)
VALUES ('分账管理状态', null, 'ledgerStatus', 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 = 'ledgerStatus'), 'ledger_create', 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 = 'ledgerStatus'), 'ledger_pending_review', 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 = 'ledgerStatus'), 'ledger_approval', null, '2', NOW(), NOW());

-- 添加供应商的组织机构
INSERT INTO sys_org (parent_id, department_id, NAME, dept_no, internal_code, sub_count_max, sub_count, gradation, simple_pinyin, full_pinyin, user_modified, gmt_create, gmt_modified,seq,org_type)
VALUES (NULL,NULL,'供应商','supplier','*',11,11,0,'gys','gongyingshang' 'admin',NULL,NOW(),NOW(),NULL,0);
