-- Module: appbase

-- Module: rbac

-- Module: customer_management
-- Table from customer_handover.json
CREATE TABLE IF NOT EXISTS `customer_handover` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `customer_id` VARCHAR(32) NOT NULL COMMENT '被交接的客户ID',
  `from_owner_id` VARCHAR(32) NOT NULL COMMENT '原客户负责人ID',
  `to_owner_id` VARCHAR(32) NOT NULL COMMENT '新客户负责人ID',
  `handover_reason` VARCHAR(100) NOT NULL COMMENT '交接触发原因：resignation=离职, position_change=岗位调整',
  `current_stage` VARCHAR(20) NOT NULL DEFAULT 'preparation' COMMENT '交接流程阶段：preparation=准备, review=审核, confirmation=确认, completed=完成',
  `reviewer_id` VARCHAR(32) COMMENT '负责审核交接清单的人员ID',
  `prepared_at` TIMESTAMP COMMENT '原负责人完成交接清单准备的时间',
  `reviewed_at` TIMESTAMP COMMENT '审核人完成审核的时间',
  `confirmed_at` TIMESTAMP COMMENT '接手人确认接收的时间',
  `completed_at` TIMESTAMP COMMENT '整个交接流程完成的时间',
  `created_at` TIMESTAMP NOT NULL COMMENT '交接流程创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '最后更新时间',
  `notes` TEXT COMMENT '交接过程中的备注信息',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户交接表';

CREATE INDEX `idx_handover_customer` ON `customer_handover` (`customer_id`);
CREATE INDEX `idx_handover_from_owner` ON `customer_handover` (`from_owner_id`);
CREATE INDEX `idx_handover_to_owner` ON `customer_handover` (`to_owner_id`);
CREATE INDEX `idx_handover_stage` ON `customer_handover` (`current_stage`);
CREATE INDEX `idx_handover_created` ON `customer_handover` (`created_at`);

-- Table from customer_handover_items.json
CREATE TABLE IF NOT EXISTS `customer_handover_items` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `handover_id` VARCHAR(32) NOT NULL COMMENT '关联的交接记录ID',
  `item_type` VARCHAR(50) NOT NULL COMMENT '交接项目类型：basic_info=基本信息, opportunities=未结商机, contracts=历史合同, service_tickets=服务工单, payment_issues=回款问题',
  `item_id` VARCHAR(32) COMMENT '关联的具体记录ID（如商机ID、合同ID等）',
  `item_description` TEXT NOT NULL COMMENT '项目详细描述或补充说明',
  `is_completed` VARCHAR(1) NOT NULL DEFAULT '0' COMMENT '是否已完成交接：1=是, 0=否',
  `created_at` TIMESTAMP NOT NULL COMMENT '项目创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '最后更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户交接项目明细表';

CREATE INDEX `idx_handover_items_handover` ON `customer_handover_items` (`handover_id`);
CREATE INDEX `idx_handover_items_type` ON `customer_handover_items` (`item_type`);
CREATE INDEX `idx_handover_items_item_id` ON `customer_handover_items` (`item_id`);

-- Table from customer_pool.json
CREATE TABLE IF NOT EXISTS `customer_pool` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `customer_id` VARCHAR(32) NOT NULL COMMENT '回收到公海的客户ID',
  `original_owner_id` VARCHAR(32) NOT NULL COMMENT '客户原来的负责人ID',
  `recycle_reason` VARCHAR(100) NOT NULL COMMENT '回收原因：inactive_days=未跟进天数超限, manual=手动回收',
  `inactive_days` INT COMMENT '触发回收的未跟进天数',
  `recycled_at` TIMESTAMP NOT NULL COMMENT '客户被回收到公海的时间',
  `assigned_to` VARCHAR(32) COMMENT '分配给的新负责人ID（如果已分配）',
  `assigned_at` TIMESTAMP COMMENT '客户被分配的时间',
  `pool_status` VARCHAR(20) NOT NULL DEFAULT 'available' COMMENT '公海状态：available=可领取, assigned=已分配, claimed=已认领',
  `created_at` TIMESTAMP NOT NULL COMMENT '记录创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户公海池表';

CREATE UNIQUE INDEX `idx_pool_customer` ON `customer_pool` (`customer_id`);
CREATE INDEX `idx_pool_original_owner` ON `customer_pool` (`original_owner_id`);
CREATE INDEX `idx_pool_assigned_to` ON `customer_pool` (`assigned_to`);
CREATE INDEX `idx_pool_status` ON `customer_pool` (`pool_status`);
CREATE INDEX `idx_pool_recycled` ON `customer_pool` (`recycled_at`);

