Newer
Older
smart-metering-front / src / views / device / borrow / borrwoApply.vue
<!-- 设备借用申请 -->
<script lang="ts" setup name="BorrowApply">
import { ref } from 'vue'
import type { Ref } from 'vue'
import type { DateModelType } from 'element-plus'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { SCHEDULE } from '@/utils/scheduleDict'
import ButtonBox from '@/components/buttonBox/buttonBox.vue'
import ApprovalDialog from '@/components/Approval/ApprovalDialog.vue'
import type { IMenu } from '@/components/buttonBox/buttonBox'
import type { IlistQuery, IlistType, dictType } from '@/views/device/borrow/borrow'
import { cancelApproval, deleteApply, exportApply, getBorrowApplyList, submitApply } from '@/api/device/borrow'
import {
  submitReceiveApplyList,
} from '@/api/device/receive'

import { getDictByCode } from '@/api/system/dict'
import { toTreeList } from '@/utils/structure'
import type { userType } from '@/views/system/user/user-interface'
import type { deptType } from '@/views/device/standingBook/standingBook-interface'
import { getUserList } from '@/api/system/user'
import { getDeptTreeList } from '@/api/system/dept'
import { exportFile } from '@/utils/exportUtils'
import { printJSON } from '@/utils/printUtils'
import type { TableColumn } from '@/components/NormalTable/table_interface'
const { proxy } = getCurrentInstance() as any
const active = ref('') // 选中的按钮
const activeTitle = ref('') // active对应的审批状态名字
const approvalDialog = ref() // 审批对话框显隐
const menu = ref<IMenu[]>([]) // 审批状态按钮组合
// 筛选时间段数据
const timeRange = ref<[DateModelType, DateModelType]>(['', ''])
const loadingTable = ref(false) // 表格loading
const list = ref<IlistType[]>([])// 表格数据
const total = ref(20)// 页码总数
const useDeptList = ref<deptType[]>([]) // 部门列表
const usePersonList = ref<userType[]>([]) // 申请人列表(用户)
const usePersonOptions = ref<userType[]>([]) // 申请人列表(用户)--模糊搜索数据
const applyPersonLoading = ref(false) // 申请人模糊搜索框loading

// 查询条件
const listQuery: Ref<IlistQuery> = ref({
  applyNo: '', // 申请编号
  applyName: '', // 申请名称
  applyUnit: '', // 申请部门id
  applyUnitName: '', // 申请部门名称
  applyPerson: '', // 申请人id
  applyPersonName: '', // 申请人姓名
  startTime: '', // 领用开始日期
  endTime: '', // 领用结束日期
  approvalStatus: active.value, // 	审批状态 1 2 3 对应待借用|已借用|已归还
  processResult: '', // 	处置结果
  applyType: '2', // 申请类型 借用2
  createUser: '', // 创建人
  formId: SCHEDULE.DEVICE_BORROW_APPROVAL,
  offset: 1,
  limit: 20,
})

const permUrl = {
  edit: '/device/borrow/borrowapply/update', // 编辑
  agree: '/device/borrow/borrowapply/agree', // 同意
  reject: '/device/borrow/borrowapply/reject', // 驳回
  refuse: '/device/borrow/borrowapply/refuse', // 拒绝
  cancel: '/device/borrow/borrowapply/cancel', // 取消
  delete: '/device/borrow/borrowapply/delete', // 删除
}

const approvalStatusMap = ref({}) as any// 审批状态字典{1:草稿箱}
const approvalStatusReserveMap = ref({}) as any// 审批状态字典{草稿箱: 1}

