-- WechatAdminWeb 订阅模块业务表（MySQL 8+）
-- 业务用户物理表名：biz_user（与 Sequelize 模型 biz_user、freezeTableName 一致）
-- 执行前请确认库名；与 api/model/biz_*.js 字段一致

SET NAMES utf8mb4;

-- 业务用户（与 sys_user 后台账号区分）
CREATE TABLE IF NOT EXISTS `biz_user` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL DEFAULT '',
  `mobile` VARCHAR(20) NULL DEFAULT NULL,
  `email` VARCHAR(120) NULL DEFAULT NULL,
  `company_name` VARCHAR(200) NULL DEFAULT NULL,
  `status` ENUM('active', 'disabled') NOT NULL DEFAULT 'active',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_biz_user_mobile` (`mobile`),
  KEY `idx_biz_user_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='业务用户';

-- 套餐
CREATE TABLE IF NOT EXISTS `biz_plans` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `plan_code` VARCHAR(64) NOT NULL DEFAULT '',
  `plan_name` VARCHAR(128) NOT NULL DEFAULT '',
  `monthly_price` DECIMAL(12, 2) NOT NULL DEFAULT 0,
  `auth_fee` DECIMAL(12, 2) NOT NULL DEFAULT 0,
  `account_limit` INT NOT NULL DEFAULT 0,
  `active_user_limit` INT NOT NULL DEFAULT 0,
  `msg_quota` INT NOT NULL DEFAULT 0,
  `mass_quota` INT NOT NULL DEFAULT 0,
  `friend_quota` INT NOT NULL DEFAULT 0,
  `sns_quota` INT NOT NULL DEFAULT 0,
  `enabled_features` JSON NULL,
  `status` ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_plans_code` (`plan_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='套餐';

-- 订阅实例
CREATE TABLE IF NOT EXISTS `biz_subscriptions` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `plan_id` BIGINT UNSIGNED NOT NULL,
  `status` ENUM('pending', 'active', 'expired', 'cancelled') NOT NULL DEFAULT 'pending',
  `start_time` DATETIME NOT NULL,
  `end_time` DATETIME NOT NULL,
  `renew_mode` ENUM('manual', 'auto') NOT NULL DEFAULT 'manual',
  `payment_channel` ENUM('offline', 'pay_link') NULL DEFAULT NULL,
  `payment_ref` VARCHAR(200) NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_biz_sub_user` (`user_id`),
  KEY `idx_biz_sub_plan` (`plan_id`),
  KEY `idx_biz_sub_status_end` (`status`, `end_time`),
  CONSTRAINT `fk_biz_sub_user` FOREIGN KEY (`user_id`) REFERENCES `biz_user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_biz_sub_plan` FOREIGN KEY (`plan_id`) REFERENCES `biz_plans` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订阅';

-- API Token（库内仅存 hash；plan_id 冗余便于鉴权少联表）
CREATE TABLE IF NOT EXISTS `biz_api_tokens` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `plan_id` BIGINT UNSIGNED NULL DEFAULT NULL,
  `token_name` VARCHAR(100) NOT NULL DEFAULT '',
  `token_hash` VARCHAR(64) NOT NULL,
  `status` ENUM('active', 'revoked', 'expired') NOT NULL DEFAULT 'active',
  `expire_at` DATETIME NOT NULL,
  `last_used_at` DATETIME NULL DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_token_hash` (`token_hash`),
  KEY `idx_biz_token_user` (`user_id`),
  KEY `idx_biz_token_plan` (`plan_id`),
  CONSTRAINT `fk_biz_token_user` FOREIGN KEY (`user_id`) REFERENCES `biz_user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_biz_token_plan` FOREIGN KEY (`plan_id`) REFERENCES `biz_plans` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='API Token';

-- 月用量
CREATE TABLE IF NOT EXISTS `biz_usage_monthly` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `plan_id` BIGINT UNSIGNED NOT NULL,
  `stat_month` CHAR(7) NOT NULL COMMENT 'YYYY-MM',
  `msg_count` INT NOT NULL DEFAULT 0,
  `mass_count` INT NOT NULL DEFAULT 0,
  `friend_count` INT NOT NULL DEFAULT 0,
  `sns_count` INT NOT NULL DEFAULT 0,
  `active_user_count` INT NOT NULL DEFAULT 0,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_biz_usage_user_month` (`user_id`, `stat_month`),
  KEY `idx_biz_usage_plan` (`plan_id`),
  CONSTRAINT `fk_biz_usage_user` FOREIGN KEY (`user_id`) REFERENCES `biz_user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_biz_usage_plan` FOREIGN KEY (`plan_id`) REFERENCES `biz_plans` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='月用量';
