Переглянути джерело

pms 瑞鹰钻井日报汇总 项目部账号 查询报错

zhangcl 1 тиждень тому
батько
коміт
f30a1c7f0b

+ 35 - 1
src/views/pms/iotrhdailyreport/DeptTree2.vue

@@ -124,6 +124,39 @@ const sortTreeBySort = (treeNodes: Tree[]) => {
   return sortedNodes
 }
 
+// 添加获取顶级部门的方法
+const getTopDeptId = async (): Promise<number> => {
+  try {
+    // 如果已经加载了部门树,直接获取第一级节点的第一个节点
+    if (deptList.value && deptList.value.length > 0) {
+      // 假设第一级节点就是顶级节点
+      return deptList.value[0].id
+    }
+
+    // 如果没有加载部门树,则根据传入的deptId重新获取
+    const res = await DeptApi.specifiedSimpleDepts(props.deptId)
+    const treeData = handleTree(res)
+
+    // 对树进行排序
+    const sortedTree = sortTreeBySort(treeData)
+
+    if (sortedTree && sortedTree.length > 0) {
+      return sortedTree[0].id
+    }
+
+    // 如果没有找到顶级部门,返回默认值
+    return 158
+  } catch (error) {
+    console.error('获取顶级部门ID失败:', error)
+    return 158 // 默认值
+  }
+}
+
+// 修改3: 将方法暴露给父组件
+defineExpose({
+  getTopDeptId,
+})
+
 /** 基于名字过滤 */
 const filterNode = (name: string, data: Tree) => {
   if (!name) return true
@@ -149,7 +182,8 @@ watch(
     if (newVal !== oldVal) {
       getTree()
     }
-  }
+  },
+  { immediate: true } // 添加立即执行,确保组件初始化时加载树
 )
 
 /** 初始化 */

+ 90 - 7
src/views/pms/iotrydailyreport/summary.vue

@@ -12,6 +12,9 @@ import { Motion, AnimatePresence } from 'motion-v'
 import { rangeShortcuts } from '@/utils/formatTime'
 import download from '@/utils/download'
 
