
-- ./distribution_agreement_items.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists distribution_agreement_items;
CREATE TABLE distribution_agreement_items
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `agreement_id` VARCHAR(32) NOT NULL  comment '分销协议ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属分销商机构ID',
  `prodtypeid` VARCHAR(32)   comment '产品分类ID',
  `productid` VARCHAR(32)   comment '产品ID',
  `discount` double(5,4) NOT NULL  DEFAULT '1.0000' comment '分销折扣',
  `min_order_qty` int   comment '最小订购量',
  `sale_price` double(15,4)   comment '分销指导价',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_at` datetime NOT NULL  comment '创建时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '分销协议产品折扣明细表'
;

CREATE  INDEX distribution_agreement_items_idx_dai_agreement ON distribution_agreement_items(agreement_id);
CREATE  INDEX distribution_agreement_items_idx_dai_product ON distribution_agreement_items(agreement_id,prodtypeid,productid);

-- ./suppliers.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists suppliers;
CREATE TABLE suppliers
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属分销商机构ID',
  `supplier_code` VARCHAR(64) NOT NULL  comment '供应商编号',
  `supplier_name` VARCHAR(255) NOT NULL  comment '供应商名称',
  `contact_person` VARCHAR(100)   comment '联系人',
  `contact_phone` VARCHAR(50)   comment '联系电话',
  `contact_email` VARCHAR(255)   comment '联系邮箱',
  `address` VARCHAR(500)   comment '地址',
  `tax_number` VARCHAR(64)   comment '税号',
  `bank_name` VARCHAR(255)   comment '开户银行',
  `bank_account` VARCHAR(64)   comment '银行账号',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '供应商表'
;

CREATE  INDEX suppliers_idx_suppliers_reseller ON suppliers(resellerid);
CREATE UNIQUE INDEX suppliers_idx_suppliers_code ON suppliers(resellerid,supplier_code);

-- ./distribution_agreements.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists distribution_agreements;
CREATE TABLE distribution_agreements
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属分销商机构ID',
  `sub_reseller_id` VARCHAR(32) NOT NULL  comment '二级分销商ID',
  `agreement_code` VARCHAR(64) NOT NULL  comment '协议编号',
  `agreement_name` VARCHAR(255) NOT NULL  comment '协议名称',
  `sign_date` date   comment '签署日期',
  `start_date` date NOT NULL  comment '生效日期',
  `end_date` date   comment '到期日期',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `default_discount` double(5,4)   DEFAULT '1.0000' comment '默认分销折扣',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '分销协议表'
;

CREATE  INDEX distribution_agreements_idx_da_reseller ON distribution_agreements(resellerid);
CREATE  INDEX distribution_agreements_idx_da_sub_reseller ON distribution_agreements(sub_reseller_id);
CREATE UNIQUE INDEX distribution_agreements_idx_da_code ON distribution_agreements(resellerid,agreement_code);

-- ./sub_resellers.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists sub_resellers;
CREATE TABLE sub_resellers
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属分销商机构ID',
  `sub_reseller_code` VARCHAR(64) NOT NULL  comment '二级分销商编号',
  `sub_reseller_name` VARCHAR(255) NOT NULL  comment '二级分销商名称',
  `contact_person` VARCHAR(100)   comment '联系人',
  `contact_phone` VARCHAR(50)   comment '联系电话',
  `contact_email` VARCHAR(255)   comment '联系邮箱',
  `address` VARCHAR(500)   comment '地址',
  `tax_number` VARCHAR(64)   comment '税号',
  `bank_name` VARCHAR(255)   comment '开户银行',
  `bank_account` VARCHAR(64)   comment '银行账号',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '二级分销商表'
;

CREATE  INDEX sub_resellers_idx_sr_reseller ON sub_resellers(resellerid);
CREATE UNIQUE INDEX sub_resellers_idx_sr_code ON sub_resellers(resellerid,sub_reseller_code);

-- ./supplychain_accounting.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists supplychain_accounting;
CREATE TABLE supplychain_accounting
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属主分销商机构ID',
  `supply_contract_id` VARCHAR(32)   comment '供销合同ID',
  `supply_contract_item_id` VARCHAR(32)   comment '供销合同产品明细ID',
  `distribution_agreement_id` VARCHAR(32)   comment '分销协议ID',
  `distribution_agreement_item_id` VARCHAR(32)   comment '分销协议产品明细ID',
  `sub_distributor_id` VARCHAR(32)   comment '二级分销商ID',
  `supplier_id` VARCHAR(32)   comment '供应商ID',
  `prodtypeid` VARCHAR(32)   comment '产品分类ID',
  `productid` VARCHAR(32)   comment '产品ID',
  `quantity` double(15,4) NOT NULL  DEFAULT '0' comment '数量',
  `unit_price` double(15,4) NOT NULL  DEFAULT '0' comment '销售单价',
  `supply_discount` double(5,4)   comment '进货折扣',
  `supply_amount` double(15,2) NOT NULL  DEFAULT '0' comment '进货金额(应付供应商)',
  `dist_discount` double(5,4)   comment '分销折扣',
  `dist_amount` double(15,2) NOT NULL  DEFAULT '0' comment '分销金额(二级分销商应付)',
  `profit_amount` double(15,2) NOT NULL  DEFAULT '0' comment '利润金额',
  `sale_date` date NOT NULL  comment '销售日期',
  `source_type` CHAR(1)   DEFAULT '1' comment '来源类型',
  `source_id` VARCHAR(32)   comment '来源记录ID',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '供销记账表'
;

CREATE  INDEX supplychain_accounting_idx_sa_reseller ON supplychain_accounting(resellerid);
CREATE  INDEX supplychain_accounting_idx_sa_sale_date ON supplychain_accounting(sale_date);
CREATE  INDEX supplychain_accounting_idx_sa_product ON supplychain_accounting(productid);
CREATE  INDEX supplychain_accounting_idx_sa_subdist ON supplychain_accounting(sub_distributor_id);
CREATE  INDEX supplychain_accounting_idx_sa_supplier ON supplychain_accounting(supplier_id);
CREATE  INDEX supplychain_accounting_idx_sa_source ON supplychain_accounting(source_type,source_id);

-- ./sub_distributors.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists sub_distributors;
CREATE TABLE sub_distributors
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属主分销商机构ID',
  `sub_dist_code` VARCHAR(64) NOT NULL  comment '二级分销商编号',
  `sub_dist_name` VARCHAR(255) NOT NULL  comment '二级分销商名称',
  `contact_person` VARCHAR(100)   comment '联系人',
  `contact_phone` VARCHAR(50)   comment '联系电话',
  `contact_email` VARCHAR(255)   comment '联系邮箱',
  `address` VARCHAR(500)   comment '地址',
  `tax_number` VARCHAR(64)   comment '税号',
  `bank_name` VARCHAR(255)   comment '开户银行',
  `bank_account` VARCHAR(64)   comment '银行账号',
  `managed_by` VARCHAR(32)   comment '负责销售ID',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '二级分销商表'
;

CREATE  INDEX sub_distributors_idx_sd_reseller ON sub_distributors(resellerid);
CREATE UNIQUE INDEX sub_distributors_idx_sd_code ON sub_distributors(resellerid,sub_dist_code);
CREATE  INDEX sub_distributors_idx_sd_manager ON sub_distributors(managed_by);

-- ./sales_ledger.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists sales_ledger;
CREATE TABLE sales_ledger
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属分销商机构ID',
  `sub_reseller_id` VARCHAR(32)   comment '二级分销商ID',
  `supplier_id` VARCHAR(32)   comment '供应商ID',
  `agreement_id` VARCHAR(32)   comment '分销协议ID',
  `contract_id` VARCHAR(32)   comment '供销合同ID',
  `prodtypeid` VARCHAR(32)   comment '产品分类ID',
  `productid` VARCHAR(32)   comment '产品ID',
  `sale_date` date NOT NULL  comment '销售日期',
  `quantity` double(15,2) NOT NULL  comment '销售数量',
  `unit_price` double(15,4) NOT NULL  comment '销售单价',
  `supply_discount` double(5,4)   comment '进货折扣',
  `supply_amount` double(15,2)   comment '进货金额',
  `distribution_discount` double(5,4)   comment '分销折扣',
  `distribution_amount` double(15,2)   comment '分销金额',
  `profit_amount` double(15,2)   comment '利润金额',
  `settlement_status` CHAR(1) NOT NULL  DEFAULT '0' comment '结算状态',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '销售记账表'
;

CREATE  INDEX sales_ledger_idx_sl_reseller ON sales_ledger(resellerid);
CREATE  INDEX sales_ledger_idx_sl_sale_date ON sales_ledger(sale_date);
CREATE  INDEX sales_ledger_idx_sl_product ON sales_ledger(prodtypeid,productid);
CREATE  INDEX sales_ledger_idx_sl_sub_reseller ON sales_ledger(sub_reseller_id);
CREATE  INDEX sales_ledger_idx_sl_supplier ON sales_ledger(supplier_id);

-- ./supply_contracts.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists supply_contracts;
CREATE TABLE supply_contracts
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属分销商机构ID',
  `supplier_id` VARCHAR(32) NOT NULL  comment '供应商ID',
  `contract_code` VARCHAR(64) NOT NULL  comment '合同编号',
  `contract_name` VARCHAR(255) NOT NULL  comment '合同名称',
  `sign_date` date   comment '签署日期',
  `start_date` date NOT NULL  comment '生效日期',
  `end_date` date   comment '到期日期',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `default_discount` double(5,4)   DEFAULT '1.0000' comment '默认折扣',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '供销合同表'
;

CREATE  INDEX supply_contracts_idx_sc_reseller ON supply_contracts(resellerid);
CREATE  INDEX supply_contracts_idx_sc_supplier ON supply_contracts(supplier_id);
CREATE UNIQUE INDEX supply_contracts_idx_sc_code ON supply_contracts(resellerid,contract_code);

-- ./supply_contract_items.json





-- 建库时请用以下语句，支持emoji字符
-- CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
drop table if exists supply_contract_items;
CREATE TABLE supply_contract_items
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `contract_id` VARCHAR(32) NOT NULL  comment '供销合同ID',
  `resellerid` VARCHAR(32) NOT NULL  comment '所属分销商机构ID',
  `prodtypeid` VARCHAR(32)   comment '产品分类ID',
  `productid` VARCHAR(32)   comment '产品ID',
  `discount` double(5,4) NOT NULL  DEFAULT '1.0000' comment '进货折扣',
  `settlement_price` double(15,4)   comment '结算单价',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci   comment '备注',
  `created_at` datetime NOT NULL  comment '创建时间'


,primary key(id)


)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
engine=innodb 
comment '供销合同产品折扣明细表'
;

CREATE  INDEX supply_contract_items_idx_sci_contract ON supply_contract_items(contract_id);
CREATE  INDEX supply_contract_items_idx_sci_product ON supply_contract_items(contract_id,prodtypeid,productid);

