-- 订单管理系统数据库建表脚本
-- 说明：以下脚本可直接导入 MySQL / MariaDB（建议 MySQL 8.0+）。
-- 字符集：utf8mb4
-- 引擎：InnoDB
-- 约定：不显式创建外键，避免导入顺序和级联策略带来的阻塞；通过索引和业务校验保证关联一致性。
-- 规范：状态字段均采用需求文档中约定的固定枚举值，避免后续实现口径不一致。
-- 统一字段映射请参见《字段映射检查表.md》。

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `performance_stat_cache`;
DROP TABLE IF EXISTS `ai_recognition_log`;
DROP TABLE IF EXISTS `file_attachment`;
DROP TABLE IF EXISTS `audit_log`;
DROP TABLE IF EXISTS `system_config`;
DROP TABLE IF EXISTS `system_reminder`;
DROP TABLE IF EXISTS `customer_arrears`;
DROP TABLE IF EXISTS `logistics_trace`;
DROP TABLE IF EXISTS `logistics_task`;
DROP TABLE IF EXISTS `sales_order_approve_log`;
DROP TABLE IF EXISTS `sales_order_item`;
DROP TABLE IF EXISTS `sales_order`;
DROP TABLE IF EXISTS `base_supplier`;
DROP TABLE IF EXISTS `product`;
DROP TABLE IF EXISTS `crm_customer`;
DROP TABLE IF EXISTS `sys_menu`;
DROP TABLE IF EXISTS `sys_role`;
DROP TABLE IF EXISTS `sys_user`;

