Newer
Older
xc-business-system / src / views / business / measure / item / list.vue
<!-- 检定项管理列表 -->
<script lang="ts" setup name="BusinessMeasureItemListCom">
import type { Ref } from 'vue'
import { ElLoading, ElMessage } from 'element-plus'
import type { DateModelType } from 'element-plus'
import type { IList, IListQuery } from './item-interface'
import confirmConfigDialog from './dialog/confirmConfigDialog.vue'
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 { getClassificationList } from '@/api/business/measure/classification'
import { addItem, exportItemList, getDeviceNameList, getItemList, getModelAllList, updateItem } from '@/api/business/measure/item'
import { keepSearchParams } from '@/utils/keepQuery'

onBeforeRouteLeave((to: any) => {
  keepSearchParams(to.path, 'BusinessMeasureItemListCom')
})
const $router = useRouter()
const loadingTable = ref(false)
const operateWidth = ref('160') // 操作栏的宽度

// 查询条件
const listQuery: Ref<IListQuery> = ref({
  asTemplate: '', //	是否为模板(查询引用模板时该字段必传1)
  belongStandardEquipment: '', //	所属检校标准装置(字典code)
  dataSync: '', //	检定项数据是否同步(1/0)
  deviceName: '', //	设备名称
  deviceType: '', //	设备分类(字典code)
  helpFieldInstruction: '', //	辅助字段说明
  helpInstruction: '', //	辅助字段
  itemCategoryId: '', //	设备检定项分类表id(查询引用模板时该字段必传)
  model: '', //	规格型号
  noConfigFlag: '', //	未配置检定项(1查询未配置,不传为查询所有)
  syncTimeEnd: '', //		自动检定系统最新同步时间结束
  syncTimeStart: '', //		自动检定系统最新同步时间开始
  limit: 20,
  offset: 1,
})

// 表头
const columns = ref<TableColumn[]>([
  { text: '设备名称', value: 'deviceName', align: 'center' },
  { text: '规格型号', value: 'model', align: 'center' },
  { text: '辅助字段', value: 'helpInstruction', align: 'center' },
  { text: '辅助字段说明', value: 'helpFieldInstruction', 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: 'syncTime', align: 'center', width: '180' },
])
const list = ref<IList[]>([]) // 列表
const total = ref(0) // 数据总条数
const checkoutList = ref<IList[]>([]) // 选中的内容
const timeRange = ref<[DateModelType, DateModelType]>(['', '']) // 查询条件
const selectedRow = ref<IList>() // 点击操作的行数据
// ------------------------------------------字典----------------------------------------------
const deviceTypeList = ref<dictType[]>([])// 设备分类
const deviceTypeMap = ref({}) as any // 设备分类map
const standardList = ref<dictType[]>([])// 检校标准装置
const standardMapList = ref<dictType[]>([])// 检校标准装置
const isSyncMap = ref({}) as any // 是否同步{1: 是}
const isSyncList = ref({}) as any // 是否同步
const deviceNameList = ref<string[]>([]) // 设备名称
const deviceNameOriginList = ref<string[]>([]) // 设备名称
const modelList = ref<any[]>([])// 规格型号
const helpList = ref<any[]>([])// 辅助字段
const allList = ref<any[]>([])
// 检定项分类请求参数
const itemCategoryListQuery = ref({
  belongStandardEquipment: '', // 检校标准装置
  categoryName: '', // 检定项分类名称
  categoryNo: '', // 检定项分类编号
  deviceType: '', // 设备分类
  measureCategory: '', // 检校类别
  limit: 999999,
  offset: 1,
})
const itemCategoryList = ref<{ id: string;name: string }[]>([]) // 检定项分类
const itemCategoryOriginList = ref<{ id: string;name: string }[]>([]) // 检定项分类
async function getDict() {
  // 是否同步
  getDictByCode('bizBusinessMeasureIsSync').then((response) => {
    isSyncList.value = response.data
    response.data.forEach((item: any) => {
      isSyncMap.value[`${item.value}`] = item.name
    })
  })
  // 设备分类
  const res = await getDictByCode('eqptDeviceType')
  deviceTypeList.value = res.data
  res.data.forEach((item: any) => {
    deviceTypeMap.value[`${item.value}`] = item.name
  })

  // 检校标准装置
  getDictByCode('bizStandardEquipmentType').then((response) => {
    standardList.value = response.data
    standardMapList.value = response.data
  })

  // 检定项分类
  getClassificationList(itemCategoryListQuery.value).then((response) => {
    itemCategoryList.value = response.data.rows.map((item: any) => {
      return {
        id: item.id, // 检定项分类id
        name: item.categoryName, // 检定项分类名称
        belongStandardEquipment: item.belongStandardEquipment, // 检校标准装置
        belongStandardEquipmentName: item.belongStandardEquipmentName, // 检校标准装置名称
        deviceType: item.deviceType, // 设备分类
        deviceTypeName: item.deviceTypeName, // 设备分类名称
      }
    })
    itemCategoryOriginList.value = itemCategoryList.value
  })

  // 设备名称
  getDeviceNameList().then((res) => {
    deviceNameList.value = res.data
    deviceNameOriginList.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)))
  })
}
// --------------------------------------配置检定项--------------------------------------------
const confirmConfigRef = ref() // 点击配置检定项确认操作对话框组件ref

