-- Dashboard Service 数据库Schema
-- 版本: 1.0.0
-- 描述: 定义Dashboard相关的表结构

-- ============================================
-- 表结构说明
-- Dashboard服务主要依赖以下现有表：
-- - orders: 订单表
-- - pull_records: API拉取记录表
-- - stocks: 库存表
-- - warehouses: 仓库表
-- - system_configs: 系统配置表
-- ============================================

-- 如需单独创建Dashboard相关表，可使用以下SQL

-- ============================================
-- 预警记录表 (可选，用于存储历史预警)
-- ============================================
-- CREATE TABLE IF NOT EXISTS `dashboard_alerts` (
--     `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
--     `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
--     `alert_type` VARCHAR(50) NOT NULL COMMENT '预警类型',
--     `alert_level` VARCHAR(20) NOT NULL COMMENT '预警级别: critical, warning, info',
--     `title` VARCHAR(100) NOT NULL COMMENT '预警标题',
--     `message` VARCHAR(500) NOT NULL COMMENT '预警详情',
--     `count` INT DEFAULT 0 COMMENT '关联数量',
--     `is_read` TINYINT DEFAULT 0 COMMENT '是否已读: 0-未读, 1-已读',
--     `read_at` DATETIME DEFAULT NULL COMMENT '读取时间',
--     `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
--     `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
--     `deleted_at` DATETIME DEFAULT NULL,
--     INDEX `idx_tenant_id` (`tenant_id`),
--     INDEX `idx_alert_type` (`alert_type`),
--     INDEX `idx_created_at` (`created_at`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Dashboard预警记录表';

-- ============================================
-- Dashboard缓存表 (可选，用于存储缓存数据)
-- ============================================
-- CREATE TABLE IF NOT EXISTS `dashboard_cache` (
--     `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
--     `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
--     `cache_key` VARCHAR(100) NOT NULL COMMENT '缓存键',
--     `cache_value` TEXT COMMENT '缓存值(JSON)',
--     `expire_at` DATETIME DEFAULT NULL COMMENT '过期时间',
--     `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
--     `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
--     `deleted_at` DATETIME DEFAULT NULL,
--     UNIQUE KEY `uk_tenant_cache_key` (`tenant_id`, `cache_key`),
--     INDEX `idx_expire_at` (`expire_at`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Dashboard缓存表';

-- ============================================
-- 仪表盘配置表 (可选，用于自定义仪表盘布局)
-- ============================================
-- CREATE TABLE IF NOT EXISTS `dashboard_config` (
--     `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
--     `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
--     `config_key` VARCHAR(100) NOT NULL COMMENT '配置键',
--     `config_value` TEXT COMMENT '配置值(JSON)',
--     `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
--     `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
--     `deleted_at` DATETIME DEFAULT NULL,
--     UNIQUE KEY `uk_tenant_config` (`tenant_id`, `config_key`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='仪表盘配置表';

-- ============================================
-- 初始化系统配置
-- ============================================
-- INSERT INTO system_configs (`group`, `key`, `value`, type, description, sort, status) VALUES
-- ('tenant_quota', 'monthly_order_limit', '1000', 'number', '月度订单限额', 1, 1),
-- ('tenant_quota', 'daily_api_limit', '5000', 'number', '每日API调用限额', 2, 1),
-- ('stock_alert_settings', 'global_threshold', '10', 'number', '全局库存预警阈值', 1, 1);
