Newer
Older
xc-metering-front / src / views / tested / device / info / components / list.vue
<!-- 设备信息表格 -->
<script lang="ts" setup name="DeviceInfoList">
import { reactive, ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { IlistObjType, IlistQueryType } from './interface'
import { delTextBtn, editTextBtn } from '@/utils/applyBtns'
import ApprovalDialog from '@/components/Approval/ApprovalDialog.vue'
import { cancelInfo, deleteInfo, editInfo, exportDevice, getInfoList, getInfoListPage, getInfoListTogether, importDevice, submitInfo } from '@/api/eqpt/device/info'
import { getDictByCode } from '@/api/system/dict'
import { SCHEDULE } from '@/utils/scheduleDict'
import { exportFile } from '@/utils/exportUtils'
const $props = defineProps({
  statusName: {
    type: String,
    default: '',
  },
  // 设备类型(1受检设备;2特种设备)
  equipmentType: {
    type: String,
    default: '1',
  },
})
const applyDict = ref<{ [key: string]: string }>({
  审批: '',
  草稿箱: '1',
  审批中: '3',
  已通过: '4',
  未通过: '5',
  已取消: '6',
})
const normalTableRef= ref()
const { proxy } = getCurrentInstance() as any
const listQuery = reactive<IlistQueryType>({
  certificateValidStart: '',
  certificateValidEnd: '',
  deptIds: '', // 使用部门
  usageStatus: '', // 使用状态
  usePosition: '', // 使用岗位
  equipmentNo: '', // 统一编号
  equipmentName: '', // 设备名称
  equipmentType: $props.equipmentType, // 设备类型(1受检设备;2特种设备)
  offset: 1,
  limit: 20,
  approvalStatus: '',
  formId: SCHEDULE.DEVICE_INFO_APPROVAL,
})
const columns = ref([
  {
    text: '统一编号',
    value: 'equipmentNo',
    align: 'center',
  },
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '型号规格',
    value: 'model',
    align: 'center',
  },
  {
    text: '出厂编号',
    value: 'manufactureNo',
    align: 'center',
  },
  {
    text: '使用岗位',
    value: 'usePosition',
    align: 'center',
  },
  {
    text: '计量标识',
    value: 'meterIdentifyName',
    align: 'center',
  },
  {
    text: '检定周期',
    value: 'checkCycle',
    align: 'center',
  },
  {
    text: '证书有效期',
    value: 'certificateValid',
    align: 'center',
  },
])
const allColums = ref([
  {
    text: '使用状态',
    value: 'usageStatusName',
    align: 'center',
  },
  {
    text: '备注',
    value: 'remark',
    align: 'center',
  },
])
const otherColums = ref([
  {
    text: '审批类型',
    value: 'approvalTypeName',
    align: 'center',
  },
  {
    text: '创建人',
    value: 'createUserName',
    align: 'center',
  },
  {
    text: '创建时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '审批状态',
    value: 'approvalStatusName',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取列表数据
const fetchData = (isNowPage = true) => {
  list.value = []
  listLoading.value = true
  // if (!isNowPage) {
  //   // 是否显示当前页,否则跳转第一页
  //   listQuery.offset = 1
  // }
  getInfoListPage(listQuery, $props.statusName).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    normalTableRef.value.handleChecked()
    listLoading.value = false
  }).catch((_) => {
    listLoading.value = false
  })
}
// 获取使用状态列表
const useStatusList = ref()
const fetchUseStatus = () => {
  getDictByCode('eqptDeviceUseStatus').then((res) => {
    useStatusList.value = res.data
  })
}
fetchUseStatus()
// 查询数据
const search = () => {
  fetchData(false)
}
// 证书有效期开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  if (newVal) {
    listQuery.certificateValidStart = `${newVal[0]} 00:00:00`
    listQuery.certificateValidEnd = `${newVal[1]} 23:59:59`
  }
  else {
    listQuery.certificateValidStart = ''
    listQuery.certificateValidEnd = ''
  }
})
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.equipmentNo = ''
  listQuery.equipmentName = ''
  listQuery.usageStatus = ''
  listQuery.usePosition = ''
  listQuery.certificateValidEnd = ''
  listQuery.certificateValidStart = ''
  listQuery.deptIds = ''
  listQuery.offset = 1
  listQuery.limit = 20
  search()
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  // search()
  fetchData()
}
// 获取聚合表格数据
const togetherLoading = ref(true)
const togetherList = ref([])
const fetchTogetherData = () => {
  togetherLoading.value = true
  getInfoListTogether(listQuery).then((res) => {
    console.log('获取聚合表格数据')
    togetherList.value = res.data
    togetherLoading.value = false
  })
}
// 获取展开行内表格数据
const expandLoading = ref(true)
const expandList = ref([])
const fetchExpandData = (params: any) => {
  expandLoading.value = true
  getInfoList(params).then((res) => {
    expandList.value = res.data
    expandLoading.value = false
  })
}
const getRowKeys = (row: any) => { // 获取当前行id
  return row.equipmentName // 这里看这一行中需要根据哪个属性值是id
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 控制展开行
const expands = ref()
const expandChange = (row: any, expandedRows: any) => {
  if (expandedRows.length) {
    expands.value = []
    if (row) {
      expands.value.push(row.equipmentName)
    }
  }
  else {
    expands.value = []
  }
}
watch(() => expands.value, (newVal) => {
  fetchExpandData({ equipmentName: newVal[0] })
},
{
  deep: true,
})
const $router = useRouter()
// 新建编辑操作
const handler = (row: IlistObjType, type: string) => {
  $router.push({
    path: `/${$props.equipmentType === '1' ? 'dinfo' : 'speciallist'}/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
      statusName: $props.statusName,
      equipmentType: $props.equipmentType,
    },
  })
}
// 导出列表
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),
    }
    exportDevice(data).then((res) => {
      exportFile(res.data, $props.equipmentType === '1' ? '设备信息' : '特种设备')
      loading.close()
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('无可导出内容')
  }
}
const fileRef = ref() // 文件上传input,获取input的引用
const onFileChange = (event: any) => {
  // 原生上传
  // console.log(event.target.files)
  if (event.target.files?.length !== 0) {
    // 创建formdata对象
    const fd = new FormData()
    fd.append('file', event.target.files[0])
    // fd.append('equipmentType', $props.equipmentType)
    const loading = ElLoading.service({
      lock: true,
      background: 'rgba(255, 255, 255, 0.8)',
    })
    importDevice(fd, $props.equipmentType).then((res) => {
      if (res.code === 200) {
        ElMessage.success('上传成功')
        fileRef.value.value = ''
        loading.close()
        fetchData(true)
      }
      else {
        // ElMessage.error(res.message)
        loading.close()
      }
    }).catch(() => {
      // ElMessage.error(err.message)
      loading.close()
    })
  }
}
// 导入
const importList = () => {
  fileRef.value.click()
}
// 详情
const detail = (row: IlistObjType) => {
  if ($props.statusName === '草稿箱' || $props.statusName === '未通过' || $props.statusName === '已取消') {
    handler(row, 'update')
  }
  else {
    $router.push({
      path: `/${$props.equipmentType === '1' ? 'info' : 'speciallist'}/detail`,
      query: {
        row: JSON.stringify(row),
        id: row.id,
        statusName: $props.statusName,
        equipmentType: $props.equipmentType,
      },
    })
  }
}
// 删除
const valiate = (val: string | any) => {
  if (val) {
    return true
  }
  else {
    return false
  }
}
// 删除
const delHandler = (row: IlistObjType) => {
  if ($props.statusName === '全部') {
    // 删除设备
    ElMessageBox.prompt('请填写删除原因', '确认删除', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      inputValidator: valiate,
      inputErrorMessage: '请输入原因',
    })
      .then(({ value }) => {
        console.log(row, 'row')
        editInfo({
          approvalType: '3',
          equipmentId: row.id,
          id: null,
        }).then((res) => {
          // ElMessage.success('操作成功')
          // search()
          submitInfo({ id: res.data, formId: SCHEDULE.DEVICE_INFO_APPROVAL, reason: value, processId: row.processId }).then((res) => {
            ElMessage.success('操作成功')
            search()
          })
        })
        // ElMessage({
        //   type: 'success',
        //   message: `Your email is:${value}`,
        // })
      })
  }
  else {
    ElMessageBox.confirm(
      '确认删除此记录吗?',
      '确认',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    ).then(() => {
    // 分删除设备和删除记录
      deleteInfo({ id: row.id }).then((res) => {
        ElMessage.success('操作成功')
        // close()
        search()
      })
    })
  }
}
// 取消
const canHandler = (row: IlistObjType) => {
  ElMessageBox.confirm(
    '确认取消此申请吗?',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    cancelInfo({ id: row.id, processInstanceId: row.processId, comments: '' }).then((res) => {
      ElMessage.success('操作成功')
      // close()
      search()
    })
  })
}
// 提交
const submit = (row: any) => {
  ElMessageBox.confirm(
    '确认提交吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then((res) => {
    submitInfo({ id: row.id, formId: SCHEDULE.DEVICE_INFO_APPROVAL }).then((res) => {
      ElMessage.success('已提交')
      search()
    })
  })
}
// 同意拒绝
const approvalDialogRef = ref()
const approveHandler = (row: any, type: string) => {
  approvalDialogRef.value.initDialog(type, row.taskId, row.processId, row.id)
}
// 切换表格标识   true:普通表格  false:聚合表格
const tableFlage = ref(true)
const changeTable = () => {
  tableFlage.value = !tableFlage.value
  if (tableFlage.value) {
    //  获取普通表格数据
    fetchData()
  }
  else {
    // 获取聚合表格数据
    fetchTogetherData()
  }
}
// 审批状态发生变化
watch(() => $props.statusName, (newVal) => {
  if (newVal) {
    listQuery.approvalStatus = applyDict.value[newVal] as string
    fetchData()
    tableFlage.value = true
  }
},
{
  deep: true,
  immediate: true,
})
const permUrl = ref({
  edit: '/tested/device/info/edit',
  del: '/tested/device/info/delete',
  submit: '/tested/device/info/submit',
  cancel: '/tested/device/info/cancel',
  agree: '/tested/device/info/agree',
  reject: '/tested/device/info/reject',
})
</script>

<template>
  <app-container>
    <!-- 审批弹窗 -->
    <approval-dialog ref="approvalDialogRef" @on-success="search" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.equipmentNo" placeholder="统一编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.equipmentName" placeholder="设备名称" clearable />
      </search-item>
      <!-- <search-item>
        <el-input v-model.trim="listQuery.equipmentName" placeholder="设备名称" clearable />
      </search-item> -->
      <search-item>
        <el-input v-model.trim="listQuery.usePosition" placeholder="使用岗位" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.usageStatus" placeholder="使用状态" clearable>
          <el-option v-for="item in useStatusList" :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="证书有效期结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <template v-if="$props.statusName === '全部'" #btns-right>
        <icon-button icon="icon-change" title="切换列表状态" @click="changeTable" />
        <icon-button icon="icon-sweep" title="扫描" />
        <icon-button icon="icon-add" title="新增" @click="handler({}, 'create')" />
        <icon-button icon="icon-import" title="批量导入" @click="importList" />
        <icon-button icon="icon-template" title="模板下载" />
        <icon-button icon="icon-export" title="导出" @click="exportList" />
        <input ref="fileRef" style="display: none;" type="file" accept=".xls,.xlsx" @change="onFileChange">
      </template>
      <div v-if=" tableFlage ">
        <!-- 普通表格 -->
        <normal-table
          v-show=" tableFlage "
          ref="normalTableRef" :data="list" :total="total" :columns="columns" :query="listQuery"
          :list-loading="listLoading" :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
        >
          <template #columns>
            <!-- 全部状态 -->
            <el-table-column
              v-for=" column of allColums " v-if=" $props.statusName === '全部' " :key=" column.value "
              :label=" column.text " :prop=" column.value " :align=" column.align "
              :show-overflow-tooltip="true"
            >
              <template #default=" scope ">
                <span>
                  {{ scope.row[column.value] }}</span>
              </template>
            </el-table-column>
            <!-- 其他审批状态 -->
            <el-table-column
              v-for=" column of otherColums " v-else :key=" column.value " :label=" column.text "
              :prop=" column.value " :align=" column.align "
              :show-overflow-tooltip="true"
            >
              <template #default=" scope ">
                <span>
                  {{ scope.row[column.value] }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="160" align="center">
              <template #default=" scope ">
                <el-button link type="primary" size="small" @click="detail(scope.row)">
                  查看
                </el-button>
                <el-button
                  v-if="proxy.buttonPerm.edit.if({ approvalStatusName: $props.statusName }, permUrl.edit)" link type="primary" size="small"
                  @click="handler(scope.row, 'update')"
                >
                  编辑
                </el-button>
                <el-button
                  v-if="proxy.buttonPerm.agree.if({ approvalStatusName: $props.statusName }, permUrl.agree)" link type="primary" size="small"
                  @click="approveHandler(scope.row, 'agree')"
                >
                  同意
                </el-button>
                <el-button
                  v-if="proxy.buttonPerm.reject.if({ approvalStatusName: $props.statusName }, permUrl.reject)" link type="primary" size="small"
                  @click="approveHandler(scope.row, 'refuse')"
                >
                  拒绝
                </el-button>
                <el-button
                  v-if="proxy.buttonPerm.submit.if({ approvalStatusName: $props.statusName }, permUrl.submit)" link type="primary" size="small"
                  @click="submit(scope.row)"
                >
                  提交
                </el-button>
                <el-button
                  v-if="proxy.buttonPerm.cancel.if({ approvalStatusName: $props.statusName }, permUrl.cancel)" link type="primary" size="small"
                  @click="canHandler(scope.row)"
                >
                  取消
                </el-button>
                <el-button
                  v-if="proxy.buttonPerm.delete.if({ approvalStatusName: $props.statusName }, permUrl.del)" link type="danger" size="small"
                  @click="delHandler(scope.row)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </div>
      <div v-if=" !tableFlage ">
        <!-- 展开行表格 -->
        <normal-table
          v-show=" !tableFlage " class="nortable-header" :data=" [] " :total=" 0 " :columns=" columns " :options="
            {
              needIndex: false, // 是否需要序号列
              border: true, // 是否需要上方边框
            }
          " :pagination=" false " :is-showmulti-select=" true " :query="{}"
        />
        <el-table
          v-show=" !tableFlage " v-loading=" togetherLoading " :data=" togetherList " border style="width: 100%;"
          :row-key=" getRowKeys " :expand-row-keys=" expands " :show-header=" false " @expand-change=" expandChange "
        >
          <el-table-column type="expand">
            <template #default="props">
              <normal-table
                :data=" expandList " :total=" 0 " :columns=" columns " :list-loading=" expandLoading " :options="
                  {
                    needIndex: false, // 是否需要序号列
                    border: true, // 是否需要上方边框
                  }
                " :pagination=" false " :is-showmulti-select=" true " :show-header=" false " :query="{}"
              />
            </template>
          </el-table-column>
          <el-table-column label="设备名称" prop="equipmentName" align="center" />
          <el-table-column label="数量" align="center">
            <template #default=" scope ">
              共计{{ scope.row.count }}台
            </template>
          </el-table-column>
        </el-table>
      </div>
    </table-container>
  </app-container>
</template>

<style lang="scss" scoped>
.nortable-header {
  ::v-deep(.el-table__body-wrapper) {
    display: none;
  }
}
</style>