+import { useUserStore } from '@/store/modules/user'
+import * as DeptApi from "@/api/system/dept";
+
 interface Query {
   pageNo: number
   pageSize: number
@@ -22,18 +25,27 @@ interface Query {
   projectClassification: 1 | 2
 }
 
-const id = 158
+// 添加 DeptTree2 的 ref
+const deptTreeRef = ref<InstanceType<typeof DeptTree2>>()
+
+// 将 id 改为 ref,并根据条件动态赋值
+const id = ref(158)
+
+// 跟踪是否选择了部门树节点
+const hasSelectedDeptNode = ref(false)
 
 const query = ref<Query>({
   pageNo: 1,
   pageSize: 10,
-  deptId: 158,
+  deptId: 158,  // 初始值,会在onMounted中根据条件调整
   createTime: [
     ...rangeShortcuts[2].value().map((item) => dayjs(item).format('YYYY-MM-DD HH:mm:ss'))
   ],
   projectClassification: 1
 })
 
+const dept = ref() // 当前登录人所属部门对象
+
 const totalWorkKeys: [string, string, string, string, number][] = [
   ['totalCount', '个', '总数', 'i-tabler:report-analytics text-sky', 0],
   [
@@ -79,7 +91,7 @@ const getTotal = useDebounceFn(async () => {
 
   try {
     let res1: any[]
-    if (query.value.createTime.length !== 0) {
+    if (query.value.createTime && query.value.createTime.length !== 0) {
       res1 = await IotRyDailyReportApi.ryDailyReportStatistics({
         createTime: query.value.createTime,
         projectClassification: query.value.projectClassification
@@ -147,7 +159,28 @@ const formatter = (row: List, column: any) => {
 const getList = useDebounceFn(async () => {
   listLoading.value = true
   try {
-    const res = await IotRyDailyReportApi.getIotRyDailyReportSummary(query.value)
+    // 创建查询参数,如果 createTime 为空则不传
+    const params: any = {
+      pageNo: query.value.pageNo,
+      pageSize: query.value.pageSize,
+      deptId: query.value.deptId,
+      projectClassification: query.value.projectClassification
+    }
+
+    // 只有 createTime 有值时才添加
+    if (query.value.createTime && query.value.createTime.length === 2) {
+      params.createTime = query.value.createTime
+    }
+
+    // 可选条件
+    if (query.value.contractName) {
+      params.contractName = query.value.contractName
+    }
+    if (query.value.taskName) {
+      params.taskName = query.value.taskName
+    }
+
+    const res = await IotRyDailyReportApi.getIotRyDailyReportSummary(params)
 
     const { list: reslist } = res
 
@@ -209,6 +242,17 @@ const getChart = useDebounceFn(async () => {
   chartLoading.value = true
 
   try {
+    // 创建查询参数,如果 createTime 为空则不传
+    const params: any = {
+      deptId: query.value.deptId,
+      projectClassification: query.value.projectClassification
+    }
+
+    // 只有 createTime 有值时才添加
+    if (query.value.createTime && query.value.createTime.length === 2) {
+      params.createTime = query.value.createTime
+    }
+
     const res = await IotRyDailyReportApi.getIotRyDailyReportSummaryPolyline(query.value)
 
     chartData.value = {
@@ -324,6 +368,8 @@ const render = () => {
 }
 
 const handleDeptNodeClick = (node: any) => {
+  hasSelectedDeptNode.value = true // 标记用户已经选择了部门节点
+
   query.value.deptId = node.id
   handleQuery()
 }
@@ -340,10 +386,13 @@ const handleQuery = (setPage = true) => {
 }
 
 const resetQuery = () => {
+  // 重置时,重置选择状态
+  hasSelectedDeptNode.value = false
+
   query.value = {
     pageNo: 1,
     pageSize: 10,
-    deptId: 157,
+    deptId: id.value, // 使用动态计算的id值
     createTime: [],
     projectClassification: 1
   }
@@ -355,7 +404,41 @@ watch(
   () => handleQuery(false)
 )
 
-onMounted(() => {
+onMounted(async () => {
+  const deptId = useUserStore().getUser.deptId
+  dept.value = await DeptApi.getDept(deptId)
+  dept.value.parentId
+
+  // 根据条件动态设置id值
+  if (deptId.value === 158) {
+    // 情况1: 当前登录人部门id等于158,使用原逻辑
+    id.value = 158
+    query.value.deptId = 158
+  } else if (dept.value?.parentId === 158) {
+    // 情况2: 当前登录人上级部门id等于158
+    // 需要获取组织部门树的顶级节点id
+    await nextTick()
+    // 获取组织部门树的顶级节点id
+    if (deptTreeRef.value) {
+      try {
+        const topDeptId = await deptTreeRef.value.getTopDeptId()
+        id.value = topDeptId || 158
+      } catch (error) {
+        console.error('获取顶级部门失败:', error)
+        id.value = 158
+      }
+    } else {
+      id.value = 158
+    }
+
+    // 设置查询条件的部门id为当前登录人部门id
+    query.value.deptId = id.value
+  } else {
+    // 其他情况,使用默认值
+    id.value = 158
+    query.value.deptId = 158
+  }
+
   handleQuery()
 })
 
@@ -430,7 +513,7 @@ const tolist = (id: number) => {
 <template>
   <div class="grid grid-cols-[16%_1fr] gap-5">
     <div class="bg-white dark:bg-[#1d1e1f] rounded-lg shadow p-4">
-      <DeptTree2 :deptId="id" @node-click="handleDeptNodeClick" />
+      <DeptTree2 ref="deptTreeRef" :deptId="id" @node-click="handleDeptNodeClick" />
     </div>
     <div class="grid grid-rows-[62px_164px_1fr] h-full gap-5">
       <el-form