<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>
|