Newer
Older
xc-business-system / src / views / business / measure / item / list.vue
<!-- 检定项管理列表 -->
<script lang="ts" setup name="BusinessMeasureItemList">
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { DateModelType } from 'element-plus'
import type { IList, IListQuery } from './item-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import type { dictType } from '@/global'
import { exportFile } from '@/utils/exportUtils'
import { exportItemList, getDeviceNameList, getItemList, getModelAllList } from '@/api/business/measure/item'
const $router = useRouter()
const loadingTable = ref(false)
const operateWidth = ref('160') // 操作栏的宽度
// 查询条件
const listQuery: Ref<IListQuery> = ref({
  dataSync: '', // 检定项数据是否同步
  deviceName: '', // 设备名称
  deviceType: '', // 设备分类
  helpInstruction: '', // 辅助字段
  model: '', // 型号规格
  modelNo: '', // 型号规格编号
  syncTimeEnd: '', // 自动检定系统最新同步时间结束
  syncTimeStart: '', // 自动检定系统最新同步时间开始
  limit: 20,
  offset: 1,
})

// 表头
const columns = ref<TableColumn[]>([
  { text: '型号规格编号', value: 'modelNo', align: 'center', width: '160' },
  { text: '设备名称', value: 'deviceName', align: 'center' },
  { text: '型号规格', value: 'model', align: 'center' },
  { text: '辅助字段', value: 'helpInstruction', align: 'center' },
  { text: '辅助字段说明', value: 'helpInstruction', align: 'center' },
  { text: '设备分类', value: 'deviceTypeName', align: 'center' },
  { text: '检校标准装置', value: 'belongStandardEquipmentName', align: 'center' },
  { text: '设备检定项分类', value: 'itemCategoryName', align: 'center' },
  { text: '检定项更新时间', value: 'updateTime', align: 'center', width: '180' },
  { text: '自动检定系统数据是否同步', value: 'dataSyncStr', align: 'center' },
  { text: '自动检定系统最新同步时间', value: 'updateTime', align: 'center', width: '180' },
])
const list = ref<IList[]>([]) // 列表
const total = ref(0) // 数据总条数
const checkoutList = ref<string[]>([]) // 选中的内容
const timeRange = ref<[DateModelType, DateModelType]>(['', '']) // 查询条件
// ------------------------------------------字典----------------------------------------------
const deviceTypeList = ref<dictType[]>([])// 设备分类
const isSyncMap = ref({}) as any // 是否同步{1: 是}
const isSyncList = ref({}) as any // 是否同步
const deviceNameList = ref<string[]>([]) // 设备名称
const modelList = ref<any[]>([])// 规格型号
const helpList = ref<any[]>([])// 辅助字段
const allList = ref<any[]>([])
function getDict() {
  // 是否同步
  getDictByCode('bizBusinessMeasureIsSync').then((response) => {
    isSyncList.value = response.data
    response.data.forEach((item: any) => {
      isSyncMap.value[`${item.value}`] = item.name
    })
  })
  // 设备分类
  getDictByCode('eqptDeviceType').then((response) => {
    deviceTypeList.value = response.data
  })
  // 设备名称
  getDeviceNameList().then((res) => {
    deviceNameList.value = res.data
  })
  // 规格型号及辅助字段
  getModelAllList({}).then((res) => {
    allList.value = res.data
    modelList.value = Array.from(new Set(res.data.filter((item: any) => item.model).map((item: any) => item.model)))
    helpList.value = Array.from(new Set(res.data.filter((item: any) => item.helpInstruction).map((item: any) => item.helpInstruction)))
  })
}
// ---------------------------------------------------------------------------------------------

// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: { id: string }) => item.id)
}

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.value.offset = 1
  }
  getItemList(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}

// 清除条件
const clearList = () => {
  listQuery.value = {
    dataSync: '', // 检定项数据是否同步
    deviceName: '', // 设备名称
    deviceType: '', // 设备分类
    helpInstruction: '', // 辅助字段
    model: '', // 型号规格
    modelNo: '', // 型号规格编号
    syncTimeEnd: '', // 自动检定系统最新同步时间结束
    syncTimeStart: '', // 自动检定系统最新同步时间开始
    limit: 20,
    offset: 1,
  }
  timeRange.value = ['', '']
  fetchData()
}

// 搜索
const searchList = () => {
  fetchData(true)
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
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(true)
}

// 操作
const handleEdit = (row: IList, val: 'detail' | 'edit') => {
  $router.push({
    path: `measureItem/${val}/${row.id}`,
  })
}

