Files
wx_service/docs/sql/smoke.sql
T
nepiedg 6cf7eb2294 Enhance smoking tracking features with AI next smoke time suggestions
- Added new API endpoint `GET /api/v1/smoke/next_smoke_time` to provide AI-generated suggestions for the next smoking time based on user data.
- Introduced a new database table `fa_smoke_ai_next_smoke` to store structured AI time node suggestions.
- Updated smoke handler and service to integrate the new AI next smoke time functionality.
- Enhanced documentation to reflect the new API endpoint and its usage, including details on how to generate AI time nodes.
2026-01-20 07:08:52 +00:00

101 lines
5.8 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 戒烟/抽烟记录主表
-- 注意:该表字段来自旧系统(createtime/updatetime/deletetime 为秒级时间戳)
CREATE TABLE `fa_smoke_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '用户ID',
`smoke_time` date DEFAULT NULL COMMENT '抽烟时间',
`smoke_at` datetime DEFAULT NULL COMMENT '真实抽烟时间(可补录,精确到时分秒;为空则可用 createtime 近似)',
`remark` text COMMENT '抽烟原因',
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
`level` bigint(2) DEFAULT '1' COMMENT '烟瘾程度',
`num` int(2) DEFAULT '1' COMMENT '抽烟数量',
PRIMARY KEY (`id`,`uid`),
KEY `idx_smoke_uid_date` (`uid`,`smoke_time`),
KEY `idx_smoke_uid_at` (`uid`,`smoke_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='抽烟记录';
-- AI 戒烟建议(按天缓存,避免重复调用;通常 date=昨天)
-- 说明:
-- - access gating:会员用户直接可用;非会员需要先完成“看广告解锁”(见 fa_smoke_ai_advice_unlocks
-- - input_snapshot:建议将“昨日总量 + 时间节点列表”作为 JSON 落库,便于追溯与复现
CREATE TABLE IF NOT EXISTS `fa_smoke_ai_advice` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '用户ID',
`type` varchar(30) NOT NULL DEFAULT 'daily_advice' COMMENT '用途类型(daily_advice/next_smoke_time/...)',
`advice_date` date NOT NULL COMMENT '建议针对的日期(通常=昨天)',
`prompt_version` varchar(30) NOT NULL DEFAULT 'v1' COMMENT '提示词版本',
`provider` varchar(30) DEFAULT NULL COMMENT 'AI 提供方(可选)',
`model` varchar(60) DEFAULT NULL COMMENT '模型名(可选)',
`input_snapshot` json NOT NULL COMMENT '当次输入快照(昨日总量+节点)',
`advice` mediumtext NOT NULL COMMENT 'AI 建议内容(文本/Markdown',
`tokens_in` int DEFAULT NULL,
`tokens_out` int DEFAULT NULL,
`cost_cent` int DEFAULT NULL COMMENT '成本(分,可选)',
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_smoke_ai_advice` (`uid`,`type`,`advice_date`,`prompt_version`),
KEY `idx_smoke_ai_advice_uid_date` (`uid`,`advice_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='每日AI戒烟建议';
-- 每日广告解锁记录(非会员:当天/指定日期解锁后即可生成建议)
CREATE TABLE IF NOT EXISTS `fa_smoke_ai_advice_unlocks` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '用户ID',
`unlock_date` date NOT NULL COMMENT '解锁的建议日期(通常=昨天)',
`ad_watched_at` datetime NOT NULL COMMENT '完成观看时间',
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_smoke_ai_unlock` (`uid`,`unlock_date`),
KEY `idx_smoke_ai_unlock_uid_date` (`uid`,`unlock_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI戒烟建议-广告解锁';
-- 用户基础信息(首次进入补全,用于基准/AI/看板公式等)
-- 说明:
-- - 使用 GORM 默认 created_at/updated_at/deleted_atdatetime(3)
-- - smoke_motivations/quit_motivations 建议存 JSON 数组(例如 ["压力大","社交"]
CREATE TABLE IF NOT EXISTS `fa_smoke_user_profile` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime(3) NULL DEFAULT NULL,
`updated_at` datetime(3) NULL DEFAULT NULL,
`deleted_at` datetime(3) NULL DEFAULT NULL,
`uid` int NOT NULL COMMENT '用户ID',
`baseline_cigs_per_day` int NOT NULL DEFAULT 0 COMMENT '基础烟量(日均抽烟支数)',
`smoking_years` decimal(6,2) NOT NULL DEFAULT 0.00 COMMENT '烟龄(年)',
`pack_price_cent` int NOT NULL DEFAULT 0 COMMENT '单包价格(分)',
`smoke_motivations` json DEFAULT NULL COMMENT '抽烟动机(JSON数组)',
`quit_motivations` json DEFAULT NULL COMMENT '戒烟动力(JSON数组)',
`wake_up_time` varchar(5) NOT NULL DEFAULT '' COMMENT '起床时间(HH:MM)',
`sleep_time` varchar(5) NOT NULL DEFAULT '' COMMENT '入睡时间(HH:MM)',
`onboarding_completed_at` datetime(3) DEFAULT NULL COMMENT '首次补全完成时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_smoke_profile_uid` (`uid`),
KEY `idx_smoke_profile_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='戒烟-用户基础信息';
-- AI 下次抽烟时间建议(结构化缓存:当天)
-- 说明:
-- - not_before_atAI 给出的“不早于”时间;suggested_at:AI 建议的下次抽烟时间(>= not_before_at
-- - time_nodes:建议时间节点(JSON 数组,例如 ["10:30","11:10","14:00"]
CREATE TABLE IF NOT EXISTS `fa_smoke_ai_next_smoke` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '用户ID',
`plan_date` date NOT NULL COMMENT '计划日期(当天)',
`ai_advice_id` bigint unsigned NOT NULL COMMENT '关联AI建议ID(fa_smoke_ai_advice.id)',
`node_type` varchar(20) NOT NULL COMMENT '节点类型(not_before/suggested/node)',
`node_at` datetime NOT NULL COMMENT '时间点',
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_smoke_ai_next_node` (`ai_advice_id`,`node_type`,`node_at`),
KEY `idx_smoke_ai_next_uid_date` (`uid`,`plan_date`),
KEY `idx_smoke_ai_next_advice` (`ai_advice_id`,`node_type`,`node_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI下次抽烟时间节点';