dsh-data-dashboard 0.0.0-stage → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +35 -2
  2. package/assets/theater-board.svg +1 -0
  3. package/backend/incremental/flow.mjs +492 -0
  4. package/backend/incremental/jobs.mjs +58 -0
  5. package/backend/incremental/xlsx-rows.mjs +145 -0
  6. package/backend/job-worker.mjs +22 -0
  7. package/backend/modules/banquet/index.mjs +46 -0
  8. package/backend/modules/inventory/index.mjs +45 -0
  9. package/backend/modules/inventory/legacy-fields.mjs +83 -0
  10. package/backend/modules/invoice/index.mjs +72 -0
  11. package/backend/modules/movement/index.mjs +68 -0
  12. package/backend/modules/opening/index.mjs +48 -0
  13. package/backend/modules/target/index.mjs +45 -0
  14. package/backend/modules/terminal/index.mjs +114 -0
  15. package/backend/modules.mjs +12 -0
  16. package/backend/public/admin.css +192 -0
  17. package/backend/public/admin.js +713 -0
  18. package/backend/public/board.css +43 -0
  19. package/backend/public/board.html +47 -0
  20. package/backend/public/board.js +96 -0
  21. package/backend/public/index.html +110 -0
  22. package/backend/public/review.css +5 -0
  23. package/backend/public/review.js +452 -0
  24. package/backend/real-data/generate-dashboard.mjs +135 -0
  25. package/backend/real-data/inventory-history.mjs +316 -0
  26. package/backend/real-data/organization-mapping.mjs +102 -0
  27. package/backend/real-data/source-payload.mjs +459 -0
  28. package/backend/real-data/terminal-components.mjs +280 -0
  29. package/backend/real-data/terminal-structure.css +105 -0
  30. package/backend/real-data/terminal-structure.mjs +276 -0
  31. package/backend/real-data/v10-components.mjs +112 -0
  32. package/backend/real-data/v10-details.css +18 -0
  33. package/backend/real-data/v10-details.mjs +251 -0
  34. package/backend/real-data/v10-layout.css +27 -0
  35. package/backend/real-data/v10-model.mjs +521 -0
  36. package/backend/real-data/v10-overview-data.mjs +248 -0
  37. package/backend/real-data/v10-overview.css +264 -0
  38. package/backend/real-data/v10-overview.mjs +263 -0
  39. package/backend/real-data/v10-presentation.mjs +342 -0
  40. package/backend/real-data/v10-shell.html +1097 -0
  41. package/backend/real-data/v10-template.mjs +351 -0
  42. package/backend/recognition/fingerprint.mjs +32 -0
  43. package/backend/recognition/headers.mjs +2 -0
  44. package/backend/recognition/module-definition.mjs +4 -0
  45. package/backend/recognition/result.mjs +29 -0
  46. package/backend/recognition/upload-limits.mjs +14 -0
  47. package/backend/review-engine.mjs +364 -0
  48. package/backend/review-service.mjs +182 -0
  49. package/backend/server.mjs +545 -0
  50. package/backend/store.mjs +68 -0
  51. package/backend/target-template.mjs +47 -0
  52. package/backend/validate-workbook.mjs +251 -0
  53. package/backend/web-sync/README.md +37 -0
  54. package/backend/web-sync/client.mjs +63 -0
  55. package/backend/web-sync/exporter.mjs +260 -0
  56. package/backend/web-sync/local.html +34 -0
  57. package/backend/web-sync/server.mjs +147 -0
  58. package/build-manifest.json +79 -0
  59. package/client.js +24 -0
  60. package/cordis.patch.yml +3 -0
  61. package/index.js +117 -0
  62. package/lib/dist-manifest.mjs +19 -0
  63. package/lib/market-index.mjs +56 -0
  64. package/lib/package-audit.mjs +34 -0
  65. package/lib/settings.mjs +71 -0
  66. package/lib/tool-definitions.mjs +56 -0
  67. package/lib/tools.mjs +255 -0
  68. package/locale/en.json +6 -0
  69. package/locale/zh.json +6 -0
  70. package/package.json +71 -4
