ops.js 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. 'use strict';
  2. // 店铺运营数据:上传解析、矩阵查询、导入记录、原件下载、重新解析、CSV 导出。
  3. // 存储由 ops-store 决定(默认本地 SQLite),此处只做协议与编排。
  4. const crypto = require('crypto');
  5. const { sendJson, readBody } = require('../util.js');
  6. const opsImport = require('../../ops-import.js');
  7. const { createOpsStore } = require('../../ops-store.js');
  8. const { loadSchools } = require('../../schools-store.js');
  9. const { makeChatCfg } = require('../context.js');
  10. const { chatCompletion, extractJson } = require('../agent/llm.js');
  11. const MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
  12. const UPLOAD_ID_RE = /^\d{8}-\d{6}-[a-z0-9]{6}$/;
  13. const XLSX_MIME = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  14. const PARSER_VERSION = 1;
  15. const BASE_FIELDS = [
  16. { key: 'order_cnt', label: '订单量', type: 'int', source: 'manual' },
  17. { key: 'revenue', label: '营收', type: 'money', source: 'manual' },
  18. { key: 'exposure', label: '曝光量', type: 'int', source: 'manual' },
  19. { key: 'click_rate', label: '进店转化率', type: 'rate', source: 'manual' },
  20. ];
  21. const BASE_FIELD_KEYS = BASE_FIELDS.map((f) => f.key);
  22. function pad2(n) {
  23. return String(n).padStart(2, '0');
  24. }
  25. function newUploadId() {
  26. const d = new Date();
  27. const stamp = `${d.getFullYear()}${pad2(d.getMonth() + 1)}${pad2(d.getDate())}-${pad2(d.getHours())}${pad2(d.getMinutes())}${pad2(d.getSeconds())}`;
  28. return `${stamp}-${crypto.randomBytes(3).toString('hex')}`;
  29. }
  30. function sanitizeName(name) {
  31. const text = String(name || 'upload.xlsx')
  32. .replace(/[\\/:*?"<>|\u0000-\u001f]/g, '_')
  33. .trim()
  34. .slice(0, 120);
  35. return text || 'upload.xlsx';
  36. }
  37. function splitList(value) {
  38. return String(value || '')
  39. .split(',')
  40. .map((s) => s.trim())
  41. .filter(Boolean);
  42. }
  43. function parseFieldKeys(value) {
  44. return splitList(value);
  45. }
  46. function parseYmd(value) {
  47. const text = String(value || '').replace(/-/g, '');
  48. return /^\d{8}$/.test(text) ? text : '';
  49. }
  50. /** 存储不可用时的统一响应(summary 例外,它要返回 200 + enabled:false) */
  51. async function requireHealthy(store, res) {
  52. const health = await store.health();
  53. if (health.ok) return true;
  54. sendJson(res, 503, {
  55. error: '存储不可用',
  56. reason: health.reason || 'storage-unreachable',
  57. detail: health.detail || '',
  58. });
  59. return false;
  60. }
  61. function normalizeFieldValue(value, type) {
  62. if (value === null || value === undefined || value === '') return null;
  63. if (type === 'text') return String(value);
  64. const n = Number(value);
  65. if (Number.isFinite(n)) return n;
  66. return type === 'int' || type === 'money' || type === 'rate' ? null : String(value);
  67. }
  68. function fieldValue(row, field) {
  69. if (field.source === 'platform') {
  70. const data = row.platform || {};
  71. const raw = Object.prototype.hasOwnProperty.call(data, field.label) ? data[field.label] : null;
  72. return normalizeFieldValue(raw, field.type);
  73. }
  74. return normalizeFieldValue(row[field.key], field.type);
  75. }
  76. /** 按字段名与样本值推断展示类型 */
  77. function inferFieldType(label, samples) {
  78. const name = String(label || '');
  79. if (/率|占比/.test(name)) return 'rate';
  80. if (/额|费|价|收入|支出|补贴|成本|销售额/.test(name)) return 'money';
  81. if (/时长|时间段|时间$|列表|名称|地址|ID$|id$/.test(name)) return 'text';
  82. const present = samples.filter((v) => v !== null && v !== undefined && v !== '');
  83. if (!present.length) return 'text';
  84. const numeric = present.filter((v) => typeof v === 'number' || (typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v))));
  85. return numeric.length === present.length ? 'int' : 'text';
  86. }
  87. /** 可选字段目录:4 个基础字段 + 平台导出里的全部列 */
  88. async function buildFieldCatalog(store) {
  89. const fields = BASE_FIELDS.map((f) => ({ ...f }));
  90. let columns = [];
  91. let sample = [];
  92. try {
  93. const uploads = await store.listUploads();
  94. const seen = new Set();
  95. for (const upload of uploads) {
  96. for (const col of ((upload.report && upload.report.columns) || [])) seen.add(col);
  97. }
  98. sample = (await store.queryPlatformRows({})).slice(0, 300);
  99. if (!seen.size) {
  100. for (const row of sample) for (const col of Object.keys(row.data || {})) seen.add(col);
  101. }
  102. columns = [...seen];
  103. } catch { /* 存储不可用时只有基础字段 */ }
  104. for (const column of columns) {
  105. const key = `p:${column}`;
  106. if (fields.some((f) => f.key === key)) continue;
  107. if (['日期', '门店编号', '门店名称'].includes(column)) continue;
  108. fields.push({
  109. key,
  110. label: column,
  111. type: inferFieldType(column, sample.map((row) => (row.data || {})[column])),
  112. source: 'platform',
  113. });
  114. }
  115. return fields;
  116. }
  117. function buildMatrix(rows, dates, fields) {
  118. const schools = new Map();
  119. for (const row of rows) {
  120. const schoolName = row.school || '未分组';
  121. if (!schools.has(schoolName)) schools.set(schoolName, new Map());
  122. const shopMap = schools.get(schoolName);
  123. if (!shopMap.has(row.shopKey)) {
  124. shopMap.set(row.shopKey, {
  125. key: row.shopKey,
  126. shopId: row.shopId,
  127. shopName: row.shopName,
  128. status: row.status,
  129. series: {},
  130. quality: {},
  131. totals: {},
  132. });
  133. }
  134. const shop = shopMap.get(row.shopKey);
  135. for (const field of fields) {
  136. if (!shop.series[field.key]) shop.series[field.key] = {};
  137. shop.series[field.key][row.date] = fieldValue(row, field);
  138. }
  139. if (Array.isArray(row.quality) && row.quality.length) shop.quality[row.date] = row.quality;
  140. }
  141. const groups = [];
  142. for (const [school, shopMap] of [...schools.entries()].sort((a, b) => a[0].localeCompare(b[0], 'zh-Hans-CN'))) {
  143. const shops = [...shopMap.values()].sort((a, b) => String(a.shopName).localeCompare(String(b.shopName), 'zh-Hans-CN'));
  144. for (const shop of shops) {
  145. for (const field of fields) {
  146. const series = shop.series[field.key] || {};
  147. if (field.type === 'int' || field.type === 'money') {
  148. let sum = 0;
  149. let seen = false;
  150. for (const date of dates) {
  151. const value = series[date];
  152. if (typeof value === 'number') { sum += value; seen = true; }
  153. }
  154. shop.totals[field.key] = seen ? Math.round(sum * 100) / 100 : null;
  155. } else if (field.type === 'rate') {
  156. let weight = 0;
  157. let acc = 0;
  158. const base = shop.series.exposure || {};
  159. for (const date of dates) {
  160. const rate = series[date];
  161. const exposure = base[date];
  162. if (typeof exposure === 'number' && exposure > 0 && typeof rate === 'number') {
  163. weight += exposure;
  164. acc += rate * exposure;
  165. }
  166. }
  167. shop.totals[field.key] = weight > 0 ? acc / weight : null;
  168. } else {
  169. shop.totals[field.key] = null;
  170. }
  171. }
  172. }
  173. groups.push({ school, shops });
  174. }
  175. return groups;
  176. }
  177. function numOrNull(value) {
  178. if (value === null || value === undefined || value === '') return null;
  179. const n = Number(value);
  180. return Number.isFinite(n) ? n : null;
  181. }
  182. /** 平台全字段行 → 矩阵行:4 个基础指标另存,原始 124 字段留在 platform */
  183. function platformRowToMatrix(row) {
  184. const d = row.data || {};
  185. return {
  186. date: row.date,
  187. shopKey: String(row.shopId || ''),
  188. shopId: String(row.shopId || ''),
  189. shopName: row.shopName || '',
  190. school: row.school || '',
  191. status: '',
  192. order_cnt: numOrNull(d['有效订单']),
  193. revenue: numOrNull(d['营业额']),
  194. exposure: numOrNull(d['曝光人数']),
  195. click_rate: numOrNull(d['进店转化率']),
  196. platform: d,
  197. quality: [],
  198. };
  199. }
  200. /** 人工表 + 平台明细合并:同一天同一店,人工的 4 个指标覆盖平台,其余平台字段保留 */
  201. async function loadMergedRows(store, { from, to, schools, shops } = {}) {
  202. let platform = [];
  203. try {
  204. platform = await store.queryPlatformRows({ from, to });
  205. } catch { /* 平台表不可用不影响人工数据 */ }
  206. const manual = await store.queryRows({ from, to, schools, shops });
  207. const map = new Map();
  208. for (const row of platform) map.set(`${row.date}|${row.shopId}`, platformRowToMatrix(row));
  209. for (const row of manual) {
  210. const key = `${row.date}|${row.shopKey}`;
  211. const base = map.get(key);
  212. map.set(key, { ...(base || {}), ...row, platform: base ? base.platform : null });
  213. }
  214. let rows = [...map.values()];
  215. if (schools && schools.length) rows = rows.filter((r) => schools.includes(String(r.school || '')));
  216. if (shops && shops.length) rows = rows.filter((r) => shops.includes(String(r.shopId || r.shopKey || '')));
  217. return rows;
  218. }
  219. function addDays(ymd, n) {
  220. const d = new Date(Date.UTC(Number(ymd.slice(0, 4)), Number(ymd.slice(4, 6)) - 1, Number(ymd.slice(6, 8))));
  221. d.setUTCDate(d.getUTCDate() + n);
  222. return `${d.getUTCFullYear()}${pad2(d.getUTCMonth() + 1)}${pad2(d.getUTCDate())}`;
  223. }
  224. function dayCount(from, to) {
  225. const a = Date.UTC(Number(from.slice(0, 4)), Number(from.slice(4, 6)) - 1, Number(from.slice(6, 8)));
  226. const b = Date.UTC(Number(to.slice(0, 4)), Number(to.slice(4, 6)) - 1, Number(to.slice(6, 8)));
  227. return Math.max(1, Math.round((b - a) / 86400000) + 1);
  228. }
  229. function pctDelta(cur, prev) {
  230. if (prev == null || prev === 0 || cur == null) return null;
  231. return Math.round(((cur - prev) / prev) * 1000) / 10;
  232. }
  233. function sumSeries(rows, key) {
  234. let sum = 0;
  235. for (const row of rows) if (typeof row[key] === 'number') sum += row[key];
  236. return sum;
  237. }
  238. function platformNum(row, column) {
  239. if (!row.platform) return null;
  240. return numOrNull(row.platform[column]);
  241. }
  242. /** 零单门店诊断:用平台字段区分「未开业 / 闭店 / 无曝光 / 无进店 / 无转化」 */
  243. function diagnoseZeroOrder(row) {
  244. const p = row.platform;
  245. const orders = Number(row.order_cnt);
  246. if (!(orders === 0)) return null;
  247. if (!p) {
  248. return { reason: 'unknown', reasonText: '缺少平台明细,无法判断' };
  249. }
  250. const bizHours = String(p['营业时长'] == null ? '' : p['营业时长']).trim();
  251. const zeroBiz = /^0\s*小时\s*0\s*分$/.test(bizHours) || bizHours === '0' || bizHours === '';
  252. const abnormal = numOrNull(p['异常关店时长']) || 0;
  253. const exposure = numOrNull(p['曝光人数']);
  254. const visit = numOrNull(p['进店人数']);
  255. const validShop = String(p['是否有效门店'] || '').trim();
  256. if (zeroBiz) {
  257. if (validShop === '否') {
  258. return { reason: 'not-open', reasonText: '未开业(非有效门店,当日无营业时长)', bizHours, abnormal, exposure, visit, validShop };
  259. }
  260. if (abnormal > 0) {
  261. return { reason: 'closed', reasonText: `当日闭店(异常关店 ${abnormal} 分钟,无营业时长)`, bizHours, abnormal, exposure, visit, validShop };
  262. }
  263. return { reason: 'not-open', reasonText: '未营业(当日无营业时长,也未记录异常关店)', bizHours, abnormal, exposure, visit, validShop };
  264. }
  265. if (exposure === 0) {
  266. return { reason: 'no-traffic', reasonText: '营业中但无曝光(未进流量池)', bizHours, abnormal, exposure, visit, validShop };
  267. }
  268. if (visit === 0) {
  269. return { reason: 'no-visit', reasonText: `有曝光 ${exposure} 但无进店`, bizHours, abnormal, exposure, visit, validShop };
  270. }
  271. return { reason: 'no-conversion', reasonText: `有进店 ${visit} 但零下单(转化问题)`, bizHours, abnormal, exposure, visit, validShop };
  272. }
  273. /** 一次聚合:KPI 口径集中在这里,前端不再二次计算 */
  274. function aggregateRows(rows) {
  275. let revenue = 0;
  276. let orders = 0;
  277. let exposure = 0;
  278. let visit = 0;
  279. let buyers = 0;
  280. let expense = 0;
  281. let subsidy = 0;
  282. let ratingSum = 0;
  283. let ratingCount = 0;
  284. let rateWeight = 0;
  285. let rateAcc = 0;
  286. for (const row of rows) {
  287. if (typeof row.revenue === 'number') revenue += row.revenue;
  288. if (typeof row.order_cnt === 'number') orders += row.order_cnt;
  289. if (typeof row.exposure === 'number') exposure += row.exposure;
  290. if (typeof row.click_rate === 'number' && row.exposure > 0) {
  291. rateWeight += row.exposure;
  292. rateAcc += row.click_rate * row.exposure;
  293. }
  294. const v = platformNum(row, '进店人数');
  295. if (v != null) visit += v;
  296. const b = platformNum(row, '下单人数');
  297. if (b != null) buyers += b;
  298. const e = platformNum(row, '支出');
  299. if (e != null) expense += e;
  300. const sub = platformNum(row, '活动总补贴');
  301. if (sub != null) subsidy += sub;
  302. const rating = platformNum(row, '店铺评分');
  303. if (rating != null && rating > 0) {
  304. ratingSum += rating;
  305. ratingCount += 1;
  306. }
  307. }
  308. const conversion = visit > 0 && exposure > 0
  309. ? visit / exposure
  310. : (rateWeight > 0 ? rateAcc / rateWeight : null);
  311. return {
  312. revenue: Math.round(revenue * 100) / 100,
  313. orders,
  314. exposure,
  315. visit,
  316. buyers,
  317. expense: Math.round(expense * 100) / 100,
  318. subsidy: Math.round(subsidy * 100) / 100,
  319. rating: ratingCount ? Math.round((ratingSum / ratingCount) * 100) / 100 : null,
  320. avgTicket: orders > 0 ? Math.round((revenue / orders) * 100) / 100 : null,
  321. conversion: conversion == null ? null : Math.round(conversion * 10000) / 10000,
  322. };
  323. }
  324. async function buildDashboard(store, { from, to, schools, shops }) {
  325. const filters = { schools, shops };
  326. let rows = await loadMergedRows(store, { from, to, ...filters });
  327. const dates = [...new Set(rows.map((r) => r.date))].sort();
  328. const start = from || dates[0];
  329. const end = to || dates[dates.length - 1];
  330. if (!start || !end) {
  331. return { from: null, to: null, prev: null, kpis: null, trend: [], ranking: [], funnel: null, structure: [] };
  332. }
  333. // 未指定区间时按实际数据范围收敛
  334. if (!from || !to) rows = await loadMergedRows(store, { from: start, to: end, ...filters });
  335. const span = dayCount(start, end);
  336. const prevTo = addDays(start, -1);
  337. const prevFrom = addDays(prevTo, -(span - 1));
  338. let prevRows = [];
  339. try {
  340. prevRows = await loadMergedRows(store, { from: prevFrom, to: prevTo, ...filters });
  341. } catch { /* 上期缺失不影响本期 */ }
  342. const cur = aggregateRows(rows);
  343. const prev = prevRows.length ? aggregateRows(prevRows) : null;
  344. const kpi = (value, prevValue, unit, isRate) => ({
  345. value,
  346. prev: prevValue,
  347. delta: prevValue == null || value == null
  348. ? null
  349. : (isRate ? Math.round((value - prevValue) * 1000) / 10 : pctDelta(value, prevValue)),
  350. unit,
  351. });
  352. const kpis = {
  353. revenue: kpi(cur.revenue, prev && prev.revenue, 'money'),
  354. orders: kpi(cur.orders, prev && prev.orders, 'int'),
  355. avgTicket: kpi(cur.avgTicket, prev && prev.avgTicket, 'money'),
  356. exposure: kpi(cur.exposure, prev && prev.exposure, 'int'),
  357. conversion: kpi(cur.conversion, prev && prev.conversion, 'pp', true),
  358. rating: { value: cur.rating, prev: null, delta: null, unit: 'num' },
  359. };
  360. const byDate = new Map();
  361. const byShop = new Map();
  362. for (const row of rows) {
  363. if (!byDate.has(row.date)) byDate.set(row.date, []);
  364. byDate.get(row.date).push(row);
  365. const key = String(row.shopId || row.shopKey || '');
  366. if (!key) continue;
  367. if (!byShop.has(key)) byShop.set(key, { shopId: key, shopName: row.shopName || key, school: row.school || '', rows: [] });
  368. byShop.get(key).rows.push(row);
  369. }
  370. const trend = [...byDate.entries()].sort((a, b) => (a[0] < b[0] ? -1 : 1)).map(([date, list]) => {
  371. const agg = aggregateRows(list);
  372. return { date, revenue: agg.revenue, orders: agg.orders };
  373. });
  374. const allShops = [...byShop.values()]
  375. .map((shop) => {
  376. const agg = aggregateRows(shop.rows);
  377. return { shopId: shop.shopId, shopName: shop.shopName, school: shop.school, revenue: agg.revenue, orders: agg.orders };
  378. })
  379. .sort((a, b) => b.revenue - a.revenue);
  380. const ranking = allShops.slice(0, 10);
  381. const bottom = allShops.slice(-5).reverse();
  382. const funnel = {
  383. exposure: cur.exposure,
  384. visit: cur.visit,
  385. order: cur.buyers || cur.orders,
  386. };
  387. const zeroOrder = [];
  388. for (const row of rows) {
  389. const diagnosis = diagnoseZeroOrder(row);
  390. if (!diagnosis) continue;
  391. zeroOrder.push({
  392. date: row.date,
  393. shopId: row.shopId,
  394. shopName: row.shopName,
  395. school: row.school || '',
  396. orders: 0,
  397. ...diagnosis,
  398. });
  399. }
  400. const zeroOrderSummary = zeroOrder.reduce((acc, item) => {
  401. acc[item.reason] = (acc[item.reason] || 0) + 1;
  402. return acc;
  403. }, {});
  404. const structure = [...byDate.entries()].sort((a, b) => (a[0] < b[0] ? -1 : 1)).map(([date, list]) => {
  405. const agg = aggregateRows(list);
  406. return { date, income: agg.revenue, expense: agg.expense, subsidy: agg.subsidy };
  407. });
  408. return {
  409. from: start,
  410. to: end,
  411. prev: prevRows.length ? { from: prevFrom, to: prevTo } : null,
  412. kpis,
  413. trend,
  414. ranking,
  415. bottom,
  416. zeroOrder,
  417. zeroOrderSummary,
  418. shopCount: allShops.length,
  419. funnel,
  420. structure,
  421. };
  422. }
  423. const INSIGHT_SYSTEM_PROMPT = [
  424. '你是连锁餐饮公司的经营分析师。用户会给一段 JSON,包含某段时间的经营汇总(kpis)、按日趋势(trend)、门店排行(ranking)与垫底(bottom)、转化漏斗(funnel)、收入支出结构(structure)。',
  425. '请用中文输出 3~5 条要点,每行一条,以「- 」开头。要求:',
  426. '1. 先给结论,再给依据;',
  427. '2. 必须引用 JSON 里的具体数字(金额保留两位小数、人数/单量用整数、比率用百分比);',
  428. '3. 指出风险(下滑、零单门店、成本占比高、漏斗转化偏低等),并给一句可执行建议;',
  429. '4. 数据不足(如 prev 为 null、只有 1 天)就直接说明,不要编造;',
  430. '5. 若 zeroOrder 非空,必须用一条要点按原因归类汇总(未开业/闭店/无曝光/无进店/无转化各多少店次),并点明哪一类需要运营介入;不要笼统说「零单门店」;',
  431. '6. 每条不超过 60 字,不要输出 JSON、标题或前后缀。',
  432. ].join('\n');
  433. function insightContext(data) {
  434. const k = (key) => (data.kpis[key] ? { value: data.kpis[key].value, delta: data.kpis[key].delta } : null);
  435. const rate = (a, b) => (b > 0 ? Math.round((a / b) * 1000) / 10 : null);
  436. const funnel = data.funnel || {};
  437. return {
  438. range: { from: data.from, to: data.to, days: data.trend.length, prev: data.prev },
  439. kpis: {
  440. revenue: k('revenue'),
  441. orders: k('orders'),
  442. avgTicket: k('avgTicket'),
  443. exposure: k('exposure'),
  444. conversion: k('conversion'),
  445. rating: k('rating'),
  446. },
  447. funnel: {
  448. exposure: funnel.exposure,
  449. visit: funnel.visit,
  450. order: funnel.order,
  451. visitRate: rate(funnel.visit, funnel.exposure),
  452. orderRate: rate(funnel.order, funnel.visit),
  453. },
  454. trend: data.trend.slice(-14),
  455. shopCount: data.shopCount,
  456. topShops: (data.ranking || []).slice(0, 5).map((s) => ({ name: s.shopName, revenue: s.revenue, orders: s.orders })),
  457. bottomShops: (data.bottom || []).map((s) => ({ name: s.shopName, revenue: s.revenue, orders: s.orders })),
  458. zeroOrderSummary: data.zeroOrderSummary || {},
  459. zeroOrder: (data.zeroOrder || []).slice(0, 20).map((z) => ({
  460. date: z.date,
  461. shop: z.shopName,
  462. reason: z.reason,
  463. reasonText: z.reasonText,
  464. })),
  465. structure: (data.structure || []).map((s) => ({
  466. date: s.date,
  467. income: s.income,
  468. expense: s.expense,
  469. subsidy: s.subsidy,
  470. expenseRatio: s.income > 0 ? Math.round((s.expense / s.income) * 1000) / 10 : null,
  471. })),
  472. };
  473. }
  474. function parseInsightPoints(text) {
  475. return String(text || '')
  476. .split(/\r?\n/)
  477. .map((line) => line.replace(/^\s*(?:[-*•]|\d+[.、)])\s*/, '').trim())
  478. .filter(Boolean)
  479. .slice(0, 6);
  480. }
  481. /** 由聚合结果生成 Report JSON(图文 / 交互 / PDF 三版共用;数值全部来自后端计算) */
  482. function buildReport(data, meta = {}) {
  483. const { kpis, trend, ranking, bottom, zeroOrder, funnel, structure, from, to, prev, shopCount } = data;
  484. const money = (v) => (v == null ? null : Math.round(v * 100) / 100);
  485. const pct = (v) => (v == null ? null : Math.round(v * 1000) / 10);
  486. const findings = [];
  487. const rev = kpis.revenue || {};
  488. if (rev.delta != null) {
  489. findings.push({
  490. severity: rev.delta < -5 ? 'warning' : rev.delta > 5 ? 'positive' : 'neutral',
  491. finding: `营业额 ${money(rev.value)} 元,较上期 ${rev.delta > 0 ? '增长' : '下降'} ${Math.abs(rev.delta)}%`,
  492. evidence: [`营业额:${money(rev.value)}`, `上期营业额:${money(rev.prev)}`],
  493. interpretation: rev.delta < 0 ? '规模收缩,需要结合订单量与流量拆解原因' : '规模增长,关注是否由活动或流量投放带来',
  494. confidence: 'high',
  495. });
  496. }
  497. const conv = kpis.conversion || {};
  498. if (conv.value != null) {
  499. findings.push({
  500. severity: conv.delta != null && conv.delta < -0.5 ? 'warning' : 'neutral',
  501. finding: `进店转化率 ${pct(conv.value)}%${conv.delta != null ? `,较上期 ${conv.delta > 0 ? '+' : ''}${conv.delta}pp` : ''}`,
  502. evidence: [`进店转化率:${pct(conv.value)}%`],
  503. interpretation: '转化率反映进店后的下单意愿,需结合定价、售罄与出餐时长判断',
  504. confidence: 'medium',
  505. });
  506. }
  507. const zeroByReason = (zeroOrder || []).reduce((acc, item) => {
  508. acc[item.reason] = acc[item.reason] || [];
  509. acc[item.reason].push(item);
  510. return acc;
  511. }, {});
  512. const zeroTotal = (zeroOrder || []).length;
  513. if (zeroTotal) {
  514. const parts = Object.entries(zeroByReason).map(([reason, list]) => `${REASON_LABEL[reason] || reason} ${list.length} 店次`);
  515. findings.push({
  516. severity: (zeroByReason['no-conversion'] || []).length ? 'warning' : 'neutral',
  517. finding: `零单合计 ${zeroTotal} 店次:${parts.join('、')}`,
  518. evidence: [`零单店次:${zeroTotal}`],
  519. interpretation: '未开业/闭店属于营业状态问题;有进店无下单才是转化问题,需要优先处理',
  520. confidence: 'high',
  521. });
  522. }
  523. if (ranking && ranking.length && rev.value > 0) {
  524. const share = Math.round((ranking[0].revenue / rev.value) * 1000) / 10;
  525. findings.push({
  526. severity: share > 40 ? 'warning' : 'neutral',
  527. finding: `头部门店「${ranking[0].shopName}」贡献营业额 ${money(ranking[0].revenue)} 元,占比 ${share}%`,
  528. evidence: [`头部门店:${ranking[0].shopName}(${money(ranking[0].revenue)} 元)`],
  529. interpretation: share > 40 ? '集中度偏高,单店波动会显著影响大盘' : '门店贡献相对分散',
  530. confidence: 'high',
  531. });
  532. }
  533. if (!prev) {
  534. findings.push({
  535. severity: 'neutral',
  536. finding: '所选区间之前没有可比数据,本期不计算环比趋势',
  537. evidence: ['上期数据:无'],
  538. interpretation: '只能做横向门店对比与当日诊断,不要据此判断长期趋势',
  539. confidence: 'high',
  540. });
  541. }
  542. const alerts = [];
  543. for (const [reason, list] of Object.entries(zeroByReason)) {
  544. alerts.push({
  545. severity: reason === 'no-conversion' ? 'P1' : 'P2',
  546. store_id: list[0].shopId || null,
  547. title: `${REASON_LABEL[reason] || reason}:${list.length} 店次`,
  548. evidence: list.slice(0, 5).map((z) => `${formatYmd(z.date)} ${z.shopName}:${z.reasonText}`),
  549. hypotheses: reason === 'no-conversion'
  550. ? ['菜品/定价与周边相比缺乏竞争力', '出餐或配送时长导致流失', '活动力度不足或库存售罄']
  551. : ['门店未上线或当日排班缺失', '平台侧闭店/审核状态异常'],
  552. next_checks: reason === 'no-conversion'
  553. ? ['核对当日菜品售罄记录', '查看差评与退款原因', '对比同商圈同类门店客单价']
  554. : ['确认门店上线状态与营业时间设置', '核对平台闭店记录'],
  555. });
  556. }
  557. if (conv.delta != null && conv.delta < -0.5) {
  558. alerts.push({
  559. severity: 'P1', store_id: null, title: '进店转化率下滑',
  560. evidence: [`本期进店转化率:${pct(conv.value)}%`, `上期进店转化率:${pct(conv.prev)}%`],
  561. hypotheses: ['菜单改价或活动结束', '高峰期出餐变慢', '竞品补贴'],
  562. next_checks: ['对比改价/活动时间', '查看出餐时长与超时单'],
  563. });
  564. }
  565. const actionPlan = [];
  566. if ((zeroByReason['no-conversion'] || []).length) {
  567. actionPlan.push({
  568. priority: 'P0',
  569. action: '对「有进店无下单」门店做单店诊断:菜品可售、价格带、出餐与配送时长',
  570. owner_role: '运营负责人',
  571. time_horizon: '24 小时内',
  572. success_metric: '次日进店→下单转化率回升',
  573. related_alerts: ['有进店无下单'],
  574. });
  575. }
  576. if ((zeroByReason['not-open'] || []).length || (zeroByReason['closed'] || []).length) {
  577. actionPlan.push({
  578. priority: 'P1',
  579. action: '核对未开业/闭店门店的上线状态、营业时间与异常关店记录',
  580. owner_role: '门店督导',
  581. time_horizon: '48 小时内',
  582. success_metric: '营业时长恢复、零单店次下降',
  583. related_alerts: ['未开业', '当日闭店'],
  584. });
  585. }
  586. if (conv.delta != null && conv.delta < -0.5) {
  587. actionPlan.push({
  588. priority: 'P1',
  589. action: '排查转化率下滑:活动是否结束、是否改价、是否有售罄',
  590. owner_role: '运营负责人',
  591. time_horizon: '72 小时内',
  592. success_metric: '转化率回到上期水平',
  593. related_alerts: ['进店转化率下滑'],
  594. });
  595. }
  596. if (prev) {
  597. actionPlan.push({
  598. priority: 'P2',
  599. action: '补全成本与结算口径,再评估毛利与真实利润',
  600. owner_role: '财务',
  601. time_horizon: '2 周内',
  602. success_metric: '可计算毛利与费用率',
  603. related_alerts: [],
  604. });
  605. }
  606. const kpiList = [
  607. ['revenue', '营业额', 'CNY', '平台「营业额」列求和'],
  608. ['orders', '有效订单', '单', '平台「有效订单」列求和'],
  609. ['avgTicket', '客单价', 'CNY', '营业额 ÷ 有效订单'],
  610. ['exposure', '曝光人数', '人', '平台「曝光人数」列求和'],
  611. ['conversion', '进店转化率', '%', 'Σ进店人数 ÷ Σ曝光人数'],
  612. ['rating', '店铺评分', '分', '有效评分门店均值(排除 0 分)'],
  613. ].map(([key, label, unit, definition]) => {
  614. const item = kpis[key] || {};
  615. const fmt = (value) => {
  616. if (value == null) return null;
  617. if (unit === '%') return pct(value);
  618. if (unit === '单' || unit === '人') return Math.round(value);
  619. return money(value);
  620. };
  621. return {
  622. key, label, unit, definition,
  623. current: fmt(item.value),
  624. previous: fmt(item.prev),
  625. difference: item.value != null && item.prev != null && unit !== '%'
  626. ? Math.round((item.value - item.prev) * 100) / 100
  627. : (item.value != null && item.prev != null ? Math.round((item.value - item.prev) * 1000) / 10 : null),
  628. change_pct: item.delta,
  629. status: item.delta == null ? 'unknown' : item.delta > 0 ? 'up' : item.delta < 0 ? 'down' : 'flat',
  630. };
  631. });
  632. return {
  633. meta: {
  634. title: meta.title || '经营分析报告',
  635. organization: meta.organization || '',
  636. period_start: formatYmd(from),
  637. period_end: formatYmd(to),
  638. generated_at: new Date().toISOString(),
  639. currency: 'CNY',
  640. data_scope: `${shopCount} 家门店 · ${trend.length} 天 · ${zeroTotal} 个零单店次`,
  641. source_files: meta.sourceFiles || [],
  642. },
  643. executive_summary: {
  644. overall_assessment: rev.delta == null
  645. ? '本期有经营数据,但缺少可比的上期,暂不做趋势判断;以下结论基于本期横向对比与门店诊断。'
  646. : `本期营业额 ${money(rev.value)} 元,较上期${rev.delta >= 0 ? '增长' : '下降'} ${Math.abs(rev.delta)}%,订单 ${kpis.orders.value} 单。`,
  647. key_findings: findings,
  648. },
  649. kpis: kpiList,
  650. sections: [
  651. { id: 'overview', title: '经营概览', summary: rev.delta == null ? '本期无上期对比' : `营业额环比 ${rev.delta}%`, paragraphs: [], chart_ids: ['chart_trend'], table_ids: [], evidence_keys: ['revenue', 'orders'] },
  652. { id: 'stores', title: '门店表现', summary: `共 ${shopCount} 家门店,Top1 为「${(ranking[0] || {}).shopName || '—'}」`, paragraphs: [], chart_ids: ['chart_rank'], table_ids: ['table_stores'], evidence_keys: ['revenue'] },
  653. { id: 'funnel', title: '流量与转化', summary: `曝光 ${funnel.exposure} → 进店 ${funnel.visit} → 下单 ${funnel.order}`, paragraphs: [], chart_ids: ['chart_funnel'], table_ids: [], evidence_keys: ['conversion'] },
  654. { id: 'finance', title: '收入与费用', summary: '仅展示平台收入、支出与补贴,不等同于利润', paragraphs: [], chart_ids: ['chart_structure'], table_ids: [], evidence_keys: ['revenue'] },
  655. { id: 'quality', title: '数据质量', summary: '', paragraphs: [], chart_ids: [], table_ids: [], evidence_keys: [] },
  656. ],
  657. stores: (ranking || []).slice(0, 10).map((shop, index) => ({
  658. store_id: shop.shopId,
  659. name: shop.shopName,
  660. rank: index + 1,
  661. facts: [`营业额 ${money(shop.revenue)} 元,订单 ${shop.orders} 单`],
  662. diagnosis: index === 0 ? '本期贡献最高,需保持供给与活动稳定' : '贡献靠后,结合流量与转化排查',
  663. risks: [],
  664. recommendations: [],
  665. })),
  666. charts: [
  667. { id: 'chart_trend', type: 'line_bar', title: '营业额与订单趋势', description: '柱为营业额,线为订单量;天数较少时不做平滑趋势判断。', source: '平台明细按日聚合', unit: 'CNY', data: trend },
  668. { id: 'chart_rank', type: 'horizontal_bar', title: '门店营业额 Top 10', description: '按所选区间营业额排序。', source: '平台明细按门店聚合', unit: 'CNY', data: ranking },
  669. { id: 'chart_funnel', type: 'funnel', title: '曝光 → 进店 → 下单', description: '三层人数不可跨口径混算。', source: '平台「曝光人数/进店人数/下单人数」', unit: '人', data: funnel },
  670. { id: 'chart_structure', type: 'bar', title: '收入与支出结构', description: '收入为营业额;支出与补贴按平台口径。', source: '平台「营业额/支出/活动总补贴」', unit: 'CNY', data: structure },
  671. ],
  672. tables: [{
  673. id: 'table_stores',
  674. title: '门店明细(Top 10)',
  675. columns: [
  676. { key: 'name', label: '门店' }, { key: 'revenue', label: '营业额' },
  677. { key: 'orders', label: '订单' }, { key: 'share', label: '占比' },
  678. ],
  679. rows: (ranking || []).map((shop) => ({
  680. name: shop.shopName, revenue: money(shop.revenue), orders: shop.orders,
  681. share: rev.value > 0 ? Math.round((shop.revenue / rev.value) * 1000) / 10 : null,
  682. })),
  683. footnote: '占比 = 门店营业额 ÷ 本期总营业额',
  684. }],
  685. alerts,
  686. action_plan: actionPlan,
  687. data_quality: {
  688. coverage: `${formatYmd(from)} ~ ${formatYmd(to)},共 ${trend.length} 天`,
  689. warnings: prev ? [] : ['所选区间前无数据,环比不可用'],
  690. comparability: '同店对比要求门店集合一致;本期未做同店筛选',
  691. missing_metrics: ['成本、平台佣金明细缺失,无法计算毛利与净利'],
  692. },
  693. methodology: [
  694. '金额与单量均由后端按「日期+门店」明细求和,不由模型生成',
  695. '进店转化率 = Σ进店人数 ÷ Σ曝光人数(同层级口径)',
  696. '客单价 = 营业额 ÷ 有效订单,订单为 0 时为 null',
  697. '上期为紧邻的等长区间,比率变化用百分点(pp)',
  698. ],
  699. };
  700. }
  701. const REPORT_AI_PROMPT = [
  702. '你是连锁餐饮公司的经营分析师。用户会给一段已经计算好的经营数据 JSON(含 kpis/trend/ranking/bottom/funnel/structure/zeroOrder)。',
  703. '数值全部由后端计算,你不得修改、杜撰或重算;只做解释、诊断与建议。',
  704. '只输出一个 JSON 对象,不要代码块、不要前后缀,结构如下:',
  705. '{"overall_assessment":"一段客观判断",',
  706. ' "key_findings":[{"severity":"positive|neutral|warning|critical","finding":"结论","evidence":["指标:数值"],"interpretation":"解释","confidence":"high|medium|low"}],',
  707. ' "sections":[{"id":"overview|stores|funnel|finance","summary":"本节结论","paragraphs":["事实与解释"]}],',
  708. ' "alerts":[{"severity":"P0|P1|P2","title":"告警标题","evidence":["数据证据"],"hypotheses":["待验证原因"],"next_checks":["核查步骤"]}],',
  709. ' "action_plan":[{"priority":"P0|P1|P2","action":"具体行动","owner_role":"负责人角色","time_horizon":"时间窗","success_metric":"可衡量指标"}]}',
  710. '要求:3~5 条 findings、2~4 条 alerts、1~4 条 action;金额保留两位小数,比率用百分比;',
  711. 'zeroOrder 非空时必须按原因归类(未开业/闭店/无曝光/无进店/无转化各多少店次)并点明哪类需要运营介入;',
  712. '没有上期数据时明确说明不可做趋势判断;不输出「赋能」「抓手」等套话。',
  713. '术语必须与指标名一致:revenue 是「营业额」(不是收入/结算收入/利润),avgTicket 是「客单价」,conversion 是「进店转化率」;日期一律写成 YYYY-MM-DD。',
  714. 'evidence 必须写成「中文指标名:数值」(如「营业额:14926.92」「零单店次:11」),禁止出现 zeroOrder、revenue、top_shop 等内部字段名。',
  715. ].join('\n');
  716. const EVIDENCE_CN = {
  717. revenue: '营业额', orders: '有效订单', avgTicket: '客单价', exposure: '曝光人数',
  718. conversion: '进店转化率', rating: '店铺评分', zeroOrder: '零单店次',
  719. previous: '上期值', prev: '上期值', top_shop: '头部门店', shop_id: '门店ID',
  720. visit: '进店人数', buyers: '下单人数', expense: '支出', subsidy: '补贴',
  721. };
  722. /** 把模型偶发的内部字段名(zeroOrder/revenue 等)翻成中文,普通用户才看得懂 */
  723. function humanizeText(value) {
  724. return String(value == null ? '' : value)
  725. .replace(/\b(zeroOrder|revenue|orders|avgTicket|exposure|conversion|rating|previous|prev|top_shop|shop_id|visit|buyers|expense|subsidy)\b(:?)/g,
  726. (match, key, colon) => EVIDENCE_CN[key] + (colon || ''));
  727. }
  728. function humanizeEvidence(list) {
  729. return (list || []).map(humanizeText);
  730. }
  731. /** 把模型返回的叙述字段覆盖到规则版报告上;结构不合法就返回 null(保留规则版) */
  732. function overlayAiReport(base, ai, meta = {}) {
  733. if (!ai || typeof ai !== 'object') return null;
  734. const report = JSON.parse(JSON.stringify(base));
  735. let touched = false;
  736. if (typeof ai.overall_assessment === 'string' && ai.overall_assessment.trim()) {
  737. report.executive_summary.overall_assessment = ai.overall_assessment.trim();
  738. touched = true;
  739. }
  740. if (Array.isArray(ai.key_findings) && ai.key_findings.length) {
  741. const findings = ai.key_findings
  742. .filter((f) => f && f.finding)
  743. .slice(0, 6)
  744. .map((f) => ({
  745. severity: ['positive', 'neutral', 'warning', 'critical'].includes(f.severity) ? f.severity : 'neutral',
  746. finding: humanizeText(f.finding).slice(0, 120),
  747. evidence: humanizeEvidence(f.evidence).slice(0, 5),
  748. interpretation: f.interpretation ? humanizeText(f.interpretation).slice(0, 200) : '',
  749. confidence: ['high', 'medium', 'low'].includes(f.confidence) ? f.confidence : 'medium',
  750. }));
  751. if (findings.length) {
  752. report.executive_summary.key_findings = findings;
  753. touched = true;
  754. }
  755. }
  756. if (Array.isArray(ai.sections)) {
  757. const byId = new Map(report.sections.map((sec) => [sec.id, sec]));
  758. for (const sec of ai.sections) {
  759. const target = sec && byId.get(sec.id);
  760. if (!target) continue;
  761. if (sec.summary) target.summary = String(sec.summary).slice(0, 200);
  762. if (Array.isArray(sec.paragraphs) && sec.paragraphs.length) {
  763. target.paragraphs = sec.paragraphs.slice(0, 4).map((t) => String(t).slice(0, 400));
  764. }
  765. touched = true;
  766. }
  767. }
  768. if (Array.isArray(ai.alerts) && ai.alerts.length) {
  769. report.alerts = ai.alerts.slice(0, 5).map((a) => ({
  770. severity: ['P0', 'P1', 'P2'].includes(a.severity) ? a.severity : 'P2',
  771. store_id: a.store_id || null,
  772. title: humanizeText(a.title || '').slice(0, 80),
  773. evidence: humanizeEvidence(a.evidence).slice(0, 6),
  774. hypotheses: Array.isArray(a.hypotheses) ? a.hypotheses.slice(0, 4).map(String) : [],
  775. next_checks: Array.isArray(a.next_checks) ? a.next_checks.slice(0, 4).map(String) : [],
  776. }));
  777. touched = true;
  778. }
  779. if (Array.isArray(ai.action_plan) && ai.action_plan.length) {
  780. report.action_plan = ai.action_plan.slice(0, 4).map((p) => ({
  781. priority: ['P0', 'P1', 'P2'].includes(p.priority) ? p.priority : 'P2',
  782. action: String(p.action || '').slice(0, 160),
  783. owner_role: String(p.owner_role || '').slice(0, 40),
  784. time_horizon: String(p.time_horizon || '').slice(0, 40),
  785. success_metric: String(p.success_metric || '').slice(0, 80),
  786. related_alerts: Array.isArray(p.related_alerts) ? p.related_alerts : [],
  787. }));
  788. touched = true;
  789. }
  790. if (!touched) return null;
  791. report.ai = { source: 'llm', model: meta.model || '', generatedAt: new Date().toISOString() };
  792. return report;
  793. }
  794. const REASON_LABEL = { 'not-open': '未开业', closed: '当日闭店', 'no-traffic': '无曝光', 'no-visit': '无进店', 'no-conversion': '有进店无下单', unknown: '数据不足' };
  795. function formatYmd(ymd) {
  796. const s = String(ymd || '');
  797. return /^\d{8}$/.test(s) ? `${s.slice(0, 4)}-${s.slice(4, 6)}-${s.slice(6, 8)}` : '';
  798. }
  799. function csvCell(value) {
  800. const text = value == null ? '' : String(value);
  801. return /[",\n\r]/.test(text) ? '"' + text.replace(/"/g, '""') + '"' : text;
  802. }
  803. /** 从文件名识别已登记学校(取最长匹配,如「浙江工商桐庐校区20261018…」→ 浙江工商大学桐庐校区) */
  804. function resolveSchoolFromFileName(fileName) {
  805. let best = '';
  806. let bestName = '';
  807. for (const school of loadSchools()) {
  808. for (const candidate of [school.name, ...(school.aliases || [])]) {
  809. const name = String(candidate || '').trim();
  810. if (name && fileName.includes(name) && name.length > bestName.length) {
  811. best = school.name;
  812. bestName = name;
  813. }
  814. }
  815. }
  816. return best;
  817. }
  818. function register(app, ctx) {
  819. const store = createOpsStore(ctx.config);
  820. // ---------- 上传 ----------
  821. app.post('/api/ops/upload', async ({ req, res }) => {
  822. const body = await readBody(req);
  823. const fileName = sanitizeName(body.fileName);
  824. if (!/\.xlsx$/i.test(fileName)) {
  825. return sendJson(res, 400, { error: '只支持 .xlsx 文件' });
  826. }
  827. const base64 = String(body.contentBase64 || '').replace(/^data:[^;]+;base64,/, '');
  828. if (!base64) return sendJson(res, 400, { error: '缺少文件内容' });
  829. let content;
  830. try {
  831. content = Buffer.from(base64, 'base64');
  832. } catch {
  833. return sendJson(res, 400, { error: 'base64 解码失败' });
  834. }
  835. if (!content.length) return sendJson(res, 400, { error: '文件内容为空' });
  836. if (content.length > MAX_UPLOAD_BYTES) {
  837. return sendJson(res, 413, { error: '文件超过 10MB 上限' });
  838. }
  839. if (!(await requireHealthy(store, res))) return;
  840. const sha256 = crypto.createHash('sha256').update(content).digest('hex');
  841. const existing = await store.findUploadBySha(sha256);
  842. if (!body.force) {
  843. if (existing) {
  844. return sendJson(res, 409, { error: '该文件已导入过', uploadId: existing, duplicate: true });
  845. }
  846. }
  847. const year = Number(body.year) || new Date().getFullYear();
  848. let school = String(body.school || '').trim().slice(0, 60);
  849. let parsed;
  850. try {
  851. parsed = opsImport.parseAny(content, { year, school });
  852. } catch (e) {
  853. return sendJson(res, 400, { error: e.message || '解析失败', code: e.code || 'OPS_PARSE_FAILED' });
  854. }
  855. if (!parsed.rows.length) {
  856. return sendJson(res, 400, { error: '未解析到有效数据行' });
  857. }
  858. const uploadId = newUploadId();
  859. const uploadedAt = new Date().toISOString();
  860. const from = parsed.dates[0] || '';
  861. const to = parsed.dates[parsed.dates.length - 1] || '';
  862. // ---------- 平台导出(全字段明细) ----------
  863. if (parsed.kind === 'platform-export') {
  864. // 表里没有学校列:优先用上传时选定的学校,其次从文件名识别(如「浙江工商桐庐校区20261018…」)
  865. if (!school) school = resolveSchoolFromFileName(fileName);
  866. if (!school) {
  867. return sendJson(res, 400, { error: '请选择学校(平台导出表里没有学校列)', code: 'OPS_SCHOOL_REQUIRED' });
  868. }
  869. const existingRows = await store.queryPlatformRows({ from, to });
  870. const known = new Map(existingRows.map((r) => [`${r.date}|${r.shopId}`, r]));
  871. let newRows = 0;
  872. let identicalRows = 0;
  873. let conflictRows = 0;
  874. const incoming = [];
  875. for (const row of parsed.rows) {
  876. const key = `${row.date}|${row.shopId}`;
  877. const old = known.get(key);
  878. if (!old) { newRows += 1; incoming.push(row); continue; }
  879. const sameData = JSON.stringify(old.data) === JSON.stringify(row.data);
  880. if (sameData && old.school === school) { identicalRows += 1; continue; }
  881. conflictRows += 1;
  882. if (body.force) incoming.push(row);
  883. }
  884. const toWrite = body.force ? parsed.rows : incoming;
  885. const report = {
  886. kind: 'platform-export',
  887. rows: parsed.rows.length,
  888. shops: parsed.shops.length,
  889. newRows,
  890. identicalRows,
  891. conflictRows,
  892. writtenRows: toWrite.length,
  893. warningCount: parsed.warnings.length,
  894. dates: { from, to },
  895. year,
  896. schoolOverride: school,
  897. parser: { kind: 'platform-export', version: PARSER_VERSION },
  898. columns: parsed.columns || [],
  899. warnings: parsed.warnings.slice(0, 500),
  900. };
  901. try {
  902. await store.withTransaction(async (tx) => {
  903. // 事务内二次校验:并发同名文件只允许一个写入
  904. if (!body.force && await tx.findUploadBySha(sha256)) {
  905. const err = new Error('duplicate');
  906. err.code = 'OPS_DUPLICATE';
  907. throw err;
  908. }
  909. if (body.force) await tx.deletePlatformRowsBySha(sha256);
  910. await tx.insertUpload({
  911. id: uploadId,
  912. originalName: fileName,
  913. sha256,
  914. sizeBytes: content.length,
  915. uploadedAt,
  916. year,
  917. parserKind: 'platform-export',
  918. parserVersion: PARSER_VERSION,
  919. report,
  920. content,
  921. });
  922. if (toWrite.length) {
  923. await tx.upsertPlatformRows(toWrite.map((row) => ({ ...row, school, uploadId })));
  924. }
  925. });
  926. } catch (e) {
  927. if (e.code === 'OPS_DUPLICATE') {
  928. return sendJson(res, 409, { error: '该文件已导入过', uploadId: await store.findUploadBySha(sha256), duplicate: true });
  929. }
  930. return sendJson(res, 500, { error: '写入失败', detail: e.message || String(e) });
  931. }
  932. return sendJson(res, 200, {
  933. ok: true,
  934. uploadId,
  935. report,
  936. warnings: parsed.warnings.slice(0, 200),
  937. duplicateData: report.newRows === 0 && report.conflictRows === 0,
  938. });
  939. }
  940. // ---------- 人工统计表 ----------
  941. // 表格里没有「学校名称」列、上传时也没指定 → 让用户补选,避免落成空白分组
  942. if (!parsed.shops.some((shop) => shop.school && shop.school.trim())) {
  943. return sendJson(res, 400, {
  944. error: '表格里没有「学校名称」列,请在上传时选择或填写学校',
  945. code: 'OPS_SCHOOL_REQUIRED',
  946. });
  947. }
  948. // 覆盖计数:该区间内已存在的「日期 + 店铺」组合
  949. let overwritten = 0;
  950. try {
  951. const existingDaily = await store.queryRows({ from, to });
  952. const knownDaily = new Set(existingDaily.map((r) => `${r.date}|${r.shopKey}`));
  953. for (const row of parsed.rows) if (knownDaily.has(`${row.date}|${row.shopKey}`)) overwritten += 1;
  954. } catch { /* 计数失败不影响导入 */ }
  955. const report = {
  956. kind: 'manual-ops',
  957. rows: parsed.rows.length,
  958. shops: parsed.shops.length,
  959. overwritten,
  960. warningCount: parsed.warnings.length,
  961. dates: { from, to },
  962. year,
  963. schoolOverride: school,
  964. parser: { kind: 'manual-ops', version: PARSER_VERSION },
  965. warnings: parsed.warnings.slice(0, 500),
  966. };
  967. try {
  968. await store.withTransaction(async (tx) => {
  969. if (!body.force && await tx.findUploadBySha(sha256)) {
  970. const err = new Error('duplicate');
  971. err.code = 'OPS_DUPLICATE';
  972. throw err;
  973. }
  974. if (body.force) await tx.deleteRowsBySha(sha256);
  975. await tx.insertUpload({
  976. id: uploadId,
  977. originalName: fileName,
  978. sha256,
  979. sizeBytes: content.length,
  980. uploadedAt,
  981. year,
  982. parserKind: 'manual-ops',
  983. parserVersion: PARSER_VERSION,
  984. report,
  985. content,
  986. });
  987. await tx.upsertRows(parsed.rows.map((row) => ({ ...row, uploadId })));
  988. await tx.upsertShops(parsed.shops.map((shop) => ({ ...shop, lastUploadId: uploadId })));
  989. await tx.pruneOrphanShops();
  990. });
  991. } catch (e) {
  992. if (e.code === 'OPS_DUPLICATE') {
  993. return sendJson(res, 409, { error: '该文件已导入过', uploadId: await store.findUploadBySha(sha256), duplicate: true });
  994. }
  995. return sendJson(res, 500, { error: '写入失败', detail: e.message || String(e) });
  996. }
  997. sendJson(res, 200, {
  998. ok: true,
  999. uploadId,
  1000. report,
  1001. warnings: parsed.warnings.slice(0, 200),
  1002. });
  1003. });
  1004. // ---------- 概览 ----------
  1005. app.get('/api/ops/summary', async ({ res }) => {
  1006. const health = await store.health();
  1007. if (!health.ok) {
  1008. return sendJson(res, 200, {
  1009. enabled: false,
  1010. driver: store.kind,
  1011. reason: health.reason || 'storage-unreachable',
  1012. detail: health.detail || '',
  1013. });
  1014. }
  1015. try {
  1016. const data = await store.summary();
  1017. sendJson(res, 200, { enabled: true, driver: store.kind, ...data });
  1018. } catch (e) {
  1019. sendJson(res, 200, {
  1020. enabled: false,
  1021. driver: store.kind,
  1022. reason: 'storage-unreachable',
  1023. detail: e.message || String(e),
  1024. });
  1025. }
  1026. });
  1027. // ---------- 矩阵查询 ----------
  1028. app.get('/api/ops/fields', async ({ res }) => {
  1029. if (!(await requireHealthy(store, res))) return;
  1030. try {
  1031. const fields = await buildFieldCatalog(store);
  1032. sendJson(res, 200, {
  1033. fields: fields.map(({ key, label, type, source }) => ({ key, label, type, source })),
  1034. defaults: BASE_FIELD_KEYS,
  1035. });
  1036. } catch (e) {
  1037. sendJson(res, 503, { error: '读取失败', detail: e.message || String(e) });
  1038. }
  1039. });
  1040. // ---------- 统一报告生成(经营解读与经营报告共用一份 JSON) ----------
  1041. const REPORT_CACHE_TTL_MS = 10 * 60 * 1000;
  1042. const reportCache = new Map();
  1043. async function getReportPayload(params, { ai = false, force = false } = {}) {
  1044. const key = JSON.stringify(params);
  1045. const hit = reportCache.get(key);
  1046. const fresh = hit && Date.now() - hit.at < REPORT_CACHE_TTL_MS;
  1047. if (!force && fresh && (!ai || hit.source === 'llm')) {
  1048. return { ...hit.payload, cached: true };
  1049. }
  1050. const data = await buildDashboard(store, params);
  1051. if (!data.kpis) return { empty: true };
  1052. const uploads = await store.listUploads().catch(() => []);
  1053. const base = buildReport(data, {
  1054. organization: (params.schools && params.schools[0]) || '',
  1055. sourceFiles: uploads.slice(0, 3).map((u) => u.originalName),
  1056. });
  1057. base.ai = { source: 'rules', model: '', generatedAt: new Date().toISOString() };
  1058. let payload = base;
  1059. let source = 'rules';
  1060. let aiError = '';
  1061. const chatCfg = makeChatCfg(ctx);
  1062. const configured = chatCfg.chatApiKey && chatCfg.chatApiKey !== 'your-api-key-here';
  1063. if (ai && configured) {
  1064. let aiOk = false;
  1065. for (let attempt = 0; attempt < 2 && !aiOk; attempt += 1) {
  1066. try {
  1067. const text = await chatCompletion({
  1068. apiBase: chatCfg.chatApiBase,
  1069. apiKey: chatCfg.chatApiKey,
  1070. model: chatCfg.chatModel,
  1071. temperature: 0.3,
  1072. timeoutMs: 60000,
  1073. messages: [
  1074. { role: 'system', content: REPORT_AI_PROMPT },
  1075. { role: 'user', content: JSON.stringify(insightContext(data)) },
  1076. ],
  1077. });
  1078. const merged = overlayAiReport(base, extractJson(text), { model: chatCfg.chatModel });
  1079. if (merged) {
  1080. payload = merged;
  1081. source = 'llm';
  1082. aiOk = true;
  1083. aiError = '';
  1084. } else {
  1085. aiError = '模型返回结构不合法,已退回规则版';
  1086. }
  1087. } catch (e) {
  1088. aiError = e.message || String(e);
  1089. if (attempt === 0) await new Promise((resolve) => setTimeout(resolve, 800));
  1090. }
  1091. }
  1092. } else if (ai && !configured) {
  1093. aiError = '未配置对话模型,当前为规则版';
  1094. }
  1095. if (aiError) payload.aiError = aiError;
  1096. reportCache.set(key, { at: Date.now(), source, payload });
  1097. return { ...payload, cached: false };
  1098. }
  1099. // 经营解读 = 报告 JSON 的摘要视图
  1100. app.post('/api/ops/insights', async ({ req, res }) => {
  1101. if (!(await requireHealthy(store, res))) return;
  1102. const body = await readBody(req);
  1103. const params = {
  1104. from: parseYmd(body.from) || undefined,
  1105. to: parseYmd(body.to) || undefined,
  1106. schools: splitList(body.schools),
  1107. shops: splitList(body.shops),
  1108. };
  1109. try {
  1110. const report = await getReportPayload(params, { ai: true, force: !!body.refresh });
  1111. if (report.empty || !report.executive_summary) {
  1112. return sendJson(res, 200, { enabled: true, points: [], note: '所选范围没有数据' });
  1113. }
  1114. const findings = report.executive_summary.key_findings || [];
  1115. const points = findings.slice(0, 5).map((f) => f.finding);
  1116. const alerts = (report.alerts || []).slice(0, 3).map((a) => ({ severity: a.severity, title: a.title }));
  1117. sendJson(res, 200, {
  1118. enabled: true,
  1119. points,
  1120. alerts,
  1121. source: (report.ai && report.ai.source) || 'rules',
  1122. model: (report.ai && report.ai.model) || '',
  1123. generatedAt: (report.ai && report.ai.generatedAt) || new Date().toISOString(),
  1124. aiError: report.aiError || '',
  1125. range: { from: report.meta.period_start, to: report.meta.period_end },
  1126. cached: !!report.cached,
  1127. });
  1128. } catch (e) {
  1129. sendJson(res, 502, { enabled: true, error: '生成失败', detail: e.message || String(e) });
  1130. }
  1131. });
  1132. app.get('/api/ops/report', async ({ res, query }) => {
  1133. if (!(await requireHealthy(store, res))) return;
  1134. const params = {
  1135. from: parseYmd(query.from) || undefined,
  1136. to: parseYmd(query.to) || undefined,
  1137. schools: splitList(query.schools),
  1138. shops: splitList(query.shops),
  1139. };
  1140. try {
  1141. const report = await getReportPayload(params, { ai: query.ai === '1', force: query.refresh === '1' });
  1142. if (report.empty) return sendJson(res, 200, { empty: true });
  1143. sendJson(res, 200, { report });
  1144. } catch (e) {
  1145. sendJson(res, 503, { error: '报告生成失败', detail: e.message || String(e) });
  1146. }
  1147. });
  1148. // ---------- 报告中心(保存 / 列表 / 查看 / 删除) ----------
  1149. app.post('/api/ops/reports', async ({ req, res }) => {
  1150. if (!(await requireHealthy(store, res))) return;
  1151. const body = await readBody(req);
  1152. const params = {
  1153. from: parseYmd(body.from) || undefined,
  1154. to: parseYmd(body.to) || undefined,
  1155. schools: splitList(body.schools),
  1156. shops: splitList(body.shops),
  1157. };
  1158. try {
  1159. // 前端会把它当前看到的 Report JSON 一起提交,保证「保存的就是所见」
  1160. let report = body.report;
  1161. if (!report || !report.meta || !Array.isArray(report.kpis)) {
  1162. report = await getReportPayload(params, { ai: false });
  1163. }
  1164. if (!report || report.empty) return sendJson(res, 400, { error: '所选范围没有数据,无法保存' });
  1165. const id = `rpt_${newUploadId()}`;
  1166. const record = {
  1167. id,
  1168. createdAt: new Date().toISOString(),
  1169. periodFrom: String(report.meta.period_start || '').replace(/-/g, ''),
  1170. periodTo: String(report.meta.period_end || '').replace(/-/g, ''),
  1171. schools: params.schools,
  1172. shops: params.shops,
  1173. source: (report.ai && report.ai.source) || 'rules',
  1174. model: (report.ai && report.ai.model) || '',
  1175. title: report.meta.title || '经营分析报告',
  1176. report,
  1177. };
  1178. await store.insertReport(record);
  1179. sendJson(res, 200, { ok: true, id, createdAt: record.createdAt, source: record.source });
  1180. } catch (e) {
  1181. sendJson(res, 500, { error: '保存失败', detail: e.message || String(e) });
  1182. }
  1183. });
  1184. app.get('/api/ops/reports', async ({ res }) => {
  1185. if (!(await requireHealthy(store, res))) return;
  1186. try {
  1187. const reports = await store.listReports();
  1188. // 把保存时记录的店铺 key 映射成名称,历史记录才看得懂范围
  1189. let shopNameByKey = new Map();
  1190. try {
  1191. const summary = await store.summary();
  1192. shopNameByKey = new Map((summary.shops || []).map((s) => [String(s.key || s.shopId || ''), s.shopName || '']));
  1193. } catch { /* 映射失败时保留 key */ }
  1194. sendJson(res, 200, {
  1195. reports: reports.map((item) => ({
  1196. ...item,
  1197. schoolNames: item.schools || [],
  1198. shopNames: (item.shops || []).map((key) => shopNameByKey.get(String(key)) || String(key)),
  1199. })),
  1200. });
  1201. } catch (e) {
  1202. sendJson(res, 503, { error: '读取失败', detail: e.message || String(e) });
  1203. }
  1204. });
  1205. app.get('/api/ops/reports/:id', async ({ res, params }) => {
  1206. if (!/^rpt_\d{8}-\d{6}-[a-z0-9]{6}$/.test(params.id)) return sendJson(res, 400, { error: '非法的报告 ID' });
  1207. if (!(await requireHealthy(store, res))) return;
  1208. try {
  1209. const report = await store.getReport(params.id);
  1210. if (!report) return sendJson(res, 404, { error: '报告不存在' });
  1211. sendJson(res, 200, { report });
  1212. } catch (e) {
  1213. sendJson(res, 503, { error: '读取失败', detail: e.message || String(e) });
  1214. }
  1215. });
  1216. app.del('/api/ops/reports/:id', async ({ res, params }) => {
  1217. if (!/^rpt_\d{8}-\d{6}-[a-z0-9]{6}$/.test(params.id)) return sendJson(res, 400, { error: '非法的报告 ID' });
  1218. if (!(await requireHealthy(store, res))) return;
  1219. try {
  1220. const removed = await store.deleteReport(params.id);
  1221. sendJson(res, 200, { ok: true, removed });
  1222. } catch (e) {
  1223. sendJson(res, 500, { error: '删除失败', detail: e.message || String(e) });
  1224. }
  1225. });
  1226. app.get('/api/ops/dashboard', async ({ res, query }) => {
  1227. if (!(await requireHealthy(store, res))) return;
  1228. const from = parseYmd(query.from);
  1229. const to = parseYmd(query.to);
  1230. const schools = splitList(query.schools);
  1231. const shops = splitList(query.shops);
  1232. try {
  1233. const data = await buildDashboard(store, { from: from || undefined, to: to || undefined, schools, shops });
  1234. sendJson(res, 200, data);
  1235. } catch (e) {
  1236. sendJson(res, 503, { error: '聚合失败', detail: e.message || String(e) });
  1237. }
  1238. });
  1239. app.get('/api/ops/rows', async ({ res, query }) => {
  1240. if (!(await requireHealthy(store, res))) return;
  1241. const from = parseYmd(query.from);
  1242. const to = parseYmd(query.to);
  1243. const schools = splitList(query.schools);
  1244. const shops = splitList(query.shops);
  1245. let rows;
  1246. try {
  1247. rows = await loadMergedRows(store, { from: from || undefined, to: to || undefined, schools, shops });
  1248. } catch (e) {
  1249. return sendJson(res, 503, { error: '查询失败', detail: e.message || String(e) });
  1250. }
  1251. const catalog = await buildFieldCatalog(store);
  1252. const wantedFields = parseFieldKeys(query.fields);
  1253. let fields = wantedFields.length
  1254. ? wantedFields.map((key) => catalog.find((f) => f.key === key)).filter(Boolean)
  1255. : catalog.filter((f) => BASE_FIELD_KEYS.includes(f.key));
  1256. if (!fields.length) fields = catalog.filter((f) => BASE_FIELD_KEYS.includes(f.key));
  1257. const dates = [...new Set(rows.map((r) => r.date))].sort();
  1258. sendJson(res, 200, {
  1259. from: from || (dates[0] || null),
  1260. to: to || (dates[dates.length - 1] || null),
  1261. dates,
  1262. fields: fields.map(({ key, label, type }) => ({ key, label, type })),
  1263. groups: buildMatrix(rows, dates, fields),
  1264. });
  1265. });
  1266. // ---------- 导入记录 ----------
  1267. app.get('/api/ops/uploads', async ({ res }) => {
  1268. if (!(await requireHealthy(store, res))) return;
  1269. try {
  1270. const uploads = await store.listUploads();
  1271. sendJson(res, 200, { uploads });
  1272. } catch (e) {
  1273. sendJson(res, 503, { error: '读取失败', detail: e.message || String(e) });
  1274. }
  1275. });
  1276. // ---------- 原件下载 ----------
  1277. app.get('/api/ops/uploads/:id/file', async ({ res, params }) => {
  1278. if (!UPLOAD_ID_RE.test(params.id)) return sendJson(res, 400, { error: '非法的上传 ID' });
  1279. if (!(await requireHealthy(store, res))) return;
  1280. const record = await store.getContent(params.id);
  1281. if (!record) return sendJson(res, 404, { error: '找不到该上传记录' });
  1282. const name = sanitizeName(record.originalName);
  1283. res.writeHead(200, {
  1284. 'Content-Type': XLSX_MIME,
  1285. 'Content-Length': record.content.length,
  1286. 'Content-Disposition': `attachment; filename*=UTF-8''${encodeURIComponent(name)}`,
  1287. });
  1288. res.end(record.content);
  1289. });
  1290. // ---------- 重新解析 ----------
  1291. app.post('/api/ops/uploads/:id/reparse', async ({ res, params }) => {
  1292. if (!UPLOAD_ID_RE.test(params.id)) return sendJson(res, 400, { error: '非法的上传 ID' });
  1293. if (!(await requireHealthy(store, res))) return;
  1294. const record = await store.getContent(params.id);
  1295. if (!record) return sendJson(res, 404, { error: '找不到该上传记录' });
  1296. const upload = await store.getUpload(params.id);
  1297. const year = (upload && upload.year) || new Date().getFullYear();
  1298. const school = (upload && upload.report && upload.report.schoolOverride) || '';
  1299. let parsed;
  1300. try {
  1301. parsed = opsImport.parseAny(record.content, { year, school });
  1302. } catch (e) {
  1303. return sendJson(res, 400, { error: e.message || '解析失败', code: e.code || 'OPS_PARSE_FAILED' });
  1304. }
  1305. const from = parsed.dates[0] || '';
  1306. const to = parsed.dates[parsed.dates.length - 1] || '';
  1307. if (parsed.kind === 'platform-export') {
  1308. const report = {
  1309. kind: 'platform-export',
  1310. rows: parsed.rows.length,
  1311. shops: parsed.shops.length,
  1312. newRows: parsed.rows.length,
  1313. identicalRows: 0,
  1314. conflictRows: 0,
  1315. writtenRows: parsed.rows.length,
  1316. warningCount: parsed.warnings.length,
  1317. dates: { from, to },
  1318. year,
  1319. schoolOverride: school,
  1320. parser: { kind: 'platform-export', version: PARSER_VERSION },
  1321. warnings: parsed.warnings.slice(0, 500),
  1322. reparsedAt: new Date().toISOString(),
  1323. };
  1324. try {
  1325. await store.withTransaction(async (tx) => {
  1326. await tx.deletePlatformRowsByUpload(params.id);
  1327. await tx.upsertPlatformRows(parsed.rows.map((row) => ({ ...row, school, uploadId: params.id })));
  1328. await tx.updateReport(params.id, report);
  1329. });
  1330. } catch (e) {
  1331. return sendJson(res, 500, { error: '重新解析写入失败', detail: e.message || String(e) });
  1332. }
  1333. return sendJson(res, 200, { ok: true, uploadId: params.id, report });
  1334. }
  1335. const report = {
  1336. kind: 'manual-ops',
  1337. rows: parsed.rows.length,
  1338. shops: parsed.shops.length,
  1339. overwritten: 0,
  1340. warningCount: parsed.warnings.length,
  1341. dates: { from, to },
  1342. year,
  1343. schoolOverride: school,
  1344. parser: { kind: 'manual-ops', version: PARSER_VERSION },
  1345. warnings: parsed.warnings.slice(0, 500),
  1346. reparsedAt: new Date().toISOString(),
  1347. };
  1348. try {
  1349. await store.withTransaction(async (tx) => {
  1350. await tx.deleteRowsByUpload(params.id);
  1351. await tx.upsertRows(parsed.rows.map((row) => ({ ...row, uploadId: params.id })));
  1352. await tx.upsertShops(parsed.shops.map((shop) => ({ ...shop, lastUploadId: params.id })));
  1353. await tx.pruneOrphanShops();
  1354. await tx.updateReport(params.id, report);
  1355. });
  1356. } catch (e) {
  1357. return sendJson(res, 500, { error: '重新解析写入失败', detail: e.message || String(e) });
  1358. }
  1359. sendJson(res, 200, { ok: true, uploadId: params.id, report });
  1360. });
  1361. // ---------- 导出 ----------
  1362. app.get('/api/ops/export', async ({ res, query }) => {
  1363. if (!(await requireHealthy(store, res))) return;
  1364. const from = parseYmd(query.from);
  1365. const to = parseYmd(query.to);
  1366. const schools = splitList(query.schools);
  1367. const shops = splitList(query.shops);
  1368. let rows;
  1369. try {
  1370. rows = await loadMergedRows(store, { from: from || undefined, to: to || undefined, schools, shops });
  1371. } catch (e) {
  1372. return sendJson(res, 503, { error: '查询失败', detail: e.message || String(e) });
  1373. }
  1374. const catalog = await buildFieldCatalog(store);
  1375. const wantedFields = parseFieldKeys(query.fields);
  1376. let fields = wantedFields.length
  1377. ? wantedFields.map((key) => catalog.find((f) => f.key === key)).filter(Boolean)
  1378. : catalog.filter((f) => BASE_FIELD_KEYS.includes(f.key));
  1379. if (!fields.length) fields = catalog.filter((f) => BASE_FIELD_KEYS.includes(f.key));
  1380. const sorted = rows.slice().sort((a, b) => {
  1381. const school = String(a.school || '').localeCompare(String(b.school || ''), 'zh-Hans-CN');
  1382. if (school !== 0) return school;
  1383. const shop = String(a.shopName || '').localeCompare(String(b.shopName || ''), 'zh-Hans-CN');
  1384. if (shop !== 0) return shop;
  1385. return a.date < b.date ? -1 : a.date > b.date ? 1 : 0;
  1386. });
  1387. const lines = [];
  1388. lines.push(['学校', '店铺ID', '店铺名称', '日期', ...fields.map((f) => f.label)].map(csvCell).join(','));
  1389. for (const row of sorted) {
  1390. lines.push([
  1391. row.school || '',
  1392. row.shopId || '',
  1393. row.shopName || '',
  1394. row.date,
  1395. ...fields.map((field) => {
  1396. const value = fieldValue(row, field);
  1397. return value == null ? '' : value;
  1398. }),
  1399. ].map(csvCell).join(','));
  1400. }
  1401. const csv = '\ufeff' + lines.join('\r\n');
  1402. res.writeHead(200, {
  1403. 'Content-Type': 'text/csv; charset=utf-8',
  1404. 'Content-Disposition': `attachment; filename="ops-${from || 'all'}-${to || 'all'}.csv"`,
  1405. });
  1406. res.end(csv);
  1407. });
  1408. }
  1409. module.exports = { register };