462 行
18 KiB
TypeScript
462 行
18 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,
|
|
} 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);
|
|
}
|
|
|
|
// ===== 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),
|
|
};
|
|
}
|