-- Table from customers.json
CREATE TABLE IF NOT EXISTS `customers` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `customer_name` VARCHAR(255) NOT NULL COMMENT '客户公司名称或个人姓名',
  `customer_type` VARCHAR(20) NOT NULL COMMENT '客户类型：individual=个人, enterprise=企业',
  `phone` VARCHAR(20) COMMENT '客户手机号码',
  `email` VARCHAR(255) COMMENT '客户邮箱地址',
  `tax_id` VARCHAR(50) COMMENT '企业统一社会信用代码或税号',
  `industry` VARCHAR(100) COMMENT '客户所属行业',
  `customer_level` VARCHAR(20) NOT NULL DEFAULT 'potential' COMMENT '客户分级：important=重要, normal=普通, potential=潜在',
  `address` TEXT COMMENT '客户详细地址',
  `owner_id` VARCHAR(32) NOT NULL COMMENT '当前负责该客户的销售人员ID',
  `region` VARCHAR(100) COMMENT '客户所在区域',
  `last_follow_up` TIMESTAMP COMMENT '最后一次跟进时间',
  `created_at` TIMESTAMP NOT NULL COMMENT '客户档案创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '最后更新时间',
  `status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '客户状态：active=活跃, inactive=非活跃, in_pool=公海',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户档案表';

CREATE UNIQUE INDEX `idx_customers_phone` ON `customers` (`phone`);
CREATE UNIQUE INDEX `idx_customers_tax_id` ON `customers` (`tax_id`);
CREATE INDEX `idx_customers_owner` ON `customers` (`owner_id`);
CREATE INDEX `idx_customers_name` ON `customers` (`customer_name`);
CREATE INDEX `idx_customers_level` ON `customers` (`customer_level`);
CREATE INDEX `idx_customers_status` ON `customers` (`status`);
CREATE INDEX `idx_customers_last_follow` ON `customers` (`last_follow_up`);

-- Module: opportunity_management
-- Table from opportunities.json
CREATE TABLE IF NOT EXISTS `opportunities` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `customer_id` VARCHAR(32) NOT NULL COMMENT '关联客户管理模块的客户ID',
  `customer_name` VARCHAR(255) NOT NULL COMMENT '客户名称，必填字段',
  `opportunity_name` VARCHAR(255) NOT NULL COMMENT '商机标题或项目名称',
  `estimated_amount` DECIMAL(15,2) NOT NULL COMMENT '预估成交金额，必填字段',
  `current_stage` VARCHAR(50) NOT NULL COMMENT '当前所处的销售阶段，必填字段',
  `expected_close_date` DATE NOT NULL COMMENT '预计成交日期，必填字段',
  `source_type` VARCHAR(20) NOT NULL DEFAULT 'manual' COMMENT 'manual=手动录入, lead=线索转化',
  `owner_id` VARCHAR(32) NOT NULL COMMENT '负责该商机的销售人员ID',
  `owner_name` VARCHAR(100) NOT NULL COMMENT '负责该商机的销售人员姓名',
  `region` VARCHAR(100) COMMENT '商机所属区域，用于分析筛选',
  `description` TEXT COMMENT '商机详细描述信息',
  `probability` FLOAT(5,2) NOT NULL DEFAULT '0.00' COMMENT '基于历史转化率计算的成交概率百分比',
  `predicted_revenue` DECIMAL(15,2) NOT NULL DEFAULT '0.00' COMMENT '基于成交概率计算的预测收入',
  `created_at` TIMESTAMP NOT NULL,
  `updated_at` TIMESTAMP NOT NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT 'active=活跃, won=已成交, lost=已丢失, closed=已关闭',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商机表';

CREATE INDEX `idx_opportunities_customer` ON `opportunities` (`customer_id`);
CREATE INDEX `idx_opportunities_owner` ON `opportunities` (`owner_id`);
CREATE INDEX `idx_opportunities_stage` ON `opportunities` (`current_stage`);
CREATE INDEX `idx_opportunities_region` ON `opportunities` (`region`);
CREATE INDEX `idx_opportunities_status` ON `opportunities` (`status`);
CREATE INDEX `idx_opportunities_expected_close` ON `opportunities` (`expected_close_date`);

-- Table from opportunity_predictions.json
CREATE TABLE IF NOT EXISTS `opportunity_predictions` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `opportunity_id` VARCHAR(32) NOT NULL COMMENT '关联的商机ID',
  `predicted_amount` DECIMAL(15,2) NOT NULL DEFAULT '0.00' COMMENT '基于历史转化率计算的预测成交金额',
  `confidence_level` DECIMAL(5,4) NOT NULL DEFAULT '0.0000' COMMENT '预测的置信度（0-1）',
  `prediction_date` DATE NOT NULL COMMENT '预测生成日期',
  `actual_amount` DECIMAL(15,2) COMMENT '实际成交金额（成交后更新）',
  `deviation_rate` DECIMAL(5,4) COMMENT '预测与实际的偏差率',
  `created_at` TIMESTAMP NOT NULL COMMENT '记录创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商机预测表';

CREATE UNIQUE INDEX `idx_predictions_opportunity` ON `opportunity_predictions` (`opportunity_id`, `prediction_date`);
CREATE INDEX `idx_predictions_date` ON `opportunity_predictions` (`prediction_date`);

-- Table from opportunity_stage_history.json
CREATE TABLE IF NOT EXISTS `opportunity_stage_history` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `opportunity_id` VARCHAR(32) NOT NULL COMMENT '关联的商机ID',
  `from_stage` VARCHAR(50) COMMENT '变更前的销售阶段',
  `to_stage` VARCHAR(50) NOT NULL COMMENT '变更后的销售阶段',
  `change_reason` TEXT NOT NULL COMMENT '阶段变更的原因说明，必填字段',
  `changed_by_id` VARCHAR(32) NOT NULL COMMENT '执行阶段变更的用户ID',
  `changed_by_name` VARCHAR(100) NOT NULL COMMENT '执行阶段变更的用户姓名',
  `changed_at` TIMESTAMP NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商机阶段变更历史表';

CREATE INDEX `idx_stage_history_opportunity` ON `opportunity_stage_history` (`opportunity_id`);
CREATE INDEX `idx_stage_history_changed_by` ON `opportunity_stage_history` (`changed_by_id`);
CREATE INDEX `idx_stage_history_changed_at` ON `opportunity_stage_history` (`changed_at`);

-- Table from sales_stages.json
CREATE TABLE IF NOT EXISTS `sales_stages` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `stage_name` VARCHAR(100) NOT NULL COMMENT '销售阶段名称，如\'初步接洽\'、\'需求确认\'等',
  `stage_order` INT NOT NULL COMMENT '阶段在销售漏斗中的顺序，从小到大',
  `conversion_rate` FLOAT(5,2) NOT NULL DEFAULT '0.00' COMMENT '该阶段到下一阶段的历史平均转化率',
  `is_won_stage` VARCHAR(5) NOT NULL DEFAULT 'no' COMMENT 'yes=成交阶段, no=非成交阶段',
  `is_lost_stage` VARCHAR(5) NOT NULL DEFAULT 'no' COMMENT 'yes=丢失阶段, no=非丢失阶段',
  `created_at` TIMESTAMP NOT NULL,
  `updated_at` TIMESTAMP NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='销售阶段配置表';

CREATE UNIQUE INDEX `idx_sales_stages_order` ON `sales_stages` (`stage_order`);
CREATE UNIQUE INDEX `idx_sales_stages_name` ON `sales_stages` (`stage_name`);

-- Module: contract_management
-- Table from contract.json
CREATE TABLE IF NOT EXISTS `contract` (
  `id` VARCHAR(32) NOT NULL COMMENT '合同ID，主键',
  `contract_number` VARCHAR(50) NOT NULL COMMENT '合同编号，唯一标识',
  `title` VARCHAR(200) NOT NULL COMMENT '合同标题',
  `party_a` VARCHAR(100) NOT NULL COMMENT '甲方（我方）',
  `party_b` VARCHAR(100) NOT NULL COMMENT '乙方（对方）',
  `contract_type` VARCHAR(32) NOT NULL COMMENT '合同类型，引用appcodes表的id',
  `status` VARCHAR(20) NOT NULL DEFAULT 'draft' COMMENT '合同状态：draft-草稿, active-生效, expired-过期, terminated-终止',
  `amount` DECIMAL(15,2) COMMENT '合同金额',
  `start_date` DATE NOT NULL COMMENT '合同开始日期',
  `end_date` DATE NOT NULL COMMENT '合同结束日期',
  `sign_date` DATE COMMENT '签署日期',
  `owner_id` VARCHAR(32) NOT NULL COMMENT '合同负责人，引用users表的id',
  `org_id` VARCHAR(32) NOT NULL COMMENT '所属组织，引用organization表的id',
  `ai_compliance_result` TEXT COMMENT 'AI合规检查结果',
  `ai_key_dates` TEXT COMMENT 'AI提取的关键时点JSON数据',
  `payment_terms` TEXT COMMENT '付款节点规则，如\'30%预付款+50%进度款+20%尾款\'',
  `credit_period` INT COMMENT '账期天数',
  `penalty_clause` TEXT COMMENT '违约金相关条款',
  `opportunity_id` VARCHAR(32) COMMENT '关联的商机ID',
  `customer_id` VARCHAR(32) COMMENT '关联的客户ID',
  `tax_rate` DECIMAL(5,4) DEFAULT '0.1300' COMMENT '税率，默认13%',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='合同表';

CREATE UNIQUE INDEX `idx_contract_number` ON `contract` (`contract_number`);
CREATE INDEX `idx_contract_org` ON `contract` (`org_id`);
CREATE INDEX `idx_contract_status` ON `contract` (`status`);

-- Table from contract_ai_config.json
CREATE TABLE IF NOT EXISTS `contract_ai_config` (
  `id` VARCHAR(32) NOT NULL COMMENT 'AI配置ID',
  `ai_service_url` VARCHAR(500) NOT NULL COMMENT 'AI服务URL地址',
  `api_key` VARCHAR(255) NOT NULL COMMENT 'API密钥',
  `org_id` VARCHAR(32) NOT NULL COMMENT '所属组织',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='AI配置表';

CREATE UNIQUE INDEX `idx_ai_config_org` ON `contract_ai_config` (`org_id`);

-- Table from contract_attachment.json
CREATE TABLE IF NOT EXISTS `contract_attachment` (
  `id` VARCHAR(32) NOT NULL COMMENT '附件ID，主键',
  `contract_id` VARCHAR(32) NOT NULL COMMENT '关联的合同ID，引用contract表的id',
  `file_name` VARCHAR(255) NOT NULL COMMENT '文件名',
  `file_path` VARCHAR(500) NOT NULL COMMENT '文件存储路径',
  `file_size` INT NOT NULL COMMENT '文件大小（字节）',
  `file_type` VARCHAR(50) NOT NULL COMMENT '文件类型（MIME类型）',
  `version` INT NOT NULL DEFAULT '1' COMMENT '文件版本号',
  `description` VARCHAR(200) COMMENT '附件描述',
  `uploaded_by` VARCHAR(32) NOT NULL COMMENT '上传人，引用users表的id',
  `org_id` VARCHAR(32) NOT NULL COMMENT '所属组织',
  `created_at` TIMESTAMP NOT NULL COMMENT '上传时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='合同附件表';

CREATE INDEX `idx_attachment_contract` ON `contract_attachment` (`contract_id`);
CREATE INDEX `idx_attachment_org` ON `contract_attachment` (`org_id`);
CREATE INDEX `idx_attachment_version` ON `contract_attachment` (`file_name`, `version`);

-- Table from contract_milestones.json
CREATE TABLE IF NOT EXISTS `contract_milestones` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `contract_id` VARCHAR(32) NOT NULL COMMENT '关联的合同ID',
  `milestone_name` VARCHAR(100) NOT NULL COMMENT '里程碑名称，如\'预付款到账\'、\'产品交付\'、\'验收完成\'',
  `milestone_type` VARCHAR(20) NOT NULL COMMENT '里程碑类型：payment=付款, delivery=交付, acceptance=验收',
  `planned_date` DATE NOT NULL COMMENT '计划完成日期',
  `actual_date` DATE COMMENT '实际完成日期',
  `amount` DECIMAL(15,2) COMMENT '该里程碑关联的金额',
  `status` VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT '状态：pending=待处理, completed=已完成, overdue=已逾期',
  `description` TEXT COMMENT '里程碑详细描述',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '最后更新时间',
  `reminder_sent` VARCHAR(1) NOT NULL DEFAULT '0' COMMENT '逾期提醒是否已发送：1=是, 0=否',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='合同里程碑管理表';

CREATE INDEX `idx_milestones_contract` ON `contract_milestones` (`contract_id`);
CREATE INDEX `idx_milestones_status` ON `contract_milestones` (`status`);
CREATE INDEX `idx_milestones_planned` ON `contract_milestones` (`planned_date`);

-- Table from contract_versions.json
CREATE TABLE IF NOT EXISTS `contract_versions` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `contract_id` VARCHAR(32) NOT NULL COMMENT '关联的合同ID',
  `version_number` VARCHAR(20) NOT NULL COMMENT '版本号，如v1.0, v1.1等',
  `content` TEXT NOT NULL COMMENT '合同完整内容或差异内容',
  `diff_content` TEXT COMMENT '与上一版本的差异内容（HTML格式）',
  `modified_by` VARCHAR(32) NOT NULL COMMENT '修改人用户ID',
  `modified_reason` TEXT COMMENT '版本修改原因',
  `created_at` TIMESTAMP NOT NULL COMMENT '版本创建时间',
  `is_current` VARCHAR(1) NOT NULL DEFAULT '0' COMMENT '是否为当前生效版本：1=是, 0=否',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='合同版本管理表';

CREATE INDEX `idx_contract_versions_contract` ON `contract_versions` (`contract_id`);
CREATE UNIQUE INDEX `idx_contract_versions_version` ON `contract_versions` (`contract_id`, `version_number`);
CREATE INDEX `idx_contract_versions_current` ON `contract_versions` (`contract_id`, `is_current`);

-- Table from order_payments.json
CREATE TABLE IF NOT EXISTS `order_payments` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `order_id` VARCHAR(32) NOT NULL COMMENT '关联的订单ID',
  `payment_amount` DECIMAL(15,2) NOT NULL COMMENT '本次付款金额',
  `payment_date` DATE NOT NULL COMMENT '付款日期',
  `payment_method` VARCHAR(50) COMMENT '付款方式',
  `payment_reference` VARCHAR(100) COMMENT '付款凭证号或参考号',
  `status` VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT '状态：pending=待确认, confirmed=已确认, rejected=已拒绝',
  `notes` TEXT COMMENT '付款备注',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `confirmed_at` TIMESTAMP COMMENT '付款确认时间',
  `confirmed_by` VARCHAR(32) COMMENT '付款确认人ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单付款记录表';

CREATE INDEX `idx_payments_order` ON `order_payments` (`order_id`);
CREATE INDEX `idx_payments_status` ON `order_payments` (`status`);
CREATE INDEX `idx_payments_date` ON `order_payments` (`payment_date`);

-- Table from orders.json
CREATE TABLE IF NOT EXISTS `orders` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键 - UUID格式',
  `order_number` VARCHAR(50) NOT NULL COMMENT '订单编号，唯一标识',
  `contract_id` VARCHAR(32) NOT NULL COMMENT '关联的合同ID',
  `customer_id` VARCHAR(32) NOT NULL COMMENT '客户ID',
  `order_type` VARCHAR(20) NOT NULL COMMENT '订单类型：advance=预付款, progress=进度款, final=尾款, other=其他',
  `delivery_batch` VARCHAR(100) COMMENT '交付批次，如\'Q3季度服务交付\'',
  `acceptance_deadline` DATE COMMENT '验收截止日期',
  `amount` DECIMAL(15,2) NOT NULL COMMENT '该订单的金额',
  `paid_amount` DECIMAL(15,2) NOT NULL DEFAULT '0.00' COMMENT '已支付金额',
  `tax_rate` DECIMAL(5,4) NOT NULL DEFAULT '0.1300' COMMENT '税率',
  `credit_period` INT COMMENT '账期天数要求',
  `status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '状态：active=活跃, completed=完成, cancelled=取消',
  `description` TEXT COMMENT '订单详细描述',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '最后更新时间',
  `owner_id` VARCHAR(32) NOT NULL COMMENT '订单负责人ID',
  `org_id` VARCHAR(32) NOT NULL COMMENT '所属组织ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单表';

