<!doctype html>
|
<html lang="zh-CN">
|
<head>
|
<meta charset="UTF-8" />
|
<title>稳定性汇总报告</title>
|
<style>
|
* {
|
box-sizing: border-box;
|
margin: 0;
|
padding: 0;
|
}
|
|
body {
|
color: #333;
|
font-family: 'SimSun', 'Microsoft YaHei', sans-serif;
|
font-size: 4mm;
|
line-height: 1.35;
|
}
|
|
.report {
|
width: 100%;
|
margin-bottom: 8mm;
|
}
|
|
.report:last-child {
|
margin-bottom: 0;
|
}
|
|
.report-title {
|
margin-bottom: 5mm;
|
break-after: avoid;
|
font-size: 4.2mm;
|
font-weight: 700;
|
line-height: 1.45;
|
page-break-after: avoid;
|
text-align: center;
|
}
|
|
.result-table {
|
width: 100%;
|
border-collapse: collapse;
|
table-layout: fixed;
|
}
|
|
.result-table th,
|
.result-table td {
|
border: 0.2mm solid #ddd;
|
padding: 1.1mm 0.8mm;
|
text-align: center;
|
vertical-align: middle;
|
overflow-wrap: anywhere;
|
word-break: normal;
|
}
|
|
.result-table th {
|
height: 9mm;
|
font-weight: 700;
|
white-space: nowrap;
|
}
|
|
.result-table tbody tr {
|
break-inside: avoid;
|
page-break-inside: avoid;
|
}
|
|
.empty-state {
|
padding-top: 30mm;
|
color: #999;
|
text-align: center;
|
}
|
|
@media screen {
|
body {
|
background: #f0f0f0;
|
padding: 10mm 0;
|
}
|
|
#print-container {
|
width: 210mm;
|
min-height: 297mm;
|
margin: 0 auto;
|
padding: 15mm;
|
background: #fff;
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.12);
|
}
|
}
|
|
@media print {
|
@page {
|
size: A4 portrait;
|
margin: 15mm;
|
}
|
|
body {
|
background: #fff;
|
}
|
}
|
</style>
|
</head>
|
<body>
|
<main id="print-container"></main>
|
|
<script>
|
(function () {
|
function hasOwn(object, key) {
|
return Object.prototype.hasOwnProperty.call(object, key);
|
}
|
|
function firstValue() {
|
for (let index = 0; index < arguments.length; index++) {
|
const value = arguments[index];
|
if (value !== null && value !== undefined && value !== '') return value;
|
}
|
return '';
|
}
|
|
function escapeHtml(value) {
|
return String(value === null || value === undefined ? '' : value)
|
.replace(/&/g, '&')
|
.replace(/</g, '<')
|
.replace(/>/g, '>')
|
.replace(/"/g, '"')
|
.replace(/'/g, ''');
|
}
|
|
function toList(value) {
|
return Array.isArray(value) ? value : [];
|
}
|
|
function normalizePeriods(report, data) {
|
const source = firstValue(report.periods, report.months, report.columns, data.periods, data.months, data.columns);
|
return toList(source).map(function (period, index) {
|
if (period && typeof period === 'object') {
|
const month = firstValue(period.month, period.value, period.key, index);
|
return {
|
key: firstValue(period.key, period.field, month, index),
|
label: firstValue(period.label, period.title, String(month).endsWith('月') ? month : month + '月'),
|
month: month,
|
};
|
}
|
|
const text = String(period === null || period === undefined ? '' : period);
|
return {
|
key: period,
|
label: text.endsWith('月') ? text : text + '月',
|
month: period,
|
};
|
});
|
}
|
|
function getPeriodResult(row, period, index) {
|
const results = firstValue(row.results, row.values, row.data);
|
if (Array.isArray(results)) return results[index];
|
if (!results || typeof results !== 'object') return '';
|
|
const candidateKeys = [period.key, period.month, period.label, index];
|
for (let keyIndex = 0; keyIndex < candidateKeys.length; keyIndex++) {
|
const key = candidateKeys[keyIndex];
|
if (hasOwn(results, key)) return results[key];
|
}
|
return '';
|
}
|
|
function generateColgroup(columnCount) {
|
const width = 100 / columnCount;
|
let html = '<colgroup>';
|
for (let index = 0; index < columnCount; index++) {
|
html += '<col style="width:' + width + '%" />';
|
}
|
return html + '</colgroup>';
|
}
|
|
function generateRows(rows, periods) {
|
return rows
|
.map(function (row) {
|
let cells =
|
'<td>' +
|
escapeHtml(firstValue(row.item, row.name, row.inspectionItem, row.projectName)) +
|
'</td><td>' +
|
escapeHtml(firstValue(row.standard, row.specification, row.standardValue)) +
|
'</td>';
|
|
periods.forEach(function (period, index) {
|
cells += '<td>' + escapeHtml(getPeriodResult(row, period, index)) + '</td>';
|
});
|
return '<tr>' + cells + '</tr>';
|
})
|
.join('');
|
}
|
|
function generateReport(report, data) {
|
const periods = normalizePeriods(report, data);
|
const rows = toList(firstValue(report.rows, report.items, report.details));
|
const schemeName = firstValue(report.schemeName, report.title, data.schemeName, data.title, data.template_name);
|
const batchNo = firstValue(report.batchNo, report.batchNumber, report.pihao);
|
const headers = periods.map(function (period) {
|
return '<th>' + escapeHtml(period.label) + '</th>';
|
});
|
|
return (
|
'<section class="report">' +
|
'<h1 class="report-title">' +
|
escapeHtml(schemeName) +
|
' 实验结果—— 批号:' +
|
escapeHtml(batchNo) +
|
'</h1>' +
|
'<table class="result-table">' +
|
generateColgroup(periods.length + 2) +
|
'<thead><tr><th>检验项目</th><th>标准规定</th>' +
|
headers.join('') +
|
'</tr></thead><tbody>' +
|
generateRows(rows, periods) +
|
'</tbody></table></section>'
|
);
|
}
|
|
function render(data) {
|
const templateData = data || {};
|
const reports = toList(firstValue(templateData.reports, templateData.batches, templateData.list));
|
const container = document.getElementById('print-container');
|
|
if (!reports.length) {
|
container.innerHTML = '<div class="empty-state">暂无打印数据</div>';
|
return;
|
}
|
|
container.innerHTML = reports
|
.map(function (report) {
|
return generateReport(report || {}, templateData);
|
})
|
.join('');
|
}
|
|
window.renderStabilitySummary = render;
|
render(window.templateData || {});
|
})();
|
</script>
|
</body>
|
</html>
|