// 统计页面模块 const StatsPage = { // 初始化 init() { this.loadStats(); }, // 加载统计数据 async loadStats() { try { const [dishesRes, ordersRes] = await Promise.all([ fetch(`${API_BASE}/dishes/`), fetch(`${API_BASE}/orders/`) ]); const allDishes = await dishesRes.json(); const allOrders = await ordersRes.json(); // 计算菜品出现次数 const dishCompletedCounts = {}; allOrders .filter(o => o.status === 'completed') .forEach(order => { (order.dishes_detail || []).forEach(dish => { dishCompletedCounts[dish.id] = (dishCompletedCounts[dish.id] || 0) + 1; }); }); AppState.setDishCompletedCounts(dishCompletedCounts); // 统计数据 const totalDishes = allDishes.length; const completedOrders = allOrders.filter(o => o.status === 'completed').length; const inProgressOrders = allOrders.filter(o => o.status === 'in_progress').length; // 统计参与人员 const participantCounts = {}; allOrders.forEach(order => { (order.participants || []).forEach(p => { participantCounts[p] = (participantCounts[p] || 0) + 1; }); }); const uniqueParticipants = Object.keys(participantCounts).length; // 渲染统计卡片 this.renderStatCards({ totalDishes, completedOrders, inProgressOrders, uniqueParticipants }); // 渲染菜品排行榜 this.renderDishRank(allDishes, dishCompletedCounts); } catch (e) { console.error('加载统计失败:', e); } }, // 渲染统计卡片 renderStatCards(stats) { const container = document.getElementById('stats-cards'); if (!container) return; container.innerHTML = `
${stats.totalDishes}
菜品总数
${stats.completedOrders}
已完成聚会
${stats.inProgressOrders}
进行中
`; }, // 渲染菜品排行榜 renderDishRank(dishes, counts) { const container = document.getElementById('dish-rank-list'); if (!container) return; // 按出现次数排序 const dishesWithCounts = dishes.map(dish => ({ ...dish, orderCount: counts[dish.id] || 0 })).filter(d => d.orderCount > 0) .sort((a, b) => b.orderCount - a.orderCount); if (dishesWithCounts.length === 0) { container.innerHTML = `

暂无数据

`; return; } container.innerHTML = dishesWithCounts.slice(0, 10).map((dish, index) => { let rankClass = 'default'; if (index === 0) rankClass = 'gold'; else if (index === 1) rankClass = 'silver'; else if (index === 2) rankClass = 'bronze'; return `
${index + 1} ${dish.name}
${dish.name}
出现 ${dish.orderCount} 次
`; }).join(''); } }; // 导出 window.StatsPage = StatsPage;