Newer
Older
xc-business-system / src / views / resource / environment / require / list.vue
dutingting on 31 Dec 13 KB 需求开发
<!-- 工作间环境条件要求一览表 -->
<script name="EnvironmentRequireList" lang="ts" setup>
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { IConditionRequire, IListQuery, IWorkRoomRequireInfo } from './require-confition-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import type { IDictType } from '@/commonInterface/resource-interface'
import { deleteEnvRequire, detailEnvRequire, getEnvRequireList, getStream, saveEnvRequire, updateEnvRequire } from '@/api/resource/environment'
import { getDictByCode } from '@/api/system/dict'
import type { deptType } from '@/global'
import { printPdf } from '@/utils/printUtils'

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

// 查询条件
const searchQuery = ref<IListQuery>({
  workRoom: '', // 房间
  groupCode: '', //		部门
  labCode: '', //		实验室
  standardEquipmentName: '', // 标准名称
  offset: 1,
  limit: 20,
})
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading
const expandLoading = ref(false) // 展开表格的loading

// 表头
const columns = ref<TableColumn[]>([
  { text: '实验室', value: 'labCodeName', align: 'center' },
  { text: '部门', value: 'groupCodeName', align: 'center' },
  { text: '工作间', value: 'workRoom', align: 'center', width: '200' },
  { text: '标准名称', value: 'standardEquipmentName', align: 'center', width: '200' },
  { text: '开展量传参数', value: 'params', align: 'center', width: '200' },
  { text: '环境条件要求', value: 'environmentRequire', align: 'center', width: '200' },
  { text: '环境条件依据', value: 'environmentAccord', align: 'center', width: '200' },
  { text: '备注', value: 'remark', align: 'center' },
])
const requireList = ref<Array<IWorkRoomRequireInfo>>([]) // 表格数据
const expandRowKey = ref<string>('')
const conditionListExpand = ref<Array<IConditionRequire>>([])
const checkoutList = ref<Array<IWorkRoomRequireInfo>>([]) // 多选表格数据

// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: { id: string }) => item.id)
}
// 字典值
const environmentAccordDict = ref<IDictType[]>([])

const expandRowKeys = computed(() => {
  return [expandRowKey.value]
})

// 逻辑
const add = () => {
  router.push({
    query: {
      type: 'create',
    },
    path: 'requireDetail',
  })
}

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

// 编辑
const update = (row: IWorkRoomRequireInfo) => {
  router.push({
    query: {
      type: 'update',
      id: row.id,
    },
    path: 'requireDetail',
  })
}

const getConditionRequireList = (room: IWorkRoomRequireInfo) => {
  expandLoading.value = true
  detailEnvRequire({ id: room.id }).then((res) => {
    if (res.code === 200) {
      conditionListExpand.value = res.data.conditionList.map((item: IConditionRequire) => {
        return {
          ...item,
          environmentAccordName: environmentAccordDict.value.filter(dict => dict.value === item.environmentAccord)[0].name,
        }
      })

      expandLoading.value = false
    }
  }).catch(() => {
    expandLoading.value = false
  })
}

const detailAllEnvRequire = async () => {
  for (const reqItem of requireList.value) {
    await detailEnvRequire({ id: reqItem.id }).then((res) => {
      if (res.code === 200) {
        res.data.conditionList.forEach((con: IConditionRequire) => {
          reqItem.params += `${con.params};\n`
          // const accordName = environmentAccordDict.value.filter(dict => dict.value === con.environmentAccord)[0].name
          // reqItem.environmentAccordName += `${accordName};\n`
          reqItem.environmentAccord += `${con.environmentAccord};\n`
          reqItem.environmentRequire += `${con.environmentRequire};\n`
          if (con.remark !== '') {
            reqItem.remark += `${con.remark};\n`
          }
        })
      }
    })

    reqItem.params = reqItem.params!.substring(0, reqItem.params!.length - 2)
    reqItem.environmentRequire = reqItem.environmentRequire!.substring(0, reqItem.environmentRequire!.length - 2)
    reqItem.environmentAccordName = reqItem.environmentAccordName!.substring(0, reqItem.environmentAccordName!.length - 2)
    reqItem.remark = reqItem.remark!.substring(0, reqItem.remark!.length - 2)
  }
}

// 数据查询
const fetchData = async (isNowPage = false) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    searchQuery.value.offset = 1
  }
  await getEnvRequireList(searchQuery.value).then((response) => {
    if (response.code === 200) {
      requireList.value = response.data.rows.map((item: IWorkRoomRequireInfo) => {
        return {
          ...item,
          remark: '',
          params: '',
          environmentRequire: '',
          environmentAccordName: '',
        }
      })
      total.value = parseInt(response.data.total)

      detailAllEnvRequire()
    }
    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)
}

// 改变页容量
function handleSizeChange(val: number) {
  changePage({ size: val })
}
// 改变当前页
function handleCurrentChange(val: number) {
  changePage({ page: val })
}

