
-- ./supplier_resource_price.json





drop table if exists supplier_resource_price;
CREATE TABLE supplier_resource_price
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `supplier_org_id` VARCHAR(32) NOT NULL  comment '供应商机构ID',
  `resource_type` VARCHAR(32) NOT NULL  comment '资源类型',
  `resource_ref_id` VARCHAR(32) NOT NULL  comment '资源引用ID',
  `resource_ref_name` VARCHAR(255)   comment '资源名称',
  `unit_price` double(15,8)   DEFAULT '0' comment '统一单价',
  `price_unit` VARCHAR(32) NOT NULL  comment '价格单位',
  `input_price` double(15,8)   comment '输入单价(LLM专用)',
  `output_price` double(15,8)   comment '输出单价(LLM专用)',
  `currency` CHAR(8)   DEFAULT 'CNY' comment '货币',
  `effective_date` date NOT NULL  comment '生效日期',
  `expiry_date` date   comment '失效日期',
  `status` CHAR(1)   DEFAULT '1' comment '状态',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
comment '供应商资源定价表'
;

CREATE  INDEX supplier_resource_price_idx_srp_supplier ON supplier_resource_price(supplier_org_id);
CREATE  INDEX supplier_resource_price_idx_srp_resource ON supplier_resource_price(resource_type,resource_ref_id);
CREATE  INDEX supplier_resource_price_idx_srp_dates ON supplier_resource_price(effective_date,expiry_date);
CREATE  INDEX supplier_resource_price_idx_srp_status ON supplier_resource_price(status);
CREATE UNIQUE INDEX supplier_resource_price_idx_srp_unique ON supplier_resource_price(supplier_org_id,resource_type,resource_ref_id,effective_date);

-- ./sub_resellers.json





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_resellerid` VARCHAR(32) NOT NULL  comment '二级分销商机构ID',
  `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   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
comment '二级分销商表'
;

CREATE  INDEX sub_resellers_idx_sr_reseller ON sub_resellers(resellerid);
CREATE UNIQUE INDEX sub_resellers_idx_sr_sub_resellerid ON sub_resellers(resellerid,sub_resellerid);

-- ./distribution_agreements.json





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   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
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);

-- ./sales_ledger.json





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   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
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_contract_items.json





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   comment '备注',
  `created_at` datetime NOT NULL  comment '创建时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
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);

-- ./supplychain_accounting.json





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   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
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);

-- ./platform_supply_products.json





drop table if exists platform_supply_products;
CREATE TABLE platform_supply_products
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `relation_id` VARCHAR(32) NOT NULL  comment '供销关系ID',
  `supplier_org_id` VARCHAR(32) NOT NULL  comment '供应方机构ID',
  `buyer_org_id` VARCHAR(32) NOT NULL  comment '需求方机构ID',
  `source_product_id` VARCHAR(32) NOT NULL  comment '供应方产品ID',
  `supply_price` double(15,4) NOT NULL  DEFAULT '0' comment '供货价',
  `suggested_retail_price` double(15,4)   comment '建议零售价',
  `discount` double(5,4)   DEFAULT '1.0000' comment '折扣率',
  `commission_rate` double(5,4)   DEFAULT '0.0000' comment '佣金率',
  `min_order_qty` int   comment '最小订货量',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `remark` longtext   comment '备注',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
comment '平台供销产品明细表'
;

CREATE  INDEX platform_supply_products_idx_psp_relation ON platform_supply_products(relation_id);
CREATE  INDEX platform_supply_products_idx_psp_supplier_product ON platform_supply_products(supplier_org_id,source_product_id);
CREATE  INDEX platform_supply_products_idx_psp_buyer ON platform_supply_products(buyer_org_id);
CREATE UNIQUE INDEX platform_supply_products_idx_psp_unique ON platform_supply_products(relation_id,source_product_id);

-- ./sub_distributors.json





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_distributorid` VARCHAR(32) NOT NULL  comment '二级分销商机构ID',
  `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   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  DEFAULT 'CURRENT_TIMESTAMP' comment '创建时间',
  `updated_at` datetime   DEFAULT 'CURRENT_TIMESTAMP' comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
comment '二级分销商表'
;

CREATE  INDEX sub_distributors_idx_sd_reseller ON sub_distributors(resellerid);
CREATE UNIQUE INDEX sub_distributors_idx_sd_sub_distributorid ON sub_distributors(resellerid,sub_distributorid);
CREATE  INDEX sub_distributors_idx_sd_manager ON sub_distributors(managed_by);

-- ./platform_supply_relations.json





