/** * 健康评估结果 Excel 下载:按模版列顺序组装行数据 * 等级枚举与 unitDetailData LEVEL_STATUS_MAP / HEALTH_STATUS 一致 */ import * as XLSX from "xlsx"; import { HEALTH_STATUS } from "../components/health/unitDetailData"; const NO_EVAL_NOTE = "(未达到评估标准)"; const NO_EVAL_DISPLAY = "/"; /** Sheet1 评分列:综合/系统/部件/结构 */ export const FARM_SCORE_COLS = [4, 5, 6, 7]; /** Sheet2 评分列(不含综合健康等级) */ export const TURBINE_SCORE_COLS = [ 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ]; /** Sheet2 综合健康等级列 */ export const TURBINE_LEVEL_COL = 7; const STATUS_KEY_TO_LEVEL = Object.keys(HEALTH_STATUS).reduce((acc, key) => { if (key === "none") return acc; const label = HEALTH_STATUS[key].label; acc[key] = label; acc[label] = label; return acc; }, {}); function cell(value) { if (value == null || value === "") return ""; return value; } function isNoEvalScore(score) { if (score == null || score === "") return false; const value = Number(score); return Number.isFinite(value) && value === -1; } /** 评分:-1 → 「/」,其余原样 */ function scoreCell(score) { if (score == null || score === "") return ""; if (isNoEvalScore(score)) return NO_EVAL_DISPLAY; return score; } /** * 综合健康等级 → 中文优/良/中/差 * 评分为 -1 时与评分一致展示「/」 */ function levelCell(level, score) { if (isNoEvalScore(score)) return NO_EVAL_DISPLAY; if (level == null || level === "") return ""; const key = String(level).trim(); return STATUS_KEY_TO_LEVEL[key] || STATUS_KEY_TO_LEVEL[key.toLowerCase()] || key; } /** * 若某列存在「/」(未达评估标准),表头追加说明 * @param {Object} sheet * @param {Array} dataRows * @param {number[]} colIndexes */ export function applyNoEvalHeaderNotes(sheet, dataRows, colIndexes) { if (!sheet || !Array.isArray(colIndexes)) return; const rows = Array.isArray(dataRows) ? dataRows : []; colIndexes.forEach((c) => { const hasNoEval = rows.some((row) => row && row[c] === NO_EVAL_DISPLAY); if (!hasNoEval) return; const ref = XLSX.utils.encode_cell({ r: 0, c }); const existing = sheet[ref]; const label = existing && existing.v != null ? String(existing.v) : ""; if (!label || label.includes("未达到评估标准")) return; sheet[ref] = { t: "s", v: `${label}${NO_EVAL_NOTE}` }; }); } /** * Sheet1「风场范围」一行 * @param {Object} windVo healthscoresWindVO */ export function buildFarmSheetRow(windVo = {}) { return [ cell(windVo.fieldCode || windVo.fieldId), cell(windVo.fieldName), cell(windVo.sourceDatetime), cell(windVo.totalCount), scoreCell(windVo.overallScore), scoreCell(windVo.systemScore), scoreCell(windVo.componentScore), scoreCell(windVo.structureScore), cell(windVo.excellentCount), cell(windVo.goodCount), cell(windVo.fairCount), cell(windVo.poorCount), cell(windVo.createTime), ]; } /** * Sheet2 风机明细多行 * @param {Array} list healthOverviewListVOList * @param {Object} windVo healthscoresWindVO(补风场编号/名称) */ export function buildTurbineSheetRows(list = [], windVo = {}) { const fieldCode = windVo.fieldCode || windVo.fieldId || ""; const fieldName = windVo.fieldName || ""; return (Array.isArray(list) ? list : []).map((item, index) => [ index + 1, cell(item.fieldId || fieldCode), cell(fieldName), cell(item.engineId || item.engineName), cell(item.machineTypeCode), cell(item.sourceDatetime || windVo.sourceDatetime), scoreCell(item.overallScore), levelCell(item.overallLevel, item.overallScore), scoreCell(item.systemScore), scoreCell(item.yawSystemScore), scoreCell(item.pitchSystemScore), scoreCell(item.controlSystemScore), scoreCell(item.hydraulicSystemScore), scoreCell(item.componentScore), scoreCell(item.generatorScore), scoreCell(item.converterScore), scoreCell(item.gearboxScore), scoreCell(item.mainShaftScore), scoreCell(item.structureScore), scoreCell(item.rotorScore), scoreCell(item.towerScore), cell(item.createTime), ]); }