Newer
Older
sensorHubPlusFront / src / views / data / query / list.vue
liyaguang 8 days ago 11 KB 页面搭建
<!-- 设备列表 -->
<script name="DataQueryList" lang="ts" setup>
import type { DateModelType } from 'element-plus'
import { ElLoading, ElMessage, dayjs } from 'element-plus'
import type { IBizDataInfo, IListQuery } from './data-query'
import DataDetailDialog from './dataDetailDialog.vue'
import type { IDictType } from '@/views/basic/common-interface'
import type { IGroupInfo } from '@/views/basic/group/group-info'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import { exportFile } from '@/utils/exportUtils'
import { getGroupList } from '@/api/basic/group'
import { exportDeviceData, getDeviceBizDataListPage } from '@/api/data/query'

const props = defineProps({
  embedded: {
    type: Boolean,
    default: false,
  },
  devCode: {
    type: String,
    default: '',
  },
})

const refDataDialog = ref()

// 分组列表
const groupList = ref<Array<IGroupInfo>>([])

// 查询条件
const searchQuery = ref<IListQuery>({
  groupId: '', // 分组
  devCode: props.devCode === undefined ? '' : props.devCode,
  deviceType: '',
  bizType: '',
  beginTime: '',
  endTime: '',
  offset: 1,
  limit: 20,
})
const dateRangeLog = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const dateRangeUp = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading

// 表头
const columns = ref<TableColumn[]>(props.embedded ? [
  // { text: '分组', value: 'groupName', align: 'center', width: '180' },
  { text: '设备编号', value: 'devcode', align: 'center' },
  { text: '设备类型', value: 'deviceTypeName', align: 'center' },
  { text: '电量(%)', value: 'cell', align: 'center', width: '80' },
  { text: '业务类型', value: 'bizTypeName', align: 'center' },
  { text: '值', value: 'value', align: 'center' },
  // { text: '单位', value: 'unit', align: 'center', width: '100' },
  { text: '采集时间', value: 'uptime', align: 'center' },
  { text: '上报时间', value: 'logtime', align: 'center' },
  // { text: '设备状态', value: 'status', align: 'center', width: '90' },
] : [
  { text: '分组', value: 'groupName', align: 'center', width: '180' },
  { text: '设备编号', value: 'devcode', align: 'center', width: '140' },
  { text: '设备类型', value: 'deviceTypeName', align: 'center', width: '200' },
  { text: '电量(%)', value: 'cell', align: 'center', width: '80' },
  { text: '业务类型', value: 'bizTypeName', align: 'center', width: '140' },
  { text: '值', value: 'value', align: 'center' },
  { text: '单位', value: 'unit', align: 'center', width: '100' },
  { text: '采集时间', value: 'uptime', align: 'center', width: '180' },
  { text: '上报时间', value: 'logtime', align: 'center', width: '180' },
  { text: '设备状态', value: 'status', align: 'center', width: '90' },
])
const dataList = ref<Array<IBizDataInfo>>([]) // 表格数据

const statusDict = ref<Array<IDictType>>([])
const deviceTypeDict = ref<Array<IDictType>>([])
const bizTypeDict = ref<Array<IDictType>>([])

// 逻辑

// 导出
const exportDeviceDataList = () => {
  if (dataList.value.length > 0) {
    const filename = `设备数据列表${new Date().valueOf()}.xlsx`

    const loading = ElLoading.service({
      lock: true,
      text: '下载中请稍后',
      background: 'rgba(255, 255, 255, 0.8)',
    })

    // 导出接口
    exportDeviceData(searchQuery.value).then((res) => {
      const blob = new Blob([res.data])
      exportFile(blob, filename)

      nextTick(() => {
        // 关闭loading
        loading.close()
      })
    }).catch(() => {
      loading.close()
    })
  }
  else {
    ElMessage.warning('无数据可导出数据')
  }
}

// 详情
const detail = (row: IBizDataInfo) => {
  refDataDialog.value.initDialog(row)
}

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    searchQuery.value.offset = 1
  }
  getDeviceBizDataListPage(searchQuery.value).then((response) => {
    if (response.code === 200) {
      dataList.value = response.data.rows.map((item: IBizDataInfo) => {
        return {
          ...item,
        }
      })
      total.value = parseInt(response.data.total)
    }
    calcTableHeight()
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}

const searchList = () => {
  fetchData(true)
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.value.limit = val.size
  }
  if (val && val.page) {
    searchQuery.value.offset = val.page
  }
  fetchData(true)
}

// 重置
const reset = () => {
  searchQuery.value = {
    groupId: '', // 分组
    devCode: props.devCode,
    deviceType: '',
    bizType: '',
    beginTime: '',
    endTime: '',
    offset: 1,
    limit: 20,
  }
  dateRangeUp.value = ['', '']
  fetchData(true)
}