CREATE UNIQUE INDEX `idx_orders_number` ON `orders` (`order_number`);
CREATE INDEX `idx_orders_contract` ON `orders` (`contract_id`);
CREATE INDEX `idx_orders_customer` ON `orders` (`customer_id`);
CREATE INDEX `idx_orders_status` ON `orders` (`status`);
CREATE INDEX `idx_orders_owner` ON `orders` (`owner_id`);

-- Module: financial_management
-- Table from financial_vouchers.json
CREATE TABLE IF NOT EXISTS `financial_vouchers` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键',
  `voucher_number` VARCHAR(64) COMMENT '凭证编号',
  `voucher_type` VARCHAR(32) COMMENT '凭证类型: receipt(收款), payment(支出)',
  `contract_id` VARCHAR(64) COMMENT '关联合同ID',
  `order_id` VARCHAR(64) NOT NULL COMMENT '关联订单ID（可为空，用于合同级凭证）',
  `amount` DECIMAL(15,2) COMMENT '凭证金额',
  `voucher_date` DATE COMMENT '凭证日期',
  `description` VARCHAR(500) COMMENT '凭证描述，包含合同编号和订单编号',
  `reference_id` VARCHAR(64) COMMENT '引用的收款或支出记录ID',
  `org_id` VARCHAR(64) COMMENT '组织ID，用于多租户隔离',
  `created_at` TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='财务凭证';