@@ -0,0 +1,263 @@
1
+ import { createV10Presentation } from './v10-presentation.mjs';
2
+ import { createV10OverviewData } from './v10-overview-data.mjs';
3
+
4
+ // Serialized into each offline dashboard. Keep rendering helpers in this factory.
5
+ export function createV10Overview({ T, g, model, Pn }) {
6
+ const el = (tag, props, ...children) => {
7
+ const { key, ...attributes } = props || {};
8
+ return (children.length > 1 && T.jsxs ? T.jsxs : T.jsx)(tag, { ...attributes, ...(children.length ? { children: children.length === 1 ? children[0] : children } : {}) }, key);
9
+ };
10
+ const presentation = createV10Presentation(model), adapter = createV10OverviewData(model, presentation);
11
+ const finite = value => typeof value === 'number' && Number.isFinite(value);
12
+ const available = item => item?.state === 'available' && finite(item.value);
13
+ const minus = text => String(text).replaceAll('-', '−');
14
+ const number = (value, digits = 0) => minus(model.format(value, digits, digits));
15
+ const metric = (value, unit, label = '') => ({ value, unit, label, state: finite(value) ? 'available' : 'pending', reason: finite(value) ? null : '当前来源不足以完整判定' });
16
+ const stateText = () => '—';
17
+ const valueText = (item, digits = 0) => available(item) ? number(item.unit === '元' ? item.value / 10000 : item.value, digits) : stateText(item);
18
+ const reason = item => [item?.reason, ...(item?.notes || [])].filter(Boolean).join(';');
19
+ const extra = (module, label) => module.extra.find(item => item.label === label);
20
+ const cross = (module, key) => module.crossMetrics.find(item => item.key === key);
21
+ const unitText = item => item?.unit === '元' ? '万元' : item?.unit || '';
22
+ const negative = item => available(item) && item.value < 0;
23
+ function Value({ item, digits = 0, unit = unitText(item), className = '' }) {
24
+ return el('span', { className: `${className}${negative(item) ? ' is-negative' : ''}`, 'data-state': item?.state, title: reason(item), 'aria-label': available(item) ? undefined : reason(item) || '待确认' },
25
+ valueText(item, digits), available(item) && unit && el('small', {}, unit));
26
+ }
27
+ function Growth({ item, label = '' }) {
28
+ const text = available(item) ? `${item.value > 0 ? '↑ ' : item.value < 0 ? '↓ ' : ''}${number(Math.abs(item.value), 1)}%` : '—';
29
+ return el('span', { className: 'ov-growth', title: [item?.label, reason(item)].filter(Boolean).join(';'), 'aria-label': `${item?.label || '同比'} ${available(item) ? text : reason(item) || '待确认'}`, 'data-state': item?.state },
30
+ el('em', { className: available(item) ? item.value > 0 ? 'is-up' : item.value < 0 ? 'is-down' : 'is-flat' : 'is-pending' }, text), label && el('span', {}, label));
31
+ }
32
+ function Comparison({ item, opening = false }) {
33
+ return el('span', { className: `ov-comparison${opening ? ' ov-comparison-inline' : ''}` },
34
+ el(Growth, { item, label: '同比' }), el('span', { className: 'ov-previous', title: reason(item) }, opening ? '去年同期 ' : '去年同期 ',
35
+ el(Value, { item: item?.previous, digits: opening ? 0 : 2, unit: opening ? '瓶' : '万' })));
36
+ }
37
+ function Stat({ label, name, item, digits = 0, unit, growth }) {
38
+ return el('div', { title: [item?.label, reason(item)].filter(Boolean).join(';') }, el('span', {}, label),
39
+ el('b', { 'data-hero-metric': name, 'data-state': item?.state, className: `${name === 'negative' ? 'ov-negative-amount' : negative(item) ? 'is-negative' : ''} ${!available(item) ? 'is-pending' : ''}` },
40
+ el(Value, { item, digits, unit })), growth && el(Growth, { item: growth }));
41
+ }
42
+ function MonthlyChart({ points, kind, filters, drill }) {
43
+ const valid = points.flatMap(item => [item.current, item.previous]).filter(available);
44
+ const max = Math.max(0, ...valid.map(item => item.value)), min = Math.min(0, ...valid.map(item => item.value)), span = max - min || 1, zero = -min / span * 100;
45
+ const negativeMax = Math.max(1, ...points.map(p => available(p.current) ? Math.abs(p.current.negative || 0) : 0));
46
+ const year = Number(filters.month.slice(0, 4));
47
+ const bar = (item, prior) => available(item) && el('i', { className: `${prior ? 'ov-prior-net' : 'ov-net'}${item.value < 0 ? ' is-negative' : ''}`,
48
+ 'data-value': item.value, style: { bottom: `${zero + Math.min(0, item.value) / span * 100}%`, height: `${Math.abs(item.value) / span * 100}%`, minHeight: '1px' } });
49
+ return el(g.Fragment, {},
50
+ el('div', { className: 'ov-chart-header', title: '净额保留正负方向,两年共用刻度;底部斜纹单独展示当年负金额,使用独立刻度,不与净额柱相加。缺失不补零。' },
51
+ el('span', {}, min < 0 ? '月度对比 · 含负净额' : '月度对比'),
52
+ el('span', { className: 'ov-chart-legend' }, el('span', {}, el('i', { className: 'ov-year-swatch' }), year), el('span', {}, el('i', { className: 'ov-prior-swatch' }), year - 1), el('span', {}, el('i', { className: 'ov-negative-swatch' }), '负金额'))),
53
+ el('div', { className: 'ov-chart', 'aria-label': `${model.titles[kind]}月度对比;两年净额同刻度,底部负金额独立刻度` }, ...points.map(({ month: key, current: item, previous }) => {
54
+ const month = Number(key.slice(5)), current = key === filters.month, pickable = model.months.includes(key), deduction = available(item) && finite(item.negative) && item.negative < 0;
55
+ const title = `${key}:净额 ${valueText(item, 2)} 万元${!available(item) ? `(${reason(item)})` : ''};上年同期 ${valueText(previous, 2)} 万元;负金额 ${available(item) ? number((item.negative || 0) / 10000, 2) : '—'} 万元(底部独立刻度)${item.range ? `;当年期间 ${item.range.start} 至 ${item.range.end}` : ''}${previous.range ? `;去年期间 ${previous.range.start} 至 ${previous.range.end}` : ''};${pickable ? '点击切换月份' : '本月无可选来源'}`;
56
+ return el('button', { key, type: 'button', disabled: !pickable, className: `ov-month${current ? ' is-current' : ''}`, 'data-overview-month': key, 'aria-pressed': current, 'aria-label': title, title,
57
+ onClick: event => { event.stopPropagation(); if (pickable) drill('month', key); } },
58
+ el('span', { className: 'ov-column' },
59
+ el('span', { className: 'ov-net-lane' }, el('span', { className: 'ov-zero', style: { bottom: `${zero}%` } }), bar(previous, true), bar(item, false),
60
+ current && available(item) && el('b', { className: 'ov-net-label', style: { bottom: `${(item.value - min) / span * 100}%` } }, number(item.value / 10000))),
61
+ el('span', { className: 'ov-negative-lane', 'aria-hidden': true }, deduction && el('i', { className: 'ov-deduction', 'data-value': item.negative, style: { height: `${Math.max(2, Math.abs(item.negative) / negativeMax * 22)}px` } }),
62
+ current && deduction && el('b', { className: 'ov-negative-label' }, number(item.negative / 10000))),
63
+ !available(item) && !available(previous) && el('span', { className: 'ov-missing', 'data-state': item.state }, '—')),
64
+ el('span', { className: 'ov-month-label' }, `${month}月`));
65
+ })), el('span', { className: 'ov-chart-note' }, '底部斜纹为负金额 · 独立刻度'));
66
+ }
67
+ function Hero({ kind, data, comparison, filters, open, drill }) {
68
+ const module = data.modules[kind], view = presentation.describe(kind, data, filters), movement = kind === 'movement';
69
+ const ytdRatio = cross(module, 'invoiceMovementYtdRatio');
70
+ const ytd = extra(module, '当年截至所选月来源金额'), negativeAmount = extra(module, '负金额合计');
71
+ return el('article', { className: `overview-card ov-${kind} ov-hero`, style: { '--hero-accent': module.color }, role: 'button', tabIndex: 0, 'aria-label': `查看${module.title}详情`,
72
+ onClick: () => open(kind), onKeyDown: event => { if (event.target === event.currentTarget && ['Enter', ' '].includes(event.key)) { event.preventDefault(); open(kind); } } },
73
+ el('div', { className: 'ov-heading' }, el('i', { className: 'ov-dot', 'aria-hidden': true }), el('h2', {}, module.title), el('span', { className: 'ov-action' }, '详情 →')),
74
+ el('p', { className: 'ov-label' }, module.main.label),
75
+ el('div', { className: 'ov-hero-values' }, el('div', { className: 'ov-value', 'data-metric': kind }, el(Value, { item: module.main, digits: 2 })), el(Comparison, { item: comparison.yoy })),
76
+ el('div', { className: 'ov-stats' },
77
+ el(Stat, { label: '年累计', name: 'ytd', item: ytd, digits: 2, unit: '万', growth: comparison.ytdYoY }),
78
+ el(Stat, { label: model.periodOf(filters).mode === 'range' ? '区间负金额' : '当月负金额', name: 'negative', item: negativeAmount, digits: 2, unit: '万' }),
79
+ el(Stat, { label: movement ? '扫码终端' : '经销商', name: 'count', item: view.kpis[4], unit: '家' }),
80
+ movement ? el(Stat, { label: '累计动销比', name: 'ratio', item: ytdRatio, digits: 1, unit: '%' })
81
+ : el(Stat, { label: '达成率', name: 'attainment', item: view.kpis[5], digits: 1 })),
82
+ el(MonthlyChart, { points: comparison.monthly, kind, filters, drill }));
83
+ }
84
+ function Auxiliary({ label, item, digits = 0, unit = '' }) {
85
+ return el('span', { title: [item?.label, reason(item)].filter(Boolean).join(';') }, label,
86
+ el('b', { className: negative(item) ? 'is-negative' : '', 'data-state': item?.state }, el(Value, { item, digits, unit })));
87
+ }
88
+ function Operations({ data, open }) {
89
+ const terminal = data.modules.terminal, inventory = data.modules.inventory, banquet = data.modules.banquet;
90
+ const inventoryVisit = inventory.inventoryMode === 'visit-history';
91
+ const totals = terminal.structure?.totals || {};
92
+ const terminalEffective = extra(terminal, '有效终端数'), terminalRate = extra(terminal, '有效率');
93
+ const inventoryDifference = { label: '盘点减系统金额差额', unit: '元', value: null,
94
+ state: inventory.state === 'unavailable' ? 'missing' : 'pending',
95
+ reason: inventory.state === 'unavailable' ? '未接入有效库存来源' : '两类库存金额的计价与估值基础尚未确认,暂不计算金额差额' };
96
+ const cover = model.payload.coverage.banquet;
97
+ const fixedPeriod = cover?.start && cover?.end ? cover.start.slice(0, 4) === cover.end.slice(0, 4)
98
+ ? `${Number(cover.start.slice(5, 7))}–${Number(cover.end.slice(5, 7))} 月固定`
99
+ : `${cover.start.slice(0, 7)} 至 ${cover.end.slice(0, 7)} 固定` : '固定期间待确认';
100
+ const entries = [
101
+ { module: terminal, auxiliary: el(g.Fragment, {}, el(Auxiliary, { label: '有效终端', item: terminalEffective }),
102
+ el('span', { className: 'ov-tag', title: reason(terminalRate) }, '有效率 ', el(Value, { item: terminalRate, digits: 1, unit: '%' }))), note: `${number(totals.agreements)} 份协议;不随产品筛选` },
103
+ inventoryVisit ? { module: inventory, main: inventory.main, digits: 0,
104
+ auxiliary: el(g.Fragment, {}, el(Auxiliary, { label: '系统', item: extra(inventory, '所选记录系统瓶数合计'), unit: '瓶' }),
105
+ el(Auxiliary, { label: '盘点与系统差异', item: extra(inventory, '盘点减系统差异合计'), unit: '瓶' })),
106
+ note: `${inventory.period};最近一次已知盘点,单位瓶;不随品牌/品类筛选` }
107
+ : { module: inventory, main: extra(inventory, '所选记录盘点金额合计'), digits: 2,
108
+ auxiliary: el(g.Fragment, {}, el(Auxiliary, { label: '系统', item: extra(inventory, '所选记录系统金额合计'), digits: 2, unit: '万' }),
109
+ el(Auxiliary, { label: '差额', item: inventoryDifference }), el(Auxiliary, { label: '可销', item: cross(inventory, 'inventorySellableMonths'), digits: 1, unit: '月' })), note: `${inventory.period};盘点金额与系统金额按来源分别展示,单位万元` },
110
+ { module: banquet, auxiliary: el(g.Fragment, {}, el(Auxiliary, { label: '开瓶', item: extra(banquet, '固定期间宴席开瓶') }),
111
+ el(Auxiliary, { label: '场均', item: extra(banquet, '场均开瓶'), digits: 1, unit: '瓶' }), el('span', { title: banquet.period }, fixedPeriod)), note: banquet.period },
112
+ ];
113
+ return el('section', { className: 'overview-card ov-operations', 'aria-label': '签约、库存与宴席' },
114
+ ...entries.map(({ module, main = module.main, digits = 0, auxiliary, note }) => el('button', { key: module.id, type: 'button', className: `ov-operation ov-${module.id}`, onClick: () => open(module.id), 'aria-label': `查看${module.title}详情`, title: note },
115
+ el('span', { className: 'ov-operation-heading' }, el('i', { className: 'ov-dot', style: { background: module.color } }), module.title, el('span', { className: 'ov-action' }, '→')),
116
+ el('span', { className: 'ov-secondary-value', 'data-metric': module.id }, el(Value, { item: main, digits })), el('span', { className: 'ov-kv' }, auxiliary))));
117
+ }
118
+ function DayChange({ item }) {
119
+ const day = date => date ? `${Number(date.slice(5, 7))}/${Number(date.slice(8))}` : '—';
120
+ const bottles = value => finite(value) ? `${number(value)} 瓶` : '—';
121
+ const diff = finite(item?.difference) ? `${item.difference > 0 ? '+' : ''}${number(item.difference)} 瓶` : '';
122
+ const label = `${day(item?.date)} 较 ${day(item?.previousDate)}`;
123
+ const detail = [`${item?.date || '末日'}:${bottles(item?.current)}`, `${item?.previousDate || '昨日'}:${bottles(item?.previous)}`, diff && `变化 ${diff}`, reason(item)].filter(Boolean).join(';');
124
+ return el('span', { className: 'ov-day-change', 'data-state': item?.state, title: detail, 'aria-label': `较昨日开瓶 ${detail}` },
125
+ el(Growth, { item, label }), el('span', { className: 'ov-day-values' }, bottles(item?.current), el('small', {}, ` / 昨日 ${bottles(item?.previous)}`)));
126
+ }
127
+ function Opening({ data, description, open }) {
128
+ const module = data.modules.opening, points = description.openingDaily, previous = description.openingPreviousDaily;
129
+ const valid = [...points, ...previous].filter(available), max = Math.max(0, ...valid.map(item => item.value)), min = Math.min(0, ...valid.map(item => item.value)), range = max - min || 1;
130
+ const x = index => 3 + index * 394 / Math.max(points.length - 1, 1), y = value => 74 - (value - min) / range * 68, baseline = y(0);
131
+ const paths = (series, prior) => {
132
+ const segments = []; let segment = [];
133
+ series.forEach((item, index) => { if (available(item)) segment.push({ item, index }); else if (segment.length) { segments.push(segment); segment = []; } });
134
+ if (segment.length) segments.push(segment);
135
+ return segments.map((part, index) => {
136
+ const line = part.map(({ item, index: i }, at) => `${at ? 'L' : 'M'} ${x(i)} ${y(item.value)}`).join(' ');
137
+ return el('g', { key: `${prior}-${index}` },
138
+ !prior && el('path', { d: `${line} L ${x(part.at(-1).index)} ${baseline} L ${x(part[0].index)} ${baseline} Z`, fill: 'url(#ov-opening-fill)' }),
139
+ el('path', { d: line, fill: 'none', stroke: prior ? '#aab3bd' : '#4a6f94', strokeWidth: prior ? 1.2 : 1.6, strokeDasharray: prior ? '3 3' : undefined, vectorEffect: 'non-scaling-stroke', 'data-opening-series': prior ? 'previous' : 'current' }),
140
+ part.length === 1 && el('circle', { cx: x(part[0].index), cy: y(part[0].item.value), r: 1.5, fill: prior ? '#aab3bd' : '#4a6f94' }));
141
+ });
142
+ };
143
+ const first = points[0]?.date, last = points.at(-1)?.date, year = first?.slice(0, 4);
144
+ const ticks = [...new Set([first && Number(first.slice(8)), 10, 20, last && Number(last.slice(8))])].filter(day => first && day >= Number(first.slice(8)) && day <= Number(last.slice(8))).sort((a, b) => a - b);
145
+ return el('button', { type: 'button', className: 'overview-card ov-opening', 'aria-label': '查看消费者开瓶详情', onClick: () => open('opening') },
146
+ el('span', { className: 'ov-heading' }, el('i', { className: 'ov-dot' }), el('span', {}, module.title), el('span', { className: 'ov-action' }, '→')),
147
+ el('span', { className: 'ov-opening-value' }, el('span', { className: 'ov-secondary-value', 'data-metric': 'opening' }, el(Value, { item: module.main }))),
148
+ el(Comparison, { item: description.openingYoY, opening: true }),
149
+ el(DayChange, { item: description.openingGrowth }),
150
+ valid.length ? el('svg', { className: 'ov-sparkline', viewBox: '0 0 400 80', preserveAspectRatio: 'none', role: 'img', 'aria-label': `${description.openingPeriod}每日开瓶;两年按相同日期比较,缺日断开` },
151
+ el('defs', {}, el('linearGradient', { id: 'ov-opening-fill', x1: '0', y1: '0', x2: '0', y2: '1' }, el('stop', { offset: '0', stopColor: '#4a6f94', stopOpacity: .16 }), el('stop', { offset: '1', stopColor: '#4a6f94', stopOpacity: 0 }))),
152
+ ...paths(previous, true), ...paths(points, false)) : el('span', { className: 'ov-spark-empty' }, '—'),
153
+ el('span', { className: 'ov-day-axis', title: description.openingPeriod }, ...ticks.map(day => el('span', { key: day }, `${day}日`))),
154
+ el('span', { className: 'ov-opening-legend', title: `${description.openingPeriod};${reason(description.openingYoY)};组织口径,不随产品筛选` },
155
+ el('span', {}, el('i', {}), year || '本年'), el('span', { className: previous.some(available) ? '' : 'is-pending' }, el('i', { className: 'is-prior' }), year ? Number(year) - 1 : '上年'), el('span', {}, '不随产品筛选')));
156
+ }
157
+ function Regions({ items, filters, groupField, drill }) {
158
+ const modes = [['movement', '动销/发票'], ['terminal', '签约有效率'], ['opening', '开瓶']];
159
+ const [mode, setMode] = g.useState('movement'), listRef = g.useRef(null);
160
+ const scopeKey = JSON.stringify([filters.month, filters.period, filters.start, filters.end, filters.region, filters.office, filters.unit, filters.product]);
161
+ g.useLayoutEffect(() => { listRef.current?.scrollTo({ top: 0, behavior: 'instant' }); }, [scopeKey]);
162
+ const rows = [...items].sort((a, b) => (available(b.invoice) ? b.invoice.value : -Infinity) - (available(a.invoice) ? a.invoice.value : -Infinity) || a.name.localeCompare(b.name, 'zh-CN'));
163
+ const title = filters.unit !== 'all' ? '当前单元经营' : filters.region !== 'all' ? '下级经营一览' : '区域经营一览';
164
+ const level = groupField === 'region' ? '大区' : groupField === 'office' ? '办事处' : '主任单元';
165
+ const range = rows.find(row => row.openingTotal.range?.end >= row.openingTotal.range?.start)?.openingTotal.range;
166
+ const openingPeriod = range ? `${range.start} 至 ${range.end}` : '共同期间待确认';
167
+ const subtitle = mode === 'opening' ? `年累计 · ${openingPeriod}` : mode === 'terminal' ? `${model.periodOf(filters).mode === 'range' ? `截至 ${model.periodOf(filters).end}` : '截至所选月末'} · 按终端去重` : `${model.periodOf(filters).mode === 'range' ? model.periodOf(filters).label : `${Number(filters.month.slice(5)) || '当前'} 月`} · 点击${level}下钻`;
168
+ const pairs = mode === 'terminal' ? [['signed', '签约'], ['effectiveCount', '有效']] : [['invoice', '发票'], ['movement', '动销']];
169
+ const values = rows.flatMap(row => pairs.map(([key]) => row[key]).filter(available).map(item => item.value));
170
+ const max = Math.max(0, ...values), min = Math.min(0, ...values), span = max - min || 1, zero = -min / span * 100;
171
+ const field = (row, key, digits = 0, unit = '', alert = false) => el('span', { className: `ov-number${alert ? ' is-alert' : ''}`, 'data-region-metric': key }, el(Value, { item: row[key], digits, unit }), ['invoice', 'movement', 'openingTotal'].includes(key) && el(Growth, { item: row[key === 'openingTotal' ? 'openingYoY' : `${key}YoY`] }));
172
+ const bar = row => el('span', { className: 'ov-region-bars', 'aria-label': pairs.map(([key, label]) => `${label} ${valueText(row[key], mode === 'movement' ? 2 : 0)} ${available(row[key]) ? unitText(row[key]) : reason(row[key])}`).join(';') },
173
+ ...pairs.map(([key, label]) => el('span', { key, className: 'ov-region-bar-track', title: `${label}:${valueText(row[key], mode === 'movement' ? 2 : 0)} ${available(row[key]) ? unitText(row[key]) : reason(row[key])}` },
174
+ min < 0 && el('s', { style: { left: `${zero}%` } }), available(row[key])
175
+ ? el('i', { className: `ov-${key}-bar${negative(row[key]) ? ' is-negative' : ''}`, style: { left: `${row[key].value >= 0 ? zero : zero + row[key].value / span * 100}%`, width: `${Math.abs(row[key].value) / span * 100}%`, minWidth: row[key].value === 0 ? '1px' : undefined } })
176
+ : el('em', { 'data-state': row[key].state }, '—'))));
177
+ function onTabKey(event, index) {
178
+ const next = event.key === 'ArrowRight' ? (index + 1) % modes.length : event.key === 'ArrowLeft' ? (index + modes.length - 1) % modes.length : event.key === 'Home' ? 0 : event.key === 'End' ? modes.length - 1 : null;
179
+ if (next === null) return;
180
+ event.preventDefault(); setMode(modes[next][0]); event.currentTarget.parentElement.children[next].focus();
181
+ }
182
+ return el('section', { className: `overview-card ov-regional ov-region-${mode}`, 'aria-label': title, 'data-region-view': mode },
183
+ el('div', { className: 'ov-region-header' }, el('div', { className: 'ov-heading' }, el('i', { className: 'ov-dot' }), el('h2', {}, title), el(UpLevel, { filters, drill, compact: true })),
184
+ el('span', { className: 'ov-region-note', title: subtitle }, subtitle),
185
+ el('div', { className: 'ov-region-tabs', role: 'tablist', 'aria-label': '区域经营视图' }, ...modes.map(([key, label], index) => el('button', {
186
+ key, id: `ov-region-tab-${key}`, type: 'button', role: 'tab', tabIndex: mode === key ? 0 : -1,
187
+ 'aria-selected': mode === key, 'aria-controls': 'ov-region-panel', onClick: () => setMode(key), onKeyDown: event => onTabKey(event, index),
188
+ }, label)))),
189
+ el('div', { className: 'ov-region-columns' }, el('span', {}), el('span', {}, level),
190
+ ...(mode === 'opening' ? [el('span', { key: 'total' }, '总开瓶数 / 瓶'), el('span', { key: 'rate' }, '总开瓶率')]
191
+ : [el('span', { key: 'legend', className: 'ov-pair-legend' }, ...pairs.map(([key, label]) => el('span', { key }, el('i', { className: `ov-${key}-bar` }), label))),
192
+ el('span', { key: 'first' }, mode === 'terminal' ? '签约数 / 家' : '发票 / 万元'), el('span', { key: 'second' }, mode === 'terminal' ? '有效数 / 家' : '动销 / 万元'), el('span', { key: 'rate' }, mode === 'terminal' ? '签约有效率' : '动销/发票')])),
193
+ el('div', { className: 'ov-region-list', ref: listRef, id: 'ov-region-panel', role: 'tabpanel', tabIndex: 0, 'aria-labelledby': `ov-region-tab-${mode}`, 'aria-label': `${level}列表,可纵向滚动查看全部组织` },
194
+ ...rows.map((row, index) => el('button', { key: row.key, type: 'button', className: 'ov-region-row', disabled: !row.drillable, 'data-region-key': row.key,
195
+ 'aria-label': `${row.drillable ? '筛选' : '查看'}${row.name}`, title: row.fullName, onClick: () => row.drillable && drill(groupField, row.key) },
196
+ el('span', { className: 'ov-rank' }, String(index + 1).padStart(2, '0')),
197
+ el('span', { className: 'ov-region-name', title: row.fullName }, el('strong', {}, row.name),
198
+ mode === 'movement' ? el('small', { title: reason(row.invoiceShare) }, '发票占比 ', el(Value, { item: row.invoiceShare, digits: 1, unit: '%' }))
199
+ : el('small', {}, mode === 'terminal' ? '签约终端 · 家' : '年累计 · 共同期间')),
200
+ ...(mode === 'opening' ? [field(row, 'openingTotal'), field(row, 'openingRate', 1, '%')]
201
+ : [bar(row), field(row, pairs[0][0], mode === 'movement' ? 2 : 0), field(row, pairs[1][0], mode === 'movement' ? 2 : 0),
202
+ mode === 'terminal' ? field(row, 'effectiveRate', 1, '%') : field(row, 'ratio', 0, '%', available(row.ratio) && row.ratio.value > 300)]))),
203
+ !rows.length && el('p', { className: 'ov-empty' }, '当前范围暂无组织来源记录')),
204
+ el('div', { className: 'ov-region-foot' }, el('span', {}, mode === 'opening' ? '累计同比 · 不随产品筛选 · — 表示待确认或缺失' : '金额下方为同比 · — 表示待确认或缺失'),
205
+ rows.length > 3 && el('span', {}, '向下滚动查看更多')));
206
+ }
207
+ const shiftDay = (date, days) => new Date(Date.parse(`${date}T00:00:00Z`) + days * 86400000).toISOString().slice(0, 10);
208
+ const isDate = value => typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value) && Number.isFinite(Date.parse(`${value}T00:00:00Z`)) && new Date(`${value}T00:00:00Z`).toISOString().slice(0, 10) === value;
209
+ // One step up the organization drill: unit → office → region → all, then a
210
+ // selected product. Shown wherever a drill can narrow the scope.
211
+ function upLevel(filters) {
212
+ if (filters.unit !== 'all') return { key: 'unit', label: model.label(filters.office) };
213
+ if (filters.office !== 'all') return { key: 'office', label: model.label(filters.region) };
214
+ if (filters.region !== 'all') return { key: 'region', label: '全战区' };
215
+ if (filters.product && filters.product !== 'all') return { key: 'product', label: '全部产品' };
216
+ return null;
217
+ }
218
+ function UpLevel({ filters, drill, compact = false }) {
219
+ const step = upLevel(filters);
220
+ if (!step) return null;
221
+ return el('button', { type: 'button', className: `up-level${compact ? ' is-compact' : ''}`, title: `返回上一级:${step.label}`, 'aria-label': `返回上一级:${step.label}`,
222
+ onClick: event => { event.stopPropagation(); drill(step.key, 'all'); } }, '‹ 返回上一级', !compact && el('small', {}, step.label));
223
+ }
224
+ // Date range bar: start, end, quick picks, apply and reset. Month mode is
225
+ // the default; the inputs then show the selected month's days.
226
+ function PeriodFilter({ filters, drill, detail }) {
227
+ const period = model.periodOf(filters), bounds = model.dateBounds, range = period.mode === 'range';
228
+ const shown = period.mode === 'fixed' ? { start: '', end: '' } : { start: period.start, end: bounds ? [period.end, bounds.max].sort()[0] : period.end };
229
+ const [draft, setDraft] = g.useState(shown);
230
+ const key = JSON.stringify([period.mode, period.start, period.end]);
231
+ g.useEffect(() => setDraft(shown), [key]);
232
+ const clamp = start => bounds ? [start, bounds.min].sort().at(-1) : start;
233
+ const max = bounds?.max;
234
+ const quick = max ? [['近7天', clamp(shiftDay(max, -6)), max], ['近30天', clamp(shiftDay(max, -29)), max], ['本月', clamp(`${max.slice(0, 7)}-01`), max]] : [];
235
+ const issue = !isDate(draft.start) || !isDate(draft.end) ? '请选择开始和结束日期' : draft.start > draft.end ? '开始日期不能晚于结束日期'
236
+ : bounds && (draft.end < bounds.min || draft.start > bounds.max) ? `超出来源日期 ${bounds.min} 至 ${bounds.max}` : null;
237
+ const changed = draft.start !== shown.start || draft.end !== shown.end || !range;
238
+ const apply = value => { const next = value || draft; if (isDate(next.start) && isDate(next.end) && next.start <= next.end) drill('range', next); };
239
+ const active = item => range && period.start === item[1] && period.end === item[2];
240
+ const field = (label, keyName) => el('label', { className: 'period-date' }, el('span', {}, label),
241
+ el('input', { type: 'date', value: draft[keyName], min: bounds?.min, max, onChange: event => {
242
+ const next = { ...draft, [keyName]: event.target.value };
243
+ setDraft(next);
244
+ if (detail && isDate(next.start) && isDate(next.end) && next.start <= next.end && next.start >= bounds?.min && next.end <= max) apply(next);
245
+ }, onKeyDown: event => { if (!detail && event.key === 'Enter' && !issue) apply(); } }));
246
+ return el('div', { className: `period-bar${range ? ' is-range' : ''}`, role: 'group', 'aria-label': '时间范围' },
247
+ el('span', { className: 'period-title' }, el('svg', { viewBox: '0 0 24 24', 'aria-hidden': true }, el('rect', { x: 3, y: 5, width: 18, height: 16, rx: 2 }), el('path', { d: 'M3 10h18M8 3v4m8-4v4' })), '时间'),
248
+ el('div', { className: 'period-range' }, field('开始', 'start'), el('span', { className: 'period-arrow', 'aria-hidden': true }, '→'), field('结束', 'end')),
249
+ el('div', { className: 'period-quick', role: 'group', 'aria-label': '快捷时间' }, ...quick.map(item => el('button', { key: item[0], type: 'button', className: active(item) ? 'is-active' : '', 'aria-pressed': active(item), onClick: () => apply({ start: item[1], end: item[2] }) }, item[0])),
250
+ !detail && el('button', { key: 'default', type: 'button', className: `period-reset${!range && filters.month === model.defaults.month ? ' is-active' : ''}`, 'aria-pressed': !range && filters.month === model.defaults.month, onClick: () => { setDraft(shown); drill('month', model.defaults.month); } }, '恢复默认')),
251
+ !detail && el('button', { type: 'button', className: 'period-apply', disabled: Boolean(issue) || !changed, title: issue || undefined, onClick: () => apply() }, '应用'),
252
+ el('button', { type: 'button', className: 'period-clear', title: '全部重置:时间回到最新月份,组织和产品回到全部', 'aria-label': '全部重置', onClick: () => { drill('region', 'all'); drill('product', 'all'); drill('month', model.defaults.month); } }, '↺'),
253
+ el('span', { className: `period-note${issue && changed ? ' is-error' : ''}`, 'aria-live': 'polite' }, issue && changed ? issue : range ? `${period.days} 天 · 签约、库存取末日状态` : `${period.label}${bounds ? ` · 数据至 ${bounds.max.slice(5)}` : ''}`));
254
+ }
255
+ function Overview({ data, filters, open, drill }) {
256
+ const description = g.useMemo(() => adapter.describe(data, filters), [data, filters]);
257
+ return el('div', { className: 'overview-shell', 'aria-label': '战区经营总览六板块' },
258
+ el(Hero, { kind: 'invoice', data, comparison: description.heroes.invoice, filters, open, drill }), el(Hero, { kind: 'movement', data, comparison: description.heroes.movement, filters, open, drill }),
259
+ el(Operations, { data, open }), el(Opening, { data, description, open }),
260
+ el(Regions, { items: description.regional, filters, groupField: data.groupField, drill }));
261
+ }
262
+ return { Overview, PeriodFilter, UpLevel };
263
+ }