-- CID服务商项目数据库初始化脚本

-- 广告源表
CREATE TABLE IF NOT EXISTS `ad_sources` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL COMMENT '广告源名称',
  `provider` varchar(50) NOT NULL COMMENT '提供商: self, google, facebook, etc.',
  `api_endpoint` varchar(255) DEFAULT NULL COMMENT 'API端点',
  `api_key` varchar(255) DEFAULT NULL COMMENT 'API密钥',
  `config` text COMMENT '配置信息(JSON格式)',
  `status` varchar(20) NOT NULL DEFAULT 'active' COMMENT '状态: active, inactive',
  `priority` int(11) NOT NULL DEFAULT 0 COMMENT '优先级',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `created_by` bigint(20) DEFAULT NULL COMMENT '创建人',
  `updated_by` bigint(20) DEFAULT NULL COMMENT '更新人',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_ad_sources_name` (`name`),
  KEY `idx_ad_sources_provider` (`provider`),
  KEY `idx_ad_sources_status` (`status`),
  KEY `idx_ad_sources_priority` (`priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='广告源表';

-- 策略表
CREATE TABLE IF NOT EXISTS `strategies` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL COMMENT '策略名称',
  `description` varchar(500) DEFAULT NULL COMMENT '描述',
  `tenant_level` varchar(20) NOT NULL COMMENT '适用租户级别: basic, standard, premium',
  `min_conversion` decimal(5,4) NOT NULL DEFAULT 0.0000 COMMENT '最低转化率',
  `max_conversion` decimal(5,4) NOT NULL DEFAULT 1.0000 COMMENT '最高转化率',
  `source_weights` text COMMENT '广告源权重配置(JSON格式)',
  `max_ads_per_req` int(11) NOT NULL DEFAULT 5 COMMENT '每次请求最大广告数',
  `priority` int(11) NOT NULL DEFAULT 0 COMMENT '优先级',
  `status` varchar(20) NOT NULL DEFAULT 'active' COMMENT '状态: active, inactive',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `created_by` bigint(20) DEFAULT NULL COMMENT '创建人',
  `updated_by` bigint(20) DEFAULT NULL COMMENT '更新人',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_strategies_name` (`name`),
  KEY `idx_strategies_tenant_level` (`tenant_level`),
  KEY `idx_strategies_status` (`status`),
  KEY `idx_strategies_priority` (`priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='匹配策略表';

-- CID请求记录表
CREATE TABLE IF NOT EXISTS `cid_requests` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint(20) NOT NULL COMMENT '租户ID',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `request_type` varchar(50) NOT NULL COMMENT '请求类型',
  `parameters` text COMMENT '请求参数(JSON格式)',
  `response_data` text COMMENT '响应数据(JSON格式)',
  `status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT '状态: pending, completed, failed',
  `process_time` int(11) 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 '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_cid_requests_tenant_id` (`tenant_id`),
  KEY `idx_cid_requests_user_id` (`user_id`),
  KEY `idx_cid_requests_status` (`status`),
  KEY `idx_cid_requests_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='CID请求记录表';

-- 插入默认广告源
INSERT INTO `ad_sources` (`name`, `provider`, `status`, `priority`, `created_by`, `updated_by`) VALUES
('自营广告', 'self', 'active', 100, 1, 1),
('Google Ads', 'google', 'active', 80, 1, 1),
('Facebook Ads', 'facebook', 'active', 70, 1, 1)
ON DUPLICATE KEY UPDATE `updated_at` = CURRENT_TIMESTAMP;

-- 插入默认策略
INSERT INTO `strategies` (`name`, `description`, `tenant_level`, `min_conversion`, `max_conversion`, `source_weights`, `max_ads_per_req`, `priority`, `status`, `created_by`, `updated_by`) VALUES
('基础策略', '适用于基础租户的默认策略', 'basic', 0.0100, 0.0500, '{"self": 100, "google": 0, "facebook": 0}', 3, 10, 'active', 1, 1),
('标准策略', '适用于标准租户的默认策略', 'standard', 0.0500, 0.1500, '{"self": 70, "google": 20, "facebook": 10}', 5, 20, 'active', 1, 1),
('高级策略', '适用于高级租户的默认策略', 'premium', 0.1500, 1.0000, '{"self": 40, "google": 30, "facebook": 30}', 10, 30, 'active', 1, 1)
ON DUPLICATE KEY UPDATE `updated_at` = CURRENT_TIMESTAMP;