693 行
28 KiB
TypeScript
693 行
28 KiB
TypeScript
import { eq, desc, and, sql } from "drizzle-orm";
|
||
import { drizzle } from "drizzle-orm/mysql2";
|
||
import {
|
||
InsertUser, users,
|
||
usernameAccounts,
|
||
trainingPlans, InsertTrainingPlan,
|
||
trainingVideos, InsertTrainingVideo,
|
||
poseAnalyses, InsertPoseAnalysis,
|
||
trainingRecords, InsertTrainingRecord,
|
||
ratingHistory, InsertRatingHistory,
|
||
dailyCheckins, InsertDailyCheckin,
|
||
userBadges, InsertUserBadge,
|
||
tutorialVideos, InsertTutorialVideo,
|
||
tutorialProgress, InsertTutorialProgress,
|
||
trainingReminders, InsertTrainingReminder,
|
||
notificationLog, InsertNotificationLog,
|
||
} from "../drizzle/schema";
|
||
import { ENV } from './_core/env';
|
||
|
||
let _db: ReturnType<typeof drizzle> | null = null;
|
||
|
||
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;
|
||
}
|
||
|
||
// ===== 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 async function createUsernameAccount(username: 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) {
|
||
await db.update(users).set({ lastSignedIn: new Date() }).where(eq(users.id, user[0].id));
|
||
return { user: user[0], isNew: false };
|
||
}
|
||
}
|
||
|
||
// Create new user with username as openId
|
||
const openId = `username_${username}_${Date.now()}`;
|
||
await db.insert(users).values({
|
||
openId,
|
||
name: username,
|
||
loginMethod: "username",
|
||
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 undefined;
|
||
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] : undefined;
|
||
}
|
||
|
||
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));
|
||
}
|
||
|
||
// ===== 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 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));
|
||
}
|
||
|
||
// ===== 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);
|
||
}
|
||
|
||
// ===== 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;
|
||
}
|
||
|
||
// ===== 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);
|
||
}
|
||
|
||
// ===== 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;
|
||
}
|
||
|
||
// ===== 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 completedRecords = records.filter(r => r.completed === 1);
|
||
const totalShots = analyses.reduce((sum, a) => sum + (a.shotCount || 0), 0);
|
||
const avgScore = analyses.length > 0 ? analyses.reduce((sum, a) => sum + (a.overallScore || 0), 0) / analyses.length : 0;
|
||
|
||
return {
|
||
ntrpRating: userRow.ntrpRating || 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),
|
||
};
|
||
}
|