CREATE TABLE `sys_role` (
  `id` bigint NOT NULL COMMENT '主键',
  `role_name` varchar(64) NOT NULL COMMENT '角色名称',
  `role_code` varchar(64) NOT NULL COMMENT '角色编码',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sys_role_code` (`role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表';

CREATE TABLE `sys_user` (
  `id` bigint NOT NULL COMMENT '主键',
  `username` varchar(64) NOT NULL COMMENT '登录账号',
  `password_hash` varchar(255) NOT NULL COMMENT '密码哈希',
  `real_name` varchar(64) NOT NULL COMMENT '姓名',
  `mobile` varchar(20) DEFAULT NULL COMMENT '手机号',
  `role_id` bigint NOT NULL COMMENT '角色ID',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sys_user_username` (`username`),
  KEY `idx_sys_user_role_id` (`role_id`),
  KEY `idx_sys_user_mobile` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统用户表';

CREATE TABLE `sys_menu` (
  `id` bigint NOT NULL COMMENT '主键',
  `parent_id` bigint NOT NULL DEFAULT 0 COMMENT '父级菜单ID',
  `menu_name` varchar(64) NOT NULL COMMENT '菜单名称',
  `menu_path` varchar(255) DEFAULT NULL COMMENT '路由路径',
  `menu_type` varchar(32) NOT NULL COMMENT '菜单类型：menu/button',
  `permission_code` varchar(128) DEFAULT NULL COMMENT '权限标识',
  `icon` varchar(64) DEFAULT NULL COMMENT '图标',
  `sort_no` int NOT NULL DEFAULT 0 COMMENT '排序号',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  KEY `idx_sys_menu_parent_id` (`parent_id`),
  KEY `idx_sys_menu_permission_code` (`permission_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜单表';

CREATE TABLE `crm_customer` (
  `id` bigint NOT NULL COMMENT '主键',
  `customer_name` varchar(64) NOT NULL COMMENT '客户姓名',
  `mobile` varchar(20) NOT NULL COMMENT '手机号',
  `address` varchar(255) DEFAULT NULL COMMENT '地址',
  `settlement_type` varchar(32) DEFAULT NULL COMMENT '结算方式',
  `settlement_days` int DEFAULT NULL COMMENT '月结天数',
  `settlement_start_type` varchar(32) DEFAULT NULL COMMENT '账期起算口径：订单日期/对账日期/发货日期',
  `customer_type` varchar(32) DEFAULT NULL COMMENT '客户类型',
  `salesman_id` bigint DEFAULT NULL COMMENT '归属业务员ID',
  `credit_limit` decimal(18,2) DEFAULT 0.00 COMMENT '信用额度',
  `arrears_amount` decimal(18,2) DEFAULT 0.00 COMMENT '欠款金额',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_customer_name_mobile` (`customer_name`, `mobile`),
  KEY `idx_customer_mobile` (`mobile`),
  KEY `idx_customer_salesman_id` (`salesman_id`),
  KEY `idx_customer_type` (`customer_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='客户表';

CREATE TABLE `product` (
  `id` bigint NOT NULL COMMENT '主键',
  `product_name` varchar(128) NOT NULL COMMENT '产品名称',
  `specification` varchar(128) NOT NULL COMMENT '规格',
  `unit` varchar(32) NOT NULL COMMENT '单位',
  `category` varchar(64) NOT NULL COMMENT '分类',
  `cost_price` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '成本价',
  `sale_price` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '售价',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  KEY `idx_product_name` (`product_name`),
  KEY `idx_product_specification` (`specification`),
  KEY `idx_product_category` (`category`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='产品表';

CREATE TABLE `base_supplier` (
  `id` bigint NOT NULL COMMENT '主键',
  `supplier_name` varchar(128) NOT NULL COMMENT '名称',
  `supplier_type` varchar(32) NOT NULL COMMENT '类型：工厂/供应商',
  `contact_name` varchar(64) DEFAULT NULL COMMENT '联系人',
  `contact_mobile` varchar(20) DEFAULT NULL COMMENT '联系电话',
  `address` varchar(255) DEFAULT NULL COMMENT '地址',
  `template_type` varchar(64) DEFAULT NULL COMMENT '下发文本模板类型',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  KEY `idx_supplier_name` (`supplier_name`),
  KEY `idx_supplier_type` (`supplier_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='工厂/供应商表';

CREATE TABLE `sales_order` (
  `id` bigint NOT NULL COMMENT '主键',
  `order_no` varchar(64) NOT NULL COMMENT '订单编号',
  `customer_id` bigint DEFAULT NULL COMMENT '客户ID',
  `customer_name_snapshot` varchar(64) NOT NULL COMMENT '客户名称快照',
  `customer_mobile_snapshot` varchar(20) NOT NULL COMMENT '客户手机号快照',
  `salesman_id` bigint NOT NULL COMMENT '业务员ID',
  `order_source` varchar(64) DEFAULT NULL COMMENT '订单来源标签',
  `order_status` varchar(32) NOT NULL COMMENT '订单状态',
  `delivery_type` varchar(32) DEFAULT NULL COMMENT '发货类型',
  `factory_id` bigint DEFAULT NULL COMMENT '工厂ID',
  `sale_price_total` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '销售价总额',
  `cost_price_total` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '成本价总额',
  `rebate_total` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '回扣总额',
  `freight_total` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '运费总额',
  `tax_total` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '税费总额',
  `other_fee_total` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '其他费用总额',
  `profit_total` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '利润',
  `profit_rate` decimal(10,4) NOT NULL DEFAULT 0.0000 COMMENT '利润率',
  `created_by` bigint NOT NULL COMMENT '创建人',
  `submitted_at` datetime DEFAULT NULL COMMENT '提交时间',
  `approved_by` bigint DEFAULT NULL COMMENT '审批人',
  `approved_at` datetime DEFAULT NULL COMMENT '审批时间',
  `canceled_at` datetime DEFAULT NULL COMMENT '取消时间',
  `completed_at` datetime DEFAULT NULL COMMENT '完成时间',
  `remark` varchar(1000) DEFAULT NULL COMMENT '备注',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sales_order_no` (`order_no`),
  KEY `idx_sales_order_customer_id` (`customer_id`),
  KEY `idx_sales_order_salesman_id` (`salesman_id`),
  KEY `idx_sales_order_status` (`order_status`),
  KEY `idx_sales_order_factory_id` (`factory_id`),
  KEY `idx_sales_order_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='销售订单主表';

CREATE TABLE `sales_order_item` (
  `id` bigint NOT NULL COMMENT '主键',
  `order_id` bigint NOT NULL COMMENT '订单ID',
  `product_id` bigint DEFAULT NULL COMMENT '产品ID',
  `product_name_snapshot` varchar(128) NOT NULL COMMENT '产品名称快照',
  `specification_snapshot` varchar(128) NOT NULL COMMENT '规格快照',
  `unit_snapshot` varchar(32) NOT NULL COMMENT '单位快照',
  `quantity` decimal(18,4) NOT NULL DEFAULT 0.0000 COMMENT '数量',
  `sale_price` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '销售单价',
  `cost_price` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '成本单价',
  `rebate_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '回扣金额',
  `freight_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '运费分摊',
  `tax_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '税费分摊',
  `other_fee_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '其他费用分摊',
  `line_profit` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '行利润',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  KEY `idx_sales_order_item_order_id` (`order_id`),
  KEY `idx_sales_order_item_product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='销售订单明细表';

CREATE TABLE `sales_order_approve_log` (
  `id` bigint NOT NULL COMMENT '主键',
  `order_id` bigint NOT NULL COMMENT '订单ID',
  `approve_type` varchar(32) NOT NULL COMMENT '审批类型：提交审核/最终审批/取消审批',
  `approve_result` varchar(32) NOT NULL COMMENT '审批结果：通过/退回/拒绝',
  `approve_opinion` varchar(1000) DEFAULT NULL COMMENT '审批意见',
  `approver_id` bigint NOT NULL COMMENT '审批人ID',
  `approver_name` varchar(64) NOT NULL COMMENT '审批人名称快照',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '审批时间',
  PRIMARY KEY (`id`),
  KEY `idx_sales_order_approve_log_order_id` (`order_id`),
  KEY `idx_sales_order_approve_log_approver_id` (`approver_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='销售订单审批记录表';

CREATE TABLE `logistics_task` (
  `id` bigint NOT NULL COMMENT '主键',
  `order_id` bigint NOT NULL COMMENT '订单ID',
  `task_no` varchar(64) NOT NULL COMMENT '任务编号',
  `driver_id` bigint DEFAULT NULL COMMENT '司机用户ID',
  `status` varchar(32) NOT NULL COMMENT '任务状态',
  `pickup_address` varchar(255) DEFAULT NULL COMMENT '取货地址',
  `delivery_address` varchar(255) DEFAULT NULL COMMENT '送达地址',
  `pickup_content` varchar(1000) DEFAULT NULL COMMENT '取件内容',
  `quantity` decimal(18,4) DEFAULT 0.0000 COMMENT '数量',
  `factory_id` bigint DEFAULT NULL COMMENT '工厂ID',
  `accepted_at` datetime DEFAULT NULL COMMENT '接单时间',
  `pickup_confirmed_at` datetime DEFAULT NULL COMMENT '揽货时间',
  `delivered_at` datetime DEFAULT NULL COMMENT '送达时间',
  `remark` varchar(1000) DEFAULT NULL COMMENT '备注',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` tinyint NOT NULL DEFAULT 0 COMMENT '逻辑删除：0否 1是',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_logistics_task_no` (`task_no`),
  KEY `idx_logistics_task_order_id` (`order_id`),
  KEY `idx_logistics_task_driver_id` (`driver_id`),
  KEY `idx_logistics_task_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='物流任务表';

CREATE TABLE `logistics_trace` (
  `id` bigint NOT NULL COMMENT '主键',
  `order_id` bigint NOT NULL COMMENT '订单ID',
  `task_id` bigint DEFAULT NULL COMMENT '任务ID',
  `node_time` datetime NOT NULL COMMENT '节点时间',
  `node_desc` varchar(500) NOT NULL COMMENT '节点描述',
  `node_type` varchar(64) DEFAULT NULL COMMENT '节点类型',
  `source_platform` varchar(64) DEFAULT NULL COMMENT '来源平台',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_logistics_trace_order_id` (`order_id`),
  KEY `idx_logistics_trace_task_id` (`task_id`),
  KEY `idx_logistics_trace_node_time` (`node_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='物流轨迹表';

CREATE TABLE `customer_arrears` (
  `id` bigint NOT NULL COMMENT '主键',
  `customer_id` bigint NOT NULL COMMENT '客户ID',
  `order_id` bigint NOT NULL COMMENT '订单ID',
  `arrears_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '欠款金额',
  `settlement_start_date` date DEFAULT NULL COMMENT '起算日期',
  `due_date` date DEFAULT NULL COMMENT '到期日期',
  `status` varchar(32) NOT NULL COMMENT '状态：pending/overdue/paid/closed',
  `reminded_at` datetime DEFAULT NULL COMMENT '提醒时间',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_customer_arrears_customer_id` (`customer_id`),
  KEY `idx_customer_arrears_order_id` (`order_id`),
  KEY `idx_customer_arrears_due_date` (`due_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='客户欠款记录表';

CREATE TABLE `system_reminder` (
  `id` bigint NOT NULL COMMENT '主键',
  `reminder_type` varchar(64) NOT NULL COMMENT '提醒类型',
  `biz_type` varchar(64) NOT NULL COMMENT '业务类型',
  `biz_id` bigint NOT NULL COMMENT '业务主键',
  `receiver_user_id` bigint NOT NULL COMMENT '接收人ID',
  `reminder_title` varchar(255) NOT NULL COMMENT '提醒标题',
  `reminder_content` varchar(2000) NOT NULL COMMENT '提醒内容',
  `status` varchar(32) NOT NULL COMMENT '状态：pending/sent/read/canceled',
  `sent_at` datetime DEFAULT NULL COMMENT '发送时间',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_system_reminder_biz_type` (`biz_type`),
  KEY `idx_system_reminder_biz_id` (`biz_id`),
  KEY `idx_system_reminder_receiver_user_id` (`receiver_user_id`),
  KEY `idx_system_reminder_reminder_type` (`reminder_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统提醒记录表';

CREATE TABLE `system_config` (
  `id` bigint NOT NULL COMMENT '主键',
  `config_key` varchar(128) NOT NULL COMMENT '配置键',
  `config_value` varchar(2000) NOT NULL COMMENT '配置值',
  `config_name` varchar(255) NOT NULL COMMENT '配置名称',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1启用 0停用',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_system_config_key` (`config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统配置表';

CREATE TABLE `file_attachment` (
  `id` bigint NOT NULL COMMENT '主键',
  `biz_type` varchar(64) NOT NULL COMMENT '业务类型',
  `biz_id` bigint NOT NULL COMMENT '业务主键',
  `file_name` varchar(255) NOT NULL COMMENT '文件名',
  `file_url` varchar(1000) NOT NULL COMMENT '文件地址',
  `file_type` varchar(64) DEFAULT NULL COMMENT '文件类型',
  `file_size` bigint DEFAULT NULL COMMENT '文件大小',
  `created_by` bigint DEFAULT NULL COMMENT '上传人',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_file_attachment_biz_type` (`biz_type`),
  KEY `idx_file_attachment_biz_id` (`biz_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文件附件表';

CREATE TABLE `ai_recognition_log` (
  `id` bigint NOT NULL COMMENT '主键',
  `biz_type` varchar(64) NOT NULL COMMENT '业务类型',
  `biz_id` bigint NOT NULL COMMENT '业务主键',
  `image_url` varchar(1000) NOT NULL COMMENT '图片地址',
  `raw_result` longtext COMMENT '原始识别结果',
  `confidence` decimal(10,4) DEFAULT NULL COMMENT '置信度',
  `corrected_result` longtext COMMENT '人工修正结果',
  `created_by` bigint DEFAULT NULL COMMENT '操作人',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_ai_recognition_log_biz_type` (`biz_type`),
  KEY `idx_ai_recognition_log_biz_id` (`biz_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI识别结果表';

CREATE TABLE `performance_stat_cache` (
  `id` bigint NOT NULL COMMENT '主键',
  `stat_type` varchar(32) NOT NULL COMMENT '统计类型：月/季/年',
  `stat_period` varchar(32) NOT NULL COMMENT '统计周期',
  `industry_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '工业品金额',
  `daily_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '日用品金额',
  `order_count` int NOT NULL DEFAULT 0 COMMENT '订单数量',
  `order_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '订单金额',
  `commission_amount` decimal(18,2) NOT NULL DEFAULT 0.00 COMMENT '提成核对金额',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_performance_stat_cache_type_period` (`stat_type`, `stat_period`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='业绩统计缓存表';

CREATE TABLE `audit_log` (
  `id` bigint NOT NULL COMMENT '主键',
  `operator_id` bigint DEFAULT NULL COMMENT '操作人ID',
  `operator_name` varchar(64) DEFAULT NULL COMMENT '操作人名称',
  `operate_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
  `operate_type` varchar(64) NOT NULL COMMENT '操作类型',
  `biz_type` varchar(64) NOT NULL COMMENT '业务类型',
  `biz_id` bigint NOT NULL COMMENT '业务主键',
  `before_value` longtext COMMENT '操作前值',
  `after_value` longtext COMMENT '操作后值',
  `result` varchar(32) NOT NULL COMMENT '结果：success/fail',
  `remark` varchar(1000) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_audit_log_biz_type` (`biz_type`),
  KEY `idx_audit_log_biz_id` (`biz_id`),
  KEY `idx_audit_log_operator_id` (`operator_id`),
  KEY `idx_audit_log_operate_time` (`operate_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审计日志表';

SET FOREIGN_KEY_CHECKS = 1;
