Newer
Older
smartwell_front / src / views / home / operation / history / index.vue
<!--
  Description: 设备运维-设备报警记录
  Author: 李亚光
  Date: 2023-06-28
 -->
<script lang="ts" setup name="HistoryAlarm">
import dayjs from 'dayjs'
import { ElLoading, ElMessage } from 'element-plus'
import { shortcuts } from '@/utils/common'
import { getDictByCode } from '@/api/system/dict'
import { exportDeviceHistory, getDeviceHistoryListPage } from '@/api/home/operation/history'
import { getManufacturerListPage } from '@/api/home/device/product'
import { uniqueMultiArray } from '@/utils/Array'
import { getDeviceTypeListPage } from '@/api/home/device/type'
import { getAlarmTypeListPage } from '@/api/home/rule/alarm'
import { exportFile } from '@/utils/exportUtils'
const alarmTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 报警类型
const deviceTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 设备类型
const manufacturerList = ref<{ id: string; name: string; value: string }[]>([]) // 设备类型
// 表格数据
const list = ref([])
const total = ref(0)
// 初始展示列
const columns = ref<any>([
  { text: '设备编号', value: 'devCode', align: 'center' },
  { text: '报警类型', value: 'alarmType', align: 'center' },
  { text: '报警原因', value: 'alarmContent', align: 'center' },
  { text: '厂商', value: 'manufactureName', align: 'center' },
  { text: '设备类型', value: 'devTypeName', align: 'center' },
  { text: '位置', value: 'position', align: 'center' },
  { text: '管理单位', value: 'deptName', align: 'center' },
  { text: '报警时间', value: 'alarmTime', align: 'center', width: '110' },
  { text: '解除时间', value: 'cancelTime', align: 'center', width: '190' },
  { text: '解除时长', value: 'cancelDuration', align: 'center', width: '190' },
])
// 最终展示列
const columnsConfig = ref([])
// 修改列
const editColumns = (data: any) => {
  columnsConfig.value = data
}
const loadingTable = ref(true)
//  查询条件
const listQuery = ref({
  limit: 20,
  offset: 1,
  alarmTypeId: '',
  devTypeId: '',
  deptId: '',
  manufacturerId: '',
  position: '',
  devCode: '',
  begTime: '',
  endTime: '',
})
// 开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.value.begTime = ''
  listQuery.value.endTime = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.value.begTime = `${newVal[0]} 00:00:00`
      listQuery.value.endTime = `${newVal[1]} 23:59:59`
    }
  }
})
// 查询数据
const fetchData = () => {
  loadingTable.value = true
  getDeviceHistoryListPage(listQuery.value).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
// 重置查询条件f
const reset = () => {
  datetimerange.value = []
  listQuery.value = {
    limit: 20,
    offset: 1,
    alarmTypeId: '',
    devTypeId: '',
    deptId: '',
    manufacturerId: '',
    position: '',
    devCode: '',
    begTime: '',
    endTime: '',
  }
  fetchData()
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData()
}
// 查看
const $router = useRouter()
const detailRow = (row: any) => {
  $router.push({
    name: 'operationHistoryDetail',
    query: {
      id: row.id,
      type: 'history',
      row: JSON.stringify(row),
    },
  })
}
// 导出列表
const exportList = () => {
  if (!list.value.length) {
    ElMessage.warning('暂无导出数据')
    return
  }
  const loading = ElLoading.service({
    lock: true,
    background: 'rgba(255, 255, 255, 0.8)',
  })
  exportDeviceHistory(listQuery.value).then((res) => {
    exportFile(res.data, '历史报警列表(设备).xlsx')
    loading.close()
  }).catch(() => {
    loading.close()
  })
}
// 字典
const fetchDict = () => {
  // 报警类型
  getAlarmTypeListPage({ offset: 1, limit: 99999 }).then((res) => {
    alarmTypeList.value = uniqueMultiArray(res.data.rows.map((item: any) => ({
      name: item.alarmType,
      value: item.id,
      id: item.id,
    })), 'name')
  })
  // 设备类型
  getDeviceTypeListPage({ offset: 1, limit: 99999 }).then((res) => {
    deviceTypeList.value = res.data.rows.map((item: any) => ({
      name: item.typeName,
      value: item.id,
      id: item.id,
    }))
  })
  // 厂商列表
  getManufacturerListPage().then((res) => {
    manufacturerList.value = res.data.rows.map((item: any) => ({
      name: item.name || '',
      id: item.id,
      value: item.id,
    }))
  })
}
fetchDict()
onMounted(() => {
  datetimerange.value = [dayjs().subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss'), dayjs().format('YYYY-MM-DD HH:mm:ss')]
  setTimeout(() => {
    fetchData()
  })
})
const { proxy } = getCurrentInstance() as any
</script>

<template>
  <!-- 布局 -->
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="fetchData" @clear="reset">
      <search-item>
        <el-select v-model="listQuery.alarmTypeId" placeholder="报警类型" clearable class="select" style="width: 192px;">
          <el-option v-for="item in alarmTypeList" :key="item.id" :value="item.value" :label="item.name" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.devTypeId" placeholder="设备类型" clearable class="select" style="width: 192px;">
          <el-option v-for="item in deviceTypeList" :key="item.id" :value="item.value" :label="item.name" />
        </el-select>
      </search-item>
      <!-- <search-item>
        <el-select v-model="listQuery.keywords" placeholder="报警状态" clearable class="select" style="width: 192px;" />
      </search-item> -->
      <search-item>
        <dept-select
          v-model="listQuery.deptId" placeholder="管理单位" :clearable="true" class="select"
          style="width: 192px;"
        />
      </search-item>
      <search-item>
        <el-select
          v-model="listQuery.manufacturerId" placeholder="厂商" clearable filterable class="select"
          style="width: 192px;"
        >
          <el-option v-for="item in manufacturerList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.position" placeholder="位置" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.devCode" placeholder="设备编号" clearable />
      </search-item>

      <search-item>
        <el-date-picker
          v-model="datetimerange" type="datetimerange" format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts"
          value-format="YYYY-MM-DD HH:mm:ss" range-separator="至" start-placeholder="报警开始时间" end-placeholder="报警结束时间"
          clearable
        />
      </search-item>
    </search-area>
    <!-- 表头标题 -->
    <table-container
      :is-config="true" config-title="history-alarm" :columns="columns" :config-columns="columnsConfig"
      :edit="editColumns"
    >
      <template #btns-right>
        <!-- 操作 -->
        <div>
          <el-button v-if="proxy.hasPerm('/operation/history/export')" type="primary" @click="exportList">
            导出
          </el-button>
        </div>
      </template>
      <!-- 查询结果Table显示 -->
      <normal-table
        :data="list" :total="total" :columns="columnsConfig" :query="listQuery" :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <!-- <el-table-column label="选择" width="55" align="center">
              <template #default="scope">
                <el-radio v-model="select" :label="scope.$index + 1" class="radio" />
              </template>
  </el-table-column> -->
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column v-if="proxy.hasPerm('/operation/history/detail')" label="操作" align="center" width="80">
            <template #default="scope">
              <el-button type="primary" link size="small" @click="detailRow(scope.row)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>