healthDownloadMapper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * 健康评估结果 Excel 下载:按模版列顺序组装行数据
  3. * 等级枚举与 unitDetailData LEVEL_STATUS_MAP / HEALTH_STATUS 一致
  4. */
  5. import * as XLSX from "xlsx";
  6. import { HEALTH_STATUS } from "../components/health/unitDetailData";
  7. const NO_EVAL_NOTE = "(未达到评估标准)";
  8. const NO_EVAL_DISPLAY = "/";
  9. /** Sheet1 评分列:综合/系统/部件/结构 */
  10. export const FARM_SCORE_COLS = [4, 5, 6, 7];
  11. /** Sheet2 评分列(不含综合健康等级) */
  12. export const TURBINE_SCORE_COLS = [
  13. 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  14. ];
  15. /** Sheet2 综合健康等级列 */
  16. export const TURBINE_LEVEL_COL = 7;
  17. const STATUS_KEY_TO_LEVEL = Object.keys(HEALTH_STATUS).reduce((acc, key) => {
  18. if (key === "none") return acc;
  19. const label = HEALTH_STATUS[key].label;
  20. acc[key] = label;
  21. acc[label] = label;
  22. return acc;
  23. }, {});
  24. function cell(value) {
  25. if (value == null || value === "") return "";
  26. return value;
  27. }
  28. function isNoEvalScore(score) {
  29. if (score == null || score === "") return false;
  30. const value = Number(score);
  31. return Number.isFinite(value) && value === -1;
  32. }
  33. /** 评分:-1 → 「/」,其余原样 */
  34. function scoreCell(score) {
  35. if (score == null || score === "") return "";
  36. if (isNoEvalScore(score)) return NO_EVAL_DISPLAY;
  37. return score;
  38. }
  39. /**
  40. * 综合健康等级 → 中文优/良/中/差
  41. * 评分为 -1 时与评分一致展示「/」
  42. */
  43. function levelCell(level, score) {
  44. if (isNoEvalScore(score)) return NO_EVAL_DISPLAY;
  45. if (level == null || level === "") return "";
  46. const key = String(level).trim();
  47. return STATUS_KEY_TO_LEVEL[key] || STATUS_KEY_TO_LEVEL[key.toLowerCase()] || key;
  48. }
  49. /**
  50. * 若某列存在「/」(未达评估标准),表头追加说明
  51. * @param {Object} sheet
  52. * @param {Array<Array>} dataRows
  53. * @param {number[]} colIndexes
  54. */
  55. export function applyNoEvalHeaderNotes(sheet, dataRows, colIndexes) {
  56. if (!sheet || !Array.isArray(colIndexes)) return;
  57. const rows = Array.isArray(dataRows) ? dataRows : [];
  58. colIndexes.forEach((c) => {
  59. const hasNoEval = rows.some((row) => row && row[c] === NO_EVAL_DISPLAY);
  60. if (!hasNoEval) return;
  61. const ref = XLSX.utils.encode_cell({ r: 0, c });
  62. const existing = sheet[ref];
  63. const label = existing && existing.v != null ? String(existing.v) : "";
  64. if (!label || label.includes("未达到评估标准")) return;
  65. sheet[ref] = { t: "s", v: `${label}${NO_EVAL_NOTE}` };
  66. });
  67. }
  68. /**
  69. * Sheet1「风场范围」一行
  70. * @param {Object} windVo healthscoresWindVO
  71. */
  72. export function buildFarmSheetRow(windVo = {}) {
  73. return [
  74. cell(windVo.fieldCode || windVo.fieldId),
  75. cell(windVo.fieldName),
  76. cell(windVo.sourceDatetime),
  77. cell(windVo.totalCount),
  78. scoreCell(windVo.overallScore),
  79. scoreCell(windVo.systemScore),
  80. scoreCell(windVo.componentScore),
  81. scoreCell(windVo.structureScore),
  82. cell(windVo.excellentCount),
  83. cell(windVo.goodCount),
  84. cell(windVo.fairCount),
  85. cell(windVo.poorCount),
  86. cell(windVo.createTime),
  87. ];
  88. }
  89. /**
  90. * Sheet2 风机明细多行
  91. * @param {Array} list healthOverviewListVOList
  92. * @param {Object} windVo healthscoresWindVO(补风场编号/名称)
  93. */
  94. export function buildTurbineSheetRows(list = [], windVo = {}) {
  95. const fieldCode = windVo.fieldCode || windVo.fieldId || "";
  96. const fieldName = windVo.fieldName || "";
  97. return (Array.isArray(list) ? list : []).map((item, index) => [
  98. index + 1,
  99. cell(item.fieldId || fieldCode),
  100. cell(fieldName),
  101. cell(item.engineId || item.engineName),
  102. cell(item.machineTypeCode),
  103. cell(item.sourceDatetime || windVo.sourceDatetime),
  104. scoreCell(item.overallScore),
  105. levelCell(item.overallLevel, item.overallScore),
  106. scoreCell(item.systemScore),
  107. scoreCell(item.yawSystemScore),
  108. scoreCell(item.pitchSystemScore),
  109. scoreCell(item.controlSystemScore),
  110. scoreCell(item.hydraulicSystemScore),
  111. scoreCell(item.componentScore),
  112. scoreCell(item.generatorScore),
  113. scoreCell(item.converterScore),
  114. scoreCell(item.gearboxScore),
  115. scoreCell(item.mainShaftScore),
  116. scoreCell(item.structureScore),
  117. scoreCell(item.rotorScore),
  118. scoreCell(item.towerScore),
  119. cell(item.createTime),
  120. ]);
  121. }