Newer
Older
sensorHubPlusFront / src / views / basic / device / configList.vue
liyaguang 1 day ago 8 KB 设备配置
<!-- 设备配置列表 -->
<script name="DeviceConfigList" lang="ts" setup>
import { ElLoading, ElMessage, ElMessageBox, dayjs } from 'element-plus'
import type { DateModelType } from 'element-plus'
import type { IDictType } from '../common-interface'
import type { IConfigListQuery, IDeviceInfo } from './device-info'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import { exportFile } from '@/utils/exportUtils'
import { delDevice, exportDevice } from '@/api/basic/device'
import { getDeviceConfigListPage } from '@/api/basic/config'
import batchconfig from './batchconfig.vue'
const props = defineProps({
  devcode: {
    type: String,
    default: '',
  },
  deviceType: {
    type: String,
    default: '',
  },
})

const router = useRouter()

// 查询条件
const searchQuery = ref<IConfigListQuery>({
  devcode: props.devcode,
  configType: '',
  status: '',
  beginTime: '',
  endTime: '',
  offset: 1,
  limit: 20,
})
const dateRangeOper = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading

// 表头
const columns = ref<TableColumn[]>([
  { text: '配置类型', value: 'configTypeName', align: 'center', width: '120' },
  { text: '配置内容', value: 'configJson', align: 'center' },
  { text: '最大尝试次数', value: 'imei', align: 'center', width: '120' },
  { text: '已尝试次数', value: 'productName', align: 'center', width: '120' },
  { text: '下发状态', value: 'statusName', align: 'center', width: '120' },
  { text: '操作时间', value: 'createTime', align: 'center', width: '180' },
  { text: '操作人', value: 'createUserName', align: 'center', width: '160' },
])
const dataList = ref<Array<IDeviceInfo>>([]) // 表格数据

const configStatusDict = ref<Array<IDictType>>([])
const configTypeDict = ref<Array<IDictType>>([])

// 逻辑
// 导出所有
const exportAllDevice = () => {
  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)',
    })

    // 导出接口
    exportDevice(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: IDeviceInfo) => {
  router.push({
    query: {
      type: 'detail',
      id: row.id,
    },
    path: 'detail',
  })
}

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    searchQuery.value.offset = 1
  }
  getDeviceConfigListPage(searchQuery.value).then((response) => {
    if (response.code === 200) {
      dataList.value = response.data.rows.map((item: IDeviceInfo) => {
        return {
          ...item,
          createTime: dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss'),
        }
      })
      total.value = parseInt(response.data.total)
    }
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}

const searchList = () => {
  fetchData(true)
}
// 批量下发配置
const batchConfigRef = ref()
const bacthConfig = () => {
  batchConfigRef.value.initDialog({devcode: props.devcode, deviceType: props.deviceType})
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
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 = {
    devcode: '',
    configType: '',
    status: '',
    beginTime: '',
    endTime: '',
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 删除
const deleteById = (row: IDeviceInfo) => {
  ElMessageBox.confirm(`是否删除设备 ${row.devcode}`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    delDevice(row.id).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`设备 ${row.devcode} 删除成功`)
        searchList()
      }
      else {
        ElMessage.error(`设备 ${row.devcode} 删除失败: ${res.message}`)
      }
    })
  })
}

// 获取在用状态字典值
const getConfigStatusDict = async () => {
  getDictByCode('configStatus	').then((res: any) => {
    if (res.code === 200) {
      configStatusDict.value = res.data
      sessionStorage.setItem('configStatus	', JSON.stringify(configStatusDict.value))
    }
  })
}
const getConfigTypeDict = async () => {
  getDictByCode('configType').then((res: any) => {
    if (res.code === 200) {
      configTypeDict.value = res.data
      sessionStorage.setItem('configType', JSON.stringify(configTypeDict.value))
    }
  })
}
const getDict = async () => {
  await getConfigStatusDict()
  await getConfigTypeDict()
}

// 监听父组件传过来的值
watch(props, (val) => {
  searchQuery.value.devcode = props.devcode
})

watch(dateRangeOper, (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 = ''
  }
})

onMounted(async () => {
  await getDict()
  searchList()
})
const { proxy } = getCurrentInstance() as any
</script>

<template>
  <app-container>
    <!-- 批量下发配置弹窗 -->
    <batchconfig ref="batchConfigRef" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.devcode" placeholder="设备编号" disabled />
      </search-item>
      <search-item>
        <el-select v-model="searchQuery.configType" placeholder="配置类型" clearable style="width: 192px;">
          <el-option v-for="dict in configTypeDict" :key="dict.id" :label="dict.name" :value="dict.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="searchQuery.status" placeholder="下发状态" clearable style="width: 192px;">
          <el-option v-for="item in configStatusDict" :key="item.id" :value="item.value" :label="item.name" />
        </el-select>
      </search-item>

      <search-item>
        <el-date-picker v-model="dateRangeOper" type="daterange" start-placeholder="操作时间(开始)"
          end-placeholder="操作时间(结束)" />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container title="配置列表">
      <!-- 表头区域 -->
      <template #btns-right>
        <el-button v-if="proxy.support.includes(`${props.deviceType}`)" type="primary" @click="bacthConfig">
          下发配置
        </el-button>
        <el-button type="primary" @click="exportAllDevice">
          导出列表
        </el-button>
      </template>

      <!-- 表格区域 -->
      <normal-table :data="dataList" :columns="columns" :total="total" :query="searchQuery" :list-loading="loadingTable"
        @change="changePage">
        <template #preColumns>
          <el-table-column label="序号" width="55" 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="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>