// 确认配置检定项
const confirmConfig = async (form: any) => {
  const loading = ElLoading.service({
    lock: true,
    text: '加载中',
    background: 'rgba(255, 255, 255, 0.6)',
  })
  let getId = ''
  if (form.configType === 'add') {
    const res = await addItem({ ...selectedRow.value!, deviceType: form.deviceType, appearanceFunctionCheck: '', dataSync: '', itemCategoryName: form.itemCategoryName, itemCategoryId: form.itemCategoryId, belongStandardEquipment: form.belongStandardEquipment, belongStandardEquipmentName: form.belongStandardEquipmentName })
    getId = res.data
  }
  else {
    const res = await updateItem({ ...selectedRow.value!, deviceType: form.deviceType, appearanceFunctionCheck: '', dataSync: '', itemCategoryName: form.itemCategoryName, itemCategoryId: form.itemCategoryId, belongStandardEquipment: form.belongStandardEquipment, belongStandardEquipmentName: form.belongStandardEquipmentName })
    getId = res.data
  }
  loading.close()
  console.log('跳转之前', form)

  $router.push({
    path: `measureItem/edit/${getId}`,
    query: {
      ...selectedRow.value!,
      deviceType: form.deviceType, // 设备分类(检校分类)
      itemCategoryId: form.itemCategoryId, // 设备检定项分类id
      itemCategoryName: form.itemCategoryName, // 设备检定项分类名称
      belongStandardEquipment: form.belongStandardEquipment, // 检校标准装置(字典code)
      updataOld: `${form.updataOld}`, // 是否更新了检定项或者标准装置
      isFirstConfig: `${form.isFirstConfig}`, // 是否第一次配置
    },
  })
}
// ---------------------------------------------------------------------------------------------

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

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.value.offset = 1
  }
  getItemList(listQuery.value).then((response) => {
    list.value = response.data.rows.map((item: { deviceType: string }) => {
      return {
        ...item,
        deviceTypeName: deviceTypeMap.value[item.deviceType], // 设备分类
      }
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false

    console.log(list.value)
  })
}

// 清除条件
const clearList = () => {
  listQuery.value = {
    asTemplate: '', //	是否为模板(查询引用模板时该字段必传1)
    belongStandardEquipment: '', //	所属检校标准装置(字典code)
    dataSync: '', //	检定项数据是否同步(1/0)
    deviceName: '', //	设备名称
    deviceType: '', //	设备分类(字典code)
    helpInstruction: '', //	辅助字段
    itemCategoryId: '', //	设备检定项分类表id(查询引用模板时该字段必传)
    model: '', //	规格型号
    noConfigFlag: '', //	未配置检定项(1查询未配置,不传为查询所有)
    syncTimeEnd: '', //		自动检定系统最新同步时间结束
    syncTimeStart: '', //		自动检定系统最新同步时间开始
    limit: 20,
    offset: 1,
  }
  timeRange.value = ['', '']
  getDict()
  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' | 'config') => {
  selectedRow.value = row // 表格行数据
  if (val === 'detail') {
    $router.push({
      path: `measureItem/${val}/${row.id}`,
      query: {
        ...row,
        deviceType: row.deviceType, // 设备分类(检校分类)
        itemCategoryId: row.itemCategoryId, // 设备检定项分类id
        itemCategoryName: row.itemCategoryName, // 设备检定项分类名称
        belongStandardEquipment: row.belongStandardEquipment, // 检校标准装置(字典code)
      },
    })
  }
  else if (val === 'config') { // 配置检定项
    confirmConfigRef.value.initDialog(row.configType ? row.configType : 'edit', row.deviceType, row.belongStandardEquipment, row.itemCategoryId)
  }
}

// 导出
const exportAll = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '下载中请稍后',
    background: 'rgba(255, 255, 255, 0.6)',
  })
  if (list.value.length > 0) {
    const params = {
      asTemplate: listQuery.value.asTemplate, //	是否为模板(查询引用模板时该字段必传1)
      belongStandardEquipment: listQuery.value.belongStandardEquipment, //	所属检校标准装置(字典code)
      dataSync: listQuery.value.dataSync, //	检定项数据是否同步(1/0)
      deviceName: listQuery.value.deviceName, //	设备名称
      deviceType: listQuery.value.deviceType, //	设备分类(字典code)
      helpInstruction: listQuery.value.helpInstruction, //	辅助字段
      itemCategoryId: listQuery.value.itemCategoryId, //	设备检定项分类表id(查询引用模板时该字段必传)
      model: listQuery.value.model, //	规格型号
      noConfigFlag: listQuery.value.noConfigFlag, //	未配置检定项(1查询未配置,不传为查询所有)
      syncTimeEnd: listQuery.value.syncTimeEnd, //		自动检定系统最新同步时间结束
      syncTimeStart: listQuery.value.syncTimeStart, //		自动检定系统最新同步时间开始
      limit: 20,
      offset: 1,
      ids: checkoutList.value.map(item => item.id),
    }
    exportItemList(params).then((res) => {
      const blob = new Blob([res.data])
      loading.close()
      exportFile(blob, '检定项管理.xlsx')
    })
  }
  else {
    loading.close()
    ElMessage.warning('无数据可导出数据')
  }
}