CREATE INDEX `idx_vouchers_contract_id` ON `financial_vouchers` (`contract_id`);
CREATE INDEX `idx_vouchers_order_id` ON `financial_vouchers` (`order_id`);
CREATE UNIQUE INDEX `idx_vouchers_voucher_number` ON `financial_vouchers` (`voucher_number`, `org_id`);
CREATE INDEX `idx_vouchers_org_id` ON `financial_vouchers` (`org_id`);
CREATE INDEX `idx_vouchers_type` ON `financial_vouchers` (`voucher_type`);

-- Table from payments.json
CREATE TABLE IF NOT EXISTS `payments` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键',
  `payment_number` VARCHAR(64) COMMENT '支出编号',
  `contract_id` VARCHAR(64) COMMENT '关联合同ID（必须是已核销收款的合同）',
  `vendor_id` VARCHAR(64) COMMENT '供应商ID',
  `payment_amount` DECIMAL(15,2) COMMENT '支出金额',
  `payment_date` DATE COMMENT '实际支出日期',
  `payment_method` VARCHAR(32) COMMENT '支出方式: bank_transfer, cash, check, other',
  `payment_status` VARCHAR(32) COMMENT '状态: pending(待处理), processed(已处理), verified(已核销)',
  `description` VARCHAR(500) NOT NULL COMMENT '备注信息',
  `approved_by` VARCHAR(64) NOT NULL COMMENT '审批人ID',
  `created_by` VARCHAR(64) COMMENT '财务人员ID',
  `org_id` VARCHAR(64) COMMENT '组织ID，用于多租户隔离',
  `created_at` TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='支出记录';