// 导出
const exportAll = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '下载中请稍后',
    background: 'rgba(255, 255, 255, 0.6)',
  })
  if (list.value.length > 0) {
    const params = {
      dataSync: listQuery.value.dataSync, // 检定项数据是否同步
      deviceName: listQuery.value.deviceName, // 设备名称
      deviceType: listQuery.value.deviceType, // 设备分类
      helpInstruction: listQuery.value.helpInstruction, // 辅助字段
      model: listQuery.value.model, // 型号规格
      modelNo: listQuery.value.modelNo, // 型号规格编号
      syncTimeEnd: listQuery.value.syncTimeEnd, // 自动检定系统最新同步时间结束
      syncTimeStart: listQuery.value.syncTimeStart, // 自动检定系统最新同步时间开始
      offset: 1,
      limit: 20,
      ids: checkoutList.value,
    }
    exportItemList(params).then((res) => {
      const blob = new Blob([res.data])
      loading.close()
      exportFile(blob, '监测设备.xlsx')
    })
  }
  else {
    loading.close()
    ElMessage.warning('无数据可导出数据')
  }
}
// ---------------------------------------钩子----------------------------------------------
// 监听设备名称下拉框,修改规格型号和辅助字段
watch(() => listQuery.value.deviceName, (newVal) => {
  listQuery.value.helpInstruction = '' // 辅助字段清空
  listQuery.value.model = '' // 型号规格清空
  // 修改规格型号和辅助字段列表
  const data = allList.value.filter(item => item.equipmentName === newVal)
  modelList.value = Array.from(new Set(data.filter(item => item.model).map(item => item.model)))
  helpList.value = Array.from(new Set(data.filter(item => item.helpInstruction).map(item => item.helpInstruction)))
})
// 监听规格型号下拉框,修改辅助字段
watch(() => listQuery.value.model, (newVal) => {
  listQuery.value.helpInstruction = '' // 辅助字段清空
  // 修改规格型号和辅助字段列表
  helpList.value = Array.from(new Set(allList.value.filter(item => item.helpInstruction).filter(item => item.equipmentName === listQuery.value.deviceName && item.model === newVal).map(item => item.helpInstruction)))
})
watch(timeRange, (val) => {
  if (val) {
    listQuery.value.syncTimeStart = `${val[0]}`
    listQuery.value.syncTimeEnd = `${val[1]}`
  }
  else {
    listQuery.value.syncTimeStart = ''
    listQuery.value.syncTimeEnd = ''
  }
})
onMounted(async () => {
  await getDict()
  fetchData(false)
})
</script>

<template>
  <div>
    <!-- 布局 -->
    <app-container>
      <search-area :need-clear="true" @search="searchList" @clear="clearList">
        <search-item>
          <el-input v-model.trim="listQuery.modelNo" placeholder="型号规格编号" class="short-input" clearable />
        </search-item>
        <search-item>
          <el-select v-model.trim="listQuery.deviceName" filterable class="short-input" placeholder="设备名称" clearable>
            <el-option v-for="item in deviceNameList" :key="item" :label="item" :value="item" />
          </el-select>
        </search-item>
        <search-item>
          <el-select v-model.trim="listQuery.model" filterable class="short-input" placeholder="型号规格" clearable>
            <el-option v-for="item in modelList" :key="item" :label="item" :value="item" />
          </el-select>
        </search-item>
        <search-item>
          <el-select v-model.trim="listQuery.helpInstruction" class="short-input" placeholder="辅助字段" clearable>
            <el-option v-for="item in helpList" :key="item" :label="item" :value="item" />
          </el-select>
        </search-item>
        <search-item>
          <el-select
            v-model="listQuery.deviceType"
            placeholder="设备分类"
            class="short-input"
            filterable
          >
            <el-option v-for="item in deviceTypeList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <search-item>
          <el-select
            v-model="listQuery.dataSync"
            placeholder="检定点数据是否同步"
            filterable
          >
            <el-option v-for="item in isSyncList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <search-item>
          <el-date-picker
            v-model="timeRange"
            type="datetimerange"
            range-separator="到"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            start-placeholder="完成同步时间(开始)"
            end-placeholder="完成同步时间(结束)"
          />
        </search-item>
      </search-area>
      <table-container>
        <template #btns-right>
          <icon-button icon="icon-export" title="导出" type="primary" @click="exportAll" />
          <icon-button icon="icon-batchConfig" title="批量设置" type="primary" @click="exportAll" />
        </template>
        <normal-table
          :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="loadingTable"
          is-showmulti-select @change="changePage" @multi-select="handleSelectionChange"
        >
          <template #preColumns>
            <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
              label="操作"
              align="center"
              fixed="right"
              :width="operateWidth"
            >
              <template #default="{ row }">
                <el-button
                  size="small"
                  type="primary"
                  link
                  @click="handleEdit(row, 'detail')"
                >
                  详情
                </el-button>
                <el-button
                  size="small"
                  link
                  type="primary"
                  @click="handleEdit(row, 'edit')"
                >
                  配置检定项
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>