perf: 支持课程补录

This commit is contained in:
richarjiang
2026-09-08 15:08:29 +08:00
parent d1f193e13e
commit b4b3caac70
24 changed files with 1074 additions and 945 deletions

View File

@@ -319,6 +319,9 @@ export class UserService {
},
})
const supplements = await this.prisma.lessonSupplement.aggregate({
where: { userId, revokedAt: null }, _sum: { quantity: true },
})
const now = new Date()
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1)
const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999)
@@ -344,7 +347,7 @@ export class UserService {
}, 0)
return {
totalBookings: completedBookings.length,
totalBookings: completedBookings.length + (supplements._sum.quantity ?? 0),
totalDays,
monthBookings: monthBookings.length,
monthDays,
@@ -441,6 +444,14 @@ export class UserService {
statsMap.set(stat.userId, entry)
}
const supplements = userIds.length
? await this.prisma.lessonSupplement.groupBy({
by: ['userId'], where: { userId: { in: userIds }, revokedAt: null },
_sum: { quantity: true },
})
: []
const supplementMap = new Map(supplements.map(row => [row.userId, row._sum.quantity ?? 0]))
const items = users.map((u) => {
const s = statsMap.get(u.id) ?? { total: 0, completed: 0, cancelled: 0 }
const active = u.memberships[0]
@@ -456,7 +467,7 @@ export class UserService {
? { name: active.cardType.name, type: active.cardType.type as CardTypeCategory }
: null,
totalBookings: s.total,
completedBookings: s.completed,
completedBookings: s.completed + (supplementMap.get(u.id) ?? 0),
cancelledBookings: s.cancelled,
}
})
@@ -515,6 +526,10 @@ export class UserService {
if (row.status === BookingStatus.NO_SHOW) stats.noShow += row._count.id
}
const supplements = await this.prisma.lessonSupplement.aggregate({
where: { userId, revokedAt: null }, _sum: { quantity: true },
})
return {
user: {
userId: user.id,
@@ -528,7 +543,7 @@ export class UserService {
memberships: user.memberships.map((membership) => serializeMembership(membership)),
stats: {
totalBookings: stats.total,
completedBookings: stats.completed,
completedBookings: stats.completed + (supplements._sum.quantity ?? 0),
cancelledBookings: stats.cancelled,
noShowBookings: stats.noShow,
},