// 点击增加配置项
const addConfig = () => {
  if (checkoutList.value.length > 1) {
    ElMessage.warning('只允许选择一条数据')
    return false
  }
  if (!checkoutList.value.length) {
    ElMessage.warning('请选中数据')
    return false
  }
  console.log(checkoutList.value)
  if (!checkoutList.value[0].id && !checkoutList.value[0].belongStandardEquipment && !checkoutList.value[0].itemCategoryId) {
    ElMessage.warning('选中设备未配置检定项,不允许再增加')
    return false
  }
  const index = list.value.findIndex(item => item.id === checkoutList.value[0].id)
  console.log(index)

  if (index !== -1) {
    list.value.splice(index + 1, 0, {
      id: '',
      deviceName: checkoutList.value[0].deviceName, // 设备名称
      model: checkoutList.value[0].model, // 规格型号
      helpInstruction: checkoutList.value[0].helpInstruction, // 辅助字段
      helpFieldInstruction: checkoutList.value[0].helpFieldInstruction, // 辅助字段说明
      deviceType: checkoutList.value[0].deviceType, // 设备分类(检校分类)
      deviceTypeName: checkoutList.value[0].deviceTypeName, // 设备分类(检校分类)名称
      belongStandardEquipment: checkoutList.value[0].belongStandardEquipment, // 检校标准装置
      belongStandardEquipmentName: checkoutList.value[0].belongStandardEquipmentName, // 检校标准装置名称
      itemCategoryId: checkoutList.value[0].itemCategoryId, // 设备检定项分类id
      itemCategoryName: checkoutList.value[0].itemCategoryName, // 设备检定项分类
      checkCycle: checkoutList.value[0].checkCycle,
      eqptDeviceModelId: checkoutList.value[0].eqptDeviceModelId,
      deviceModelId: checkoutList.value[0].deviceModelId,
      updateTime: '', // 检定项更新时间
      dataSyncStr: '', // 自动检定系统数据是否同步
      syncTime: '', // 自动检定系统最新同步时间
      configType: 'add', // 检定项配置类型-新增
    })
  }
}

