Newer
Older
xc-metering-front / src / views / tested / device / group / components / list.vue
<!-- 设备分组表格 -->
<script lang="ts" setup name="DeviceGroupList">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { delGroup, getGroupList } from '@/api/eqpt/device/group'
import { getPostList } from '@/api/system/post'
import { toTreeList } from '@/utils/structure'
import { getAdminDept, getUserDept, getUserDeptSon, getUserList } from '@/api/system/user'
import { getDeptList, getDeptTree, getDeptTreeList, getDeptTreeListByPid } from '@/api/system/dept'
import { keepSearchParams } from '@/utils/keepQuery'
const { proxy } = getCurrentInstance() as any
const userStore = useUserStore()
const listQuery = reactive({
  companyId: '', // 所在单位
  deptId: '', // 使用部门
  positionId: '', // 使用岗位
  offset: 1,
  limit: 20,
  subSystemId: '', // 分系统
  groupName: '', // 分组名称
  manufactureNo: '', // 出厂编号
  createUserId: userStore.id, // 分组名称
  sort: 'desc',
  orderBy: 'create_time',
})

onBeforeRouteLeave((to: any) => {
  keepSearchParams(to.path, 'DeviceGroupIndex')
})
const columns = ref([
  {
    text: '所在单位',
    value: 'companyName',
    align: 'center',
  },
  {
    text: '使用部门',
    value: 'deptName',
    align: 'center',
  },
  {
    text: '分系统',
    value: 'subSystemName',
    align: 'center',
  },
  {
    text: '使用岗位',
    value: 'positionName',
    align: 'center',
  },
  {
    text: '分组名称创建人',
    value: 'createUserName',
    align: 'center',
  },
  {
    text: '创建时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '备注',
    value: 'remark',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)
// 只看我的 复选框
const checked = ref(true)
watch(() => checked.value, (newVal) => {
  if (newVal) {
    listQuery.createUserId = userStore.id
  }
  else {
    listQuery.createUserId = ''
  }
})
// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.offset = 1
  }
  getGroupList(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  listQuery.companyId = ''
  listQuery.deptId = ''
  listQuery.positionId = ''
  listQuery.subSystemId = ''
  listQuery.groupName = ''
  listQuery.manufactureNo = ''
  listQuery.createUserId = userStore.id
  checked.value = true
  listQuery.offset = 1
  listQuery.limit = 20
  listQuery.sort = 'desc'
  listQuery.orderBy = 'create_time'
  search()
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
const $router = useRouter()
// 新建编辑操作
const handler = (row: any, type: string) => {
  $router.push({
    path: `/group/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
// 删除
const delHandler = (row: any) => {
  ElMessageBox.confirm(
    '确认删除此分组信息吗?',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delGroup(row.id).then((res) => {
      ElMessage.success('操作成功')
      search()
    })
  })
}
// 所在单位
const companyList = ref<{ id: string; value: string; name: string }[]>([])
// 部门
const deptList = ref<{ id: string; value: string; name: string }[]>([])
const deptListAll = ref<{ id: string; value: string; name: string }[]>([])
// 分系统
const systemList = ref<{ id: string; value: string; name: string }[]>([])
const systemListAll = ref<{ id: string; value: string; name: string }[]>([])
// 使用岗位
const usePositionList = ref<any[]>([])
// 获取字典
const fetchDict = () => {
  // 使用岗位
  getPostList({}).then((res) => {
    usePositionList.value = res.data
  })
  // 获取使用单位
  // getDeptTree({ pid: '0' }).then((res) => {
  //   // 单位
  //   companyList.value = res.data.filter((item: any) => item.pid === '0').map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  //   // 部门
  //   deptList.value = res.data.filter((item: any) => item.pids.split(',').length === 3).map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  //   deptListAll.value = res.data.filter((item: any) => item.pids.split(',').length === 3).map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  //   // 分系统
  //   systemList.value = res.data.filter((item: any) => item.fullName.includes('系统')).map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  //   systemListAll.value = res.data.filter((item: any) => item.fullName.includes('系统')).map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  // })
  getUserDept().then((res) => {
    // companyInfo.value = res.data
    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,
        },
      ]
    }
  })
}
fetchDict()
// 三级联动
watch(() => listQuery.companyId, (newVal) => {
  listQuery.deptId = ''
  // listQuery.subSystemId = ''
  // if (newVal) {
  //   getDeptTree({ pid: newVal }).then((res) => {
  //     deptList.value = res.data.filter((item: any) => item.pid === newVal).map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  //     systemList.value = res.data.filter((item: any) => item.pid !== newVal).map((item: any) => ({ id: item.id, value: item.id, name: item.fullName }))
  //   })
  // }
  // else {
  //   deptList.value = deptListAll.value
  //   systemList.value = systemListAll.value
  // }
  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,
})
// watch(() => listQuery.deptId, (newVal) => {
//   if (newVal) {
//     getDeptTreeList({ pid: newVal }).then((res) => {
//       systemList.value = res.data.filter((item: any) => item.pid === newVal).map((item: any) => ({ id: item.id, value: item.id, name: item.name }))
//     })
//   }
//   else {
//     systemList.value = systemListAll.value
//   }
// }, {
//   deep: true,
// })

const permUrl = ref({
  add: '/tested/device/group/add',
  edit: '/tested/device/group/edit',
  del: '/tested/device/group/delete',
})

onActivated(() => {
  // 从编辑或者新增页面回来需要重新获取列表数据
  // $router.options.history.state.forward 上一次路由地址
  if (!($router.options.history.state.forward as string || '').includes('detail')) {
    console.log('需要重新获取列表')
    fetchData(false)
  }
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <!-- <el-input v-model.trim="listQuery.companyId" placeholder="所在单位" clearable /> -->
        <!-- <dept-select v-model="listQuery.companyId" :need-top="false" placeholder="所在单位" /> -->
        <el-select v-model="listQuery.companyId" filterable placeholder="所在单位" clearable>
          <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </search-item>
      <search-item>
        <!-- <el-input v-model.trim="listQuery.deptId" placeholder="使用部门" clearable /> -->
        <!-- <dept-select v-model="listQuery.deptId" :need-top="false" placeholder="使用部门" /> -->
        <!-- <el-select v-model="listQuery.deptId" filterable placeholder="使用部门" clearable>
          <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select> -->
        <el-tree-select
          v-model="listQuery.deptId"
          style="width: 100%;"
          :data="deptList"
          :render-after-expand="false"
          check-strictly
          placeholder="部门"
        />
      </search-item>
      <!-- <search-item>
        <el-select v-model="listQuery.subSystemId" filterable placeholder="分系统" clearable>
          <el-option v-for="item in systemList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </search-item> -->
      <search-item>
        <!-- <el-input v-model.trim="listQuery.positionId" placeholder="使用岗位" clearable /> -->
        <el-select v-model="listQuery.positionId" filterable placeholder="使用岗位" clearable>
          <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.groupName" placeholder="分组名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.manufactureNo" placeholder="出厂编号" clearable />
      </search-item>
      <search-item>
        <el-checkbox v-model="checked" size="large" checked>
          只看我的
        </el-checkbox>
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm(permUrl.add)" icon="icon-add" title="新增" @click="handler({}, 'create')" />
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns as any" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" @change="changePage"
      >
        <template #columns>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handler(scope.row, 'detail')">
                查看
              </el-button>
              <el-button v-if="proxy.hasPerm(permUrl.edit)" link type="primary" size="small" @click="handler(scope.row, 'update')">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm(permUrl.del)" link type="danger" size="small" @click="delHandler(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>