// 获取在用状态字典值
const getStatusDict = async () => {
  getDictByCode('onlineStatus').then((res: any) => {
    if (res.code === 200) {
      statusDict.value = res.data
      sessionStorage.setItem('onlineStatus', JSON.stringify(statusDict.value))
    }
  })
}
const getDeviceTypeDict = async () => {
  getDictByCode('deviceType').then((res: any) => {
    if (res.code === 200) {
      deviceTypeDict.value = res.data
      sessionStorage.setItem('deviceType', JSON.stringify(deviceTypeDict.value))
    }
  })
}
const getBizTypeDict = async () => {
  getDictByCode('bizType').then((res: any) => {
    if (res.code === 200) {
      bizTypeDict.value = res.data
      sessionStorage.setItem('bizType', JSON.stringify(bizTypeDict.value))
    }
  })
}
const getAllGroupList = async () => {
  await getGroupList({}).then((res: any) => {
    if (res.code === 200) {
      groupList.value = res.data.rows
    }
  })
}
const getDict = async () => {
  await getStatusDict()
  await getDeviceTypeDict()
  await getBizTypeDict()
  await getAllGroupList()
}

const initQuerySearch = () => {
  searchQuery.value.beginTime = dayjs(new Date().valueOf() - 3600 * 24 * 6 * 1000).format('YYYY-MM-DD')
  searchQuery.value.endTime = dayjs(new Date().valueOf() + 3600 * 24 * 1000).format('YYYY-MM-DD')
  dateRangeUp.value = [searchQuery.value.beginTime, searchQuery.value.endTime]
}

watch(dateRangeUp, (val) => {
  if (val) {
    searchQuery.value.beginTime = dayjs(val[0]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[0]).format('YYYY-MM-DD')
    searchQuery.value.endTime = dayjs(val[1]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[1]).format('YYYY-MM-DD')
  }
  else {
    searchQuery.value.beginTime = ''
    searchQuery.value.endTime = ''
  }
})

watch(dateRangeLog, (val) => {
  if (val) {
    searchQuery.value.beginTime = dayjs(val[0]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[0]).format('YYYY-MM-DD')
    searchQuery.value.endTime = dayjs(val[1]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[1]).format('YYYY-MM-DD')
  }
  else {
    searchQuery.value.beginTime = ''
    searchQuery.value.endTime = ''
  }
})

// 监听父组件传过来的值
watch(props, (val) => {
  // 如果是嵌入的页面 当devcode改变时则将查询条件进行修改
  if (val.embedded === true) {
    searchQuery.value.devCode = props.devCode
  }
}, { immediate: true })

onMounted(async () => {
  await getDict()
  initQuerySearch()

  setTimeout(searchList, 500)
  window.addEventListener('resize', calcTableHeight)
})

const tableHeight = ref(400)
const calcTableHeight = () => {
  // 顶部高度
  const topHeight = 50
  // app-container的 padding距离
  const appPadding = 20
  // 查询组件的高度
  const searchDiv = document.getElementById('search-div-id')
  const searchHeight = searchDiv ? searchDiv.clientHeight : 0
  // 表格顶部的文字提示高度
  const tableTopHeight = 32 + 10
  // 表格表头
  const tableHeaderHeight = 40
  // 分页器的高度
  const tablePaginationHeight = 40
  // 判断数据长度
  const height = window.innerHeight - topHeight - appPadding - searchHeight - tableTopHeight - tableHeaderHeight - tablePaginationHeight
  if (dataList.value.length * 40 >= height) {
    tableHeight.value = height
  }
  else {
    tableHeight.value = dataList.value.length ? (dataList.value.length + 1) * 40 : 200
  }
}
onBeforeUnmount(() => {
  window.removeEventListener('resize', calcTableHeight)
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-select v-if="props.embedded === false" v-model="searchQuery.groupId" placeholder="请选择分组" clearable
          filterable style="width: 192px;">
          <el-option v-for="group in groupList" :key="group.id" :label="group.groupName" :value="group.id" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.devCode" :disabled="props.embedded === true" placeholder="设备编号" clearable />
      </search-item>
      <search-item>
        <el-select v-if="props.embedded === false" v-model="searchQuery.deviceType" placeholder="设备类型" clearable
          style="width: 192px;">
          <el-option v-for="dict in deviceTypeDict" :key="dict.id" :label="dict.name" :value="dict.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="searchQuery.bizType" placeholder="业务类型" clearable style="width: 192px;">
          <el-option v-for="dict in bizTypeDict" :key="dict.id" :label="dict.name" :value="dict.value" />
        </el-select>
      </search-item>
      <!-- <search-item>
        <el-date-picker v-model="dateRangeLog" type="daterange" start-placeholder="采集时间(开始)"
          end-placeholder="采集时间(结束)" />
      </search-item> -->
      <search-item>
        <el-date-picker v-model="dateRangeUp" type="daterange" start-placeholder="上报时间(开始)"
          end-placeholder="上报时间(结束)" />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container title="设备数据列表">
      <!-- 表头区域 -->
      <template #btns-right>
        <el-button type="primary" @click="exportDeviceDataList">
          导出
        </el-button>
      </template>

      <!-- 表格区域 -->
      <normal-table :data="dataList" :columns="columns" :total="total" :query="searchQuery" :list-loading="loadingTable"
        @change="changePage" :height="tableHeight">
        <template #preColumns>
          <el-table-column label="序号" width="65" align="center">
            <template #default="scope">
              {{ (searchQuery.offset! - 1) * searchQuery.limit! + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>

        <template #columns>
          <el-table-column fixed="right" label="操作" align="center" width="80">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>

    <data-detail-dialog ref="refDataDialog" />
  </app-container>
</template>