CREATE INDEX `idx_payments_contract_id` ON `payments` (`contract_id`);
CREATE UNIQUE INDEX `idx_payments_payment_number` ON `payments` (`payment_number`, `org_id`);
CREATE INDEX `idx_payments_org_id` ON `payments` (`org_id`);
CREATE INDEX `idx_payments_status` ON `payments` (`payment_status`);

-- Table from receipt_allocations.json
CREATE TABLE IF NOT EXISTS `receipt_allocations` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键',
  `receipt_id` VARCHAR(64) COMMENT '关联的收款记录ID',
  `order_id` VARCHAR(64) COMMENT '关联的订单ID',
  `receivable_id` VARCHAR(64) COMMENT '关联的应收记录ID',
  `allocated_amount` DECIMAL(15,2) COMMENT '分配给该订单的金额',
  `allocation_percentage` DECIMAL(5,4) NOT NULL COMMENT '分配比例（0-1之间）',
  `contract_id` VARCHAR(64) COMMENT '关联合同ID',
  `org_id` VARCHAR(64) COMMENT '组织ID，用于多租户隔离',
  `created_at` TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收款分配';

CREATE INDEX `idx_allocations_receipt_id` ON `receipt_allocations` (`receipt_id`);
CREATE INDEX `idx_allocations_order_id` ON `receipt_allocations` (`order_id`);
CREATE INDEX `idx_allocations_receivable_id` ON `receipt_allocations` (`receivable_id`);
CREATE INDEX `idx_allocations_org_id` ON `receipt_allocations` (`org_id`);

