140 lines
4.4 KiB
TypeScript
140 lines
4.4 KiB
TypeScript
import { db, eq, ne, schedules, songs, and } from '~/drizzle/db'
|
|
import { restoreCardCodeAfterScheduleRemoval } from '~~/server/services/cardCodeLifecycleService'
|
|
import { restoreReplayRequestsToPending } from '~~/server/utils/scheduleReplayBinding'
|
|
import { getServerDate } from '~~/server/utils/serverTime'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
// 验证管理员权限
|
|
const user = event.context.user
|
|
if (!user || !['SONG_ADMIN', 'ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
|
throw createError({
|
|
statusCode: 403,
|
|
message: '需要歌曲管理员及以上权限'
|
|
})
|
|
}
|
|
|
|
try {
|
|
const body = await readBody(event)
|
|
const { scheduleId } = body
|
|
|
|
if (!scheduleId) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '缺少排期 ID'
|
|
})
|
|
}
|
|
|
|
const scheduleIdNumber = Number(scheduleId)
|
|
|
|
console.log(`准备删除排期 ID=${scheduleIdNumber}`)
|
|
|
|
// 先检查排期是否存在,并获取歌曲ID
|
|
const existingSchedule = await db
|
|
.select({
|
|
id: schedules.id,
|
|
songId: schedules.songId,
|
|
isDraft: schedules.isDraft,
|
|
cardCodeId: songs.cardCodeId,
|
|
songTitle: songs.title,
|
|
songArtist: songs.artist
|
|
})
|
|
.from(schedules)
|
|
.leftJoin(songs, eq(schedules.songId, songs.id))
|
|
.where(eq(schedules.id, scheduleIdNumber))
|
|
.limit(1)
|
|
.then((rows) => rows[0])
|
|
|
|
if (!existingSchedule) {
|
|
console.log(`排期不存在 ID=${scheduleIdNumber}`)
|
|
return {
|
|
success: false,
|
|
message: '排期不存在或已被删除'
|
|
}
|
|
}
|
|
|
|
console.log(`找到排期 ID=${scheduleIdNumber}, 歌曲=${existingSchedule.songTitle || '未知歌曲'}`)
|
|
|
|
// 使用事务包装删除和重播状态更新操作
|
|
const result = await db.transaction(async (tx) => {
|
|
// 删除排期
|
|
const deletedSchedule = await tx
|
|
.delete(schedules)
|
|
.where(eq(schedules.id, scheduleIdNumber))
|
|
.returning()
|
|
|
|
console.log(`成功删除排期 ID=${scheduleIdNumber}`)
|
|
|
|
// 恢复该歌曲的重播申请状态为 PENDING
|
|
// 只有当该歌曲没有其他正式排期(非草稿)时,才恢复重播申请状态
|
|
if (existingSchedule.songId) {
|
|
const otherSchedules = await tx
|
|
.select({ id: schedules.id })
|
|
.from(schedules)
|
|
.where(
|
|
and(
|
|
eq(schedules.songId, existingSchedule.songId),
|
|
eq(schedules.isDraft, false),
|
|
ne(schedules.id, scheduleIdNumber)
|
|
)
|
|
)
|
|
.limit(1)
|
|
|
|
if (otherSchedules.length === 0) {
|
|
if (!existingSchedule.isDraft && existingSchedule.cardCodeId) {
|
|
const restoreResult = await restoreCardCodeAfterScheduleRemoval(tx, {
|
|
songId: existingSchedule.songId,
|
|
cardCodeId: existingSchedule.cardCodeId,
|
|
operatorId: user.id
|
|
})
|
|
if (
|
|
!restoreResult.changed &&
|
|
['CONCURRENT_CHANGE', 'MISSING_CARD_CODE'].includes(
|
|
String(restoreResult.reason || '')
|
|
)
|
|
) {
|
|
throw createError({ statusCode: 409, message: '点歌券返还失败,移除排期已终止' })
|
|
}
|
|
}
|
|
|
|
// 恢复重播申请(每人仅恢复最新一条,避免违反部分唯一索引)
|
|
const restoredCount = await restoreReplayRequestsToPending({
|
|
tx,
|
|
songIds: [existingSchedule.songId],
|
|
at: getServerDate()
|
|
})
|
|
|
|
if (restoredCount > 0) {
|
|
console.log(`恢复了 ${restoredCount} 个重播申请状态为 PENDING`)
|
|
}
|
|
} else {
|
|
console.log(`该歌曲仍有其他正式排期,不恢复重播申请状态`)
|
|
}
|
|
}
|
|
|
|
return deletedSchedule
|
|
})
|
|
|
|
return {
|
|
success: true,
|
|
schedule: result
|
|
}
|
|
} catch (error: any) {
|
|
console.error('移除排期失败:', error)
|
|
|
|
// 处理数据库特定错误
|
|
if (error.message?.includes('not found') || error.message?.includes('does not exist')) {
|
|
return {
|
|
success: false,
|
|
message: '排期不存在或已被删除'
|
|
}
|
|
}
|
|
|
|
// 确保返回一个成功=false的响应,而不是抛出错误
|
|
return {
|
|
success: false,
|
|
message: error.message || '移除排期失败',
|
|
error: error.code || 'UNKNOWN_ERROR'
|
|
}
|
|
}
|
|
})
|