// 选中的内容
const checkoutList = ref<string[]>([])
// 表格数据
const columns = ref<TableColumn[]>([
  {
    text: '申请编号',
    value: 'applyNo',
    align: 'center',
    width: '180',
  },
  {
    text: '申请名称',
    value: 'applyName',
    align: 'center',
  },
  {
    text: '申请部门',
    value: 'applyUnitName',
    align: 'center',
  },
  {
    text: '申请人',
    value: 'applyPersonName',
    align: 'center',
  },
  {
    text: '申请时间',
    value: 'time',
    align: 'center',
    width: '180',
  },
  {
    text: '审批状态',
    value: 'approvalStatusName',
    align: 'center',
    width: '110',
  },
])
// 时间变更
watch(timeRange, (val) => {
  if (val) {
    listQuery.value.startTime = `${val[0]}`
    listQuery.value.endTime = `${val[1]}`
  }
  else {
    listQuery.value.startTime = ''
    listQuery.value.endTime = ''
  }
})
// 数据查询
const fetchData = (isNowPage = false) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.value.offset = 1
  }
  // listQuery.value.startTime = timeRange.value[0] as string || ''
  // listQuery.value.endTime = timeRange.value[1] as string || ''
  listQuery.value.approvalStatus = active.value
  getBorrowApplyList(listQuery.value).then((response) => {
    list.value = response.data.rows.map((item: IlistType) => {
      return {
        ...item,
        approvalStatus: approvalStatusMap.value[item.approvalStatus],

      }
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}

// 获取字典值
const getDict = async () => {
  // 审批状态
  const res = await getDictByCode('approvalStatus')
  // 审批状态字典 {1:草稿箱}
  res.data.forEach((item: any) => {
    approvalStatusMap.value[`${item.value}`] = item.name
  })

  // 审批状态字典 {草稿箱: 1}
  res.data.forEach((item: any) => {
    approvalStatusReserveMap.value[item.name] = `${item.value}`
  })

  // 制作右上角的菜单
  res.data.forEach((item: dictType) => {
    if (item.name === '全部' || item.name === '草稿箱'
      || item.name === '待审批' || item.name === '审批中'
      || item.name === '已通过' || item.name === '未通过'
      || item.name === '已取消') {
      menu.value.push({
        name: item.name,
        id: `${item.value}`,
      })
    }
  })
}

const clearList = () => {
  listQuery.value = {
    applyNo: '', // 申请编号
    applyName: '', // 申请名称
    applyUnit: '', // 申请部门
    applyPerson: '', // 申请人
    startTime: '', // 申请开始日期
    endTime: '', // 申请结束日期
    approvalStatus: active.value, // 	审批状态  // 	审批状态 1 2 3 对应待借用|已借用|已归还
    processResult: '', // 	处置结果
    applyType: '2', // 申请类型 借用2
    createUser: '', // 创建人
    formId: SCHEDULE.DEVICE_BORROW_APPROVAL,
    offset: 1,
    limit: 20,
  }
  timeRange.value = ['', '']
  fetchData(true)
}

// 切换tab状态
const changeCurrentButton = (val: string) => {
  active.value = val
  activeTitle.value = menu.value.find(item => item.id === val)!.name
  window.sessionStorage.setItem('borrowButtonBoxActive', val)
  clearList()
}

// 获取用户列表(增加模糊查询)
const fetchUserList = () => {
  getUserList({ offset: 1, limit: 999999 }).then((res: any) => {
    usePersonList.value = res.data.rows
    usePersonOptions.value = res.data.rows
  })
}

// 获取使用部门
const fetchDeptTreeList = () => {
  getDeptTreeList().then((res: any) => {
    if (res.data) { // 将列表转树结构
      useDeptList.value = toTreeList(res.data, '0', true)
    }
  })
}

const $router = useRouter()

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

// 搜索
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)
}

// 打印列表
function printList() {
  // 打印列
  const properties = columns.value.map((item) => {
    return {
      field: item.value,
      displayName: item.text,
    }
  })
  if (checkoutList.value.length <= 0 && list.value.length > 0) {
    printJSON(list.value, properties, '设备借用申请列表')
  }
  else if (checkoutList.value.length > 0) {
    const printList = list.value.filter((item: IlistType) => checkoutList.value.includes(item.id))
    printJSON(printList, properties, '设备借用申请列表')
  }
  else {
    ElMessage.warning('无可打印内容')
  }
}

// 选择器模糊查询
const remoteMethod = (query: string) => {
  if (query) {
    applyPersonLoading.value = true
    setTimeout(() => {
      applyPersonLoading.value = false
      usePersonOptions.value = usePersonList.value.filter((item) => {
        return item.name.toLowerCase().includes(query.toLowerCase())
      })
    }, 200)
  }
  else {
    usePersonOptions.value = usePersonList.value
  }
}

// 审批结束回调
const approvalSuccess = () => {
  fetchData(true)
}

// 新建
const add = () => {
  $router.push({ path: '/borrow/add', query: { typeValue: 'add' } })
}

// 操作
const handleEdit = (row: IlistType, val: string, title = '') => {
  if (val === '取消') {
    const params = {
      processInstanceId: row.processId!,
      comments: '',
    }
    ElMessageBox.confirm(
      '确认取消该审批吗?',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
      .then(() => {
        cancelApproval(params).then((res) => {
          ElMessage({
            type: 'success',
            message: '取消成功',
          })
          fetchData(true)
        })
      })
  }
  else if (val === '删除') {
    ElMessageBox.confirm(
      '确认删除该审批吗?',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
      .then(() => {
        deleteApply({ id: row.id, taskId: row.taskId }).then((res) => {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          fetchData(true)
        })
      })
  }
  else if (val === '提交') {
    const postData = {
      id: row.id,
      formId: SCHEDULE.DEVICE_BORROW_APPROVAL,
      processId: row.processId,
      // ...row.approvalStatusName === '已取消' && { processId: row.processId },
    }
    ElMessageBox.confirm(
      '确认提交该审批吗?',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
      .then(() => {
        submitReceiveApplyList(postData).then((res) => {
          ElMessage({
            type: 'success',
            message: '已提交',
          })
          fetchData(true)
        })
      })
  }
  else if (val === '同意') {
    approvalDialog.value.initDialog('agree', row.taskId)
  }
  else if (val === '驳回') {
    approvalDialog.value.initDialog('reject', row.taskId)
  }
  else if (val === '拒绝') {
    approvalDialog.value.initDialog('refuse', row.taskId)
  }
  else if (val === '查看' || val === '编辑') {
    $router.push({
      path: `/borrow/${title}`,
      query: {
        typeValue: title,
        approvalStatus: approvalStatusReserveMap[row.approvalStatus],
        id: row.id,
        taskId: row.taskId,
        processId: row.processId, // 查询审批记录使用
        approvalStatusName: row.approvalStatusName,
        decisionItem: `${row.decisionItem}`,
      },
    })
  }
}
const $route = useRoute()
onMounted(async () => {
  await getDict() // 获取字典-审批状态
  if (window.sessionStorage.getItem('borrowButtonBoxActive') !== 'undefined' && window.sessionStorage.getItem('borrowButtonBoxActive') !== '' && window.sessionStorage.getItem('borrowButtonBoxActive') !== null) {
    active.value = window.sessionStorage.getItem('borrowButtonBoxActive')!
  }
  else {
    active.value = menu.value.find(item => item.name === '全部')!.id as string // 全部
    activeTitle.value === '全部'
  }
  if ($route.query.fromModule === 'workbench') {
    active.value = menu.value.find(item => item.name === '待审批')!.id! // 全部
    activeTitle.value === '待审批'
  }
  nextTick(() => {
    fetchData(true) // 获取表格数据
    fetchDeptTreeList() // 获取使用部门
    fetchUserList() // 获取人员列表
  })
})
</script>

<template>
  <app-container>
    <search-area
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.applyNo"
          placeholder="申请编号"
          class="short-input"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.applyName"
          placeholder="申请名称"
          class="short-input"
          clearable
        />
      </search-item>
      <search-item>
        <dept-select v-model="listQuery.applyUnit" placeholder="请选择申请部门" :data="useDeptList" />
      </search-item>
      <search-item>
        <el-select
          v-model="listQuery.applyPerson"
          placeholder="请选择申请人"
          style="width: 100%;"
          filterable
          remote
          remote-show-suffix
          :remote-method="remoteMethod"
          :loading="applyPersonLoading"
        >
          <el-option v-for="item in usePersonOptions" :key="item.id" :label="item.name" :value="item.id" />
        </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-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/device/borrow/borrowapply/add')" icon="icon-add" title="新建" type="primary" @click="add" />
        <icon-button v-if="proxy.hasPerm('/device/borrow/borrowapply/print')" icon="icon-print" title="打印" type="primary" @click="printList" />
      </template>
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" is-showmulti-select @change="changePage" @multiSelect="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"
            :width="(activeTitle === '全部') ? 160 : (activeTitle === '草稿箱' || activeTitle === '已取消') ? 160 : (activeTitle === '已通过' || activeTitle === '未通过' || activeTitle === '审批中') ? 100 : 160"
            fixed="right"
          >
            <template #default="{ row }">
              <el-button
                size="small"
                type="primary"
                link
                @click="handleEdit(row, '查看', 'detail')"
              >
                查看
              </el-button>
              <el-button
                v-if="proxy.buttonPerm.agree.if(row, permUrl.agree)"
                size="small"
                link
                type="primary"
                :disabled="proxy.buttonPerm.agree.disabled(row)"
                @click="handleEdit(row, '同意')"
              >
                同意
              </el-button>
              <el-button
                v-if="proxy.buttonPerm.reject.if(row, permUrl.reject)"
                size="small"
                link
                type="primary"
                :disabled="proxy.buttonPerm.reject.disabled(row)"
                @click="handleEdit(row, '驳回')"
              >
                驳回
              </el-button>
              <el-button
                v-if="proxy.buttonPerm.refuse.if(row, permUrl.refuse)"
                size="small"
                link
                type="danger"
                :disabled="proxy.buttonPerm.refuse.disabled(row)"
                @click="handleEdit(row, '拒绝')"
              >
                拒绝
              </el-button>
              <el-button
                v-if="proxy.buttonPerm.edit.if(row, permUrl.edit)"
                size="small"
                link
                type="primary"
                @click="handleEdit(row, '编辑', 'edit')"
              >
                编辑
              </el-button>
              <el-button
                v-if="proxy.buttonPerm.submit.if(row, permUrl.edit)"
                size="small"
                link
                type="primary"
                @click="handleEdit(row, '提交')"
              >
                提交
              </el-button>
              <!-- 是发起者且审批中可以取消 -->
              <el-button
                v-if="proxy.buttonPerm.cancel.if(row, permUrl.cancel)"
                size="small"
                link
                type="info"
                :disabled="proxy.buttonPerm.cancel.disabled(row)"
                @click="handleEdit(row, '取消')"
              >
                取消
              </el-button>
              <!-- 发起者和有删除权限的可以删除,已通过未通过状态不可以删除 -->
              <el-button
                v-if="proxy.buttonPerm.delete.if(row, permUrl.delete)"
                size="small"
                link
                type="danger"
                :disabled="proxy.buttonPerm.delete.disabled(row)"
                @click="handleEdit(row, '删除')"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
    <button-box :active="active" :menu="menu" @changeCurrentButton="changeCurrentButton" />
    <approval-dialog ref="approvalDialog" @on-success="approvalSuccess" />
  </app-container>
</template>

<style lang="scss" scoped>
// 样式
</style>