文件
tennis-training-hub/server/db.ts
2026-03-15 01:39:34 +08:00

1740 行
68 KiB
TypeScript
原始文件 Blame 文件历史

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
import { eq, desc, and, asc, lte, gte, sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/mysql2";
import {
InsertUser, users,
usernameAccounts,
trainingPlans, InsertTrainingPlan,
trainingVideos, InsertTrainingVideo,
poseAnalyses, InsertPoseAnalysis,
trainingRecords, InsertTrainingRecord,
liveAnalysisSessions, InsertLiveAnalysisSession,
liveActionSegments, InsertLiveActionSegment,
dailyTrainingAggregates, InsertDailyTrainingAggregate,
ratingHistory, InsertRatingHistory,
ntrpSnapshots, InsertNtrpSnapshot,
dailyCheckins, InsertDailyCheckin,
userBadges, InsertUserBadge,
achievementDefinitions, InsertAchievementDefinition,
userAchievements, InsertUserAchievement,
tutorialVideos, InsertTutorialVideo,
tutorialProgress, InsertTutorialProgress,
trainingReminders, InsertTrainingReminder,
notificationLog, InsertNotificationLog,
backgroundTasks, InsertBackgroundTask,
adminAuditLogs, InsertAdminAuditLog,
appSettings, InsertAppSetting,
visionReferenceImages, InsertVisionReferenceImage,
visionTestRuns, InsertVisionTestRun,
} from "../drizzle/schema";
import { ENV } from './_core/env';
let _db: ReturnType<typeof drizzle> | null = null;
const APP_TIMEZONE = process.env.TZ || "Asia/Shanghai";
function getDateFormatter() {
return new Intl.DateTimeFormat("en-CA", {
timeZone: APP_TIMEZONE,
year: "numeric",
month: "2-digit",
day: "2-digit",
});
}
export function getDateKey(date = new Date()) {
return getDateFormatter().format(date);
}
function toDayKey(userId: number, trainingDate: string) {
return `${userId}:${trainingDate}`;
}
export const DEFAULT_APP_SETTINGS: Omit<InsertAppSetting, "id" | "createdAt" | "updatedAt">[] = [
{
settingKey: "action_unknown_confidence_threshold",
label: "未知动作阈值",
description: "当动作识别置信度低于此值时归类为未知动作。",
value: { value: 0.45, type: "number" },
},
{
settingKey: "action_merge_gap_ms",
label: "动作合并间隔",
description: "相邻同类动作小于该间隔时会合并为同一片段。",
value: { value: 500, type: "number" },
},
{
settingKey: "action_segment_max_ms",
label: "动作片段最长时长",
description: "单个动作片段最长持续时间。",
value: { value: 10000, type: "number" },
},
{
settingKey: "ntrp_daily_refresh_hour",
label: "NTRP 每日刷新小时",
description: "每天异步刷新 NTRP 的小时数。",
value: { value: 0, type: "number" },
},
];
export const ACHIEVEMENT_DEFINITION_SEED_DATA: Omit<InsertAchievementDefinition, "id" | "createdAt" | "updatedAt">[] = [
{ key: "training_day_1", name: "开练", description: "完成首个训练日", category: "consistency", rarity: "common", icon: "🎾", metricKey: "training_days", targetValue: 1, tier: 1, sortOrder: 1, isHidden: 0, isActive: 1 },
{ key: "training_day_3", name: "三日连练", description: "连续训练 3 天", category: "consistency", rarity: "common", icon: "🔥", metricKey: "current_streak", targetValue: 3, tier: 2, sortOrder: 2, isHidden: 0, isActive: 1 },
{ key: "training_day_7", name: "一周稳定", description: "连续训练 7 天", category: "consistency", rarity: "rare", icon: "⭐", metricKey: "current_streak", targetValue: 7, tier: 3, sortOrder: 3, isHidden: 0, isActive: 1 },
{ key: "training_minutes_60", name: "首个小时", description: "累计训练 60 分钟", category: "volume", rarity: "common", icon: "⏱️", metricKey: "total_minutes", targetValue: 60, tier: 1, sortOrder: 10, isHidden: 0, isActive: 1 },
{ key: "training_minutes_300", name: "五小时达标", description: "累计训练 300 分钟", category: "volume", rarity: "rare", icon: "🕐", metricKey: "total_minutes", targetValue: 300, tier: 2, sortOrder: 11, isHidden: 0, isActive: 1 },
{ key: "training_minutes_1000", name: "千分钟训练者", description: "累计训练 1000 分钟", category: "volume", rarity: "epic", icon: "⏰", metricKey: "total_minutes", targetValue: 1000, tier: 3, sortOrder: 12, isHidden: 0, isActive: 1 },
{ key: "effective_actions_50", name: "动作起步", description: "累计完成 50 个有效动作", category: "technique", rarity: "common", icon: "🏓", metricKey: "effective_actions", targetValue: 50, tier: 1, sortOrder: 20, isHidden: 0, isActive: 1 },
{ key: "effective_actions_200", name: "动作累积", description: "累计完成 200 个有效动作", category: "technique", rarity: "rare", icon: "💥", metricKey: "effective_actions", targetValue: 200, tier: 2, sortOrder: 21, isHidden: 0, isActive: 1 },
{ key: "recordings_1", name: "录像开启", description: "完成首个录制归档", category: "recording", rarity: "common", icon: "🎥", metricKey: "recording_count", targetValue: 1, tier: 1, sortOrder: 30, isHidden: 0, isActive: 1 },
{ key: "analyses_1", name: "分析首秀", description: "完成首个分析会话", category: "analysis", rarity: "common", icon: "🧠", metricKey: "analysis_count", targetValue: 1, tier: 1, sortOrder: 31, isHidden: 0, isActive: 1 },
{ key: "live_analysis_5", name: "实时观察者", description: "完成 5 次实时分析", category: "analysis", rarity: "rare", icon: "📹", metricKey: "live_analysis_count", targetValue: 5, tier: 2, sortOrder: 32, isHidden: 0, isActive: 1 },
{ key: "score_80", name: "高分动作", description: "任意训练得分达到 80", category: "quality", rarity: "rare", icon: "🏅", metricKey: "best_score", targetValue: 80, tier: 1, sortOrder: 40, isHidden: 0, isActive: 1 },
{ key: "score_90", name: "精确击球", description: "任意训练得分达到 90", category: "quality", rarity: "epic", icon: "🥇", metricKey: "best_score", targetValue: 90, tier: 2, sortOrder: 41, isHidden: 0, isActive: 1 },
{ key: "ntrp_2_5", name: "NTRP 2.5", description: "综合评分达到 2.5", category: "rating", rarity: "rare", icon: "📈", metricKey: "ntrp_rating", targetValue: 2.5, tier: 1, sortOrder: 50, isHidden: 0, isActive: 1 },
{ key: "ntrp_3_0", name: "NTRP 3.0", description: "综合评分达到 3.0", category: "rating", rarity: "epic", icon: "🚀", metricKey: "ntrp_rating", targetValue: 3.0, tier: 2, sortOrder: 51, isHidden: 0, isActive: 1 },
{ key: "pk_session_1", name: "训练 PK", description: "完成首个 PK 会话", category: "pk", rarity: "rare", icon: "⚔️", metricKey: "pk_count", targetValue: 1, tier: 1, sortOrder: 60, isHidden: 0, isActive: 1 },
{ key: "plan_link_5", name: "按计划训练", description: "累计 5 次训练匹配训练计划", category: "plan", rarity: "rare", icon: "🗂️", metricKey: "plan_matches", targetValue: 5, tier: 1, sortOrder: 70, isHidden: 0, isActive: 1 },
];
export async function getDb() {
if (!_db && process.env.DATABASE_URL) {
try {
_db = drizzle(process.env.DATABASE_URL);
} catch (error) {
console.warn("[Database] Failed to connect:", error);
_db = null;
}
}
return _db;
}
export async function seedAppSettings() {
const db = await getDb();
if (!db) return;
for (const setting of DEFAULT_APP_SETTINGS) {
const existing = await db.select().from(appSettings).where(eq(appSettings.settingKey, setting.settingKey)).limit(1);
if (existing.length === 0) {
await db.insert(appSettings).values(setting);
}
}
}
export async function listAppSettings() {
const db = await getDb();
if (!db) return [];
return db.select().from(appSettings).orderBy(asc(appSettings.settingKey));
}
export async function updateAppSetting(settingKey: string, value: unknown) {
const db = await getDb();
if (!db) return;
await db.update(appSettings).set({ value }).where(eq(appSettings.settingKey, settingKey));
}
export async function seedAchievementDefinitions() {
const db = await getDb();
if (!db) return;
for (const definition of ACHIEVEMENT_DEFINITION_SEED_DATA) {
const existing = await db.select().from(achievementDefinitions).where(eq(achievementDefinitions.key, definition.key)).limit(1);
if (existing.length === 0) {
await db.insert(achievementDefinitions).values(definition);
}
}
}
export async function listAchievementDefinitions() {
const db = await getDb();
if (!db) return [];
return db.select().from(achievementDefinitions)
.where(eq(achievementDefinitions.isActive, 1))
.orderBy(asc(achievementDefinitions.sortOrder), asc(achievementDefinitions.id));
}
export async function listAllAchievementDefinitions() {
const db = await getDb();
if (!db) return [];
return db.select().from(achievementDefinitions)
.orderBy(asc(achievementDefinitions.sortOrder), asc(achievementDefinitions.id));
}
export async function createAdminAuditLog(entry: InsertAdminAuditLog) {
const db = await getDb();
if (!db) return;
await db.insert(adminAuditLogs).values(entry);
}
export async function listAdminAuditLogs(limit = 100) {
const db = await getDb();
if (!db) return [];
return db.select({
id: adminAuditLogs.id,
adminUserId: adminAuditLogs.adminUserId,
adminName: users.name,
actionType: adminAuditLogs.actionType,
entityType: adminAuditLogs.entityType,
entityId: adminAuditLogs.entityId,
targetUserId: adminAuditLogs.targetUserId,
payload: adminAuditLogs.payload,
createdAt: adminAuditLogs.createdAt,
}).from(adminAuditLogs)
.leftJoin(users, eq(users.id, adminAuditLogs.adminUserId))
.orderBy(desc(adminAuditLogs.createdAt))
.limit(limit);
}
export async function listUsersForAdmin(limit = 100) {
const db = await getDb();
if (!db) return [];
return db.select({
id: users.id,
name: users.name,
role: users.role,
ntrpRating: users.ntrpRating,
totalSessions: users.totalSessions,
totalMinutes: users.totalMinutes,
totalShots: users.totalShots,
currentStreak: users.currentStreak,
longestStreak: users.longestStreak,
createdAt: users.createdAt,
lastSignedIn: users.lastSignedIn,
}).from(users).orderBy(desc(users.lastSignedIn)).limit(limit);
}
export async function getAdminUserId() {
const db = await getDb();
if (!db) return null;
const [admin] = await db.select().from(users).where(eq(users.role, "admin")).orderBy(desc(users.lastSignedIn)).limit(1);
return admin?.id ?? null;
}
export async function listAllBackgroundTasks(limit = 100) {
const db = await getDb();
if (!db) return [];
return db.select({
id: backgroundTasks.id,
userId: backgroundTasks.userId,
userName: users.name,
type: backgroundTasks.type,
status: backgroundTasks.status,
title: backgroundTasks.title,
message: backgroundTasks.message,
progress: backgroundTasks.progress,
payload: backgroundTasks.payload,
result: backgroundTasks.result,
error: backgroundTasks.error,
attempts: backgroundTasks.attempts,
maxAttempts: backgroundTasks.maxAttempts,
createdAt: backgroundTasks.createdAt,
updatedAt: backgroundTasks.updatedAt,
completedAt: backgroundTasks.completedAt,
}).from(backgroundTasks)
.leftJoin(users, eq(users.id, backgroundTasks.userId))
.orderBy(desc(backgroundTasks.createdAt))
.limit(limit);
}
export async function hasRecentBackgroundTaskOfType(
type: "ntrp_refresh_user" | "ntrp_refresh_all",
since: Date,
) {
const db = await getDb();
if (!db) return false;
const result = await db.select({ count: sql<number>`count(*)` }).from(backgroundTasks)
.where(and(eq(backgroundTasks.type, type), gte(backgroundTasks.createdAt, since)));
return (result[0]?.count || 0) > 0;
}
export async function listUserIds() {
const db = await getDb();
if (!db) return [];
return db.select({ id: users.id }).from(users).orderBy(asc(users.id));
}
// ===== USER OPERATIONS =====
export async function upsertUser(user: InsertUser): Promise<void> {
if (!user.openId) throw new Error("User openId is required for upsert");
const db = await getDb();
if (!db) { console.warn("[Database] Cannot upsert user: database not available"); return; }
try {
const values: InsertUser = { openId: user.openId };
const updateSet: Record<string, unknown> = {};
const textFields = ["name", "email", "loginMethod"] as const;
type TextField = (typeof textFields)[number];
const assignNullable = (field: TextField) => {
const value = user[field];
if (value === undefined) return;
const normalized = value ?? null;
values[field] = normalized;
updateSet[field] = normalized;
};
textFields.forEach(assignNullable);
if (user.lastSignedIn !== undefined) { values.lastSignedIn = user.lastSignedIn; updateSet.lastSignedIn = user.lastSignedIn; }
if (user.role !== undefined) { values.role = user.role; updateSet.role = user.role; }
else if (user.openId === ENV.ownerOpenId) { values.role = 'admin'; updateSet.role = 'admin'; }
if (!values.lastSignedIn) values.lastSignedIn = new Date();
if (Object.keys(updateSet).length === 0) updateSet.lastSignedIn = new Date();
await db.insert(users).values(values).onDuplicateKeyUpdate({ set: updateSet });
} catch (error) { console.error("[Database] Failed to upsert user:", error); throw error; }
}
export async function getUserByOpenId(openId: string) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function getUserByUsername(username: string) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(usernameAccounts).where(eq(usernameAccounts.username, username)).limit(1);
if (result.length === 0) return undefined;
const userResult = await db.select().from(users).where(eq(users.id, result[0].userId)).limit(1);
return userResult.length > 0 ? userResult[0] : undefined;
}
export function isValidRegistrationInvite(inviteCode?: string | null) {
const expected = ENV.registrationInviteCode.trim();
if (!expected) return true;
return (inviteCode ?? "").trim() === expected;
}
export async function createUsernameAccount(
username: string,
inviteCode?: string,
): Promise<{ user: typeof users.$inferSelect; isNew: boolean }> {
const db = await getDb();
if (!db) throw new Error("Database not available");
// Check if username already exists
const existing = await db.select().from(usernameAccounts).where(eq(usernameAccounts.username, username)).limit(1);
if (existing.length > 0) {
const user = await db.select().from(users).where(eq(users.id, existing[0].userId)).limit(1);
if (user.length > 0) {
const updatedRole = ENV.adminUsernames.includes(username) ? "admin" : user[0].role;
await db.update(users).set({ lastSignedIn: new Date(), role: updatedRole }).where(eq(users.id, user[0].id));
return { user: { ...user[0], role: updatedRole, lastSignedIn: new Date() }, isNew: false };
}
}
if (!isValidRegistrationInvite(inviteCode)) {
throw new Error("新用户注册需要正确的邀请码");
}
// Create new user with username as openId
const openId = `username_${username}_${Date.now()}`;
await db.insert(users).values({
openId,
name: username,
loginMethod: "username",
role: ENV.adminUsernames.includes(username) ? "admin" : "user",
lastSignedIn: new Date(),
ntrpRating: 1.5,
totalSessions: 0,
totalMinutes: 0,
});
const newUser = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
if (newUser.length === 0) throw new Error("Failed to create user");
await db.insert(usernameAccounts).values({ username, userId: newUser[0].id });
return { user: newUser[0], isNew: true };
}
export async function updateUserProfile(userId: number, data: {
skillLevel?: "beginner" | "intermediate" | "advanced";
trainingGoals?: string;
ntrpRating?: number;
totalSessions?: number;
totalMinutes?: number;
currentStreak?: number;
longestStreak?: number;
totalShots?: number;
}) {
const db = await getDb();
if (!db) return;
await db.update(users).set(data).where(eq(users.id, userId));
}
// ===== TRAINING PLAN OPERATIONS =====
export async function createTrainingPlan(plan: InsertTrainingPlan) {
const db = await getDb();
if (!db) throw new Error("Database not available");
// Deactivate existing active plans
await db.update(trainingPlans).set({ isActive: 0 }).where(and(eq(trainingPlans.userId, plan.userId), eq(trainingPlans.isActive, 1)));
const result = await db.insert(trainingPlans).values(plan);
return result[0].insertId;
}
export async function getUserTrainingPlans(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(trainingPlans).where(eq(trainingPlans.userId, userId)).orderBy(desc(trainingPlans.createdAt));
}
export async function getActivePlan(userId: number) {
const db = await getDb();
if (!db) return null;
const result = await db.select().from(trainingPlans).where(and(eq(trainingPlans.userId, userId), eq(trainingPlans.isActive, 1))).limit(1);
return result.length > 0 ? result[0] : null;
}
export async function updateTrainingPlan(planId: number, data: Partial<InsertTrainingPlan>) {
const db = await getDb();
if (!db) return;
await db.update(trainingPlans).set(data).where(eq(trainingPlans.id, planId));
}
const PLAN_KEYWORDS: Record<string, string[]> = {
forehand: ["正手", "forehand"],
backhand: ["反手", "backhand"],
serve: ["发球", "serve"],
volley: ["截击", "volley"],
overhead: ["高压", "overhead"],
slice: ["切削", "slice"],
lob: ["挑高", "lob"],
unknown: ["综合", "基础", "训练"],
};
export async function matchActivePlanForExercise(userId: number, exerciseType?: string | null) {
const activePlan = await getActivePlan(userId);
if (!activePlan || !exerciseType) {
return null;
}
const keywords = PLAN_KEYWORDS[exerciseType] ?? [exerciseType];
const exercises = Array.isArray(activePlan.exercises) ? activePlan.exercises as Array<Record<string, unknown>> : [];
const matched = exercises.find((exercise) => {
const haystack = JSON.stringify(exercise).toLowerCase();
return keywords.some(keyword => haystack.includes(keyword.toLowerCase()));
});
if (!matched) {
return null;
}
return {
planId: activePlan.id,
confidence: 0.72,
matchedExercise: matched,
};
}
// ===== VIDEO OPERATIONS =====
export async function createVideo(video: InsertTrainingVideo) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(trainingVideos).values(video);
return result[0].insertId;
}
export async function getUserVideos(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId)).orderBy(desc(trainingVideos.createdAt));
}
export async function getVideoById(videoId: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(trainingVideos).where(eq(trainingVideos.id, videoId)).limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function getVideoByFileKey(userId: number, fileKey: string) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(trainingVideos)
.where(and(eq(trainingVideos.userId, userId), eq(trainingVideos.fileKey, fileKey)))
.limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function updateVideoStatus(videoId: number, status: "pending" | "analyzing" | "completed" | "failed") {
const db = await getDb();
if (!db) return;
await db.update(trainingVideos).set({ analysisStatus: status }).where(eq(trainingVideos.id, videoId));
}
// ===== POSE ANALYSIS OPERATIONS =====
export async function createPoseAnalysis(analysis: InsertPoseAnalysis) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(poseAnalyses).values(analysis);
return result[0].insertId;
}
export async function getAnalysisByVideoId(videoId: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(poseAnalyses).where(eq(poseAnalyses.videoId, videoId)).orderBy(desc(poseAnalyses.createdAt)).limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function getUserAnalyses(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(poseAnalyses).where(eq(poseAnalyses.userId, userId)).orderBy(desc(poseAnalyses.createdAt));
}
// ===== TRAINING RECORD OPERATIONS =====
export async function createTrainingRecord(record: InsertTrainingRecord) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(trainingRecords).values(record);
return result[0].insertId;
}
export async function getUserTrainingRecords(userId: number, limit = 50) {
const db = await getDb();
if (!db) return [];
return db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId)).orderBy(desc(trainingRecords.trainingDate)).limit(limit);
}
export async function markRecordCompleted(recordId: number, poseScore?: number) {
const db = await getDb();
if (!db) return;
await db.update(trainingRecords).set({ completed: 1, poseScore: poseScore ?? null }).where(eq(trainingRecords.id, recordId));
}
export async function upsertTrainingRecordBySource(
record: InsertTrainingRecord & { sourceType: string; sourceId: string; userId: number }
) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const existing = await db.select().from(trainingRecords)
.where(and(
eq(trainingRecords.userId, record.userId),
eq(trainingRecords.sourceType, record.sourceType),
eq(trainingRecords.sourceId, record.sourceId),
))
.limit(1);
if (existing.length > 0) {
await db.update(trainingRecords).set(record).where(eq(trainingRecords.id, existing[0].id));
return { recordId: existing[0].id, isNew: false };
}
const result = await db.insert(trainingRecords).values(record);
return { recordId: result[0].insertId, isNew: true };
}
export async function upsertDailyTrainingAggregate(input: {
userId: number;
trainingDate: string;
deltaMinutes?: number;
deltaSessions?: number;
deltaAnalysisCount?: number;
deltaLiveAnalysisCount?: number;
deltaRecordingCount?: number;
deltaPkCount?: number;
deltaTotalActions?: number;
deltaEffectiveActions?: number;
deltaUnknownActions?: number;
score?: number | null;
metadata?: Record<string, unknown>;
}) {
const db = await getDb();
if (!db) return;
const dayKey = toDayKey(input.userId, input.trainingDate);
const [existing] = await db.select().from(dailyTrainingAggregates)
.where(eq(dailyTrainingAggregates.dayKey, dayKey))
.limit(1);
if (!existing) {
const totalScore = input.score ?? 0;
await db.insert(dailyTrainingAggregates).values({
dayKey,
userId: input.userId,
trainingDate: input.trainingDate,
totalMinutes: input.deltaMinutes ?? 0,
sessionCount: input.deltaSessions ?? 0,
analysisCount: input.deltaAnalysisCount ?? 0,
liveAnalysisCount: input.deltaLiveAnalysisCount ?? 0,
recordingCount: input.deltaRecordingCount ?? 0,
pkCount: input.deltaPkCount ?? 0,
totalActions: input.deltaTotalActions ?? 0,
effectiveActions: input.deltaEffectiveActions ?? 0,
unknownActions: input.deltaUnknownActions ?? 0,
totalScore,
averageScore: totalScore > 0 ? totalScore / Math.max(1, input.deltaSessions ?? 1) : 0,
metadata: input.metadata ?? null,
});
} else {
const nextSessionCount = (existing.sessionCount || 0) + (input.deltaSessions ?? 0);
const nextTotalScore = (existing.totalScore || 0) + (input.score ?? 0);
await db.update(dailyTrainingAggregates).set({
totalMinutes: (existing.totalMinutes || 0) + (input.deltaMinutes ?? 0),
sessionCount: nextSessionCount,
analysisCount: (existing.analysisCount || 0) + (input.deltaAnalysisCount ?? 0),
liveAnalysisCount: (existing.liveAnalysisCount || 0) + (input.deltaLiveAnalysisCount ?? 0),
recordingCount: (existing.recordingCount || 0) + (input.deltaRecordingCount ?? 0),
pkCount: (existing.pkCount || 0) + (input.deltaPkCount ?? 0),
totalActions: (existing.totalActions || 0) + (input.deltaTotalActions ?? 0),
effectiveActions: (existing.effectiveActions || 0) + (input.deltaEffectiveActions ?? 0),
unknownActions: (existing.unknownActions || 0) + (input.deltaUnknownActions ?? 0),
totalScore: nextTotalScore,
averageScore: nextSessionCount > 0 ? nextTotalScore / nextSessionCount : 0,
metadata: input.metadata ? { ...(existing.metadata as Record<string, unknown> | null ?? {}), ...input.metadata } : existing.metadata,
}).where(eq(dailyTrainingAggregates.id, existing.id));
}
await refreshUserTrainingSummary(input.userId);
}
export async function listDailyTrainingAggregates(userId: number, limit = 30) {
const db = await getDb();
if (!db) return [];
return db.select().from(dailyTrainingAggregates)
.where(eq(dailyTrainingAggregates.userId, userId))
.orderBy(desc(dailyTrainingAggregates.trainingDate))
.limit(limit);
}
export async function refreshUserTrainingSummary(userId: number) {
const db = await getDb();
if (!db) return;
const records = await db.select().from(trainingRecords)
.where(and(eq(trainingRecords.userId, userId), eq(trainingRecords.completed, 1)));
const aggregates = await db.select().from(dailyTrainingAggregates)
.where(eq(dailyTrainingAggregates.userId, userId))
.orderBy(desc(dailyTrainingAggregates.trainingDate));
const totalSessions = records.length;
const totalMinutes = records.reduce((sum, item) => sum + (item.durationMinutes || 0), 0);
const totalShots = aggregates.reduce((sum, item) => sum + (item.effectiveActions || 0), 0);
let currentStreak = 0;
const sortedDays = aggregates
.filter(item => (item.sessionCount || 0) > 0)
.map(item => item.trainingDate)
.sort((a, b) => a < b ? 1 : -1);
let cursor = new Date(`${getDateKey()}T00:00:00`);
for (const day of sortedDays) {
const normalized = new Date(`${day}T00:00:00`);
const diffDays = Math.round((cursor.getTime() - normalized.getTime()) / 86400000);
if (diffDays === 0 || diffDays === 1) {
currentStreak += 1;
cursor = normalized;
continue;
}
if (currentStreak > 0) {
break;
}
cursor = normalized;
currentStreak = 1;
}
const longestStreak = Math.max(currentStreak, records.length > 0 ? (await getLongestTrainingStreak(userId)) : 0);
await db.update(users).set({
totalSessions,
totalMinutes,
totalShots,
currentStreak,
longestStreak,
}).where(eq(users.id, userId));
}
async function getLongestTrainingStreak(userId: number) {
const db = await getDb();
if (!db) return 0;
const aggregates = await db.select().from(dailyTrainingAggregates)
.where(eq(dailyTrainingAggregates.userId, userId))
.orderBy(asc(dailyTrainingAggregates.trainingDate));
let longest = 0;
let current = 0;
let prev: Date | null = null;
for (const item of aggregates) {
if ((item.sessionCount || 0) <= 0) continue;
const currentDate = new Date(`${item.trainingDate}T00:00:00`);
if (!prev) {
current = 1;
} else {
const diff = Math.round((currentDate.getTime() - prev.getTime()) / 86400000);
current = diff === 1 ? current + 1 : 1;
}
longest = Math.max(longest, current);
prev = currentDate;
}
return longest;
}
// ===== RATING HISTORY OPERATIONS =====
export async function createRatingEntry(entry: InsertRatingHistory) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(ratingHistory).values(entry);
return result[0].insertId;
}
export async function getUserRatingHistory(userId: number, limit = 30) {
const db = await getDb();
if (!db) return [];
return db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(limit);
}
export async function createNtrpSnapshot(snapshot: InsertNtrpSnapshot) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const existing = await db.select().from(ntrpSnapshots)
.where(eq(ntrpSnapshots.snapshotKey, snapshot.snapshotKey))
.limit(1);
if (existing.length > 0) {
await db.update(ntrpSnapshots).set(snapshot).where(eq(ntrpSnapshots.id, existing[0].id));
return existing[0].id;
}
const result = await db.insert(ntrpSnapshots).values(snapshot);
return result[0].insertId;
}
export async function getLatestNtrpSnapshot(userId: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(ntrpSnapshots)
.where(eq(ntrpSnapshots.userId, userId))
.orderBy(desc(ntrpSnapshots.createdAt))
.limit(1);
return result[0];
}
export async function listNtrpSnapshots(userId: number, limit = 30) {
const db = await getDb();
if (!db) return [];
return db.select().from(ntrpSnapshots)
.where(eq(ntrpSnapshots.userId, userId))
.orderBy(desc(ntrpSnapshots.createdAt))
.limit(limit);
}
export async function createLiveAnalysisSession(session: InsertLiveAnalysisSession) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(liveAnalysisSessions).values(session);
return result[0].insertId;
}
export async function createLiveActionSegments(segments: InsertLiveActionSegment[]) {
const db = await getDb();
if (!db || segments.length === 0) return;
await db.insert(liveActionSegments).values(segments);
}
export async function listLiveAnalysisSessions(userId: number, limit = 20) {
const db = await getDb();
if (!db) return [];
return db.select().from(liveAnalysisSessions)
.where(eq(liveAnalysisSessions.userId, userId))
.orderBy(desc(liveAnalysisSessions.createdAt))
.limit(limit);
}
export async function listAdminLiveAnalysisSessions(limit = 50) {
const db = await getDb();
if (!db) return [];
return db.select({
id: liveAnalysisSessions.id,
userId: liveAnalysisSessions.userId,
userName: users.name,
title: liveAnalysisSessions.title,
sessionMode: liveAnalysisSessions.sessionMode,
status: liveAnalysisSessions.status,
dominantAction: liveAnalysisSessions.dominantAction,
overallScore: liveAnalysisSessions.overallScore,
durationMs: liveAnalysisSessions.durationMs,
effectiveSegments: liveAnalysisSessions.effectiveSegments,
totalSegments: liveAnalysisSessions.totalSegments,
videoUrl: liveAnalysisSessions.videoUrl,
createdAt: liveAnalysisSessions.createdAt,
}).from(liveAnalysisSessions)
.leftJoin(users, eq(users.id, liveAnalysisSessions.userId))
.orderBy(desc(liveAnalysisSessions.createdAt))
.limit(limit);
}
export async function getLiveAnalysisSessionById(sessionId: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(liveAnalysisSessions)
.where(eq(liveAnalysisSessions.id, sessionId))
.limit(1);
return result[0];
}
export async function getLiveActionSegmentsBySessionId(sessionId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(liveActionSegments)
.where(eq(liveActionSegments.sessionId, sessionId))
.orderBy(asc(liveActionSegments.startMs));
}
export async function getAchievementProgress(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(userAchievements)
.where(eq(userAchievements.userId, userId))
.orderBy(desc(userAchievements.unlockedAt), asc(userAchievements.achievementKey));
}
// ===== DAILY CHECK-IN OPERATIONS =====
export async function checkinToday(userId: number, notes?: string, minutesTrained?: number) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const today = new Date().toISOString().slice(0, 10);
// Check if already checked in today
const existing = await db.select().from(dailyCheckins)
.where(and(eq(dailyCheckins.userId, userId), eq(dailyCheckins.checkinDate, today)))
.limit(1);
if (existing.length > 0) {
return { alreadyCheckedIn: true, streak: existing[0].streakCount };
}
// Get yesterday's check-in to calculate streak
const yesterday = new Date(Date.now() - 86400000).toISOString().slice(0, 10);
const yesterdayCheckin = await db.select().from(dailyCheckins)
.where(and(eq(dailyCheckins.userId, userId), eq(dailyCheckins.checkinDate, yesterday)))
.limit(1);
const newStreak = yesterdayCheckin.length > 0 ? (yesterdayCheckin[0].streakCount + 1) : 1;
await db.insert(dailyCheckins).values({
userId,
checkinDate: today,
streakCount: newStreak,
notes: notes ?? null,
minutesTrained: minutesTrained ?? 0,
});
// Update user streak
const [userRow] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
const longestStreak = Math.max(userRow?.longestStreak || 0, newStreak);
await db.update(users).set({ currentStreak: newStreak, longestStreak }).where(eq(users.id, userId));
return { alreadyCheckedIn: false, streak: newStreak };
}
export async function getUserCheckins(userId: number, limit = 60) {
const db = await getDb();
if (!db) return [];
return db.select().from(dailyCheckins)
.where(eq(dailyCheckins.userId, userId))
.orderBy(desc(dailyCheckins.checkinDate))
.limit(limit);
}
export async function getTodayCheckin(userId: number) {
const db = await getDb();
if (!db) return null;
const today = new Date().toISOString().slice(0, 10);
const result = await db.select().from(dailyCheckins)
.where(and(eq(dailyCheckins.userId, userId), eq(dailyCheckins.checkinDate, today)))
.limit(1);
return result.length > 0 ? result[0] : null;
}
function metricValueFromContext(metricKey: string, context: {
trainingDays: number;
currentStreak: number;
totalMinutes: number;
effectiveActions: number;
recordingCount: number;
analysisCount: number;
liveAnalysisCount: number;
bestScore: number;
ntrpRating: number;
pkCount: number;
planMatches: number;
}) {
const metricMap: Record<string, number> = {
training_days: context.trainingDays,
current_streak: context.currentStreak,
total_minutes: context.totalMinutes,
effective_actions: context.effectiveActions,
recording_count: context.recordingCount,
analysis_count: context.analysisCount,
live_analysis_count: context.liveAnalysisCount,
best_score: context.bestScore,
ntrp_rating: context.ntrpRating,
pk_count: context.pkCount,
plan_matches: context.planMatches,
};
return metricMap[metricKey] ?? 0;
}
export async function refreshAchievementsForUser(userId: number) {
const db = await getDb();
if (!db) return [];
const definitions = await listAchievementDefinitions();
const progressRows = await getAchievementProgress(userId);
const records = await db.select().from(trainingRecords).where(and(eq(trainingRecords.userId, userId), eq(trainingRecords.completed, 1)));
const aggregates = await db.select().from(dailyTrainingAggregates).where(eq(dailyTrainingAggregates.userId, userId));
const liveSessions = await db.select().from(liveAnalysisSessions).where(eq(liveAnalysisSessions.userId, userId));
const [userRow] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
const bestScore = Math.max(
0,
...records.map((record) => record.poseScore || 0),
...liveSessions.map((session) => session.overallScore || 0),
);
const planMatches = records.filter((record) => record.linkedPlanId != null).length;
const context = {
trainingDays: aggregates.filter(item => (item.sessionCount || 0) > 0).length,
currentStreak: userRow?.currentStreak || 0,
totalMinutes: userRow?.totalMinutes || 0,
effectiveActions: userRow?.totalShots || 0,
recordingCount: records.filter(record => record.sourceType === "recording").length,
analysisCount: records.filter(record => record.sourceType === "analysis_upload").length,
liveAnalysisCount: records.filter(record => record.sourceType === "live_analysis").length,
bestScore,
ntrpRating: userRow?.ntrpRating || 1.5,
pkCount: records.filter(record => ((record.metadata as Record<string, unknown> | null)?.sessionMode) === "pk").length,
planMatches,
};
const unlockedKeys: string[] = [];
for (const definition of definitions) {
const currentValue = metricValueFromContext(definition.metricKey, context);
const progressPct = definition.targetValue > 0 ? Math.min(100, (currentValue / definition.targetValue) * 100) : 0;
const progressKey = `${userId}:${definition.key}`;
const existing = progressRows.find((row) => row.achievementKey === definition.key);
const unlockedAt = currentValue >= definition.targetValue ? (existing?.unlockedAt ?? new Date()) : null;
if (!existing) {
await db.insert(userAchievements).values({
progressKey,
userId,
achievementKey: definition.key,
currentValue,
progressPct,
unlockedAt,
});
if (unlockedAt) unlockedKeys.push(definition.key);
} else {
await db.update(userAchievements).set({
currentValue,
progressPct,
unlockedAt: existing.unlockedAt ?? unlockedAt,
lastEvaluatedAt: new Date(),
}).where(eq(userAchievements.id, existing.id));
if (!existing.unlockedAt && unlockedAt) unlockedKeys.push(definition.key);
}
}
return unlockedKeys;
}
export async function listUserAchievements(userId: number) {
const db = await getDb();
if (!db) return [];
const definitions = await listAllAchievementDefinitions();
const progress = await getAchievementProgress(userId);
const progressMap = new Map(progress.map(item => [item.achievementKey, item]));
return definitions.map((definition) => {
const row = progressMap.get(definition.key);
return {
...definition,
currentValue: row?.currentValue ?? 0,
progressPct: row?.progressPct ?? 0,
unlockedAt: row?.unlockedAt ?? null,
unlocked: Boolean(row?.unlockedAt),
};
});
}
// ===== BADGE OPERATIONS =====
// Badge definitions
export const BADGE_DEFINITIONS: Record<string, { name: string; description: string; icon: string; category: string }> = {
first_login: { name: "初来乍到", description: "首次登录Tennis Hub", icon: "🎾", category: "milestone" },
first_training: { name: "初试身手", description: "完成第一次训练", icon: "💪", category: "training" },
first_video: { name: "影像记录", description: "上传第一个训练视频", icon: "📹", category: "video" },
first_analysis: { name: "AI教练", description: "完成第一次视频分析", icon: "🤖", category: "analysis" },
streak_3: { name: "三日坚持", description: "连续打卡3天", icon: "🔥", category: "streak" },
streak_7: { name: "一周达人", description: "连续打卡7天", icon: "⭐", category: "streak" },
streak_14: { name: "两周勇士", description: "连续打卡14天", icon: "🏆", category: "streak" },
streak_30: { name: "月度冠军", description: "连续打卡30天", icon: "👑", category: "streak" },
sessions_10: { name: "十次训练", description: "累计完成10次训练", icon: "🎯", category: "training" },
sessions_50: { name: "五十次训练", description: "累计完成50次训练", icon: "💎", category: "training" },
sessions_100: { name: "百次训练", description: "累计完成100次训练", icon: "🌟", category: "training" },
videos_5: { name: "视频达人", description: "上传5个训练视频", icon: "🎬", category: "video" },
videos_20: { name: "视频大师", description: "上传20个训练视频", icon: "📽️", category: "video" },
score_80: { name: "优秀姿势", description: "视频分析获得80分以上", icon: "🏅", category: "analysis" },
score_90: { name: "完美姿势", description: "视频分析获得90分以上", icon: "🥇", category: "analysis" },
ntrp_2: { name: "NTRP 2.0", description: "NTRP评分达到2.0", icon: "📈", category: "rating" },
ntrp_3: { name: "NTRP 3.0", description: "NTRP评分达到3.0", icon: "📊", category: "rating" },
ntrp_4: { name: "NTRP 4.0", description: "NTRP评分达到4.0", icon: "🚀", category: "rating" },
minutes_60: { name: "一小时训练", description: "累计训练60分钟", icon: "⏱️", category: "training" },
minutes_300: { name: "五小时训练", description: "累计训练300分钟", icon: "⏰", category: "training" },
minutes_1000: { name: "千分钟训练", description: "累计训练1000分钟", icon: "🕐", category: "training" },
shots_100: { name: "百球达人", description: "累计击球100次", icon: "🎾", category: "analysis" },
shots_500: { name: "五百球大师", description: "累计击球500次", icon: "🏸", category: "analysis" },
};
export async function getUserBadges(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(userBadges).where(eq(userBadges.userId, userId));
}
export async function awardBadge(userId: number, badgeKey: string) {
const db = await getDb();
if (!db) return false;
// Check if already has this badge
const existing = await db.select().from(userBadges)
.where(and(eq(userBadges.userId, userId), eq(userBadges.badgeKey, badgeKey)))
.limit(1);
if (existing.length > 0) return false;
await db.insert(userBadges).values({ userId, badgeKey });
return true;
}
export async function checkAndAwardBadges(userId: number) {
const db = await getDb();
if (!db) return [];
const [userRow] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
if (!userRow) return [];
const records = await db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId));
const videos = await db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId));
const analyses = await db.select().from(poseAnalyses).where(eq(poseAnalyses.userId, userId));
const completedRecords = records.filter(r => r.completed === 1);
const totalMinutes = records.reduce((sum, r) => sum + (r.durationMinutes || 0), 0);
const totalShots = analyses.reduce((sum, a) => sum + (a.shotCount || 0), 0);
const maxScore = analyses.reduce((max, a) => Math.max(max, a.overallScore || 0), 0);
const streak = userRow.currentStreak || 0;
const ntrp = userRow.ntrpRating || 1.5;
const newBadges: string[] = [];
const checks: [boolean, string][] = [
[true, "first_login"],
[completedRecords.length >= 1, "first_training"],
[videos.length >= 1, "first_video"],
[analyses.length >= 1, "first_analysis"],
[streak >= 3, "streak_3"],
[streak >= 7, "streak_7"],
[streak >= 14, "streak_14"],
[streak >= 30, "streak_30"],
[completedRecords.length >= 10, "sessions_10"],
[completedRecords.length >= 50, "sessions_50"],
[completedRecords.length >= 100, "sessions_100"],
[videos.length >= 5, "videos_5"],
[videos.length >= 20, "videos_20"],
[maxScore >= 80, "score_80"],
[maxScore >= 90, "score_90"],
[ntrp >= 2.0, "ntrp_2"],
[ntrp >= 3.0, "ntrp_3"],
[ntrp >= 4.0, "ntrp_4"],
[totalMinutes >= 60, "minutes_60"],
[totalMinutes >= 300, "minutes_300"],
[totalMinutes >= 1000, "minutes_1000"],
[totalShots >= 100, "shots_100"],
[totalShots >= 500, "shots_500"],
];
for (const [condition, key] of checks) {
if (condition) {
const awarded = await awardBadge(userId, key);
if (awarded) newBadges.push(key);
}
}
return newBadges;
}
// ===== LEADERBOARD OPERATIONS =====
export async function getLeaderboard(sortBy: "ntrpRating" | "totalMinutes" | "totalSessions" | "totalShots" = "ntrpRating", limit = 50) {
const db = await getDb();
if (!db) return [];
const sortColumn = {
ntrpRating: users.ntrpRating,
totalMinutes: users.totalMinutes,
totalSessions: users.totalSessions,
totalShots: users.totalShots,
}[sortBy];
return db.select({
id: users.id,
name: users.name,
ntrpRating: users.ntrpRating,
totalSessions: users.totalSessions,
totalMinutes: users.totalMinutes,
totalShots: users.totalShots,
currentStreak: users.currentStreak,
longestStreak: users.longestStreak,
skillLevel: users.skillLevel,
createdAt: users.createdAt,
}).from(users).orderBy(desc(sortColumn)).limit(limit);
}
// ===== VISION REFERENCE LIBRARY =====
export const VISION_REFERENCE_SEED_DATA: Omit<
InsertVisionReferenceImage,
"id" | "createdAt" | "updatedAt"
>[] = [
{
slug: "commons-forehand-tennispictures",
title: "标准图:正手挥拍",
exerciseType: "forehand",
imageUrl: "https://commons.wikimedia.org/wiki/Special:FilePath/Forehand.jpg",
sourcePageUrl: "https://commons.wikimedia.org/wiki/File:Forehand.jpg",
sourceLabel: "Wikimedia Commons",
author: "Tennispictures",
license: "CC0 1.0",
expectedFocus: ["引拍完整", "击球臂路径", "肩髋转动", "重心转移"],
tags: ["forehand", "reference", "commons", "stroke"],
notes: "用于检测模型对正手引拍、发力和随挥阶段的描述能力。",
sortOrder: 1,
isPublished: 1,
},
{
slug: "commons-backhand-federer",
title: "标准图:反手挥拍",
exerciseType: "backhand",
imageUrl: "https://commons.wikimedia.org/wiki/Special:FilePath/Backhand_Federer.jpg",
sourcePageUrl: "https://commons.wikimedia.org/wiki/File:Backhand_Federer.jpg",
sourceLabel: "Wikimedia Commons",
author: "Ian Gampon",
license: "CC BY 2.0",
expectedFocus: ["非持拍手收回", "躯干旋转", "拍面路径", "击球点位置"],
tags: ["backhand", "reference", "commons", "stroke"],
notes: "用于检测模型对单反/反手击球阶段和身体协同的判断。",
sortOrder: 2,
isPublished: 1,
},
{
slug: "commons-serena-serve",
title: "标准图:发球",
exerciseType: "serve",
imageUrl: "https://commons.wikimedia.org/wiki/Special:FilePath/Serena_Williams_Serves.JPG",
sourcePageUrl: "https://commons.wikimedia.org/wiki/File:Serena_Williams_Serves.JPG",
sourceLabel: "Wikimedia Commons",
author: "Clavecin",
license: "Public Domain",
expectedFocus: ["抛球与击球点", "肩肘链条", "躯干伸展", "落地重心"],
tags: ["serve", "reference", "commons", "overhead"],
notes: "用于检测模型对发球上举、鞭打和击球点的识别能力。",
sortOrder: 3,
isPublished: 1,
},
{
slug: "commons-volley-lewis",
title: "标准图:网前截击",
exerciseType: "volley",
imageUrl: "https://commons.wikimedia.org/wiki/Special:FilePath/Ernest_w._lewis,_volleying.jpg",
sourcePageUrl: "https://commons.wikimedia.org/wiki/File:Ernest_w._lewis,_volleying.jpg",
sourceLabel: "Wikimedia Commons",
author: "Unknown author",
license: "Public Domain",
expectedFocus: ["拍头稳定", "准备姿态", "身体前压", "短促触球"],
tags: ["volley", "reference", "commons", "net-play"],
notes: "用于检测模型对截击站位和紧凑击球结构的识别能力。",
sortOrder: 4,
isPublished: 1,
},
{
slug: "commons-tiafoe-backhand",
title: "标准图:现代反手参考",
exerciseType: "backhand",
imageUrl: "https://commons.wikimedia.org/wiki/Special:FilePath/Frances_Tiafoe_Backhand.jpg",
sourcePageUrl: "https://commons.wikimedia.org/wiki/File:Frances_Tiafoe_Backhand.jpg",
sourceLabel: "Wikimedia Commons",
author: null,
license: "Wikimedia Commons file license",
expectedFocus: ["双手协同", "脚步支撑", "肩髋分离", "随挥方向"],
tags: ["backhand", "reference", "commons", "modern"],
notes: "补充现代职业选手反手样本,便于比较传统与现代动作语言。",
sortOrder: 5,
isPublished: 1,
},
];
export async function seedVisionReferenceImages() {
const db = await getDb();
if (!db) return;
const existing = await db.select().from(visionReferenceImages).limit(1);
if (existing.length > 0) return;
for (const item of VISION_REFERENCE_SEED_DATA) {
await db.insert(visionReferenceImages).values(item);
}
}
export async function listVisionReferenceImages() {
const db = await getDb();
if (!db) return [];
return db.select().from(visionReferenceImages)
.where(eq(visionReferenceImages.isPublished, 1))
.orderBy(asc(visionReferenceImages.sortOrder), asc(visionReferenceImages.id));
}
export async function getVisionReferenceImageById(id: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(visionReferenceImages)
.where(eq(visionReferenceImages.id, id))
.limit(1);
return result[0];
}
export async function createVisionTestRun(run: InsertVisionTestRun) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(visionTestRuns).values(run);
return result[0].insertId;
}
export async function listVisionTestRuns(userId?: number, limit = 50) {
const db = await getDb();
if (!db) return [];
const query = db.select({
id: visionTestRuns.id,
taskId: visionTestRuns.taskId,
userId: visionTestRuns.userId,
userName: users.name,
referenceImageId: visionTestRuns.referenceImageId,
referenceTitle: visionReferenceImages.title,
title: visionTestRuns.title,
exerciseType: visionTestRuns.exerciseType,
imageUrl: visionTestRuns.imageUrl,
status: visionTestRuns.status,
visionStatus: visionTestRuns.visionStatus,
configuredModel: visionTestRuns.configuredModel,
expectedFocus: visionTestRuns.expectedFocus,
summary: visionTestRuns.summary,
corrections: visionTestRuns.corrections,
report: visionTestRuns.report,
warning: visionTestRuns.warning,
error: visionTestRuns.error,
createdAt: visionTestRuns.createdAt,
updatedAt: visionTestRuns.updatedAt,
}).from(visionTestRuns)
.leftJoin(users, eq(users.id, visionTestRuns.userId))
.leftJoin(visionReferenceImages, eq(visionReferenceImages.id, visionTestRuns.referenceImageId))
.orderBy(desc(visionTestRuns.createdAt))
.limit(limit);
if (userId == null) {
return query;
}
return db.select({
id: visionTestRuns.id,
taskId: visionTestRuns.taskId,
userId: visionTestRuns.userId,
userName: users.name,
referenceImageId: visionTestRuns.referenceImageId,
referenceTitle: visionReferenceImages.title,
title: visionTestRuns.title,
exerciseType: visionTestRuns.exerciseType,
imageUrl: visionTestRuns.imageUrl,
status: visionTestRuns.status,
visionStatus: visionTestRuns.visionStatus,
configuredModel: visionTestRuns.configuredModel,
expectedFocus: visionTestRuns.expectedFocus,
summary: visionTestRuns.summary,
corrections: visionTestRuns.corrections,
report: visionTestRuns.report,
warning: visionTestRuns.warning,
error: visionTestRuns.error,
createdAt: visionTestRuns.createdAt,
updatedAt: visionTestRuns.updatedAt,
}).from(visionTestRuns)
.leftJoin(users, eq(users.id, visionTestRuns.userId))
.leftJoin(visionReferenceImages, eq(visionReferenceImages.id, visionTestRuns.referenceImageId))
.where(eq(visionTestRuns.userId, userId))
.orderBy(desc(visionTestRuns.createdAt))
.limit(limit);
}
export async function completeVisionTestRun(taskId: string, data: {
visionStatus: "ok" | "fallback";
summary?: string | null;
corrections: string;
report?: unknown;
warning?: string | null;
}) {
const db = await getDb();
if (!db) return;
await db.update(visionTestRuns).set({
status: "succeeded",
visionStatus: data.visionStatus,
summary: data.summary ?? null,
corrections: data.corrections,
report: data.report ?? null,
warning: data.warning ?? null,
error: null,
}).where(eq(visionTestRuns.taskId, taskId));
}
export async function failVisionTestRun(taskId: string, error: string) {
const db = await getDb();
if (!db) return;
await db.update(visionTestRuns).set({
status: "failed",
visionStatus: "failed",
error,
}).where(eq(visionTestRuns.taskId, taskId));
}
// ===== TUTORIAL OPERATIONS =====
export const TUTORIAL_SEED_DATA: Omit<InsertTutorialVideo, "id">[] = [
{
title: "正手击球基础",
category: "forehand",
skillLevel: "beginner",
description: "学习正手击球的基本站位、握拍方式和挥拍轨迹,建立稳定的正手基础。",
keyPoints: JSON.stringify(["东方式或半西方式握拍", "侧身引拍,肩膀转动90度", "从低到高的挥拍轨迹", "随挥至对侧肩膀", "重心转移从后脚到前脚"]),
commonMistakes: JSON.stringify(["手腕过度发力", "没有转体", "击球点太靠后", "随挥不充分"]),
duration: 300,
sortOrder: 1,
},
{
title: "反手击球基础",
category: "backhand",
skillLevel: "beginner",
description: "掌握单手和双手反手的核心技术,包括握拍转换和击球时机。",
keyPoints: JSON.stringify(["双手反手更适合初学者", "早引拍,肩膀充分转动", "击球点在身体前方", "保持手臂伸展"]),
commonMistakes: JSON.stringify(["只用手臂发力", "击球点太迟", "缺少随挥", "脚步不到位"]),
duration: 300,
sortOrder: 2,
},
{
title: "发球技术",
category: "serve",
skillLevel: "beginner",
description: "从抛球、引拍到击球的完整发球动作分解与练习。",
keyPoints: JSON.stringify(["稳定的抛球是关键", "大陆式握拍", "引拍时身体充分弓身", "最高点击球", "手腕内旋加速"]),
commonMistakes: JSON.stringify(["抛球不稳定", "手臂弯曲击球", "重心没有向前", "发力时机不对"]),
duration: 360,
sortOrder: 3,
},
{
title: "截击技术",
category: "volley",
skillLevel: "intermediate",
description: "网前截击的站位、准备姿势和击球技巧。",
keyPoints: JSON.stringify(["分腿弯膝准备姿势", "拍头保持在视线前方", "短促的击球动作", "步伐迎向球"]),
commonMistakes: JSON.stringify(["挥拍幅度太大", "站位太远", "拍面角度不对", "重心太高"]),
duration: 240,
sortOrder: 4,
},
{
title: "脚步移动训练",
category: "footwork",
skillLevel: "beginner",
description: "网球基础脚步训练,包括分步、交叉步、滑步和回位。",
keyPoints: JSON.stringify(["分步判断球的方向", "交叉步快速移动", "小碎步调整位置", "击球后快速回中"]),
commonMistakes: JSON.stringify(["脚步懒散不移动", "重心太高", "回位太慢", "没有分步"]),
duration: 240,
sortOrder: 5,
},
{
title: "正手上旋",
category: "forehand",
skillLevel: "intermediate",
description: "掌握正手上旋球的发力技巧和拍面角度控制。",
keyPoints: JSON.stringify(["半西方式或西方式握拍", "从低到高的刷球动作", "加速手腕内旋", "随挥结束在头部上方"]),
commonMistakes: JSON.stringify(["拍面太开放", "没有刷球动作", "随挥不充分"]),
duration: 300,
sortOrder: 6,
},
{
title: "发球变化(切削/上旋)",
category: "serve",
skillLevel: "advanced",
description: "高级发球技术,包括切削发球和Kick发球的动作要领。",
keyPoints: JSON.stringify(["切削发球:侧旋切球", "Kick发球从下到上刷球", "抛球位置根据发球类型调整", "手腕加速是关键"]),
commonMistakes: JSON.stringify(["抛球位置没有变化", "旋转不足", "发力方向错误"]),
duration: 360,
sortOrder: 7,
},
{
title: "影子挥拍练习",
category: "shadow",
skillLevel: "beginner",
description: "不需要球的挥拍练习,专注于动作轨迹和肌肉记忆。",
keyPoints: JSON.stringify(["慢动作分解每个环节", "关注脚步和重心转移", "对着镜子检查姿势", "逐渐加快速度"]),
commonMistakes: JSON.stringify(["动作太快不规范", "忽略脚步", "没有完整的随挥"]),
duration: 180,
sortOrder: 8,
},
{
title: "墙壁练习技巧",
category: "wall",
skillLevel: "beginner",
description: "利用墙壁进行的各种练习方法,提升控球和反应能力。",
keyPoints: JSON.stringify(["保持适当距离", "控制力量和方向", "交替练习正反手", "注意脚步移动"]),
commonMistakes: JSON.stringify(["力量太大控制不住", "站位太近或太远", "只练习一种击球"]),
duration: 240,
sortOrder: 9,
},
{
title: "体能训练",
category: "fitness",
skillLevel: "beginner",
description: "网球专项体能训练,提升爆发力、敏捷性和耐力。",
keyPoints: JSON.stringify(["核心力量训练", "下肢爆发力练习", "敏捷性梯子训练", "拉伸和灵活性"]),
commonMistakes: JSON.stringify(["忽略热身", "训练过度", "动作不标准"]),
duration: 300,
sortOrder: 10,
},
{
title: "比赛策略基础",
category: "strategy",
skillLevel: "intermediate",
description: "网球比赛中的基本战术和策略运用。",
keyPoints: JSON.stringify(["控制球场深度", "变换节奏和方向", "利用对手弱点", "网前战术时机"]),
commonMistakes: JSON.stringify(["打法单一", "没有计划", "心态波动大"]),
duration: 300,
sortOrder: 11,
},
];
export async function seedTutorials() {
const db = await getDb();
if (!db) return;
const existing = await db.select().from(tutorialVideos).limit(1);
if (existing.length > 0) return; // Already seeded
for (const t of TUTORIAL_SEED_DATA) {
await db.insert(tutorialVideos).values(t);
}
}
export async function getTutorials(category?: string, skillLevel?: string) {
const db = await getDb();
if (!db) return [];
let conditions = [eq(tutorialVideos.isPublished, 1)];
if (category) conditions.push(eq(tutorialVideos.category, category));
if (skillLevel) conditions.push(eq(tutorialVideos.skillLevel, skillLevel as any));
return db.select().from(tutorialVideos).where(and(...conditions)).orderBy(tutorialVideos.sortOrder);
}
export async function getTutorialById(id: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(tutorialVideos).where(eq(tutorialVideos.id, id)).limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function getUserTutorialProgress(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(tutorialProgress).where(eq(tutorialProgress.userId, userId));
}
export async function updateTutorialProgress(userId: number, tutorialId: number, data: { watched?: number; selfScore?: number; notes?: string; comparisonVideoId?: number }) {
const db = await getDb();
if (!db) return;
const existing = await db.select().from(tutorialProgress)
.where(and(eq(tutorialProgress.userId, userId), eq(tutorialProgress.tutorialId, tutorialId)))
.limit(1);
if (existing.length > 0) {
await db.update(tutorialProgress).set(data).where(eq(tutorialProgress.id, existing[0].id));
} else {
await db.insert(tutorialProgress).values({ userId, tutorialId, ...data });
}
}
// ===== TRAINING REMINDER OPERATIONS =====
export async function getUserReminders(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(trainingReminders).where(eq(trainingReminders.userId, userId)).orderBy(desc(trainingReminders.createdAt));
}
export async function createReminder(data: InsertTrainingReminder) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(trainingReminders).values(data);
return result[0].insertId;
}
export async function updateReminder(reminderId: number, userId: number, data: Partial<InsertTrainingReminder>) {
const db = await getDb();
if (!db) return;
await db.update(trainingReminders).set(data).where(and(eq(trainingReminders.id, reminderId), eq(trainingReminders.userId, userId)));
}
export async function deleteReminder(reminderId: number, userId: number) {
const db = await getDb();
if (!db) return;
await db.delete(trainingReminders).where(and(eq(trainingReminders.id, reminderId), eq(trainingReminders.userId, userId)));
}
export async function toggleReminder(reminderId: number, userId: number, isActive: number) {
const db = await getDb();
if (!db) return;
await db.update(trainingReminders).set({ isActive }).where(and(eq(trainingReminders.id, reminderId), eq(trainingReminders.userId, userId)));
}
// ===== NOTIFICATION OPERATIONS =====
export async function getUserNotifications(userId: number, limit = 50) {
const db = await getDb();
if (!db) return [];
return db.select().from(notificationLog).where(eq(notificationLog.userId, userId)).orderBy(desc(notificationLog.createdAt)).limit(limit);
}
export async function createNotification(data: InsertNotificationLog) {
const db = await getDb();
if (!db) return;
await db.insert(notificationLog).values(data);
}
export async function markNotificationRead(notificationId: number, userId: number) {
const db = await getDb();
if (!db) return;
await db.update(notificationLog).set({ isRead: 1 }).where(and(eq(notificationLog.id, notificationId), eq(notificationLog.userId, userId)));
}
export async function markAllNotificationsRead(userId: number) {
const db = await getDb();
if (!db) return;
await db.update(notificationLog).set({ isRead: 1 }).where(eq(notificationLog.userId, userId));
}
export async function getUnreadNotificationCount(userId: number) {
const db = await getDb();
if (!db) return 0;
const result = await db.select({ count: sql<number>`count(*)` }).from(notificationLog)
.where(and(eq(notificationLog.userId, userId), eq(notificationLog.isRead, 0)));
return result[0]?.count || 0;
}
// ===== BACKGROUND TASK OPERATIONS =====
export async function createBackgroundTask(task: InsertBackgroundTask) {
const db = await getDb();
if (!db) throw new Error("Database not available");
await db.insert(backgroundTasks).values(task);
return task.id;
}
export async function listUserBackgroundTasks(userId: number, limit = 20) {
const db = await getDb();
if (!db) return [];
return db.select().from(backgroundTasks)
.where(eq(backgroundTasks.userId, userId))
.orderBy(desc(backgroundTasks.createdAt))
.limit(limit);
}
export async function getBackgroundTaskById(taskId: string) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(backgroundTasks)
.where(eq(backgroundTasks.id, taskId))
.limit(1);
return result[0];
}
export async function getUserBackgroundTaskById(userId: number, taskId: string) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(backgroundTasks)
.where(and(eq(backgroundTasks.id, taskId), eq(backgroundTasks.userId, userId)))
.limit(1);
return result[0];
}
export async function claimNextBackgroundTask(workerId: string) {
const db = await getDb();
if (!db) return null;
const now = new Date();
const [nextTask] = await db.select().from(backgroundTasks)
.where(and(eq(backgroundTasks.status, "queued"), lte(backgroundTasks.runAfter, now)))
.orderBy(asc(backgroundTasks.runAfter), asc(backgroundTasks.createdAt))
.limit(1);
if (!nextTask) {
return null;
}
await db.update(backgroundTasks).set({
status: "running",
workerId,
attempts: sql`${backgroundTasks.attempts} + 1`,
lockedAt: now,
startedAt: now,
updatedAt: now,
}).where(eq(backgroundTasks.id, nextTask.id));
return getBackgroundTaskById(nextTask.id);
}
export async function heartbeatBackgroundTask(taskId: string, workerId: string) {
const db = await getDb();
if (!db) return;
await db.update(backgroundTasks).set({
workerId,
lockedAt: new Date(),
}).where(eq(backgroundTasks.id, taskId));
}
export async function updateBackgroundTask(taskId: string, data: Partial<InsertBackgroundTask>) {
const db = await getDb();
if (!db) return;
await db.update(backgroundTasks).set(data).where(eq(backgroundTasks.id, taskId));
}
export async function completeBackgroundTask(taskId: string, result: unknown, message?: string) {
const db = await getDb();
if (!db) return;
await db.update(backgroundTasks).set({
status: "succeeded",
progress: 100,
message: message ?? "已完成",
result,
error: null,
workerId: null,
lockedAt: null,
completedAt: new Date(),
}).where(eq(backgroundTasks.id, taskId));
}
export async function failBackgroundTask(taskId: string, error: string) {
const db = await getDb();
if (!db) return;
await db.update(backgroundTasks).set({
status: "failed",
error,
workerId: null,
lockedAt: null,
completedAt: new Date(),
}).where(eq(backgroundTasks.id, taskId));
}
export async function rescheduleBackgroundTask(taskId: string, params: {
progress?: number;
message?: string;
error?: string | null;
delayMs?: number;
}) {
const db = await getDb();
if (!db) return;
await db.update(backgroundTasks).set({
status: "queued",
progress: params.progress,
message: params.message,
error: params.error ?? null,
workerId: null,
lockedAt: null,
runAfter: new Date(Date.now() + (params.delayMs ?? 0)),
}).where(eq(backgroundTasks.id, taskId));
}
export async function retryBackgroundTask(userId: number, taskId: string) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const task = await getUserBackgroundTaskById(userId, taskId);
if (!task) {
throw new Error("Task not found");
}
await db.update(backgroundTasks).set({
status: "queued",
progress: 0,
message: "任务已重新排队",
error: null,
result: null,
workerId: null,
lockedAt: null,
completedAt: null,
runAfter: new Date(),
}).where(eq(backgroundTasks.id, taskId));
return getBackgroundTaskById(taskId);
}
export async function requeueStaleBackgroundTasks(staleBefore: Date) {
const db = await getDb();
if (!db) return;
await db.update(backgroundTasks).set({
status: "queued",
message: "检测到任务中断,已重新排队",
workerId: null,
lockedAt: null,
runAfter: new Date(),
}).where(and(eq(backgroundTasks.status, "running"), lte(backgroundTasks.lockedAt, staleBefore)));
}
// ===== STATS HELPERS =====
export async function getUserStats(userId: number) {
const db = await getDb();
if (!db) return null;
const [userRow] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
if (!userRow) return null;
const analyses = await db.select().from(poseAnalyses).where(eq(poseAnalyses.userId, userId));
const records = await db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId));
const videos = await db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId));
const ratings = await db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(30);
const daily = await db.select().from(dailyTrainingAggregates).where(eq(dailyTrainingAggregates.userId, userId)).orderBy(desc(dailyTrainingAggregates.trainingDate)).limit(30);
const liveSessions = await db.select().from(liveAnalysisSessions).where(eq(liveAnalysisSessions.userId, userId)).orderBy(desc(liveAnalysisSessions.createdAt)).limit(10);
const latestSnapshot = await getLatestNtrpSnapshot(userId);
const achievements = await listUserAchievements(userId);
const completedRecords = records.filter(r => r.completed === 1);
const totalShots = Math.max(
analyses.reduce((sum, a) => sum + (a.shotCount || 0), 0),
daily.reduce((sum, item) => sum + (item.effectiveActions || 0), 0),
userRow.totalShots || 0,
);
const avgScore = analyses.length > 0 ? analyses.reduce((sum, a) => sum + (a.overallScore || 0), 0) / analyses.length : 0;
return {
ntrpRating: userRow.ntrpRating || latestSnapshot?.rating || 1.5,
totalSessions: completedRecords.length,
totalMinutes: records.reduce((sum, r) => sum + (r.durationMinutes || 0), 0),
totalVideos: videos.length,
analyzedVideos: videos.filter(v => v.analysisStatus === "completed").length,
totalShots,
averageScore: Math.round(avgScore * 10) / 10,
ratingHistory: ratings.reverse(),
recentAnalyses: analyses.slice(0, 10),
recentLiveSessions: liveSessions,
dailyTraining: daily.reverse(),
achievements,
latestNtrpSnapshot: latestSnapshot ?? null,
};
}