刘光辉
10 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!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, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#039;');
        }
 
        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>