reports.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. 'use strict';
  2. // 数据采集与智能分析路由:同步任务、数据湖查询/聚合/导出、经营周报 HTML、智能问数
  3. const { sendJson, readBody } = require('../util.js');
  4. const { ASK_CLIENT_CODES } = require('../middleware.js');
  5. const { makeChatCfg } = require('../context.js');
  6. const naposService = require('../../napos-service.js');
  7. const nlQuery = require('../../nl-query.js');
  8. const reportHtml = require('../../report-html.js');
  9. function register(app, ctx) {
  10. // 不限方法(保持拆分前行为:该路由未做方法检查)
  11. app.any('/api/reports/status', async ({ res }) => {
  12. const napos = await naposService.naposStatus();
  13. sendJson(res, 200, { napos, entities: naposService.store.listEntities() });
  14. });
  15. // ---------- 智能问数(NL2Query,对话模型驱动) ----------
  16. app.post('/api/reports/ask', async ({ req, res }) => {
  17. const body = await readBody(req);
  18. const entity = body.entity || (naposService.store.listEntities()[0] || {}).key;
  19. if (!entity) { const e = new Error('暂无数据主体,请先完成一次同步'); e.code = 'ASK_NO_ENTITY'; throw e; }
  20. try {
  21. const result = await nlQuery.ask({ q: body.q, entity, cfg: makeChatCfg(ctx), svc: naposService, history: Array.isArray(body.history) ? body.history : undefined });
  22. sendJson(res, 200, result);
  23. } catch (e) {
  24. // 参数类错误码 → 400(错误边界统一处理),其余(模型/网络失败)保持 502
  25. if (!ASK_CLIENT_CODES.has(e.code)) e.status = 502;
  26. throw e;
  27. }
  28. });
  29. app.get('/api/reports/report-html', async ({ res, query }) => {
  30. const entity = query.entity || (naposService.store.listEntities()[0] || {}).key;
  31. if (!entity) {
  32. res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
  33. return res.end('<meta charset="utf-8"><body style="font-family:sans-serif;padding:40px">暂无数据主体,请先在「数据报表」完成一次同步。</body>');
  34. }
  35. const range = nlQuery.lakeDateRange(entity, naposService);
  36. const from = /^\d{8}$/.test(query.from || '') ? query.from : range.from;
  37. const to = /^\d{8}$/.test(query.to || '') ? query.to : range.to;
  38. const html = reportHtml.buildReportHtml({ entity, from, to });
  39. res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
  40. return res.end(html);
  41. });
  42. app.get('/api/reports/entities', async ({ res }) => {
  43. sendJson(res, 200, { entities: naposService.store.listEntities() });
  44. });
  45. // 同步历史(数据采集页「同步历史」面板)
  46. app.get('/api/reports/syncs', async ({ res, query }) => {
  47. const entity = query.entity || (naposService.store.listEntities()[0] || {}).key;
  48. if (!entity) { sendJson(res, 200, { runs: [] }); return; }
  49. sendJson(res, 200, { entity, runs: naposService.store.loadSyncLogs(entity) });
  50. });
  51. // 数据中台:本地湖全景(主体覆盖、行数分布、最近同步/推送),供中台页渲染
  52. app.get('/api/reports/lake', async ({ res }) => {
  53. const store = naposService.store;
  54. const entities = store.listEntities().map((e) => {
  55. const rows = store.loadRows(e.key);
  56. const byScope = { company: 0, chain: 0, shop: 0, summary: 0 };
  57. let from = '';
  58. let to = '';
  59. for (const r of rows) {
  60. if (byScope[r.scope] != null) byScope[r.scope] += 1;
  61. if (r.date && (!from || r.date < from)) from = r.date;
  62. if (r.date && r.date > to) to = r.date;
  63. }
  64. const lastSync = (store.loadSyncLogs(e.key)[0] || null);
  65. return {
  66. key: e.key, name: e.name, shopCount: e.shopCount || 0, chainCount: e.chainCount || 0,
  67. rows: rows.length, byScope, coverage: rows.length ? { from, to } : null,
  68. lastSync: lastSync ? { at: lastSync.at || null, from: lastSync.from || null, to: lastSync.to || null, ok: !lastSync.cancelled && !lastSync.failed } : null,
  69. lastPush: ctx.aitable ? ctx.aitable.lastPushInfo(e.key) : null,
  70. pending: ctx.aitable ? ctx.aitable.pendingCount(e.key) : null,
  71. };
  72. });
  73. const totalRows = entities.reduce((s, e) => s + e.rows, 0);
  74. const coverage = entities.filter((e) => e.coverage).map((e) => e.coverage);
  75. sendJson(res, 200, {
  76. lakeDir: store.LAKE_DIR,
  77. entities, totalRows,
  78. coverage: coverage.length ? { from: coverage.map((c) => c.from).sort()[0], to: coverage.map((c) => c.to).sort().pop() } : null,
  79. aitable: {
  80. enabled: !!(ctx.config.aitable && ctx.config.aitable.enabled),
  81. operatorConfigured: !!(ctx.config.aitable && ctx.config.aitable.operatorId),
  82. },
  83. });
  84. });
  85. app.get('/api/reports/tree', async ({ res, query }) => {
  86. try {
  87. const tree = await naposService.fetchTree({ refresh: query.refresh === '1' });
  88. sendJson(res, 200, {
  89. merchant: tree.merchant, chains: tree.chains,
  90. shops: tree.shops.map((s) => ({ shopId: String(s.shopId), shopName: s.shopName, chainId: s.chainId ? String(s.chainId) : '', chainName: s.chainName || '', busyLevel: s.busyLevel || null })),
  91. entity: tree.entity,
  92. });
  93. } catch (e) {
  94. // 取数通道不可达属于上游失败,保持 502
  95. e.status = 502;
  96. throw e;
  97. }
  98. });
  99. app.get('/api/reports/columns', async ({ res }) => {
  100. sendJson(res, 200, { columns: naposService.REPORT_COLUMNS });
  101. });
  102. app.post('/api/reports/sync', async ({ req, res }) => {
  103. const body = await readBody(req);
  104. const entityPromise = naposService.runSync({
  105. from: body.from, to: body.to,
  106. scope: body.scope || 'all',
  107. chainId: body.chainId || '',
  108. shopIds: Array.isArray(body.shopIds) ? body.shopIds : undefined,
  109. }).catch((e) => {
  110. // 同步失败也要让自动推送保持静默,但保留日志
  111. ctx.logs.pushSystemLog(`同步结束(失败):${e.message || e}`, 'warn');
  112. return null;
  113. });
  114. // 同步完成后:若启用钉钉 AI 表格,自动把增量推上云端(失败不影响本地同步结果)
  115. entityPromise.then(async (snap) => {
  116. if (!snap || !snap.entity || !(ctx.config.aitable && ctx.config.aitable.enabled)) return;
  117. try {
  118. const r = await ctx.aitable.pushLake(snap.entity.key);
  119. ctx.logs.pushSystemLog(`AI 表格推送完成:新增 ${r.created} 行、更新 ${r.updated} 行`, 'success');
  120. } catch (e) {
  121. ctx.logs.pushSystemLog(`AI 表格推送失败:${e.message || e}`, 'error');
  122. }
  123. });
  124. return sendJson(res, 200, { ok: true, ...naposService.syncSnapshot() });
  125. });
  126. // ---------- 钉钉 AI 表格(在线主档案/共享层) ----------
  127. app.post('/api/reports/aitable/test', async ({ res }) => {
  128. const r = await ctx.aitable.testConnection();
  129. sendJson(res, 200, r);
  130. });
  131. app.post('/api/reports/aitable/push', async ({ req, res }) => {
  132. const body = await readBody(req);
  133. const entity = body.entity || (naposService.store.listEntities()[0] || {}).key;
  134. if (!entity) { const e = new Error('暂无数据主体,请先完成一次同步'); e.code = 'AITABLE_NO_ENTITY'; e.status = 400; throw e; }
  135. const r = await ctx.aitable.pushLake(entity, { force: !!body.force });
  136. ctx.logs.pushSystemLog(`AI 表格推送完成:新增 ${r.created} 行、更新 ${r.updated} 行`, 'success');
  137. sendJson(res, 200, r);
  138. });
  139. app.post('/api/reports/aitable/pull', async ({ req, res }) => {
  140. const body = await readBody(req);
  141. const r = await ctx.aitable.pullLake(body.entity);
  142. const accounts = (r.accounts || []).map((a) => `${a.account}(新增${a.added}/更新${a.updated})`).join('、');
  143. ctx.logs.pushSystemLog(`AI 表格拉取完成:${r.pulled} 行 ${accounts}`, 'success');
  144. sendJson(res, 200, r);
  145. });
  146. app.get('/api/reports/aitable/status', async ({ res, query }) => {
  147. const entity = query.entity || (naposService.store.listEntities()[0] || {}).key || '';
  148. sendJson(res, 200, {
  149. enabled: !!(ctx.config.aitable && ctx.config.aitable.enabled),
  150. configured: !!(ctx.config.aitable && ctx.config.aitable.baseId && ctx.config.aitable.sheetName && ctx.config.aitable.appKey),
  151. lastPush: entity ? ctx.aitable.lastPushInfo(entity) : null,
  152. pending: entity ? ctx.aitable.pendingCount(entity) : null,
  153. });
  154. });
  155. app.get('/api/reports/sync/status', async ({ res }) => {
  156. sendJson(res, 200, naposService.syncSnapshot());
  157. });
  158. app.post('/api/reports/sync/cancel', async ({ res }) => {
  159. sendJson(res, 200, naposService.requestSyncCancel());
  160. });
  161. // 明细行分页:明细(门店×天)量大(90 天 × 数十店 = 数千行),
  162. // 前端全量渲染会卡;summary 仍是全区间汇总(分页前计算),KPI 卡不受分页影响
  163. app.get('/api/reports/daily', async ({ res, query }) => {
  164. const r = naposService.queryReport({
  165. entity: query.entity, from: query.from, to: query.to,
  166. shopId: query.shopId,
  167. shops: query.shops ? query.shops.split(',').filter(Boolean) : undefined,
  168. scope: query.scope || 'shop', q: query.q,
  169. });
  170. const total = r.rows.length;
  171. const pageSize = Math.min(1000, Math.max(50, Number(query.pageSize) || 200));
  172. const pages = Math.max(1, Math.ceil(total / pageSize));
  173. const page = Math.min(Math.max(1, Number(query.page) || 1), pages);
  174. sendJson(res, 200, {
  175. ...r,
  176. rows: r.rows.slice((page - 1) * pageSize, page * pageSize),
  177. total, page, pageSize, pages,
  178. });
  179. });
  180. app.get('/api/reports/aggregate', async ({ res, query }) => {
  181. sendJson(res, 200, naposService.aggregateReport({
  182. entity: query.entity, from: query.from, to: query.to,
  183. group: query.group || 'shop',
  184. shops: query.shops ? query.shops.split(',').filter(Boolean) : undefined,
  185. }));
  186. });
  187. app.get('/api/reports/export', async ({ res, query }) => {
  188. const group = query.group === 'date' ? 'date' : (query.group === 'shop' ? 'shop' : null);
  189. let rows;
  190. if (group) {
  191. const agg = naposService.aggregateReport({ entity: query.entity, from: query.from, to: query.to, group });
  192. rows = agg.rows;
  193. } else {
  194. const daily = naposService.queryReport({
  195. entity: query.entity, from: query.from, to: query.to,
  196. shopId: query.shopId,
  197. shops: query.shops ? query.shops.split(',').filter(Boolean) : undefined,
  198. scope: query.scope || 'shop', q: query.q,
  199. });
  200. rows = daily.rows;
  201. }
  202. const columns = naposService.REPORT_COLUMNS;
  203. const csv = naposService.rowsToCsv(columns, rows);
  204. res.writeHead(200, {
  205. 'Content-Type': 'text/csv; charset=utf-8',
  206. 'Content-Disposition': `attachment; filename="report-${query.entity || 'all'}-${query.from || ''}-${query.to || ''}.csv"`,
  207. });
  208. return res.end(csv);
  209. });
  210. app.get('/api/reports/syncs', async ({ res, query }) => {
  211. const key = naposService.store.sanitizeKey(query.entity);
  212. sendJson(res, 200, { runs: naposService.store.loadSyncLogs(key) });
  213. });
  214. app.del('/api/reports/data', async ({ res, query }) => {
  215. if (!query.entity) return sendJson(res, 400, { error: '缺少 entity 参数' });
  216. naposService.store.deleteEntityData(query.entity);
  217. ctx.logs.pushSystemLog(`数据报表:已清除主体 ${query.entity} 的本地数据`, 'warn');
  218. sendJson(res, 200, { ok: true });
  219. });
  220. }
  221. module.exports = { register };