Newer
Older
xc-metering-front / src / views / tested / device / group / components / selectDevice.vue
<!-- 设备列表弹窗-单选 -->
<script lang="ts" setup name="SelectDeviceSinge">
import { ElMessage } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import useUserStore from '@/store/modules/user'
import { getInfoListPage } from '@/api/eqpt/device/info'
import { getDeptTreeList } from '@/api/system/dept'
import { toTreeList } from '@/utils/structure'
import { getDeviceNameList, getModelAllList, getModelList } from '@/api/eqpt/device/model'
import { getAdminDept, getUserDept, getUserDeptSon, getUserList } from '@/api/system/user'
import { getLocationList } from '@/api/system/installation'
import { getPostList } from '@/api/system/post'
import { getGroupList } from '@/api/eqpt/device/group'
const $props = defineProps({
  // 使用状态列表
  // 主要用于 状态管理时 过滤不同的设备使用状态
  needStatus: {
    type: Boolean,
    default: false,
  },
  // 不同设备状态
  // 主要用于 状态管理时 过滤不同的设备使用状态
  statusType: {
    type: String,
    default: '',
  },
  checkDestination: {
    type: Boolean,
    default: true,
  },
  checkDestinationValue: {
    type: String,
    default: '',
  },
  limit: {
    type: Number,
    default: 5,
  },
})
const emits = defineEmits(['add'])
const userStore = useUserStore()
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  usageStatus: '',
  usageStatusList: [],
  equipmentName: '', // 设备名称
  model: '', // 规格型号
  manufactureNo: '', // 出厂编号
  equipmentType: '1',
  manufacturer: '', // 生产厂家
  deptIds: '', // 部门
  usePositionId: '', // 使用岗位
  certificateValidStart: '',
  certificateValidEnd: '',
  directorName: '',
  checkDestination: $props.checkDestinationValue, // 检定去向
  limit: $props.limit,
  offset: 1,
  companyId: '',
  installLocationId: '',
  installLocation: '',
  groupId: '',
  checkOrganization: '',
  meterIdentify: '',
  useSign: '',
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref(0)
const select = ref(-1)
// 证书有效期开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.value.certificateValidStart = ''
  listQuery.value.certificateValidEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.value.certificateValidStart = `${newVal[0]} 00:00:00`
      listQuery.value.certificateValidEnd = `${newVal[1]} 23:59:59`
    }
  }
})
// 表头
const columns = ref<TableColumn[]>([
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '规格型号',
    value: 'model',
    align: 'center',
  },
  {
    text: '出厂编号',
    value: 'manufactureNo',
    align: 'center',
  },
  {
    text: '生产厂家',
    value: 'manufacturer',
    align: 'center',
  },
  {
    text: '使用单位',
    value: 'companyName',
    align: 'center',
  },
  {
    text: '使用部门',
    value: 'deptName',
    align: 'center',
  },
  {
    text: '使用岗位',
    value: 'usePosition',
    align: 'center',
  },
  {
    text: '安装位置',
    value: 'installLocation',
    align: 'center',
  },
  {
    text: '详细位置',
    value: 'installLocationExt',
    align: 'center',
  },
  {
    text: '设备分组',
    value: 'groupNames',
    align: 'center',
  },
  {
    text: '检定(校准)机构',
    value: 'checkOrganization',
    align: 'center',
  },
  {
    text: '负责人',
    value: 'directorName',
    align: 'center',
  },
  {
    text: '计量标识',
    value: 'meterIdentifyName',
    align: 'center',
  },
  {
    text: '使用状态',
    value: 'usageStatusName',
    align: 'center',
  },
  // {
  //   text: '检定(校准)单位',
  //   value: 'checkOrganization',
  //   align: 'center',
  // },
  {
    text: '证书有效期',
    value: 'certificateValid',
    align: 'center',
  },

])
const statusDict = ref<{ [key: string]: string[] }>({
  1: ['在用', '禁用', '延用'], // 封存
  2: ['禁用', '封存'], // 启封
  3: ['在用', '延用'], // 禁用
  4: ['在用', '禁用', '封存', '延用'], // 报废
  5: ['在用', '禁用'], // 沿用
})
// 获取使用状态列表
const usageStatusList = ref()
const fetchUseStatus = () => {
  getDictByCode('eqptDeviceUseStatus').then((res) => {
    if ($props.needStatus && $props.statusType !== 'custom') {
      usageStatusList.value = res.data.filter((item: any) => statusDict.value[$props.statusType].includes(item.name))
    }
    else {
      usageStatusList.value = res.data
    }
  })
}
// 获取数据列表
const fetchData = () => {
  fetchUseStatus()
  select.value = -1
  loadingTable.value = true
  // 判断是否需要加入设备使用状态查询条件
  const data = {
    ...listQuery.value,
    usageStatusList: [] as string[],
  }
  if ($props.needStatus) {
    switch ($props.statusType) {
      case '1':
        data.usageStatusList = ['0', '1', '4']
        break
      case '2':
        data.usageStatusList = ['1', '3']
        break
      case '3':
        data.usageStatusList = ['0', '4']
        break
      case '4':
        data.usageStatusList = ['0', '1', '3', '4']
        break
      case '5':
        data.usageStatusList = ['0', '1']
        break
      case 'custom':
        data.usageStatusList = listQuery.value.usageStatusList
        break
    }
    if ($props.statusType && $props.needStatus) {
      data.usageStatusList = listQuery.value.usageStatusList
    }
  }
  getInfoListPage(data, '全部').then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
// 搜索
const search = () => {
  fetchData()
}
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.value = {
    usageStatus: '',
    usageStatusList: [],
    equipmentName: '', // 设备名称
    model: '', // 规格型号
    manufactureNo: '', // 出厂编号
    equipmentType: '1',
    manufacturer: '', // 生产厂家
    deptIds: '', // 部门
    usePositionId: '', // 使用岗位
    certificateValidStart: '',
    certificateValidEnd: '',
    directorName: '',
    companyId: '',
    checkDestination: $props.checkDestinationValue, // 检定去向
    limit: $props.limit,
    offset: 1,
    installLocationId: '',
    installLocation: '',
    groupId: '',
    checkOrganization: '',
    meterIdentify: '',
    useSign: '',
  }
  search()
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
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
  }
  search()
}
// 点击保存
const submitForm = () => {
  if (select.value >= 0) {
    const device = list.value.filter((item, index) => index == select.value)[0]
    emits('add', device)
    dialogFormVisible.value = false
    reset()
  }
  else {
    ElMessage.warning('请先选择设备')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  // reset()
  dialogFormVisible.value = true
  fetchData() // 获取人员列表
}
defineExpose({ initDialog })
// 加载组织机构树形下拉
const deptTreeList = ref()
// 设备名称
const deviceNameList = ref<string[]>([])
// 设备在用信息
const inUseList = ref<{ id: string; value: string; name: string }[]>()
// 规格型号
const modelList = ref<any[]>([])
const allList = ref<any[]>([])
// 部门
const deptList = ref<any[]>([])
// 使用岗位
const usePositionList = ref<any[]>([])
const companyList = ref<{ id: string; value: string; name: string }[]>([])
// 安装位置
const installLocationList = ref<any[]>([])
// 计量标识
const meterIdentifyList = ref<{ id: string; value: string; name: string }[]>()
// 分组
const groupList = ref<{ id: string; value: string; name: string }[]>()
const fetchDeptTree = () => {
  // getDeptTreeList().then((res) => {
  //   if (res.data) { // 将列表转树结构
  //     deptTreeList.value = toTreeList(res.data, '0', true)
  //   }
  // })
  getDictByCode('eqptMeterIdentify').then((res) => {
    meterIdentifyList.value = res.data
    meterIdentifyList.value?.push({ name: '空', id: '1', value: '66' })
  })
  // 分组
  getGroupList({ offset: 1, limit: 99999 }).then((res) => {
    // console.log(res.data, '分组')
    groupList.value = res.data.rows.map((item: any) => ({ name: item.groupName, id: item.id, value: item.id }))
  })
  // 在用信息
  getDictByCode('eqptDeviceInUse').then((res) => {
    inUseList.value = res.data
  })
  // 设备名称
  getDeviceNameList({ equipmentType: '1' }).then((res) => {
    deviceNameList.value = res.data
  })
  // 安装位置
  getLocationList().then((res) => {
    // console.log(res.data, '安装位置')
    installLocationList.value = res.data
  })
  // 规格型号
  getModelAllList({ equipmentType: '1' }).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()
  })
  // // 部门
  // getUserDept().then((res) => {
  //   getDeptTreeList({ pid: res.data.id }).then((res) => {
  //     deptList.value = toTreeList(res.data.map((item: any) => ({ ...item, label: item.name, value: item.id })))
  //   })
  // })
  // 使用岗位
  getPostList({}).then((res) => {
    usePositionList.value = res.data
  })
  getUserDept().then((res) => {
    if ((res.data.fullName === '顶级' || res.data.version === '1' || res.data.version === 1) && userStore.dataSopeType === '1') {
      getAdminDept({}).then((res) => {
        companyList.value = res.data.map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
      })
    }
    else {
      companyList.value = [
        {
          name: res.data.fullName,
          value: res.data.id,
          id: res.data.id,
        },
      ]
    }
  })
}
// 监听单位,修改部门
watch(() => listQuery.value.companyId, (newVal) => {
  listQuery.value.deptIds = ''
  if (newVal) {
    // getUserDeptSon({ companyId: newVal }).then((res) => {
    //   deptList.value = res.data.map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
    // })
    getDeptTreeList({ pid: newVal }).then((res) => {
      deptList.value = toTreeList(res.data.map((item: any) => ({ ...item, label: item.name, value: item.id }))) as any[]
    })
  }
  else {
    deptList.value = []
  }
}, {
  deep: true,
})
fetchDeptTree()
watch(() => listQuery.value.equipmentName, (newVal) => {
  if (newVal) {
    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))).sort()
  }
  else {
    listQuery.value.model = ''
    modelList.value = Array.from(new Set(allList.value.filter(item => item.model).map(item => item.model))).sort()
  }
})
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择设备" width="90%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <el-select v-model.trim="listQuery.equipmentName" clearable filterable placeholder="设备名称">
        <el-option v-for="(item, index) in deviceNameList" :key="index" :label="item" :value="item" />
      </el-select>
      <search-item>
        <el-select v-model.trim="listQuery.model" clearable filterable placeholder="规格型号">
          <el-option v-for="item in modelList" :key="item" :label="item" :value="item" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.manufactureNo" placeholder="出厂编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.manufacturer" placeholder="生产厂家" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.companyId" clearable filterable placeholder="所在单位">
          <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </search-item>
      <search-item>
        <el-tree-select
          v-model="listQuery.deptIds"
          style="width: 100%;"
          :data="deptList"
          :render-after-expand="false"
          check-strictly
          placeholder="部门"
        />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.installLocationId" filterable clearable placeholder="安装位置">
          <el-option v-for="item in installLocationList" :key="item.id" :label="item.installLocation" :value="item.id" />
        </el-select>
      </search-item>
      <!-- <search-item>
        <el-input v-model.trim="listQuery.installLocation" placeholder="详细位置" clearable />
      </search-item> -->
      <search-item>
        <el-select v-model="listQuery.groupId" clearable placeholder="分组">
          <el-option v-for="item in groupList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.checkOrganization" placeholder="检定(校准)机构" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.meterIdentify" clearable placeholder="计量标识">
          <el-option v-for="item in meterIdentifyList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.usePositionId" clearable filterable placeholder="使用岗位">
          <el-option v-for="item in usePositionList" :key="item.id" :label="item.positionName" :value="item.id" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.directorName" placeholder="负责人" clearable />
      </search-item>
      <search-item v-if="$props.needStatus">
        <el-select v-model="listQuery.usageStatusList" multiple placeholder="使用状态" clearable>
          <el-option v-for="item in usageStatusList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.useSign" placeholder="在用信息" clearable>
          <el-option v-for="item in inUseList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="证书有效期开始时间" end-placeholder="证书有效期结束时间"
          clearable
        />
      </search-item>
      <search-item v-if="$props.checkDestination">
        <el-select v-model="listQuery.checkDestination" filterable clearable placeholder="检定去向">
          <el-option label="计量室" value="1" />
          <el-option label="外送" value="2" />
        </el-select>
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" :page-sizes="[5, 10, 20]" @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="" width="55" align="center">
            <template #default="scope">
              <el-radio v-model="select" :label="scope.$index" :disabled="scope.row.isRadioDisabled" class="radio" />
            </template>
          </el-table-column>
          <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">
            <template #default="scope">
              {{ scope.row.checkDestination === '1' ? '计量室' : scope.row.checkDestination === '2' ? '外送' : '' }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-radio__label) {
  display: none;
}
</style>