-- Table from receipts.json
CREATE TABLE IF NOT EXISTS `receipts` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键',
  `receipt_number` VARCHAR(64) COMMENT '收款编号',
  `customer_id` VARCHAR(64) COMMENT '客户ID',
  `total_amount` DECIMAL(15,2) COMMENT '本次收款总金额',
  `receipt_date` DATE COMMENT '实际收款日期',
  `receipt_method` VARCHAR(32) COMMENT '收款方式: bank_transfer, cash, check, other',
  `receipt_status` VARCHAR(32) COMMENT '状态: pending(待处理), processed(已处理), verified(已核销)',
  `description` VARCHAR(500) NOT NULL COMMENT '备注信息',
  `created_by` VARCHAR(64) COMMENT '财务人员ID',
  `org_id` VARCHAR(64) COMMENT '组织ID，用于多租户隔离',
  `created_at` TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收款记录';

CREATE INDEX `idx_receipts_customer_id` ON `receipts` (`customer_id`);
CREATE UNIQUE INDEX `idx_receipts_receipt_number` ON `receipts` (`receipt_number`, `org_id`);
CREATE INDEX `idx_receipts_org_id` ON `receipts` (`org_id`);
CREATE INDEX `idx_receipts_status` ON `receipts` (`receipt_status`);

