快乐的昕的电脑
16 小时以前 df499252aa425d30cd9ae4697e8f6ceeacb62098
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<template>
    <view class="page">
 
        <!-- 操作按钮 -->
        <view class="button-row">
            <button class="save-btn" @click="handleRefresh" :disabled="loadingForm || submitting">刷新</button>
        </view>
 
        <!-- 刀具使用记录表格 -->
        <view class="table-section">
            <table class="styled-table">
                <thead>
                    <tr>
                        <th style="width:7%">刀具编号</th>
                        <th style="width:12%">刀具名称</th>
                        <th style="width:10%">上刀时间</th>
                        <th class="num" style="width:7%">上刀计数</th>
                        <th style="width:10%">下刀时间</th>
                        <th class="num" style="width:7%">下刀计数</th>
                        <th class="num" style="width:7%">使用次数</th>
                        <th class="num" style="width:7%">使用上限</th>
                        <th class="num" style="width:7%">寿命比%</th>
                        <th class="num" style="width:7%">寿命比预警值</th>
                        <th style="width:7%">预警状态</th>
                        <th style="width:7%">刀具在机状态</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, idx) in toolRecords" :key="item.id" :class="{'row-odd': idx % 2 === 0}">
                        <td>{{ item.no }}</td>
                        <td class="left">{{ item.name }}</td>
                        <td>{{ item.upTime }}</td>
                        <td class="num">{{ item.upCount != null ? item.upCount : '' }}</td>
                        <td>{{ item.downTime }}</td>
                        <!-- 只有第一行显示 currentCjNum,其它行显示 downCount -->
                        <td class="num">
                            <template v-if="idx === 0">
                                <!-- 第一行:下机时不实时显示 currentCjNum,显示 downCount -->
                                <template v-if="!item.downTime">
                                    {{ item.currentCjNum != null ? item.currentCjNum : '' }}
                                </template>
                                <template v-else>
                                    {{ item.downCount != null ? item.downCount : '' }}
                                </template>
                            </template>
                            <template v-else>
                                {{ item.downCount != null ? item.downCount : '' }}
                            </template>
                        </td>
                        <!--使用次数-->
                        <td class="num">
                            <template v-if="idx === 0">
                                <!-- 第一行实时计算使用次数,若为下机则不实时显示 -->
                                <template v-if="!item.downTime">
                                    {{item.currentCjNum != null && item.upCount != null ? (Number(item.currentCjNum) - Number(item.upCount)) : (item.useCount != null ? item.useCount : '') }}
                                </template>
                                <template v-else>
                                    {{ item.useCount != null ? item.useCount : '' }}
                                </template>
                            </template>
                            <template v-else>
                                {{ item.useCount != null ? item.useCount : '' }}
                            </template>
                        </td>
                        <td class="num">{{ item.useLimit != null ? item.useLimit : '' }}</td>
                        <!--寿命比%-->
                        <td class="num">
                            <template v-if="idx === 0">
                                <!-- 第一行实时计算寿命比%,若为下机则不实时显示 -->
                                <template v-if="!item.downTime">
                                    {{item.currentCjNum != null && item.upCount != null && item.useLimit != null && Number(item.useLimit) > 0 ? Math.round((Number(item.currentCjNum) - Number(item.upCount)) / Number(item.useLimit) * 100) + '%' : (item.lifePercent != null ? item.lifePercent : '') }}
                                </template>
                                <template v-else>
                                    {{ item.lifePercent != null ? item.lifePercent : '' }}
                                </template>
                            </template>
                            <template v-else>
                                {{ item.lifePercent }}
                            </template>
                        </td>
                        <td class="num">{{ item.lifeWarn }}</td>
                        <td :class="item.warnStatus === '预警' ? 'warn-cell' : (item.warnStatus === '正常' ? 'ok-cell' : '')">
                            <span v-if="item.warnStatus === '预警'" class="warn-badge">警告</span>
                            <span v-else>{{ item.warnStatus }}</span>
                        </td>
                        <td>
                            {{ item.downTime ? '下机' : '在机' }}
                        </td>
                    </tr>
                    <tr v-if="!toolRecords.length">
                        <td colspan="12">暂无数据</td>
                    </tr>
                </tbody>
            </table>
        </view>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                machineNo: '',//机台编码
                workOrderNo: '',//工单号
                selectedToolNo: '',
                toolName: '',
                useLimitInput: '',
                lifeWarnInput: '', // 寿命比预警值原始输入
                toolRecords: [],
                loadingForm: false,
                submitting: false,
                workOrderCurrentCjNum: null, // 工单当前数采
            };
        },
        methods: {
            // 刷新按钮处理方法
            async handleRefresh() {
                if (this.machineNo && this.workOrderNo) {
                    await this.fetchFormData();
                    await this.fetchDefaultToolFromWorkOrder();
                    // 刷新后赋值第一行 currentCjNum
                    if (this.toolRecords.length > 0) {
                        this.$set(this.toolRecords[0], 'currentCjNum', this.workOrderCurrentCjNum);
                    }
                }
                this.$showMessage('刷新完成');
            },
 
            // 自动带出工单刀具信息,并获取工单最新采集数
            async fetchDefaultToolFromWorkOrder() {
                if (!this.machineNo) return;
                try {
                    const res = await this.$post({
                        url: '/Womdaa/GetWomdaasByShow',
                        data: JSON.stringify({ machineNo: this.machineNo }),
                        headers: { 'Content-Type': 'application/json' }
                    });
                    if (res.status === 0 && Array.isArray(res.data?.tbBillList) && res.data.tbBillList.length > 0) {
                        const order = res.data.tbBillList[0];
                        this.selectedToolNo = order.cutterId || order.cutteR_ID || '';
                        this.toolName = order.cutterName || order.cutteR_NAME || '';
                        // 获取工单最新采集数
                        this.workOrderCurrentCjNum = order.CurrentCjNum ?? order.currentCjNum ?? null;
                        // 自动填充寿命比预警值
                        if (order.modlLifeWorning !== undefined && order.modlLifeWorning !== null) {
                            const warn = Number(order.modlLifeWorning);
                            this.lifeWarnInput = warn <= 1 ? (warn * 100).toFixed(0) : warn.toFixed(0);
                        } else {
                            this.lifeWarnInput = '';
                        }
                    } else {
                        this.workOrderCurrentCjNum = null;
                    }
                } catch (e) {
                    console.warn('自动带出工单刀具失败', e);
                    this.workOrderCurrentCjNum = null;
                }
            },
 
            async fetchFormData() {
                if (!this.workOrderNo || !this.machineNo) {
                    console.warn('工单号或机台号为空,跳过获取表单数据');
                    return;
                }
                this.loadingForm = true;
                const payload = {
                    workOrderNo: this.workOrderNo.trim(),
                    machineNo: this.machineNo.trim()
                };
                try {
                    const res = await this.$post({
                        url: '/MesCutterLedger/GetFormData',
                        data: JSON.stringify(payload),
                        headers: { 'Content-Type': 'application/json' }
                    });
                    if (res.status !== 0) {
                        this.$showMessage(res.message || '获取表单数据失败');
                        this.toolRecords = [];
                        return;
                    }
                    const list = Array.isArray(res.data) ? res.data
                        : (res.data && res.data.tbBillList) ? res.data.tbBillList
                            : (res.data && res.data.data) ? res.data.data
                                : [];
 
                    const getField = (obj, ...keys) => {
                        for (const k of keys) if (obj?.[k] !== undefined && obj?.[k] !== null) return obj[k];
                        return null;
                    };
 
                    const mapped = (list || []).map(t => {
                        const upTimeRaw = getField(t, 'uP_TIME', 'UP_TIME', 'uPTime', 'UPTIME', 'UpTime');
                        const downTimeRaw = getField(t, 'dowN_TIME', 'DOWN_TIME', 'downTime', 'DOWNTIME');
                        const lifeWarnRaw = getField(t, 'lifE_WARN', 'LIFE_WARN', 'lifeWarn', 'LIFEWARN');
                        const currentCjNum = getField(t, 'currentCjNum', 'CURRENTCJNUM', 'CurrentCjNum');
 
                        const useCount = getField(t, 'usE_COUNT', 'USE_COUNT', 'useCount');
                        const useLimit = getField(t, 'usE_LIMIT', 'USE_LIMIT', 'useLimit');
 
                        let percent = '';
                        if (useCount != null && useLimit != null && !isNaN(useCount) && !isNaN(useLimit) && Number(useLimit) > 0) {
                            percent = ((Number(useCount) / Number(useLimit)) * 100).toFixed(0) + '%';
                        }
 
                        const parseNumber = v => {
                            if (v === null || v === undefined || v === '') return null;
                            const s = String(v).replace(/[,%%]/g, '').trim();
                            const n = parseFloat(s);
                            return Number.isFinite(n) ? n : null;
                        };
                        const formatPercent = n => {
                            if (n === null || n === undefined || isNaN(n)) return '';
                            if (n <= 1) return `${(n * 100).toFixed(0)}%`;
                            return `${Number(n).toFixed(0)}%`;
                        };
                        const lifeWarnNum = parseNumber(lifeWarnRaw);
 
                        let warnStatus = getField(t, 'status', 'STATUS') || '';
                        if (lifeWarnNum !== null && useCount != null && useLimit != null && !isNaN(useCount) && !isNaN(useLimit) && Number(useLimit) > 0) {
                            const percentNum = Number(useCount) / Number(useLimit);
                            warnStatus = (percentNum >= lifeWarnNum) ? '预警' : '正常';
                        } else {
                            warnStatus = warnStatus || '未知';
                        }
 
                        return {
                            id: getField(t, 'id', 'ID') || `${getField(t, 'cutteR_ID') || getField(t, 'CUTTER_ID') || ''}-${upTimeRaw || ''}`,
                            no: getField(t, 'cutteR_ID', 'CUTTER_ID', 'cutterId', 'no') || '',
                            name: getField(t, 'cutteR_NAME', 'CUTTER_NAME', 'cutterName', 'name') || '',
                            upTime: this.formatDateTime(upTimeRaw),
                            upCount: getField(t, 'uP_COUNT', 'UP_COUNT', 'upCount') ?? '',
                            downTime: this.formatDateTime(downTimeRaw),
                            downCount: getField(t, 'dowN_COUNT', 'DOWN_COUNT', 'downCount') ?? '',
                            useCount: useCount ?? '',
                            useLimit: useLimit ?? '',
                            lifePercent: percent,
                            lifeWarn: formatPercent(lifeWarnNum),
                            warnStatus,
                            currentCjNum
                        };
                    });
 
                    // 按上刀时间降序排序(越晚的越上面)
                    mapped.sort((a, b) => {
                        const parse = s => {
                            if (!s) return 0;
                            const year = new Date().getFullYear();
                            return new Date(`${year}-${s.replace(/-/g, '-')}:00`).getTime();
                        };
                        return parse(b.upTime) - parse(a.upTime);
                    });
 
                    this.toolRecords = mapped;
                } catch (error) {
                    console.error('获取表单数据错误:', error);
                    this.$showMessage('获取数据失败,请检查网络连接');
                    this.toolRecords = [];
                } finally {
                    this.loadingForm = false;
                }
            },
 
            formatDateTime(dateTimeStr) {
                if (!dateTimeStr) return '';
                try {
                    const s = String(dateTimeStr).trim();
                    if (/^\d{10}$/.test(s)) {
                        const d = new Date(Number(s) * 1000);
                        return `${d.getMonth() + 1}-${d.getDate()} ${d.getHours()}:${String(d.getMinutes()).padStart(2, '0')}`;
                    }
                    if (/^\d{13}$/.test(s)) {
                        const d = new Date(Number(s));
                        return `${d.getMonth() + 1}-${d.getDate()} ${d.getHours()}:${String(d.getMinutes()).padStart(2, '0')}`;
                    }
                    const date = new Date(dateTimeStr);
                    if (!isNaN(date.getTime())) {
                        return `${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`;
                    }
                    const match = String(dateTimeStr).match(/(\d{1,4}[-\/]\d{1,2}[-\/]\d{1,2}).*?(\d{1,2}:\d{2})/);
                    if (match) return `${match[1].replace(/-/g, '/').replace(/^\d{4}\//, (m) => m)} ${match[2]}`;
                    return String(dateTimeStr);
                } catch {
                    return String(dateTimeStr);
                }
            }
        },
        mounted() {
            this.machineNo = uni.getStorageSync('machineNo') || '';
            this.workOrderNo = uni.getStorageSync('daa001') || '';
 
            if (this.machineNo && this.workOrderNo) {
                this.fetchFormData().then(async () => {
                    await this.fetchDefaultToolFromWorkOrder();
                    // 进入页面时赋值第一行 currentCjNum
                    if (this.toolRecords.length > 0) {
                        this.$set(this.toolRecords[0], 'currentCjNum', this.workOrderCurrentCjNum);
                    }
                });
            } else {
                console.warn('机台号或工单号为空,无法获取表单数据');
            }
        }
    };
