import { Context } from "grammy"; import { CosmoeClient } from "../../client/cosmoe"; export async function handleEventDetails(ctx: Context): Promise { try { if (!ctx.message || !ctx.message.text) { return; } const match = /^\/event_(.+)$/.exec(ctx.message.text); if (match && match[1]) { const cosmoeClient = new CosmoeClient(); const eventId = match[1]; const result = await cosmoeClient.getEventDetail(eventId); if (result.code === 200) { const event = result.data; let message = `*${event.name}*\n`; message += `📅 *日期* ${event.event_date}\n\n`; message += `⏰ *时间段*\n`; if (event.slots && event.slots.length > 0) { // Check if today is the event day or before the event day (Beijing time) const eventDate = new Date(event.event_date); const now = new Date(); // Adjust to Beijing timezone (UTC+8) const beijingOffset = 8 * 60; // 8 hours in minutes const beijingTime = new Date(now.getTime() + (now.getTimezoneOffset() + beijingOffset) * 60000); // Reset time parts for comparison (compare dates only) const todayDate = new Date(beijingTime.getFullYear(), beijingTime.getMonth(), beijingTime.getDate()); const eventDay = new Date(eventDate.getFullYear(), eventDate.getMonth(), eventDate.getDate()); const isEventActive = todayDate <= eventDay; // Sort slots by range for consistent ordering const sortedSlots = [...event.slots].sort((a, b) => a.range.localeCompare(b.range)); sortedSlots.forEach((slot: any, index: number) => { message += `• ${slot.range}: ¥${slot.price} (${slot.remaining}/${slot.capacity})\n`; if (slot.remaining > 0 && isEventActive) { message += ` • 预约 -> /book\\_${eventId}\\_${index}\n`; } }); } else { message += "没有可预约的时间段\n"; } message += `\n🖼️ *封面图* ${event.cover_image_url}`; await ctx.reply(message, { parse_mode: "Markdown" }); } else { await ctx.reply(`获取活动详情时出错: ${result.msg || '未知错误'}`); } } } catch (error) { console.error("Error fetching event details:", error); await ctx.reply("获取活动详情时出错,请稍后再试"); } }