-- Table from receivables.json
CREATE TABLE IF NOT EXISTS `receivables` (
  `id` VARCHAR(64) NOT NULL COMMENT '主键',
  `order_id` VARCHAR(64) COMMENT '关联的订单ID',
  `contract_id` VARCHAR(64) COMMENT '关联合同ID',
  `customer_id` VARCHAR(64) COMMENT '客户ID',
  `receivable_amount` DECIMAL(15,2) COMMENT '订单应收金额',
  `received_amount` DECIMAL(15,2) COMMENT '已收款金额，默认为0',
  `receivable_date` DATE COMMENT '应收日期',
  `due_date` DATE NOT NULL COMMENT '账期到期日期',
  `credit_period` INT NOT NULL COMMENT '账期天数',
  `status` VARCHAR(32) COMMENT '状态: pending(待收), partial(部分收款), completed(已完成), overdue(逾期)',
  `sales_owner_id` VARCHAR(64) NOT NULL COMMENT '负责该订单跟进的销售ID',
  `org_id` VARCHAR(64) COMMENT '组织ID，用于多租户隔离',
  `created_at` TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='应收记录';

CREATE INDEX `idx_receivables_order_id` ON `receivables` (`order_id`);
CREATE INDEX `idx_receivables_contract_id` ON `receivables` (`contract_id`);
CREATE INDEX `idx_receivables_customer_id` ON `receivables` (`customer_id`);
CREATE INDEX `idx_receivables_status` ON `receivables` (`status`);
CREATE INDEX `idx_receivables_org_id` ON `receivables` (`org_id`);
CREATE INDEX `idx_receivables_due_date` ON `receivables` (`due_date`);

-- Module: workflow_approval
-- Table from approval_instance.json
CREATE TABLE IF NOT EXISTS `approval_instance` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `workflow_id` VARCHAR(32) NOT NULL COMMENT '关联的工作流定义',
  `module_type` VARCHAR(50) NOT NULL COMMENT 'customer/opportunity/contract/financial',
  `module_record_id` VARCHAR(32) NOT NULL COMMENT '关联的具体业务记录ID',
  `current_step_id` VARCHAR(32) COMMENT '当前待审批的步骤',
  `status` VARCHAR(20) NOT NULL COMMENT 'pending/approved/rejected/cancelled',
  `initiator_id` VARCHAR(32) NOT NULL COMMENT '审批发起人用户ID',
  `title` VARCHAR(200) NOT NULL COMMENT '审批标题',
  `description` VARCHAR(1000) COMMENT '审批详细描述',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `completed_at` TIMESTAMP COMMENT '完成时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批实例';

CREATE INDEX `idx_instance_workflow` ON `approval_instance` (`workflow_id`);
CREATE INDEX `idx_instance_module` ON `approval_instance` (`module_type`, `module_record_id`);
CREATE INDEX `idx_instance_status` ON `approval_instance` (`status`);
CREATE INDEX `idx_instance_org` ON `approval_instance` (`org_id`);

-- Table from approval_step.json
CREATE TABLE IF NOT EXISTS `approval_step` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `workflow_id` VARCHAR(32) NOT NULL COMMENT '关联的工作流',
  `step_name` VARCHAR(100) NOT NULL COMMENT '审批步骤名称',
  `step_order` INT NOT NULL COMMENT '步骤执行顺序',
  `approver_type` VARCHAR(20) NOT NULL COMMENT 'role/user/department/dynamic',
  `approver_value` VARCHAR(100) COMMENT '角色ID/用户ID/部门ID/动态表达式',
  `approval_type` VARCHAR(20) NOT NULL COMMENT 'single/multiple/sequential/parallel',
  `timeout_hours` INT COMMENT '审批超时时间（小时）',
  `description` VARCHAR(500) COMMENT '步骤描述',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批步骤';

CREATE INDEX `idx_step_workflow` ON `approval_step` (`workflow_id`);
CREATE INDEX `idx_step_order` ON `approval_step` (`workflow_id`, `step_order`);

-- Table from approval_task.json
CREATE TABLE IF NOT EXISTS `approval_task` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `instance_id` VARCHAR(32) NOT NULL COMMENT '关联的审批实例',
  `step_id` VARCHAR(32) NOT NULL COMMENT '关联的审批步骤',
  `approver_id` VARCHAR(32) NOT NULL COMMENT '具体审批人用户ID',
  `status` VARCHAR(20) NOT NULL COMMENT 'pending/approved/rejected',
  `decision` VARCHAR(1000) COMMENT '审批意见',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  `assigned_at` TIMESTAMP NOT NULL COMMENT '任务分配时间',
  `completed_at` TIMESTAMP COMMENT '任务完成时间',
  `due_at` TIMESTAMP COMMENT '任务截止时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批任务';

CREATE INDEX `idx_task_instance` ON `approval_task` (`instance_id`);
CREATE INDEX `idx_task_approver` ON `approval_task` (`approver_id`);
CREATE INDEX `idx_task_status` ON `approval_task` (`status`);

-- Table from approval_workflow.json
CREATE TABLE IF NOT EXISTS `approval_workflow` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `workflow_name` VARCHAR(100) NOT NULL COMMENT '审批工作流名称',
  `module_type` VARCHAR(50) NOT NULL COMMENT '关联的模块类型(customer/opportunity/contract/financial)',
  `trigger_condition` VARCHAR(500) COMMENT 'JSON格式的触发条件表达式',
  `description` VARCHAR(500) COMMENT '工作流描述',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '更新时间',
  `is_active` VARCHAR(1) NOT NULL COMMENT 'Y/N',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批工作流';

CREATE INDEX `idx_workflow_org` ON `approval_workflow` (`org_id`);
CREATE INDEX `idx_workflow_module` ON `approval_workflow` (`module_type`);
CREATE UNIQUE INDEX `uk_workflow_name_org` ON `approval_workflow` (`workflow_name`, `org_id`);

-- Module: unified_dashboard
-- Table from dashboard_config.json
CREATE TABLE IF NOT EXISTS `dashboard_config` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `dashboard_name` VARCHAR(100) COMMENT '仪表板显示名称',
  `dashboard_type` VARCHAR(50) COMMENT 'sales/finance/customer/executive',
  `config_json` VARCHAR(2000) COMMENT '仪表板布局和组件配置',
  `is_default` VARCHAR(1) COMMENT 'Y/N',
  `org_id` VARCHAR(32) COMMENT '多租户组织隔离',
  `created_by` VARCHAR(32) COMMENT '创建用户ID',
  `created_at` TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='仪表板配置';

CREATE INDEX `idx_dashboard_org` ON `dashboard_config` (`org_id`);
CREATE INDEX `idx_dashboard_type` ON `dashboard_config` (`dashboard_type`);
CREATE UNIQUE INDEX `uk_dashboard_name_org` ON `dashboard_config` (`dashboard_name`, `org_id`);

-- Table from report_template.json
CREATE TABLE IF NOT EXISTS `report_template` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `template_name` VARCHAR(100) COMMENT '报表模板名称',
  `report_type` VARCHAR(50) COMMENT 'sales/finance/customer/contract',
  `sql_query` VARCHAR(2000) COMMENT '报表数据查询SQL',
  `columns_config` VARCHAR(1000) NOT NULL COMMENT 'JSON格式的列配置',
  `filters_config` VARCHAR(1000) NOT NULL COMMENT 'JSON格式的过滤器配置',
  `chart_config` VARCHAR(1000) NOT NULL COMMENT 'JSON格式的图表配置',
  `org_id` VARCHAR(32) COMMENT '多租户组织隔离',
  `created_by` VARCHAR(32) COMMENT '创建用户ID',
  `created_at` TIMESTAMP COMMENT '创建时间',
  `is_active` VARCHAR(1) COMMENT 'Y/N',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报表模板';

CREATE INDEX `idx_template_org` ON `report_template` (`org_id`);
CREATE INDEX `idx_template_type` ON `report_template` (`report_type`);

-- Table from user_dashboard.json
CREATE TABLE IF NOT EXISTS `user_dashboard` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `user_id` VARCHAR(32) COMMENT '关联用户',
  `dashboard_config_id` VARCHAR(32) COMMENT '关联的仪表板配置',
  `layout_json` VARCHAR(2000) NOT NULL COMMENT '用户自定义布局',
  `is_favorite` VARCHAR(1) COMMENT 'Y/N',
  `org_id` VARCHAR(32) COMMENT '多租户组织隔离',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户仪表板';

CREATE UNIQUE INDEX `idx_user_dashboard_user` ON `user_dashboard` (`user_id`, `dashboard_config_id`);
CREATE INDEX `idx_user_dashboard_org` ON `user_dashboard` (`org_id`);

-- Module: rbac (from xlsx)

-- Module: appbase (from xlsx)

-- Module: rbac (from xlsx)
CREATE TABLE IF NOT EXISTS `audit_log` (
  `id` VARCHAR(255) NOT NULL,
  `permid` VARCHAR(255),
  `userid` VARCHAR(255),
  `params_kw` TEXT,
  `exe_date` DATE,
  `exe_timestamp` TIMESTAMP,
  `remote_ip` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审计日志';

CREATE INDEX `idx1` ON `audit_log` (`index:permid`);
CREATE INDEX `idx2` ON `audit_log` (`index:userid`);

CREATE TABLE IF NOT EXISTS `organization` (
  `id` VARCHAR(255) NOT NULL,
  `orgname` VARCHAR(255),
  `orgabbr` VARCHAR(255),
  `alias_name` VARCHAR(255),
  `contactor` VARCHAR(255),
  `contactor_phone` VARCHAR(255),
  `province_id` VARCHAR(255),
  `city_id` VARCHAR(255),
  `distinct_id` VARCHAR(255),
  `emailaddress` VARCHAR(255),
  `address` VARCHAR(255),
  `main_business` VARCHAR(255),
  `orgcode` VARCHAR(255) COMMENT '个人客户存身份证',
  `license_img` VARCHAR(255) COMMENT '个人客户存身份证照片',
  `id_img` VARCHAR(255) COMMENT '个人客户存身份证背面照片',
  `parentid` VARCHAR(255),
  `org_type` VARCHAR(255) COMMENT '0:业主机构；1:分销商；2:公司客户；3:个人客户；4：供应商',
  `sitename` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='机构';

CREATE INDEX `idx1` ON `organization` (`unique:orgname`);

CREATE TABLE IF NOT EXISTS `orgtypes` (
  `id` VARCHAR(255) NOT NULL,
  `orgid` VARCHAR(255),
  `orgtypeid` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='机构拥有角色';


CREATE TABLE IF NOT EXISTS `permission` (
  `id` VARCHAR(255) NOT NULL,
  `name` VARCHAR(255),
  `description` VARCHAR(255),
  `parentid` VARCHAR(255),
  `path` VARCHAR(255),
  `icon` VARCHAR(255),
  `permtype` VARCHAR(255),
  `need_audit` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='权限';

CREATE INDEX `idx1` ON `permission` (`unique:path`);

CREATE TABLE IF NOT EXISTS `role` (
  `id` VARCHAR(255) NOT NULL,
  `orgtypeid` VARCHAR(255) DEFAULT '0',
  `name` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT=' 角色';


CREATE TABLE IF NOT EXISTS `rolepermission` (
  `id` VARCHAR(255) NOT NULL,
  `roleid` VARCHAR(255),
  `permid` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色权限表';


CREATE TABLE IF NOT EXISTS `userapp` (
  `id` VARCHAR(255) NOT NULL,
  `userid` VARCHAR(255) DEFAULT '0',
  `appname` VARCHAR(255),
  `apikey` VARCHAR(255),
  `enabled_date` DATE,
  `expired_date` DATE,
  `allowed_ips` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户应用';


CREATE TABLE IF NOT EXISTS `userdepartment` (
  `id` VARCHAR(255) NOT NULL,
  `userid` VARCHAR(255) COMMENT '一个用户可以属于多个部门',
  `depid` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户部门表';


CREATE TABLE IF NOT EXISTS `userrole` (
  `id` VARCHAR(255) NOT NULL,
  `userid` VARCHAR(255),
  `roleid` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户角色';


-- Module: appbase (from xlsx)
CREATE TABLE IF NOT EXISTS `appcodes` (
  `id` VARCHAR(255) NOT NULL,
  `name` VARCHAR(255),
  `hierarchy_flg` VARCHAR(255) COMMENT '0:无层次',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='应用编码表';


CREATE TABLE IF NOT EXISTS `appcodes_kv` (
  `id` VARCHAR(255) NOT NULL,
  `parentid` VARCHAR(255),
  `k` VARCHAR(255),
  `v` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='编码键值表';

CREATE INDEX `idx1` ON `appcodes_kv` (`unique:parentid`, `k`);
CREATE INDEX `idx2` ON `appcodes_kv` (`index:parentid`);

CREATE TABLE IF NOT EXISTS `params` (
  `id` VARCHAR(255) NOT NULL,
  `params_name` VARCHAR(255),
  `params_value` VARCHAR(255),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='参数表';


CREATE TABLE IF NOT EXISTS `svgicon` (
  `id` VARCHAR(255) NOT NULL,
  `icon` TEXT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='图标';

