-- ============================================
-- 数据导出记录表 - data_exports
-- ============================================

CREATE TABLE IF NOT EXISTS `data_exports` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `tenant_id` BIGINT UNSIGNED NOT NULL COMMENT '租户ID',
    `user_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '用户ID',
    `type` VARCHAR(50) NOT NULL COMMENT '导出类型: orders/goods/inventory/suppliers/stock_report/sales_report/order_report/purchase_report/finance_report/api_usage_report',
    `format` VARCHAR(20) NOT NULL DEFAULT 'csv' COMMENT '导出格式: csv/xlsx/pdf',
    `filename` VARCHAR(255) NOT NULL COMMENT '文件名(不含扩展名)',
    `file_path` VARCHAR(500) DEFAULT NULL COMMENT '文件存储路径',
    `file_size` BIGINT UNSIGNED DEFAULT NULL COMMENT '文件大小(字节)',
    `row_count` INT UNSIGNED DEFAULT NULL COMMENT '导出行数',
    `status` VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT '状态: pending/processing/completed/failed',
    `error_msg` TEXT DEFAULT NULL COMMENT '错误信息',
    `filters_json` TEXT DEFAULT NULL COMMENT '导出时的过滤条件JSON',
    `scheduled_task_id` VARCHAR(100) DEFAULT NULL COMMENT '定时任务ID(如果是定时任务触发的导出)',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` DATETIME DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_tenant_status` (`tenant_id`, `status`),
    KEY `idx_tenant_type` (`tenant_id`, `type`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_scheduled_task_id` (`scheduled_task_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='数据导出记录表';

-- ============================================
-- 索引说明
-- ============================================
-- idx_tenant_id: 按租户查询导出记录
-- idx_tenant_status: 按租户和状态查询
-- idx_tenant_type: 按租户和类型查询
-- idx_created_at: 按创建时间排序和清理
-- idx_scheduled_task_id: 定时任务关联查询