</script>
 
<style scoped>
    .page {
        padding: 20px;
    }
 
    .button-row {
        display: flex;
        justify-content: center;
        gap: 32px;
        margin: 2vh 0;
    }
 
    .save-btn, .cancel-btn {
        width: 28%;
        padding: 1.5vh;
        background-color: #00A2E9;
        color: white;
        border: none;
        text-align: center;
        border-radius: 4px;
        transition: all 0.3s;
        font-size: 22px;
        font-weight: 500;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
 
    .cancel-btn {
        background-color: #f5f5f5;
        color: #333;
    }
 
    .save-btn:hover {
        background-color: #40a9ff;
    }
 
    .save-btn:active {
        background-color: #096dd9;
    }
 
    .save-btn:disabled, .cancel-btn:disabled {
        opacity: 0.6;
        cursor: not-allowed;
    }
 
    .table-section {
        display: flex;
        justify-content: center;
        margin: 1vh 0;
        overflow-x: auto;
        width: 100%;
    }
 
    table.styled-table {
        max-width: 1800px;
        width: 98vw;
        margin: 0 auto;
        border-collapse: separate;
        border-spacing: 0;
        border: 2px solid #bfbfbf;
        background: #fff;
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 2px 12px rgba(0,0,0,0.06);
    }
 
        table.styled-table thead th {
            background: #fafafa;
            border-bottom: 2px solid #bfbfbf;
            padding: 16px 10px;
            font-weight: bold;
            text-align: center;
            font-size: 22px;
        }
 
        table.styled-table tbody td {
            border-bottom: 1px solid #e8e8e8;
            padding: 14px 10px;
            vertical-align: middle;
            text-align: center;
            font-size: 22px;
        }
 
    .table-section table th:first-child, .table-section table td:first-child {
        border-left: 2px solid #bfbfbf;
    }
 
    .table-section table th:last-child, .table-section table td:last-child {
        border-right: 2px solid #bfbfbf;
    }
 
    .row-odd {
        background: #fff;
    }
 
        .row-odd + tr {
            background: #fafafa;
        }
 
    .num {
        text-align: center;
        padding-right: 0;
        font-variant-numeric: tabular-nums;
    }
 
    .left {
        text-align: left;
        padding-left: 8px;
    }
 
    .warn-cell {
        color: #d93025;
        font-weight: bold;
    }
 
    .ok-cell {
        color: #333;
    }
 
    .warn-badge {
        display: inline-block;
        background: #ff4d4f;
        color: #fff;
        padding: 2px 6px;
        border-radius: 3px;
        font-weight: bold;
    }
 
    /* 响应式调整 */
    @media (max-width: 1200px) {
        .save-btn, .cancel-btn {
            width: 40%;
        }
    }
</style>