234 lines
8.3 KiB
JavaScript
234 lines
8.3 KiB
JavaScript
'use strict';
|
||
|
||
const { applyFormat } = require('./formatters');
|
||
const { formatWindowStart } = require('./day_window');
|
||
|
||
/**
|
||
* 报表渲染翻译层(纯函数模块,无 ctx 依赖,可单测)。
|
||
*
|
||
* 输入:报表类型 type 的硬编码字段注册清单(fields,按勾选顺序)+ 查询器产出的数据 rows;
|
||
* 输出:适配 `app/service/systems/excel.js` 的 sheets 参数结构
|
||
* ({ name, columns, rows, merges, styledRows, textColumns? })。
|
||
*
|
||
* 约定(与 excel.js 的 buildExcelJsWorkbook 对齐):
|
||
* 1. 数据以 AOA(二维数组)形式输出,表头行内联在 rows 首位,columns 恒传空数组;
|
||
* 2. merges 统一输出 {s:{r,c}, e:{r,c}} 结构(excel.js 两套构建器均支持);
|
||
* 3. textColumns 输出 0 基列索引,由 excel.js 落地为文本单元格。
|
||
*
|
||
* 列规则(全部由字段注册清单硬编码决定,无 sheet_config 自定义):
|
||
* 1. string 字段 → 静态列(header=label),asText=true 时加入 textColumns;
|
||
* 2. number 字段 → 静态列,统一 zeroToDash(0/''/null 显示 '-');
|
||
* 3. boolean 字段 → 静态列,统一 yesNo(真→是,假→否);
|
||
* 4. array 字段 → 窗口展开列:per=窗口数,pair=children keys,表头第 1 行按窗口
|
||
* 输出 '{date}' 日期(Y/n/j 无前导零)并在每窗口 pair.length>1 时横向合并,
|
||
* 表头第 2 行按 children label 填充;子字段为 number 时单元格统一 zeroToDash;
|
||
* 5. 存在展开列时启用两行表头,其余列表头纵向合并(如 A1:A2)。
|
||
*/
|
||
|
||
/**
|
||
* 判断字段是否为普通对象。
|
||
* @param {*} field 待判断字段
|
||
* @return {boolean} 是否普通对象
|
||
*/
|
||
function isPlainObject(field) {
|
||
return Boolean(field) && typeof field === 'object' && !Array.isArray(field);
|
||
}
|
||
|
||
/**
|
||
* 规范化字段注册清单:仅保留含 key 的对象,key 统一为字符串。
|
||
* @param {Array<Object>} fields 字段清单(注册表 fields,按勾选顺序)
|
||
* @return {Array<Object>} 规范化字段清单
|
||
*/
|
||
function normalizeFields(fields) {
|
||
if (!Array.isArray(fields)) {
|
||
return [];
|
||
}
|
||
return fields
|
||
.filter(field => isPlainObject(field) && String(field.key || '').trim())
|
||
.map(field => ({ ...field, key: String(field.key).trim() }));
|
||
}
|
||
|
||
/**
|
||
* 按类型字段清单构建列计划(含窗口展开与表头合并)。
|
||
* @param {Object} [options={}] 构建选项
|
||
* @param {Array<Object>} [options.fields=[]] 字段注册清单(按勾选顺序)
|
||
* @param {Array<Object>} [options.windows=[]] 日窗口列表(展开列 per 取窗口数)
|
||
* @return {{columns: Array<Object>, headerRowCount: number, generatedMerges: Array<Object>}} 列计划
|
||
*/
|
||
function buildColumnPlan({ fields = [], windows = [] } = {}) {
|
||
const safeFields = normalizeFields(fields);
|
||
const safeWindows = Array.isArray(windows) ? windows : [];
|
||
const expandPer = Math.max(1, safeWindows.length || 7);
|
||
|
||
const columns = [];
|
||
const generatedMerges = [];
|
||
let expandStartColumn = -1;
|
||
|
||
for (const field of safeFields) {
|
||
if (field.type === 'array') {
|
||
const children = Array.isArray(field.children) ? field.children : [];
|
||
const pair = children.length > 0 ? children : [{ key: 'value', label: '值', type: 'string' }];
|
||
expandStartColumn = columns.length;
|
||
for (let windowIndex = 0; windowIndex < expandPer; windowIndex += 1) {
|
||
for (let pairIndex = 0; pairIndex < pair.length; pairIndex += 1) {
|
||
columns.push({
|
||
type: 'expand',
|
||
windowIndex,
|
||
pairIndex,
|
||
pair: pair[pairIndex] && isPlainObject(pair[pairIndex]) ? pair[pairIndex] : {},
|
||
headerRow1Template: '{date}',
|
||
headerRow2List: pair.map(child => String((child && child.label) || '')),
|
||
});
|
||
}
|
||
if (pair.length > 1) {
|
||
// 每窗口多列表头横向合并(如 D1:E1 … P1:Q1)
|
||
const groupStart = expandStartColumn + windowIndex * pair.length;
|
||
generatedMerges.push({
|
||
s: { r: 0, c: groupStart },
|
||
e: { r: 0, c: groupStart + pair.length - 1 },
|
||
});
|
||
}
|
||
}
|
||
continue;
|
||
}
|
||
columns.push({
|
||
type: field.type === 'array' ? 'static' : (field.type || 'string'),
|
||
header: String(field.label || field.key || ''),
|
||
field: field.key,
|
||
asText: field.asText === true,
|
||
});
|
||
}
|
||
|
||
const hasExpandColumn = columns.some(column => column.type === 'expand');
|
||
const headerRowCount = hasExpandColumn ? 2 : 1;
|
||
|
||
// 两行表头下,非展开列按列纵向合并(A1:A2 等)
|
||
if (headerRowCount === 2) {
|
||
columns.forEach((column, columnIndex) => {
|
||
if (column.type !== 'expand') {
|
||
generatedMerges.push({
|
||
s: { r: 0, c: columnIndex },
|
||
e: { r: 1, c: columnIndex },
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
return {
|
||
columns,
|
||
headerRowCount,
|
||
generatedMerges,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 构建表头第 1 行(展开列按窗口开始日期输出 'Y/n/j' 无前导零)。
|
||
* @param {Array<Object>} columns 列计划
|
||
* @param {Array<Object>} windows 日窗口列表
|
||
* @return {Array<string>} 表头第 1 行
|
||
*/
|
||
function buildHeaderRow1(columns, windows = []) {
|
||
return columns.map(column => {
|
||
if (column.type === 'expand') {
|
||
return formatWindowStart(windows, column.windowIndex, 'Y/n/j');
|
||
}
|
||
return column.header;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 构建表头第 2 行(单行表头模式下返回 null;展开列按 pair label 填充)。
|
||
* @param {Array<Object>} columns 列计划
|
||
* @param {number} headerRowCount 表头行数
|
||
* @return {Array<string>|null} 表头第 2 行
|
||
*/
|
||
function buildHeaderRow2(columns, headerRowCount) {
|
||
if (headerRowCount !== 2) {
|
||
return null;
|
||
}
|
||
return columns.map(column => {
|
||
if (column.type === 'expand') {
|
||
const list = column.headerRow2List;
|
||
return list.length > 0 ? list[column.pairIndex % list.length] || '' : '';
|
||
}
|
||
return '';
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 渲染单个单元格(按列类型与字段类型硬编码格式化口径)。
|
||
* @param {Object} column 列计划
|
||
* @param {Object} row 数据行
|
||
* @return {*} 单元格值
|
||
*/
|
||
function renderCell(column, row = {}) {
|
||
if (column.type === 'expand') {
|
||
const daily = Array.isArray(row.daily) ? row.daily : [];
|
||
const day = daily[column.windowIndex];
|
||
const value = day && typeof day === 'object' ? day[column.pair.key] : 0;
|
||
return applyFormat(column.pair.type === 'number' ? 'zeroToDash' : '', value);
|
||
}
|
||
|
||
const value = row[column.field];
|
||
if (column.type === 'boolean') {
|
||
return applyFormat('yesNo', value);
|
||
}
|
||
if (column.type === 'number') {
|
||
return applyFormat('zeroToDash', value);
|
||
}
|
||
return value;
|
||
}
|
||
|
||
/**
|
||
* 按类型字段注册清单渲染 sheet(配置极简:无 sheet_config 自定义列/合并/样式)。
|
||
*
|
||
* @param {Object} [options={}] 渲染选项
|
||
* @param {string} [options.type=''] 报表类型(查询器注册 key,仅透传不参与渲染口径)
|
||
* @param {string} [options.title='报表'] sheet 名称(报表标题)
|
||
* @param {Array<Object>} [options.fields=[]] 字段注册清单(按勾选顺序)
|
||
* @param {Array<Object>} [options.rows=[]] 查询器产出的数据行
|
||
* @param {Array<Object>} [options.windows=[]] 日窗口列表
|
||
* @return {Object} excel.js sheet 参数
|
||
* ({name, columns, rows, merges, styledRows},存在 asText 列时附带 textColumns)
|
||
*/
|
||
function renderSheetByType({ type = '', title = '报表', fields = [], rows = [], windows = [] } = {}) {
|
||
void type;
|
||
const plan = buildColumnPlan({ fields, windows });
|
||
const safeRows = Array.isArray(rows) ? rows : [];
|
||
|
||
const headerRow1 = buildHeaderRow1(plan.columns, windows);
|
||
const headerRow2 = buildHeaderRow2(plan.columns, plan.headerRowCount);
|
||
|
||
const aoaRows = [ headerRow1 ];
|
||
if (headerRow2) {
|
||
aoaRows.push(headerRow2);
|
||
}
|
||
for (const row of safeRows) {
|
||
aoaRows.push(plan.columns.map(column => renderCell(column, row)));
|
||
}
|
||
|
||
// asText 列收集为 0 基列索引,交由 excel.js 落地文本单元格(无 asText 时不输出)
|
||
const textColumns = plan.columns
|
||
.map((column, columnIndex) => (column.asText ? columnIndex : -1))
|
||
.filter(columnIndex => columnIndex >= 0);
|
||
|
||
const result = {
|
||
name: String(title || '报表'),
|
||
columns: [],
|
||
rows: aoaRows,
|
||
merges: plan.generatedMerges,
|
||
styledRows: [],
|
||
};
|
||
|
||
if (textColumns.length > 0) {
|
||
result.textColumns = textColumns;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
module.exports = {
|
||
renderSheetByType,
|
||
buildColumnPlan,
|
||
};
|