Newer
Older
xc-metering-front / src / views / tested / device / model / components / list.vue
<!-- 设备规格型号管理列表 -->
<script lang="ts" setup name="DeviceModelList">
import { reactive, ref, watch } from 'vue'
import axios from 'axios'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { deleteModel, exportModelList, getDeviceNameList, getModelAllList, getModelByname, getModelList } from '@/api/eqpt/device/model'
import { exportFile } from '@/utils/exportUtils'
import { getDictByCode } from '@/api/system/dict'
import { getPhotoUrl } from '@/api/system/tool'
import { download } from '@/utils/download'
import showPhoto from '@/views/tested/device/info/components/showPhoto.vue'
import useTemplateDownload from '@/utils/useTemplateDownload'
import { keepSearchParams } from '@/utils/keepQuery'
const listQuery = reactive({
  equipmentType: '',
  category: '',
  equipmentName: '',
  helpInstruction: '',
  inspectable: '',
  model: '',
  offset: 1,
  limit: 20,
  sort: 'desc',
  orderBy: 'create_time',
})
onBeforeRouteLeave((to: any) => {
  keepSearchParams(to.path, 'DeviceModelIndex')
})
const columns = ref([
  {
    text: '设备类型',
    value: 'equipmentTypeName',
    align: 'center',
  },
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '规格型号',
    value: 'model',
    align: 'center',
  },
  {
    text: '辅助字段',
    value: 'helpInstruction',
    align: 'center',
  },
  {
    text: '设备分类',
    value: 'categoryName',
    align: 'center',
  },
  {
    text: '检定周期',
    value: 'checkCycle',
    align: 'center',
  },
  // {
  //   text: '使用说明书',
  //   value: 'instructionsFile',
  //   align: 'center',
  // },
  // {
  //   text: '备注',
  //   value: 'remark',
  //   align: 'center',
  // },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  // inspectable: listQuery.inspectable.length === 2 ? '1,2' : listQuery.inspectable.join()
  getModelList({ ...listQuery }).then((response) => {
    list.value = response.data.rows.map((item: any) => ({ ...item, helpInstruction: item.helpInstruction ? item.helpInstruction : '/' }))
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
const userStore = useUserStore()
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  search()
}
const $router = useRouter()
// 新建编辑操作
const handler = (row: any, type: string) => {
  $router.push({
    path: `/dmodel/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
// 删除
const delHandler = (row: any) => {
  ElMessageBox.confirm(
    '确认删除操作?注意删除不可逆。',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    // 判断设备是否被绑定
    // 此设备型号与已存在设备绑定,请先删除设备
    deleteModel({ id: row.id }).then((res) => {
      ElMessage.success('操作成功')
      search()
    })
  })
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 导出列表
const exportList = () => {
  if (list.value.length) {
    const loading = ElLoading.service({
      lock: true,
      text: 'Loading',
      background: 'rgba(255, 255, 255, 0.8)',
    })
    const data = {
      ...listQuery,
      offset: undefined,
      limit: undefined,
      ids: selectList.value.map(item => item.id),
      // inspectable: listQuery.inspectable.join(),
    }
    exportModelList(data).then((res) => {
      exportFile(res.data, '设备规格型号')
      loading.close()
      // selectList.value = []
      // search()
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('暂无可导出数据')
  }
}
// 打开文件
const clickFile = (fileName: string) => {
  getPhotoUrl(fileName).then((res) => {
    // console.log(res.data, 'url')
    download(res.data, fileName)
  })
}
// 获取下拉列表相关
// 设备名称
const deviceNameList = ref<string[]>([])
// 设备分类
const deviceTypeList = ref<{ id: string; value: string; name: string }[]>([])
// 设备类型
const deviceCategoryList = ref<{ id: string; value: string; name: string }[]>([])
// 规格型号
const modelList = ref<any[]>([])
// 辅助字段
const helpList = ref<any[]>([])
const allList = ref<any[]>([])
const fetchSelect = () => {
  // 设备分类
  getDictByCode('eqptDeviceType').then((res) => {
    deviceTypeList.value = res.data
  })
  // 设备类型
  getDictByCode('eqptDeviceCategory').then((res) => {
    deviceCategoryList.value = res.data
  })
  // 设备名称
  getDeviceNameList({ equipmentType: '' }).then((res) => {
    deviceNameList.value = res.data
  })
  // 规格型号及辅助字段
  getModelAllList({ equipmentType: '' }).then((res) => {
    allList.value = res.data
    modelList.value = Array.from(new Set(res.data.filter((item: any) => item.model).map((item: any) => item.model))).sort()
    helpList.value = Array.from(new Set(res.data.filter((item: any) => item.helpInstruction).map((item: any) => item.helpInstruction))).sort()
  })
}
fetchSelect()
// 监听设备分类
watch(() => listQuery.equipmentType, (newVal) => {
  listQuery.equipmentName = ''
  listQuery.model = ''
  listQuery.helpInstruction = ''
  if (newVal) {
    // 设备名称
    getDeviceNameList({ equipmentType: newVal }).then((res) => {
      deviceNameList.value = res.data
    })
    getModelAllList({ equipmentType: newVal }).then((res) => {
      allList.value = res.data
      modelList.value = Array.from(new Set(res.data.filter((item: any) => item.model).map((item: any) => item.model))).sort()
      helpList.value = Array.from(new Set(res.data.filter((item: any) => item.helpInstruction).map((item: any) => item.helpInstruction))).sort()
    })
  }
  else {
    // 设备名称
    getDeviceNameList({ equipmentType: '' }).then((res) => {
      deviceNameList.value = res.data
    })
    getModelAllList({ equipmentType: '' }).then((res) => {
      allList.value = res.data
      modelList.value = Array.from(new Set(res.data.filter((item: any) => item.model).map((item: any) => item.model))).sort()
      helpList.value = Array.from(new Set(res.data.filter((item: any) => item.helpInstruction).map((item: any) => item.helpInstruction))).sort()
    })
  }
  // search()
}, {
  deep: true,
})
// 监听设备名称下拉框,修改规格型号和辅助字段
watch(() => listQuery.equipmentName, (newVal) => {
  listQuery.helpInstruction = ''
  listQuery.model = ''
  if (newVal) {
    // 修改规格型号和辅助字段列表
    const data = allList.value.filter(item => item.equipmentName === newVal)
    modelList.value = Array.from(new Set(data.filter(item => item.model).map(item => item.model))).sort()
    helpList.value = Array.from(new Set(data.filter(item => item.helpInstruction).map(item => item.helpInstruction))).sort()
  }
  else {
    modelList.value = Array.from(new Set(allList.value.filter(item => item.model).map(item => item.model))).sort()
    helpList.value = Array.from(new Set(allList.value.filter(item => item.helpInstruction).map(item => item.helpInstruction))).sort()
  }
})
// 监听规格型号下拉框,修改辅助字段
watch(() => listQuery.model, (newVal) => {
  listQuery.helpInstruction = ''
  if (newVal && listQuery.equipmentName) {
    // 修改规格型号和辅助字段列表
    helpList.value = Array.from(new Set(allList.value.filter(item => item.helpInstruction).filter(item => item.equipmentName === listQuery.equipmentName && item.model === newVal).map(item => item.helpInstruction))).sort()
  }
  else if (newVal) {
    helpList.value = Array.from(new Set(allList.value.filter(item => item.helpInstruction).filter(item => item.model === newVal).map(item => item.helpInstruction))).sort()
  }
  else {
    helpList.value = Array.from(new Set(allList.value.filter(item => item.helpInstruction).map(item => item.helpInstruction))).sort()
  }
})
// 重置
const reset = () => {
  listQuery.equipmentType = ''
  listQuery.inspectable = ''
  listQuery.category = ''
  listQuery.equipmentName = ''
  listQuery.helpInstruction = ''
  listQuery.model = ''
  listQuery.sort = 'desc'
  listQuery.orderBy = 'create_time'
  modelList.value = Array.from(new Set(allList.value.filter(item => item.model).map(item => item.model))).sort()
  helpList.value = Array.from(new Set(allList.value.filter(item => item.helpInstruction).map(item => item.helpInstruction))).sort()
  listQuery.offset = 1
  listQuery.limit = 20
  search()
}
const fileRef = ref() // 文件上传input,获取input的引用
const onFileChange = (event: any) => {
  // 原生上传
  if (event.target.files?.length !== 0) {
    // 创建formdata对象
    const fd = new FormData()
    fd.append('file', event.target.files[0])
    const loading = ElLoading.service({
      lock: true,
      background: 'rgba(255, 255, 255, 0.8)',
    })
    axios({
      url: `${window.localStorage.getItem('baseURL-eqpt')!}/equipment/model/import`,
      method: 'post',
      data: fd,
      headers: { token: userStore.token },
    }).then((res) => {
      fileRef.value.value = ''
      const response = res.data
      loading.close()
      if (response.code === 200) {
        ElMessage.success(response.message)
        reset()
      }
      else {
        // ElMessage.warning(response.message)
        ElMessage({
          message: response.message,
          type: 'warning',
          duration: 0,
          showClose: true,
        })
      }
    }).catch(() => {
      fileRef.value.value = ''
      loading.close()
      ElMessage.error('上传失败')
    })
  }
}
// 导入
const importList = () => {
  fileRef.value.click()
}
const { proxy } = getCurrentInstance() as any
const permUrl = ref({
  edit: '/device/model/update',
  del: '/device/model/delete',
  add: '/device/model/add',
  import: '/device/model/import',
})

onActivated(() => {
  // 从编辑或者新增页面回来需要重新获取列表数据
  // $router.options.history.state.forward 上一次路由地址
  if (!($router.options.history.state.forward as string || '').includes('detail')) {
    console.log('需要重新获取列表')
    fetchData(false)
  }
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-select v-model.trim="listQuery.equipmentType" placeholder="设备类型" clearable>
          <el-option v-for="item in deviceCategoryList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.equipmentName" filterable 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 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" filterable 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.trim="listQuery.category" placeholder="设备分类" clearable>
          <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.trim="listQuery.inspectable" clearable placeholder="计量站是否可检">
          <el-option label="西昌可检" value="1" />
          <el-option label="海口可检" value="2" />
          <el-option label="西昌,海口可检" value="1,2" />
          <el-option label="空" value="66" />
        </el-select>
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm(permUrl.add)" icon="icon-add" title="新增" @click="handler({}, 'create')" />
        <icon-button v-if="proxy.hasPerm(permUrl.import)" icon="icon-import" title="导入" @click="importList" />
        <icon-button icon="icon-template" title="模板下载" @click="useTemplateDownload('设备规格型号管理模块')" />
        <icon-button icon="icon-export" title="导出" @click="exportList" />
        <input ref="fileRef" style="display: none;" type="file" accept=".xls,.xlsx" @change="onFileChange">
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns as any" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      >
        <template #columns>
          <el-table-column label="使用说明书" align="center">
            <template #default="scope">
              <!-- <el-link type="primary" @click="clickFile(scope.row.instructionsFile)">
                {{ scope.row.instructionsFile }}
              </el-link> -->
              <show-photo :minio-file-name="scope.row.instructionsFile" :show-close="false" @delete="() => {}" />
            </template>
          </el-table-column>
          <el-table-column label="计量站是否可检" align="center">
            <template #default="scope">
              {{ scope.row.inspectable === '1' ? '西昌可检' : scope.row.inspectable === '2' ? '海口可检' : scope.row.inspectable === '1,2' || scope.row.inspectable === '2,1' ? '西昌可检,海口可检' : '' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handler(scope.row, 'detail')">
                查看
              </el-button>
              <el-button v-if="proxy.hasPerm(permUrl.edit)" link type="primary" size="small" @click="handler(scope.row, 'update')">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm(permUrl.del)" link type="danger" size="small" @click="delHandler(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>