drop table if exists platform_supply_relations;
CREATE TABLE platform_supply_relations
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `supplier_org_id` VARCHAR(32) NOT NULL  comment '供应方机构ID',
  `buyer_org_id` VARCHAR(32) NOT NULL  comment '需求方机构ID',
  `relation_code` VARCHAR(64) NOT NULL  comment '关系编号',
  `relation_name` VARCHAR(255) NOT NULL  comment '关系名称',
  `relation_type` VARCHAR(32) NOT NULL  DEFAULT 'distribution' comment '合作类型',
  `settlement_mode` VARCHAR(32) NOT NULL  DEFAULT 'discount' comment '结算方式',
  `default_discount` double(5,4)   DEFAULT '1.0000' comment '默认折扣率',
  `default_commission_rate` double(5,4)   DEFAULT '0.0000' comment '默认佣金率',
  `sign_date` date   comment '签署日期',
  `start_date` date NOT NULL  comment '生效日期',
  `end_date` date   comment '到期日期',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `remark` longtext   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
comment '平台内供销关系表'
;

CREATE  INDEX platform_supply_relations_idx_psr_supplier ON platform_supply_relations(supplier_org_id);
CREATE  INDEX platform_supply_relations_idx_psr_buyer ON platform_supply_relations(buyer_org_id);
CREATE  INDEX platform_supply_relations_idx_psr_pair ON platform_supply_relations(supplier_org_id,buyer_org_id);
CREATE UNIQUE INDEX platform_supply_relations_idx_psr_code ON platform_supply_relations(supplier_org_id,buyer_org_id,relation_code);
CREATE  INDEX platform_supply_relations_idx_psr_status ON platform_supply_relations(status);

-- ./suppliers.json





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 '银行账号',
  `orgid` VARCHAR(32) NOT NULL  comment '机构ID',
  `is_external` CHAR(1) NOT NULL  DEFAULT '1' comment '是否外部供应商',
  `settlement_cycle` VARCHAR(20)   comment '结算周期',
  `settlement_day` int   comment '结算日',
  `payment_type` VARCHAR(20)   comment '付款方式',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `remark` longtext   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
comment '供应商表'
;

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

-- ./product_supplier_mapping.json





drop table if exists product_supplier_mapping;
CREATE TABLE product_supplier_mapping
(

  `id` VARCHAR(32) NOT NULL  comment '主键ID',
  `product_id` VARCHAR(32) NOT NULL  comment '产品ID',
  `product_category_id` VARCHAR(32)   comment '产品分类ID',
  `supplier_type` VARCHAR(32) NOT NULL  DEFAULT 'internal' comment '供应商类型',
  `supplier_org_id` VARCHAR(32)   comment '供应方机构ID(平台内)',
  `external_supplier_id` VARCHAR(32)   comment '外部供应商ID',
  `buyer_org_id` VARCHAR(32) NOT NULL  comment '采购方机构ID',
  `supply_price` double(15,4)   comment '供货价',
  `currency` VARCHAR(16)   DEFAULT 'CNY' comment '币种',
  `min_order_qty` int   DEFAULT '1' comment '最小订货量',
  `lead_time_days` int   comment '交付周期(天)',
  `is_preferred` CHAR(1)   DEFAULT '0' comment '是否首选',
  `status` CHAR(1) NOT NULL  DEFAULT '1' comment '状态',
  `relation_id` VARCHAR(32)   comment '关联供销关系ID',
  `contract_id` VARCHAR(32)   comment '关联供销合同ID',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
comment '产品供应映射表'
;

CREATE  INDEX product_supplier_mapping_idx_psm_product ON product_supplier_mapping(product_id);
CREATE  INDEX product_supplier_mapping_idx_psm_supplier_org ON product_supplier_mapping(supplier_org_id);
CREATE  INDEX product_supplier_mapping_idx_psm_ext_supplier ON product_supplier_mapping(external_supplier_id);
CREATE  INDEX product_supplier_mapping_idx_psm_buyer ON product_supplier_mapping(buyer_org_id);
CREATE UNIQUE INDEX product_supplier_mapping_idx_psm_unique ON product_supplier_mapping(product_id,supplier_type,supplier_org_id,external_supplier_id,buyer_org_id);

-- ./distribution_agreement_items.json





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   comment '备注',
  `created_at` datetime NOT NULL  comment '创建时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
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);

-- ./supply_contracts.json





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   comment '备注',
  `created_by` VARCHAR(32)   comment '创建人',
  `created_at` datetime NOT NULL  comment '创建时间',
  `updated_at` datetime   comment '更新时间'


,primary key(id)


)
engine=innodb 
default charset=utf8 
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);

