-- 控糖消消乐：真实用户周榜、幂等局记录与轻量分享访问关系

CREATE TABLE IF NOT EXISTS `zyt_tcm_game_weekly_allocator` (
    `week_start` date NOT NULL COMMENT '周一日期',
    `sex` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '同行组性别：0未知 1男 2女',
    `create_time` int unsigned NOT NULL DEFAULT 0,
    `update_time` int unsigned NOT NULL DEFAULT 0,
    PRIMARY KEY (`week_start`, `sex`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='控糖消消乐每周同行分组并发锁';

CREATE TABLE IF NOT EXISTS `zyt_tcm_game_weekly_group` (
    `id` int unsigned NOT NULL AUTO_INCREMENT,
    `week_start` date NOT NULL COMMENT '周一日期',
    `sex` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '同行组性别：0未知 1男 2女',
    `group_no` int unsigned NOT NULL DEFAULT 1 COMMENT '同周同性别的分组序号',
    `member_count` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '组内人数，最多7人',
    `create_time` int unsigned NOT NULL DEFAULT 0,
    `update_time` int unsigned NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_week_sex_group` (`week_start`, `sex`, `group_no`),
    KEY `idx_week_sex_capacity` (`week_start`, `sex`, `member_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='控糖消消乐每周7人同行分组';

CREATE TABLE IF NOT EXISTS `zyt_tcm_game_weekly_score` (
    `id` int unsigned NOT NULL AUTO_INCREMENT,
    `group_id` int unsigned NOT NULL COMMENT '周同行分组ID',
    `week_start` date NOT NULL COMMENT '周一日期',
    `user_id` int unsigned NOT NULL COMMENT '小程序用户ID',
    `learned_count` int unsigned NOT NULL DEFAULT 0 COMMENT '本周认糖数量',
    `best_score` int unsigned NOT NULL DEFAULT 0 COMMENT '本周单局最高分',
    `games_played` int unsigned NOT NULL DEFAULT 0 COMMENT '本周完成局数',
    `share_count` int unsigned NOT NULL DEFAULT 0 COMMENT '本周发起分享次数',
    `nickname` varchar(32) NOT NULL DEFAULT '' COMMENT '周榜昵称快照',
    `avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '周榜头像快照',
    `sex` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '性别快照：0未知 1男 2女',
    `create_time` int unsigned NOT NULL DEFAULT 0,
    `update_time` int unsigned NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_week_user` (`week_start`, `user_id`),
    KEY `idx_group_rank` (`group_id`, `learned_count`, `best_score`),
    KEY `idx_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='控糖消消乐每周认糖成绩';

CREATE TABLE IF NOT EXISTS `zyt_tcm_game_session` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT,
    `session_key` varchar(64) NOT NULL COMMENT '客户端每局随机标识，用于重试去重',
    `user_id` int unsigned NOT NULL COMMENT '小程序用户ID',
    `week_start` date NOT NULL COMMENT '该局计入的周一日期',
    `learned_count` int unsigned NOT NULL DEFAULT 0 COMMENT '本局已确认认糖数量',
    `last_score` int unsigned NOT NULL DEFAULT 0 COMMENT '本局最新分数',
    `ended` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '是否已经结束并计入完成局数',
    `create_time` int unsigned NOT NULL DEFAULT 0,
    `update_time` int unsigned NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_session_key` (`session_key`),
    KEY `idx_user_week` (`user_id`, `week_start`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='控糖消消乐局进度去重';

CREATE TABLE IF NOT EXISTS `zyt_tcm_game_share_invite` (
    `id` int unsigned NOT NULL AUTO_INCREMENT,
    `invite_code` varchar(16) NOT NULL COMMENT '不包含用户ID的分享码',
    `user_id` int unsigned NOT NULL COMMENT '分享人小程序用户ID',
    `week_start` date NOT NULL COMMENT '分享码所属周',
    `open_count` int unsigned NOT NULL DEFAULT 0 COMMENT '去重后的受邀打开人数',
    `create_time` int unsigned NOT NULL DEFAULT 0,
    `update_time` int unsigned NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_invite_code` (`invite_code`),
    UNIQUE KEY `uk_week_user` (`week_start`, `user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='控糖消消乐微信分享码';

CREATE TABLE IF NOT EXISTS `zyt_tcm_game_share_visit` (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT,
    `invite_code` varchar(16) NOT NULL COMMENT '来源分享码',
    `inviter_user_id` int unsigned NOT NULL COMMENT '分享人用户ID',
    `visitor_user_id` int unsigned NOT NULL COMMENT '受邀打开用户ID',
    `create_time` int unsigned NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_invite_visitor` (`invite_code`, `visitor_user_id`),
    KEY `idx_inviter` (`inviter_user_id`),
    KEY `idx_visitor` (`visitor_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='控糖消消乐分享打开记录（不做强绑定）';