// ---------------------------------------钩子----------------------------------------------
// 监听设备名称下拉框,修改规格型号和辅助字段
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(() => listQuery.value.deviceType, (newValue) => {
  listQuery.value.belongStandardEquipment = '' // 清空检校标准装置
  listQuery.value.itemCategoryId = ''// 检定项分类
  if (newValue === '1') { // 电学
    standardMapList.value = standardList.value.slice(0, 3)
    itemCategoryList.value = itemCategoryOriginList.value.filter((item: any) => item.deviceType === '1')
  }
  else if (newValue === '2') { // 压力
    standardMapList.value = standardList.value.slice(3, 8)
    itemCategoryList.value = itemCategoryOriginList.value.filter((item: any) => item.deviceType === '2')
  }
  else if (newValue === '0') { // 无线电
    standardMapList.value = standardList.value.slice(8)
    itemCategoryList.value = itemCategoryOriginList.value.filter((item: any) => item.deviceType === '0')
  }
  else {
    standardMapList.value = standardList.value
    itemCategoryList.value = itemCategoryOriginList.value
  }
})
// 监听到所属检校标准装置, 联动检定项分类
watch(() => listQuery.value.belongStandardEquipment, (newValue) => {
  listQuery.value.itemCategoryId = '' // 清空检定项分类
  itemCategoryList.value = itemCategoryOriginList.value.filter((item: any) => item.belongStandardEquipment === newValue)
})
// 监听未配置的检定项checkbox切换
watch(() => listQuery.value.noConfigFlag, (newValue) => {
  fetchData()
})
watch(timeRange, (val) => {
  if (val) {
    listQuery.value.syncTimeStart = `${val[0]}`
    listQuery.value.syncTimeEnd = `${val[1]}`
  }
  else {
    listQuery.value.syncTimeStart = ''
    listQuery.value.syncTimeEnd = ''
  }
})
onMounted(async () => {
  getDict().then(() => {
    fetchData(false)
  })
})
onActivated(() => {
  // 从编辑或者新增页面回来需要重新获取列表数据
  // $router.options.history.state.forward 上一次路由地址
  if (!($router.options.history.state.forward as string || '').includes('detail')) {
    console.log('需要重新获取列表')
    fetchData(true)
  }
})
</script>

<template>
  <div>
    <!-- 布局 -->
    <app-container>
      <search-area :need-clear="true" @search="searchList" @clear="clearList">
        <search-item>
          <el-select
            v-model="listQuery.deviceType"
            placeholder="设备分类"
            class="short-input"
            filterable
            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="listQuery.belongStandardEquipment"
            placeholder="检校标准装置"
            filterable
            clearable
          >
            <el-option v-for="item in standardMapList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </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" filterable 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.itemCategoryId"
            placeholder="检定项分类"
            filterable
            clearable
          >
            <el-option v-for="item in itemCategoryList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </search-item>
        <search-item>
          <el-select
            v-model="listQuery.dataSync"
            placeholder="检定点数据是否同步"
            filterable
            clearable
          >
            <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-item style="margin-left: 20px;">
          <el-checkbox v-model="listQuery.noConfigFlag" :true-label="1" :false-label="0">
            未配置检定项
          </el-checkbox>
        </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="addConfig" />
        </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
                  v-if="row.id"
                  size="small"
                  type="primary"
                  link
                  @click="handleEdit(row, 'detail')"
                >
                  详情
                </el-button>
                <el-button
                  size="small"
                  link
                  type="primary"
                  @click="handleEdit(row, 'config')"
                >
                  配置检定项
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
      <confirm-config-dialog ref="confirmConfigRef" @confirm="confirmConfig" />
    </app-container>
  </div>
</template>