Newer
Older
xc-business-system / src / views / resource / software / info / list.vue
<!-- 软件一览表 -->
<script name="SoftwareInfoList" lang="ts" setup>
import type { DateModelType } from 'element-plus'
import { ElLoading, ElMessage, dayjs } from 'element-plus'
import type { IListQuery, ISoftwareInfo } from './software-info'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { exportSoftwareList, getSoftwareInfoList } from '@/api/resource/software'
import type { deptType } from '@/global'
import { getDictByCode } from '@/api/system/dict'
import { exportFile } from '@/utils/exportUtils'

const { proxy } = getCurrentInstance() as any
const router = useRouter()

// 查询条件
const searchQuery = ref<IListQuery>({
  softwareName: '', // 软件名称
  softwareVersion: '', // 版本号
  createUserName: '', // 创建人
  updateTimeStart: '', // 更新时间开始
  updateTimeEnd: '', // 更新时间结束
  groupCode: '', //		部门
  labCode: '', //		实验室
  offset: 1,
  limit: 20,
})
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])// 筛选时间段数据

// 表头
const columns = ref<TableColumn[]>([
  { text: '实验室', value: 'labCodeName', align: 'center' },
  { text: '部门', value: 'groupCodeName', align: 'center' },
  { text: '软件名称', value: 'softwareName', align: 'center' },
  { text: '版本号', value: 'softwareVersion', align: 'center' },
  { text: '创建人', value: 'createUserName', align: 'center' },
  { text: '更新时间', value: 'updateTime', align: 'center', width: '180' },
])
const softwareList = ref<Array<ISoftwareInfo>>([]) // 表格数据
const checkoutList = ref<Array<ISoftwareInfo>>([]) // 多选表格数据

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

// 逻辑
// 详情
const detail = (row: ISoftwareInfo) => {
  router.push({
    query: {
      type: 'detail',
      id: row.id,
    },
    path: 'software/detail',
  })
}

// 详情
const update = (row: ISoftwareInfo) => {
  router.push({
    query: {
      type: 'update',
      id: row.id,
    },
    path: 'software/detail',
  })
}

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    searchQuery.value.offset = 1
  }
  getSoftwareInfoList(searchQuery.value).then((response) => {
    if (response.code === 200) {
      softwareList.value = response.data.rows.map((item: ISoftwareInfo) => {
        return {
          ...item,
        }
      })
      total.value = parseInt(response.data.total)
    }
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}

const searchList = () => {
  fetchData(true)
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.value.limit = val.size
  }
  if (val && val.page) {
    searchQuery.value.offset = val.page
  }
  fetchData(true)
}

// 重置
const reset = () => {
  searchQuery.value = {
    softwareName: '', // 软件名称
    softwareVersion: '',
    createUserName: '',
    updateTimeStart: '',
    updateTimeEnd: '',
    groupCode: '', //		部门
    labCode: '', //		实验室
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}
// -----------------------------------------导出-------------------------------------------------------
// 导出
const exportAll = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '下载中请稍后',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  if (softwareList.value.length > 0) {
    const params = {
      softwareName: searchQuery.value.softwareName, // 软件名称
      softwareVersion: searchQuery.value.softwareVersion,
      createUserName: searchQuery.value.createUserName,
      updateTimeStart: searchQuery.value.updateTimeStart,
      updateTimeEnd: searchQuery.value.updateTimeEnd,
      groupCode: searchQuery.value.groupCode, //		部门
      labCode: searchQuery.value.labCode, //		实验室
      offset: 1,
      limit: 20,
      ids: checkoutList.value,
    }
    exportSoftwareList(params).then((res) => {
      const blob = new Blob([res.data])
      loading.close()
      exportFile(blob, '软件一览表.xlsx')
    })
    loading.close()
  }
  else {
    loading.close()
    ElMessage.warning('无数据可导出数据')
  }
}

// --------------------------------------字典-----------------------------------------------
const useDeptList = ref<deptType[]>([]) // 部门
const labDeptList = ref<deptType[]>([]) // 实验室
// 查询字典
const getDict = () => {
  // 实验室
  getDictByCode('bizLabCode').then((response) => {
    labDeptList.value = response.data
  })

  // 部门
  getDictByCode('bizGroupCode').then((response) => {
    useDeptList.value = response.data
  })
}
getDict()
// -------------------------------------------------------------------------------------

watch(dateRange, (val) => {
  if (val) {
    searchQuery.value.updateTimeStart = dayjs(val[0]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[0]).format('YYYY-MM-DD')
    searchQuery.value.updateTimeEnd = dayjs(val[1]).format('YYYY-MM-DD') === 'Invalid Date' ? '' : dayjs(val[1]).format('YYYY-MM-DD')
  }
  else {
    searchQuery.value.updateTimeStart = ''
    searchQuery.value.updateTimeEnd = ''
  }
})

onMounted(async () => {
  await getDict()
  searchList()
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-select
          v-model="searchQuery.labCode"
          placeholder="实验室"
          class="short-input"
          filterable
          clearable
        >
          <el-option v-for="item in labDeptList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select
          v-model="searchQuery.groupCode"
          placeholder="部门"
          class="short-input"
          filterable
          clearable
        >
          <el-option v-for="item in useDeptList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.softwareName" placeholder="软件名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.softwareVersion" placeholder="版本号" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.createUserName" placeholder="创建人" clearable />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="dateRange"
          class="short-input"
          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="导出" @click="exportAll" />
      </template>
      <normal-table
        id="reportTabel"
        :data="softwareList" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        :list-loading="loadingTable"
        is-showmulti-select
        @multi-select="handleSelectionChange"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column fixed="right" label="操作" align="center" width="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                详情
              </el-button>
              <el-button size="small" type="primary" link @click="update(row)">
                编辑
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>