'use strict'; // 校区地图:学校列表(JSON 种子 + 运行时),附带运营数据关联统计。 // 本期只有只读接口;新建/编辑/启停在下一增量实现。 const { sendJson } = require('../util.js'); const { loadSchools } = require('../../schools-store.js'); const { createOpsStore } = require('../../ops-store.js'); function namesOf(school) { return new Set([school.name, ...(school.aliases || [])].map((n) => String(n).trim()).filter(Boolean)); } function register(app, ctx) { const opsStore = createOpsStore(ctx.config); app.get('/api/schools', async ({ res }) => { const schools = loadSchools(); // 关联运营数据:按学校名(含别名)匹配 ops_shop.school 与 ops_row.school let shops = []; let rows = []; let platformRows = []; let opsAvailable = false; try { const summary = await opsStore.summary(); shops = summary.shops || []; rows = await opsStore.queryRows({}); platformRows = await opsStore.queryPlatformRows({}); opsAvailable = true; } catch { /* ops 库不可用:只影响关联字段 */ } const payload = schools.map((school) => { const names = namesOf(school); // 门店口径:人工表的 shop_key 与平台明细的 shopId 取并集 const shopKeys = new Set(); for (const shop of shops) { if (names.has(String(shop.school || ''))) shopKeys.add(String(shop.key || shop.shopId || '')); } for (const row of platformRows) { if (names.has(String(row.school || ''))) shopKeys.add(String(row.shopId || '')); } shopKeys.delete(''); const shopCount = shopKeys.size; let lastDate = ''; for (const row of rows) { if (!names.has(String(row.school || ''))) continue; if (!lastDate || row.date > lastDate) lastDate = row.date; } for (const row of platformRows) { if (!names.has(String(row.school || ''))) continue; if (!lastDate || row.date > lastDate) lastDate = row.date; } return { ...school, ops: opsAvailable ? { shopCount, lastDate: lastDate || null } : null, }; }); sendJson(res, 200, { schools: payload, opsAvailable, enabledCount: payload.filter((s) => s.enabled && s.lng != null).length, }); }); } module.exports = { register };