report.js 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. /**
  3. * 智能问数智能体 · HTML 分析报告工具
  4. * ----------------------------------------------------------------------------
  5. * build_report:为指定区间生成经营分析 HTML 报告的入口。报告本体由既有的
  6. * report-html.js / GET /api/reports/report-html 即时生成(零重复实现),工具
  7. * 只负责钳制区间、组装链接并说明报告内容;前端以「查看分析报告」按钮呈现。
  8. */
  9. const { ymdDash } = require('./lake.js');
  10. function reportSanitize(args, ctx) {
  11. const a = args || {};
  12. const { dateRange } = ctx;
  13. const from = /^\d{8}$/.test(String(a.from || '')) ? String(a.from) : dateRange.from;
  14. const to = /^\d{8}$/.test(String(a.to || '')) ? String(a.to) : dateRange.to;
  15. return { from: from < dateRange.from ? dateRange.from : from, to: to > dateRange.to ? dateRange.to : to };
  16. }
  17. function reportHandler(ctx, args) {
  18. const { entityKey, dateRange } = ctx;
  19. const { from, to } = args;
  20. const params = new URLSearchParams({ entity: entityKey, from, to });
  21. const url = `/api/reports/report-html?${params.toString()}`;
  22. const wholeLake = from === dateRange.from && to === dateRange.to;
  23. return {
  24. summary: `已就绪经营分析报告:${ymdDash(from)} ~ ${ymdDash(to)}${wholeLake ? '(全湖区间)' : ''},包含 KPI 概览、逐日趋势与门店明细(HTML,浏览器新窗口打开,可打印导出 PDF)。回答中提示用户点击「查看分析报告」查看。`,
  25. data: { url, from, to },
  26. };
  27. }
  28. module.exports = { reportSanitize, reportHandler };