-- C端 相关表结构
-- -------------------------------------
-- Table structure for customer_user 用户表
-- --------------------------------------
DROP TABLE IF EXISTS `customer_user`;
CREATE TABLE `customer_user`
(
    `id`            bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `identifier_no` varchar(32)  DEFAULT '' UNIQUE COMMENT '账号ID',
    `nickname`      varchar(50)  DEFAULT NULL UNIQUE COMMENT '昵称/会员名称',
    `password`      varchar(100) DEFAULT '' COMMENT '密码',
    `phone_number`  char(11)     DEFAULT '' COMMENT '手机号（大陆）',
    `email`         varchar(100) DEFAULT '' COMMENT '电子邮箱',
    `gmt_create`    TIMESTAMP    DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`  TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='用户表';

DROP TABLE IF EXISTS `customer_operation_log`;
CREATE TABLE `customer_operation_log`
(
    `id`               bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `customer_user_id` BIGINT UNSIGNED DEFAULT 0 COMMENT '客户id',
    `module`           varchar(64)   default '' comment '业务模块(菜单)',
    `type`             varchar(64)   default '' comment '业务类型(新增、修改)',
    `ipaddr`           varchar(128)  default '' comment '登录IP地址',
    `content`          varchar(2048) DEFAULT '' COMMENT '操作内容',
    `login_location`   varchar(255)  default '' comment '登录地点',
    `deleted`          TINYINT(1) DEFAULT 0 COMMENT '删除标识',
    `gmt_create`       TIMESTAMP     DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`     TIMESTAMP     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    INDEX              idx_cui (`customer_user_id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='用户操作日志表';

DROP TABLE IF EXISTS `c_credit_quota_logic`;
CREATE TABLE `c_credit_quota_logic`
(
    `id`               bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `dimension`        VARCHAR(64) DEFAULT '' COMMENT '维度',
    `standard_setting` VARCHAR(64) DEFAULT '' comment '标准设定',
    `weight`           DOUBLE(2, 2
) DEFAULT 0 COMMENT '权重',
    `standard_score`   DOUBLE(5,2)     DEFAULT 0 COMMENT '标准分值',
    `estimate_score`   DOUBLE(5,2)     DEFAULT 0 COMMENT '评估分值',
    `remark`           VARCHAR(256) DEFAULT '' COMMENT '标准设定备注',
    `gmt_create`       TIMESTAMP    DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`     TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='用户授信额度逻辑表';

DROP TABLE IF EXISTS `c_credit_score_quota`;
CREATE TABLE `c_credit_score_quota`
(
    `id`            bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `currency_code` CHAR(3)     DEFAULT '' COMMENT '币种代码',
    `quota`         varchar(64) DEFAULT '' comment '额度',
    `start_score`   DOUBLE(5, 2
) DEFAULT 0 COMMENT '分数区间开始',
    `end_score`     DOUBLE(5,2)     DEFAULT 0 COMMENT '分数区间结束',
    `gmt_create`    TIMESTAMP   DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`  TIMESTAMP   DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='用户授信分数额度表';

-- C端 初始化数据
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('1', '1', 0.30, 100.00, 30.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('1', '2', 0.30, 80.00, 24.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('1', '3', 0.30, 60.00, 18.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('1', '4', 0.30, 40.00, 12.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('1', '5', 0.00, 0.00, 0.00);

INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('2', '1', 0.10, 100.00, 10.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('2', '2', 0.10, 40.00, 4.00);

INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('3', '1', 0.20, 100.00, 20.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('3', '2', 0.20, 80.00, 16.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('3', '3', 0.20, 60.00, 12.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('3', '4', 0.20, 50.00, 10.00);

INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('4', '4', 0.20, 100.00, 20.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('4', '2', 0.20, 80.00, 16.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('4', '3', 0.20, 60.00, 12.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('4', '1', 0.20, 0.00, 0.00);

INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('5', '5', 0.20, 0.00, 0.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('5', '4', 0.20, 60.00, 12.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('5', '3', 0.20, 80.00, 16.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('5', '2', 0.20, 100.00, 20.00);
INSERT INTO `c_credit_quota_logic` (`dimension`, `standard_setting`, `weight`, `standard_score`, `estimate_score`)
VALUES ('5', '1', 0.20, 0.00, 0.00);

INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '1500000', 90.00, 100.00);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '1200000', 80.00, 89.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '1000000', 70.00, 79.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '800000', 60.00, 60.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '500000', 50.00, 50.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '400000', 40.00, 40.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '300000', 30.00, 30.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '200000', 20.00, 20.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '100000', 10.00, 10.99);
INSERT INTO `c_credit_score_quota` (`currency_code`, `quota`, `start_score`, `end_score`)
VALUES ('USD', '50000', 0.00, 9.99);

-- 销售合同基础表
DROP TABLE IF EXISTS `sales_contract`;
CREATE TABLE `sales_contract`
(
    `id`                       bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `customer_user_id`         BIGINT UNSIGNED DEFAULT 0 COMMENT '客户id',
    `contract_no`              varchar(64)     default '' comment '合同编号',
    `contract_name`            varchar(300)    default '' comment '合同名称',
    `importer_cert_no`         VARCHAR(32)     DEFAULT '' COMMENT '进口商统一社会信用代码/注册号',
    `importer_enterprise_name` VARCHAR(300)    DEFAULT NULL COMMENT '进口商企业名称',
    `agency_cert_no`           VARCHAR(32)     DEFAULT '' COMMENT '代理公司统一社会信用代码/注册号',
    `agency_enterprise_name`   VARCHAR(300)    DEFAULT NULL COMMENT '代理公司企业名称',
    `trade_terms`              varchar(4)      DEFAULT '' COMMENT '贸易术语',
    `port_departure`           varchar(255)    DEFAULT '' comment '起运港',
    `port_destination`         varchar(255)    DEFAULT '' comment '目的港',
    `template_type`            TINYINT(2) DEFAULT null COMMENT '模板类型1长银0自有',
    `sign_date`                DATETIME        DEFAULT null COMMENT '签署日期',
    `exchange_rate`            DECIMAL(17, 10) DEFAULT NULL COMMENT '汇率',
    `submit_status`            varchar(4)      DEFAULT '' COMMENT '提交状态1草稿2正式',
    `contract_status`          varchar(4)      DEFAULT '' COMMENT '合同状态1草稿2生效3待上传6已完成',
    `audit_status`             varchar(4)      DEFAULT '' COMMENT '审核状态1待审核2风控审核通过3法务审核通过4风控审核不通过5法务审核不通过',
    `deleted`                  TINYINT(1) DEFAULT 0 COMMENT '删除标识',
    `create_by`                varchar(64)     default '' comment '创建者',
    `update_by`                varchar(64)     default '' comment '更新者',
    `gmt_create`               TIMESTAMP       DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`             TIMESTAMP       DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    INDEX                      uk_contract_No (`contract_No`),
    INDEX                      idx_cui (`customer_user_id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='销售合同表基础表';

-- 销售商品
DROP TABLE IF EXISTS `sales_commodity`;
CREATE TABLE `sales_commodity`
(
    `id`                     bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `sales_contract_id`      BIGINT UNSIGNED DEFAULT 0 COMMENT '销售合同主键',
    `entity_no`              VARCHAR(16)    DEFAULT '' COMMENT '供应商编号',
    `entity_name`            VARCHAR(64)    DEFAULT '' COMMENT '供应商名称',
    `hs_code`                VARCHAR(32)    DEFAULT '' COMMENT '商品编码',
    `name`                   VARCHAR(300)   DEFAULT NULL COMMENT '商品名称',
    `declaration_name`       VARCHAR(300)   DEFAULT NULL COMMENT '报关品名',
    `pack_spec`              VARCHAR(300)   DEFAULT NULL COMMENT '包装规格',
    `measurement_unit`       VARCHAR(300)   DEFAULT NULL COMMENT '计量单位',
    `export_tax_rebate_rate` VARCHAR(10)    DEFAULT '' COMMENT '出口退税税率',
    `added_tax_rate`         VARCHAR(10)    DEFAULT '' COMMENT '增值税率/增值税征税率',
    `number`                 DECIMAL(17, 6) DEFAULT NULL COMMENT '数量',
    `price_unit`             DECIMAL(17, 4) DEFAULT NULL comment '单价',
    `currency`               VARCHAR(3)     DEFAULT 'USD' COMMENT '币种',
    `amount`                 DECIMAL(17, 2) DEFAULT NULL COMMENT '金额',
    `model`                  VARCHAR(64)    DEFAULT '' COMMENT '型号',
    `deleted`                TINYINT(1) DEFAULT 0 COMMENT '删除标识',
    -- `submit_status`          varchar(4)   DEFAULT '' COMMENT '提交状态1草稿2正式',
    `gmt_create`             TIMESTAMP      DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`           TIMESTAMP      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    INDEX                    idx_sales_contract_id (`sales_contract_id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='销售商品表';

-- 合同条款
DROP TABLE IF EXISTS `contract_terms`;
CREATE TABLE `contract_terms`
(
    `id`               bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `contract_id`      BIGINT UNSIGNED DEFAULT 0 COMMENT '合同条款主键',
    `contract_type`    VARCHAR(6)   DEFAULT '' COMMENT '合同类型',
    `contract_no`      varchar(64)  default '' comment '合同编号',
    `payment_way`      VARCHAR(6)   DEFAULT '' COMMENT '付款方式',
    `payment_currency` VARCHAR(3)   DEFAULT '' COMMENT '付款币种',
    `invoice_type`     VARCHAR(2)   DEFAULT '' COMMENT '发票类型1增值税专用发票2增值税普通发票',
    `settlement_cycle` INT          DEFAULT NULL COMMENT '结算周期单位天',
    `account_name`     VARCHAR(50)  DEFAULT '' COMMENT '账户名称',
    `account_number`   VARCHAR(50)  DEFAULT '' COMMENT '账号',
    `opening_bank`     VARCHAR(300) DEFAULT '' COMMENT '开户行名称',
    `swift_code`       VARCHAR(100) DEFAULT '' COMMENT '香港开户行',
    `deleted`          TINYINT(1) DEFAULT 0 COMMENT '删除标识',
    -- `submit_status`    varchar(4)   DEFAULT '' COMMENT '提交状态1草稿2正式',
    `remark`           varchar(512) DEFAULT '' COMMENT '备注',
    `gmt_create`       TIMESTAMP    DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`     TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    INDEX              idx_contract_No (`contract_No`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='合同条款表';

-- 合同条款扩展
DROP TABLE IF EXISTS `contract_terms_extend`;
CREATE TABLE `contract_terms_extend`
(
    `id`                bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `contract_terms_id` BIGINT UNSIGNED DEFAULT 0 COMMENT '合同条款主键',
    `type`              VARCHAR(3)     DEFAULT '' COMMENT '扩展类型1付款2条款',
    `payment_time`      VARCHAR(128)   DEFAULT '' COMMENT '付款时点',
    `payment_term`      INT            DEFAULT NULL COMMENT '付款期限单位天',
    `payment_name`      VARCHAR(128)   DEFAULT '' COMMENT '付款名称',
    `payment_ratio`     DECIMAL(17, 2) DEFAULT NULL COMMENT '款项比例单位%',
    `term_title`        VARCHAR(512)   DEFAULT '' COMMENT '条款标题',
    `content`           TEXT           DEFAULT NULL COMMENT '内容',
    `deleted`           TINYINT(1) DEFAULT 0 COMMENT '删除标识',
    -- `submit_status`     varchar(4)   DEFAULT '' COMMENT '提交状态1草稿2正式',
    `gmt_create`        TIMESTAMP      DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`      TIMESTAMP      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    INDEX               idx_contract_terms_id (`contract_terms_id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='合同条款扩展表';

DROP TABLE IF EXISTS `contract_file`;
CREATE TABLE `contract_file`
(
    `id`                     BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `customer_user_id`       BIGINT UNSIGNED DEFAULT 0 COMMENT '客户id',
    `contract_no`            VARCHAR(64) DEFAULT '' COMMENT '合同编号',
    `sign_flow_id`           VARCHAR(50) DEFAULT '' COMMENT '签署流程ID',
    `sign_file_id`           VARCHAR(50) DEFAULT '' COMMENT '签署文件ID',
    `sign_file_type`         VARCHAR(50) DEFAULT '' COMMENT '签署文件类型',
    `unsign_file_storage_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '（未签署的）合同附件ID',
    `signed_file_storage_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '（签署完的）合同附件ID',
    `operator_name`          VARCHAR(50) DEFAULT '' COMMENT '操作人姓名',
    `generation_time`        DATETIME    DEFAULT NULL COMMENT '生成时间',
    `deleted`                TINYINT(1) DEFAULT 0 COMMENT '删除标识',
    `gmt_create`             TIMESTAMP   DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`           TIMESTAMP   DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='合同文件';

-- 采购订单
DROP TABLE IF EXISTS `purchase_order`;
CREATE TABLE `purchase_order`
(
    `id`                   bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `customer_user_id`     BIGINT UNSIGNED DEFAULT 0 COMMENT '客户id',
    `sales_order_id`       BIGINT UNSIGNED DEFAULT 0 COMMENT '销售订单主键',
    `order_no`             VARCHAR(64)    DEFAULT '' COMMENT '订单编号',
    `entity_no`            VARCHAR(64)    DEFAULT '' COMMENT '供应商编号',
    `agency_cert_no`       VARCHAR(32)    DEFAULT '' COMMENT '代理公司统一社会信用代码/注册号',
    `purchase_contract_no` VARCHAR(64)    DEFAULT '' COMMENT '采购合同编号',
    `invoice_type`         VARCHAR(2)     DEFAULT '' COMMENT '发票类型1增值税专用发票2增值税普通发票',
    `currency`             VARCHAR(3)     DEFAULT 'CNY' COMMENT '币种',
    `amount_total`         DECIMAL(17, 2) DEFAULT 0 COMMENT '订单总额',
    `tax_amount_total`     DECIMAL(17, 2) DEFAULT 0 COMMENT '订单税额',
    `settlement_cycle`     INT            DEFAULT 0 COMMENT '结算周期单位天',
    `invoice_pull_status`  VARCHAR(2)     DEFAULT '0' COMMENT '发票拉取状态0-未拉取1已拉取',
    `deleted`              TINYINT(1) DEFAULT 0 COMMENT '删除标识',
    `gmt_create`           TIMESTAMP      DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `gmt_modified`         TIMESTAMP      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`id`),
    INDEX                  idx_sales_order_id (`sales_order_id`)
) ENGINE = InnoDB
  DEFAULT CHARACTER SET utf8mb4
  COLLATE utf8mb4_bin COMMENT ='采购订单表';


-- 采购订单-关联商品
DROP TABLE IF EXISTS `purchase_order_commodity`;
CREATE TABLE `purchase_order_commodity`
(
    `id`                 BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `purchase_order_id`  BIGINT(20) UNSIGNED NOT NULL COMMENT '采购订单id',
    `sales_commodity_id` BIGINT(20) UNSIGNED NOT NULL COMMENT '销售商品id',
    `hs_code`            VARCHAR(32)        DEFAULT '' COMMENT '商品编码',
    `name`               VARCHAR(32)        DEFAULT '' COMMENT '商品名称',
    `declaration_name`   VARCHAR(300)       DEFAULT NULL COMMENT '报关品名',
    `measurement_unit`   VARCHAR(32)        DEFAULT '' COMMENT '计量单位',
    `pack_spec`          VARCHAR(300)       DEFAULT '' COMMENT '包装规格',
    `number`             DECIMAL(17, 6)     DEFAULT 0 COMMENT '数量',
    `price_unit`         DECIMAL(17, 4)     DEFAULT 0 COMMENT '采购单价 (不含税) ',
    `tax_refund_rate`    DECIMAL(8, 6)      DEFAULT 0 COMMENT '增值税税率',
    `currency`           VARCHAR(3)         DEFAULT 'CNY' COMMENT '币种',
    `amount`             DECIMAL(17, 2)     DEFAULT 0 COMMENT '商品金额',
    `model`              VARCHAR(64)        DEFAULT '' COMMENT '型号',
    `deleted`            TINYINT(1) DEFAULT 0 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 CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT='采购订单-关联商品';

DROP TABLE IF EXISTS `purchase_order_logistics`;
CREATE TABLE `purchase_order_logistics`
(
    `id`                       BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `purchase_order_id`        BIGINT(20) UNSIGNED NOT NULL COMMENT '采购订单id',
    `company_name`             VARCHAR(300)       DEFAULT '' COMMENT '物流公司名称',
    `logistics_order_number`   VARCHAR(32)        DEFAULT '' COMMENT '物流单号',
    `transport_invoice_number` VARCHAR(32)        DEFAULT '' COMMENT '运输发票号码',
    `kprq`                     DATETIME           DEFAULT NULL COMMENT '开票日期',
    `delivery_time`            DATETIME           DEFAULT NULL COMMENT '发货时间',
    `arrive_time`              DATETIME           DEFAULT NULL COMMENT '送达时间',
    -- `transport_certificate_file_id` BIGINT(20) UNSIGNED NOT NULL COMMENT '运输证明文件id',
    `remark`                   varchar(512)       DEFAULT '' COMMENT '备注',
    `deleted`                  TINYINT(1) DEFAULT 0 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 CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT='采购订单-关联商品';

-- 采购订单-发票信息
DROP TABLE IF EXISTS `purchase_order_invoice`;
CREATE TABLE `purchase_order_invoice`
(
    `id`                     BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `order_id`               BIGINT(20) UNSIGNED NOT NULL COMMENT '采购订单id',
    `invoice_number`         VARCHAR(64)        DEFAULT '' COMMENT '发票号码',
    `invoice_code`           VARCHAR(64)        DEFAULT '' COMMENT '发票代码',
    `digital_invoice_number` VARCHAR(64)        DEFAULT '' COMMENT '数电发票号码',
    `invoice_type`           VARCHAR(2)         DEFAULT '' COMMENT '发票类型1增值税专用发票2增值税普通发票3增值税电子专用发票4增值税电子普通发票5数电票（增值税专用发票）6（数电票）增值税普通发票',
    `amount`                 DECIMAL(17, 2)     DEFAULT 0 COMMENT '发票金额',
    `tax_amount`             DECIMAL(17, 2)     DEFAULT 0 COMMENT '发票金额',
    `file_id`                BIGINT(20) UNSIGNED DEFAULT NULL COMMENT '发票文件id',
    `invoicing_date`         DATETIME           DEFAULT NULL COMMENT '开票日期',
    `certification_date`     DATETIME           DEFAULT NULL COMMENT '订单日期',
    `invoice_status`         CHAR(3)            DEFAULT '' COMMENT '发票状态Y已认证N未认证',
    `purchase_voucher_no`    varchar(64)        DEFAULT '' COMMENT '进货凭证号(发票唯一)',
    `source`                 TINYINT(2) DEFAULT 0 COMMENT '发票来源1B端2C端',
    `bz`                     varchar(512)       DEFAULT '' COMMENT '备注',
    `deleted`                TINYINT(1) DEFAULT 0 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`),
    INDEX                    idx_order_id (`order_id`)
)ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT='采购订单-发票信息';

-- 采购订单-发票商品信息
DROP TABLE IF EXISTS `purchase_order_invoice_commodity`;
CREATE TABLE `purchase_order_invoice_commodity`
(
    `id`               BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `invoice_id`       BIGINT(20) UNSIGNED NOT NULL COMMENT '发票id',
    `commodity_code`   VARCHAR(64)        DEFAULT '' COMMENT '商品和服务税收分类合并编码',
    `name`             VARCHAR(128)       DEFAULT '' COMMENT '货物或应税劳务名称',
    `measurement_unit` VARCHAR(32)        DEFAULT '' COMMENT '计量单位',
    `model`            VARCHAR(64)        DEFAULT '' COMMENT '规格型号',
    `number`           DECIMAL(17, 6)     DEFAULT 0 COMMENT '数量',
    `price_unit`       DECIMAL(17, 4)     DEFAULT '0' COMMENT '单价',
    `tax_rate`         DECIMAL(8, 6)      DEFAULT 0 COMMENT '税率',
    `amount`           DECIMAL(17, 2)     DEFAULT 0 COMMENT '金额',
    `tax_amount`       DECIMAL(17, 2)     DEFAULT 0 COMMENT '金额',
    `line_number`      VARCHAR(32)        DEFAULT '' COMMENT '行号',
    `deleted`          TINYINT(1) DEFAULT 0 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`),
    INDEX              idx_invoice_id (`invoice_id`)
)ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT='采购订单-发票商品信息';

-- 小图-监管证件代码
DROP TABLE IF EXISTS `supervise_certificate_code`;
CREATE TABLE `supervise_certificate_code`
(
    `id`           BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `code`         CHAR(2)            DEFAULT '' COMMENT '监管证件代码',
    `name`         VARCHAR(128)       DEFAULT '' COMMENT '名称',
    `source`       VARCHAR(32)        DEFAULT '' COMMENT '来源',
    `remark`       VARCHAR(128)       DEFAULT '' COMMENT '备注',
    `deleted`      TINYINT(1) DEFAULT 0 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 CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT='小图-监管证件代码';

INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('1', '进口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('2', '两用物项和技术进口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('3', '两用物项和技术出口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('4', '出口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('5', '纺织品临时出口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('6', '旧机电产品禁止进口', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('7', '自动进口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('8', '禁止出口商品', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('9', '禁止进口商品', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('A', '入境检验检疫', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('B', '出境检验检疫（电子底账）', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('D', '出/入境货物通关单（毛坯钻石用）', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('E', '濒危物种允许出口证明书', '海关总署', '其他叫法：野生动植物出口证书');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('F', '濒危物种允许进口证明书', '海关总署', '其他叫法：野生动植物进口证书');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('G', '两用物项和技术出口许可证(定向)', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('I', '麻醉精神药品进出口准许证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('J', '黄金及黄金制品进出口准许证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('L', '药品进出口准许证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('M', '密码产品和设备进口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('O', '自动进口许可证(新旧机电产品)', '海关总署', '其他叫法：机电产品自动进口许可证');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('P', '固体废物进口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('Q', '进口药品通关单', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('R', '进口兽药通关单', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('S', '进出口农药登记证明', '海关总署', '其他叫法：农药进出口登记管理放行通知单');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('U', '合法捕捞产品通关证明', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('V', '人类遗传资源材料出口、出境证明', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('X', '有毒化学品环境管理放行通知单', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('Z', '赴境外加工光盘进口备案证明', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('b', '进口广播电影电视节目带(片)提取单', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('d', '援外项目任务通知函', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('f', '音像制品（成品）进口批准单', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('g', '技术出口合同登记证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('i', '技术出口许可证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('k', '民用爆炸物品进出口审批单', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('m', '银行调运人民币现钞进出境证明', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('n', '音像制品（版权引进）批准单', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('u', '钟乳石出口批件', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('z', '古生物化石出境批件', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('H', '港澳 OPA 纺织品证明', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('K', '深加工结转申请表', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('T', '银行调运现钞出境许可证', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('W', '麻醉药品进出口准许证', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('Y', '原产地证明', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('c', '内销征税联系单', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('e', '关税配额外优惠税率进口棉花配额证', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('h', '核增核扣表', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('q', '国别关税配额证明', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('r', '预归类标志', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('s', '适用ITA税率的商品用途认定证明', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('t', '关税配额证明', '海关总署', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('v', '自动进口许可证(加工贸易)', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('x', '出口许可证(加工贸易)', '南昌海关', '');
INSERT INTO `supervise_certificate_code` (`code`, `name`, `source`, `remark`)
VALUES ('y', '出口许可证(边境小额贸易)', '南昌海关', '');

DROP TABLE IF EXISTS `invoice_type_mapping`;
CREATE TABLE `invoice_type_mapping`
(
    `id`           BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `code`         CHAR(2)            DEFAULT '' COMMENT '小图发票编码',
    `name`         VARCHAR(128)       DEFAULT '' COMMENT '小图发票名称',
    `kjt_code`     VARCHAR(32)        DEFAULT '' COMMENT '跨境通发票编码',
    `remark`       VARCHAR(128)       DEFAULT '' COMMENT '备注',
    `deleted`      TINYINT(1) DEFAULT 0 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 CHARACTER SET utf8mb4 COLLATE utf8mb4_bin COMMENT='小图-kjt-发票类型映射表';

INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('01', '增值税专用发票', '1');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('02', '货物运输业增值税专用发票', '1');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('03', '机动车销售统一发票', '1');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('04', '增值税普通发票', '2');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('08', '增值税电子专用发票', '3');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('10', '增值税电子普通发票', '4');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('11', '卷式发票', '2');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('14', '通行费发票', '4');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('15', '二手车销售统一发票', '1');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('51', '电子发票(铁路电子客票)', '5');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('61', '电子发票(航空运输客票电子发票）', '5');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('81', '电子发票(增值税专用发票)', '3');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('82', '电子发票(普通发票)', '6');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('83', '机动车销售电子统一发票', '3');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('84', '二手车销售电子统一发票', '3');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('85', '纸质发票(增值税专用发票)', '1');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('86', '纸质发票(普通发票)', '2');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('87', '纸质发票(机动车销售统一发票)', '1');
INSERT INTO `invoice_type_mapping` (`code`, `name`, `kjt_code`)
VALUES ('88', '纸质发票(二手车销售统一发票)', '1');


ALTER TABLE sales_commodity MODIFY COLUMN number DECIMAL (17, 6) DEFAULT NULL;

ALTER TABLE purchase_commodity MODIFY COLUMN number DECIMAL (17, 6) DEFAULT 0;

ALTER TABLE purchase_order_commodity MODIFY COLUMN number DECIMAL (17, 6) DEFAULT 0;

ALTER TABLE purchase_order_invoice_commodity MODIFY COLUMN number DECIMAL (17, 6) DEFAULT 0;

ALTER TABLE sales_order_goods MODIFY COLUMN count DECIMAL (17, 6) DEFAULT 0;