yyb
13 小时以前 04b1a9cfde4049be9a38b9832d5289d4a192c883
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<template>
  <div class="app-container">
 
    <!-- 筛选条件 -->
    <div class="filter-section">
      <el-select v-model="deviceFilter" placeholder="设备状态筛选" clearable style="width: 200px; margin-right: 10px;">
        <el-option label="全部" value="all" />
        <el-option label="运行中" value="start" />
        <el-option label="停止运行" value="stop" />
      </el-select>
    </div>
 
    <!-- 设备启停记录表格 -->
    <el-card class="table-card">
      <template #header>
        <span>设备运行记录</span>
      </template>
      <el-table
        :data="filteredDeviceRecords"
        style="width: 100%"
        :header-cell-style="{ background: '#F0F1F5', color: '#333333' }"
        :row-class-name="getRowClassName"
        v-loading="loading"
      >
        <el-table-column
          align="center"
          label="序号"
          type="index"
          width="60"
        />
        <el-table-column
          label="设备名称"
          prop="deviceName"
          show-overflow-tooltip
        />
        <el-table-column
          label="规格型号"
          prop="deviceModel"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备状态"
          prop="status"
          width="150"
          align="center"
        >
          <template #default="scope">
            <!-- 超时未启动时显示警告 -->
            <el-tag
              v-if="isOverdue(scope.row)"
              type="warning"
              size="small"
              effect="dark"
            >
              <el-icon><Warning /></el-icon>
              超时未启动
            </el-tag>
            <!-- 正常状态时显示设备状态 -->
            <el-tag
              v-else
              :type="getDeviceStatusType(scope.row.status)"
              size="small"
            >
              <el-icon v-if="scope.row.status === '运行中'"><VideoPlay /></el-icon>
              <el-icon v-else><VideoPause /></el-icon>
              {{ scope.row.status || '未知' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="计划运行时间"
          prop="planRuntimeTime"
          width="150"
          align="center"
        >
          <template #default="scope">
            {{ scope.row.planRuntimeTime || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          label="开始运行时间"
          prop="startRuntimeTime"
          width="180"
          align="center"
        >
          <template #default="scope">
            {{ scope.row.startRuntimeTime || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          label="结束运行时间"
          prop="endRuntimeTime"
          width="180"
          align="center"
        >
          <template #default="scope">
            {{ scope.row.endRuntimeTime || '-' }}
          </template>
        </el-table-column>
        <el-table-column
          label="运行时长"
          prop="runtimeDuration"
          width="120"
          align="center"
        >
          <template #default="scope">
            {{ getRuntimeDurationDisplay(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="120"
          align="center"
        >
          <template #default="scope">
            <!-- 超时未启动时显示启动按钮 -->
            <el-button
              v-if="isOverdue(scope.row)"
              type="warning"
              size="small"
              @click="changeDeviceStatus(scope.row, '启动运行')"
            >
              <el-icon><VideoPlay /></el-icon>
              立即启动
            </el-button>
            <!-- 正常状态时显示对应的操作按钮 -->
            <template v-else>
              <el-button
                v-if="scope.row.status === '运行中'"
                type="danger"
                size="small"
                @click="changeDeviceStatus(scope.row, '停止运行')"
              >
                <el-icon><VideoPause /></el-icon>
                停止运行
              </el-button>
              <el-button
                v-else
                type="success"
                size="small"
                @click="changeDeviceStatus(scope.row, '启动运行')"
              >
                <el-icon><VideoPlay /></el-icon>
                启动运行
              </el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
 
 
  </div>
</template>
 
<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import dayjs from 'dayjs'
import { ElMessage } from 'element-plus'
import {
  VideoPlay,
  VideoPause,
  Warning
} from '@element-plus/icons-vue'
import {editLedger, getLedgerPage} from "@/api/equipmentManagement/ledger.js";
 
// 响应式数据
const deviceFilter = ref('all')
const loading = ref(false)
const total = ref(0)
const queryParams = ref({
  current: -1,
  size: -1
})
 
// 移除概览数据,因为现在使用表格展示
 
// 设备启停记录数据
const deviceRecords = ref([])
const allDeviceRecords = ref([]) // 存储所有原始数据
 
// 根据筛选条件过滤数据
const filteredDeviceRecords = computed(() => {
  let filtered = allDeviceRecords.value
 
  // 根据设备状态筛选
  if (deviceFilter.value !== 'all') {
    if (deviceFilter.value === 'start') {
      filtered = filtered.filter(device => device.status === '运行中')
    } else if (deviceFilter.value === 'stop') {
      filtered = filtered.filter(device => device.status === '停止运行')
    }
  }
 
  return filtered
})
 
// 运行中无结束时间时,运行时长需随当前时间变化,用 tick 触发模板重算
const runtimeDisplayTick = ref(0)
 
/** 取后端可能使用的开始/结束时间字段 */
const pickStartTime = (row) => row?.startRuntimeTime ?? row?.startTime ?? row?.start_time
const pickEndTime = (row) => row?.endRuntimeTime ?? row?.endTime ?? row?.end_time
 
/**
 * 解析接口/前端写入的各类时间:时间戳、ISO 字符串、yyyy-MM-dd HH:mm:ss、Jackson 数组 [y,M,d,h,m,s]、含中文的 toLocaleString 等
 */
const parseDeviceTime = (input) => {
  if (input === null || input === undefined || input === '') return null
  if (typeof input === 'number' && !Number.isNaN(input)) {
    const d = dayjs(input)
    return d.isValid() ? d.toDate() : null
  }
  if (Array.isArray(input)) {
    const [y, mo, day, h = 0, mi = 0, se = 0] = input
    if (y == null || y === '') return null
    const d = dayjs()
        .year(Number(y))
        .month(Number(mo || 1) - 1)
        .date(Number(day || 1))
        .hour(Number(h) || 0)
        .minute(Number(mi) || 0)
        .second(Number(se) || 0)
    return d.isValid() ? d.toDate() : null
  }
  const s = String(input).trim()
  if (!s || s === '-') return null
  let d = dayjs(s)
  if (d.isValid()) return d.toDate()
  d = dayjs(s.replace(/-/g, '/'))
  if (d.isValid()) return d.toDate()
  d = dayjs(s.replace(/\//g, '-'))
  if (d.isValid()) return d.toDate()
  return null
}
 
const formatDurationMs = (durationMs) => {
  if (durationMs == null || Number.isNaN(durationMs) || durationMs < 0) return '-'
  const hours = Math.floor(durationMs / (1000 * 60 * 60))
  const minutes = Math.floor((durationMs % (1000 * 60 * 60)) / (1000 * 60))
  if (hours === 0 && minutes === 0) return '不足1分钟'
  return `${hours}小时${minutes}分钟`
}
 
const hasMeaningfulEnd = (endRaw) =>
    endRaw !== null &&
    endRaw !== undefined &&
    String(endRaw).trim() !== '' &&
    String(endRaw).trim() !== '-'
 
const formatStoredDuration = (row) => {
  const rd = row?.runtimeDuration
  if (rd === null || rd === undefined) return ''
  const t = String(rd).trim()
  return t === '' || t === '-' ? '' : String(rd)
}
 
/** 运行中:始终用「当前时间 - 开始时间」;已停止:优先接口 runtimeDuration,否则用结束-开始;无结束可看已存时长或动态推算 */
const getRuntimeDurationDisplay = (row) => {
  void runtimeDisplayTick.value
  const start = parseDeviceTime(pickStartTime(row))
  if (!start) {
    return formatStoredDuration(row) || '-'
  }
 
  const statusStr = String(row?.status ?? '').trim()
  const isRunning = statusStr === '运行中' || statusStr === '1'
  const endRaw = pickEndTime(row)
  const hasEnd = hasMeaningfulEnd(endRaw)
 
  // 无结束时间:运行中一定动态算;已停止则优先展示后端已存时长,没有再按当前时间推算
  if (!hasEnd) {
    if (isRunning) return formatDurationMs(Date.now() - start.getTime())
    const stored = formatStoredDuration(row)
    if (stored) return stored
    return formatDurationMs(Date.now() - start.getTime())
  }
 
  if (isRunning) {
    return formatDurationMs(Date.now() - start.getTime())
  }
 
  const end = parseDeviceTime(endRaw)
  const stored = formatStoredDuration(row)
  if (stored) return stored
  if (end) return formatDurationMs(end.getTime() - start.getTime())
  return '-'
}
 
// 检查设备是否超时未启动
const isOverdue = (device) => {
  if (!device.planRuntimeTime || device.status === '运行中' || device.startRuntimeTime) {
    return false
  }
  
  const planTime = new Date(device.planRuntimeTime)
  const currentTime = new Date()
  
  return currentTime > planTime
}
 
// 方法
const getList = async () => {
  loading.value = true
  try {
    const response = await getLedgerPage(queryParams.value)
    if (response.code === 200) {
      allDeviceRecords.value = response.data.records || []
      total.value = response.data.total || 0
    }
  } catch (error) {
    console.error('获取设备列表失败:', error)
    ElMessage.error('获取设备列表失败')
  } finally {
    loading.value = false
  }
}
 
const changeDeviceStatus = async (device, status) => {
  try {
    const currentTime = new Date().toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
      hour12: false
    }).replace(/\//g, '-')
    
    // 更新设备状态和相关时间字段
    if (status === '启动运行') {
      device.status = '运行中'
      device.startRuntimeTime = currentTime
      device.endRuntimeTime = null // 清空结束时间
      device.runtimeDuration = null // 清空运行时长
    } else {
      device.status = '停止运行'
      device.endRuntimeTime = currentTime
      // 计算运行时长
      if (device.startRuntimeTime) {
        const startTime = parseDeviceTime(device.startRuntimeTime)
        const endTime = parseDeviceTime(currentTime)
        if (startTime && endTime) {
          device.runtimeDuration = formatDurationMs(endTime.getTime() - startTime.getTime())
        }
      }
    }
    const params = {
            id: device.id,
            status: device.status,
            planRuntimeTime: device.planRuntimeTime,
            startRuntimeTime: device.startRuntimeTime,
            endRuntimeTime: device.endRuntimeTime,
            runtimeDuration: device.runtimeDuration,
        }
    // 调用API更新设备状态
    const response = await editLedger(params)
    if (response.code === 200) {
      ElMessage.success(`${device.deviceName} ${status}成功`)
      // 刷新列表
      await getList()
    } else {
      ElMessage.error(response.msg || '操作失败')
    }
  } catch (error) {
    console.error('更新设备状态失败:', error)
    ElMessage.error('操作失败')
  }
}
 
const getDeviceStatusType = (status) => {
  if (status === '运行中') {
    return 'success'
  } else if (status === '停止运行') {
    return 'danger'
  } else {
    return 'info'
  }
}
 
// 获取表格行的类名
const getRowClassName = ({ row }) => {
  if (isOverdue(row)) {
    return 'overdue-row'
  }
  return ''
}
 
 
 
const POLL_MS = 60 * 1000
const RUNTIME_TICK_MS = 30 * 1000
let listPollTimer = null
let runtimeTickTimer = null
 
// 组件挂载时拉取数据,并每分钟刷新一次列表;运行中时长每 30 秒刷新显示
onMounted(() => {
  getList()
  listPollTimer = setInterval(() => {
    getList()
  }, POLL_MS)
  runtimeTickTimer = setInterval(() => {
    runtimeDisplayTick.value++
  }, RUNTIME_TICK_MS)
})
 
onUnmounted(() => {
  if (listPollTimer != null) {
    clearInterval(listPollTimer)
    listPollTimer = null
  }
  if (runtimeTickTimer != null) {
    clearInterval(runtimeTickTimer)
    runtimeTickTimer = null
  }
})
</script>
 
<style scoped>
.app-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: 100vh;
}
 
 
.filter-section {
  margin-bottom: 20px;
  padding: 15px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: flex-start;
}
 
.table-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
 
:deep(.el-card__header) {
  background: #f8f9fa;
  border-bottom: 1px solid #e9ecef;
  font-weight: 500;
  font-size: 16px;
}
 
:deep(.el-table .el-table__header-wrapper th) {
  background-color: #F0F1F5 !important;
  color: #333333;
  font-weight: 600;
}
 
:deep(.el-table .el-table__body-wrapper td) {
  padding: 12px 0;
}
 
:deep(.el-select) {
  width: 100%;
}
 
:deep(.el-tag) {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
 
/* 超时未启动行的样式 */
:deep(.overdue-row) {
  background-color: #fef0f0 !important;
  border-left: 4px solid #f56c6c;
}
 
:deep(.overdue-row:hover) {
  background-color: #fde2e2 !important;
}
 
:deep(.overdue-row td) {
  background-color: transparent !important;
}
</style>