-- ******************************************************************
-- 表 kku_thirdparty_quotas 的创建SQL
-- 对应的Model: App\Module\ThirdParty\Models\ThirdPartyQuota
-- 警告: 此文件由系统自动生成，禁止修改！
-- ******************************************************************

CREATE TABLE `kku_thirdparty_quotas` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `service_id` bigint unsigned NOT NULL COMMENT '服务ID',
  `type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '配额类型',
  `limit_value` bigint NOT NULL COMMENT '限制值',
  `used_value` bigint DEFAULT '0' COMMENT '已使用值',
  `reset_at` timestamp NULL DEFAULT NULL COMMENT '重置时间',
  `window_start` timestamp NULL DEFAULT NULL COMMENT '时间窗口开始',
  `window_end` timestamp NULL DEFAULT NULL COMMENT '时间窗口结束',
  `is_active` tinyint(1) DEFAULT '1' COMMENT '是否激活',
  `alert_threshold` decimal(5,2) DEFAULT '80.00' COMMENT '告警阈值（百分比）',
  `is_exceeded` tinyint(1) DEFAULT '0' COMMENT '是否已超限',
  `exceeded_at` timestamp NULL DEFAULT NULL COMMENT '超限时间',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `uk_service_type` (`service_id`,`type`) USING BTREE,
  KEY `idx_type` (`type`) USING BTREE,
  KEY `idx_is_active` (`is_active`) USING BTREE,
  KEY `idx_is_exceeded` (`is_exceeded`) USING BTREE,
  KEY `idx_reset_at` (`reset_at`) USING BTREE,
  CONSTRAINT `fk_quotas_service` FOREIGN KEY (`service_id`) REFERENCES `kku_thirdparty_services` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='配额管理表';