const deleteById = (row: IWorkRoomRequireInfo) => {
  ElMessageBox.confirm(`是否删除工作间 ${row.workRoom} 环境要求一览表`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    // 删除记录
    deleteEnvRequire({ id: row.id }).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`工作间 ${row.workRoom} 环境要求一览表删除成功`)
        fetchData()
      }
      else {
        ElMessage.error(`工作间 ${row.workRoom} 环境要求一览表删除失败: ${res.message}`)
      }
    })
  })
}

// 重置
const reset = () => {
  searchQuery.value = {
    workRoom: '', // 房间
    groupCode: '', //		部门
    labCode: '', //		实验室
    standardEquipmentName: '', // 标准名称
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 展开行 每次只能展开一行
const expandCurrentRow = (row: IWorkRoomRequireInfo) => {
  if (expandRowKey.value === '') {
    // 全部收起的状态 展开当前点击的行
    expandRowKey.value = row.id
  }
  else if (expandRowKey.value !== row.id) {
    // 已经有展开的行 切换到当前打开的行
    expandRowKey.value = row.id
  }
  else {
    // 点击的是当前已经展开的行 收起
    expandRowKey.value = ''
  }
}

// 点击行
const expandClickedHandler = async (row: IWorkRoomRequireInfo, event: any) => {
  expandCurrentRow(row)
  if (expandRowKey.value !== '') {
    getConditionRequireList(row)
  }
}

const tableRowExpandChange = (row: IWorkRoomRequireInfo) => {
  expandCurrentRow(row)
}

const getEnvironmentAccord = async () => {
  await getDictByCode('bizEnvironmentAccord').then((res) => {
    if (res.code === 200) {
      environmentAccordDict.value = res.data
      sessionStorage.setItem('bizEnvironmentAccord', JSON.stringify(res.data))
    }
  })
}

const stream = ref() as any // 流

// 获取流
const fetchStream = async () => {
  const loading = ElLoading.service({
    lock: true,
    text: '加载中...',
    background: 'rgba(255, 255, 255, 0.6)',
  })
  const res = await getStream({ ids: checkoutList.value, pdf: true })
  stream.value = res.data
  loading.close()
}

// 点击打印
const handlePrint = () => {
  if (!checkoutList.value.length) {
    ElMessage.warning('请至少选择一项')
    return false
  }
  fetchStream().then(() => {
    const blobUrl = URL.createObjectURL(stream.value)
    printPdf(blobUrl)
  })
}

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

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

// --------------------------------------------------------------------------------------------------

const getDict = async () => {
  getEnvironmentAccord()
  getDictFun()
}

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.workRoom" placeholder="工作间" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.standardEquipmentName" placeholder="标准名称" clearable />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container title="环境要求一览表">
      <!-- 表头区域 -->
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm(`/resource/environment/require/add`)" icon="icon-add" title="新增" @click="add" />
        <icon-button icon="icon-print" title="打印" @click="handlePrint" />
      </template>

      <!-- 表格区域 -->
      <el-table
        ref="requireTableRef"
        v-loading="loadingTable" :data="requireList" style="width: 100%;"
        border row-key="id"
        :expand-row-keys="expandRowKeys"
        @expand-change="tableRowExpandChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="38" fixed="left" />

        <el-table-column type="expand" width="1">
          <el-table
            v-loading="expandLoading" :show-header="false" :data="conditionListExpand"
            border class="expand-approval-all"
          >
            <el-table-column label="开展量传参数" prop="params" align="center" />
            <el-table-column label="环境条件要求" prop="environmentRequire" align="center" />
            <el-table-column label="环境条件依据" prop="environmentAccordName" align="center" width="200" />
            <el-table-column label="备注" prop="remark" align="center" />
          </el-table>
        </el-table-column>

        <el-table-column align="center" label="序号" width="55" type="index" />
        <el-table-column
          v-for="item in columns"
          :key="item.value"
          :prop="item.value"
          :label="item.text"
          :width="item.width"
          align="center"
        />

        <el-table-column fixed="right" label="操作" align="center" width="160">
          <template #default="{ row }">
            <el-button size="small" type="primary" link @click="expandClickedHandler(row, $event)">
              <template v-if="expandRowKey !== row.id">
                展开
              </template>
              <template v-else>
                收起
              </template>
            </el-button>
            <el-button size="small" type="primary" link @click="detail(row)">
              详情
            </el-button>
            <el-button v-if="proxy.hasPerm(`/resource/environment/require/edit`)" size="small" type="primary" link @click="update(row)">
              编辑
            </el-button>

            <el-button v-if="proxy.hasPerm(`/resource/environment/require/del`)" size="small" type="danger" link @click="deleteById(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div style="width: 100%;margin-top: 10px;">
        <el-pagination
          :current-page="searchQuery.offset"
          :page-sizes="[5, 10, 20, 30, 50, 100, 150, 200]"
          :page-size="searchQuery.limit"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </table-container>
  </app-container>
</template>

<style scoped lang="scss">
:deep(.el-table__expand-icon) {
  visibility: hidden !important;
}

:deep(.el-table__expanded-cell) {
  padding-top: 0 !important;
}

:deep(.el-table .cell) {
  white-space: pre-line;
}

/* 全部状态下的列表 */
.expand-approval-all {
  width: calc(100% - 615px);
  margin-left: 455px;
}
</style>