소스 검색

调整柱状图逻辑

yanghao 1 주 전
부모
커밋
f4c342d7a4
2개의 변경된 파일72개의 추가작업 그리고 25개의 파일을 삭제
  1. 5 0
      src/api/pms/qhse/index.ts
  2. 67 25
      src/views/pms/qhse/socSummary/index.vue

+ 5 - 0
src/api/pms/qhse/index.ts

@@ -401,6 +401,11 @@ export const IotSocSummaryApi = {
   // }
   getSocSummaryStatistics: async (params) => {
     return await request.get({ url: `/rq/iot-soc-summary/stat`, params })
+  },
+
+  // 获取统计子类
+  getSocSummaryStatisticsChild: async (params) => {
+    return await request.get({ url: `/rq/iot-soc-summary/stat/child`, params })
   }
 }
 

+ 67 - 25
src/views/pms/qhse/socSummary/index.vue

@@ -192,8 +192,12 @@ const { ZmTable, ZmTableColumn } = useTableComponents()
 import { useUserStore } from '@/store/modules/user'
 const userStore = useUserStore()
 
-type SummaryItem = Record<string, number>
-type ChildMap = Record<string, SummaryItem[]>
+type SummaryItem = Record<string, any>
+type ChartDataItem = {
+  id?: string | number
+  name: string
+  value: number
+}
 
 const DRILLDOWN_KEYS = ['个人防护', '规范操作', '规范指挥', '人员位置', '作业场所'] as const
 
@@ -300,30 +304,49 @@ const downloadSOC = async (row) => {
   )
 }
 
-const child = ref<ChildMap>({})
+const childData = ref<SummaryItem[]>([])
 const totalData = ref<SummaryItem[]>([])
 const currentDrilldownKey = ref('')
+const currentDrilldownId = ref<string | number | undefined>(undefined)
+
+const normalizeChartData = (source: SummaryItem[] = []): ChartDataItem[] => {
+  return source
+    .map((item) => {
+      if (!item) return null
+
+      if ('name' in item || 'className' in item || 'label' in item) {
+        return {
+          id: item.id ?? item.socClass ?? item.valueId,
+          name: item.name ?? item.className ?? item.label ?? '',
+          value: Number(item.value ?? item.count ?? item.total ?? 0) || 0
+        }
+      }
+
+      const entries = Object.entries(item)
+      const nameEntry =
+        entries.find(
+          ([key, value]) =>
+            !['id', 'socClass', 'valueId', 'value', 'count', 'total'].includes(key) &&
+            typeof value === 'number'
+        ) || entries.find(([key]) => !['id', 'socClass', 'valueId'].includes(key))
+
+      const [name, value] = nameEntry || ['', 0]
+      return {
+        id: item.id ?? item.socClass ?? item.valueId,
+        name,
+        value: Number(value) || 0
+      }
+    })
+    .filter((item): item is ChartDataItem => !!item && !!item.name)
+}
 
 const totalChartData = computed(() => {
-  return (totalData.value as SummaryItem[]).map((item) => {
-    const [name, value] = Object.entries(item || {})[0] || ['', 0]
-    return {
-      name,
-      value: Number(value) || 0
-    }
-  })
+  return normalizeChartData(totalData.value as SummaryItem[])
 })
 
 const childChartData = computed(() => {
   if (!currentDrilldownKey.value) return []
-  const source = (child.value as ChildMap)?.[currentDrilldownKey.value] || []
-  return source.map((item) => {
-    const [name, value] = Object.entries(item || {})[0] || ['', 0]
-    return {
-      name,
-      value: Number(value) || 0
-    }
-  })
+  return normalizeChartData(childData.value as SummaryItem[])
 })
 
 const chartColorMap: Record<string, string> = {
@@ -465,6 +488,8 @@ const socChartOption = computed<EChartsOption>(() => {
           }
         },
         data: sourceData.map((item) => ({
+          id: item.id,
+          name: item.name,
           value: item.value,
           itemStyle: getBarItemStyle(item.name, isDrilldown)
         })),
@@ -490,8 +515,8 @@ async function getStatic() {
       // 日期
       observationDate: queryParams.observationDate
     })
-    child.value = res.child
-    totalData.value = res.total
+    totalData.value = res.total || res || []
+    childData.value = []
     staticLoading.value = false
   } else {
     staticLoading.value = true
@@ -501,22 +526,39 @@ async function getStatic() {
       // 日期
       observationDate: queryParams.observationDate
     })
-    child.value = res.child
-    totalData.value = res.total
+    totalData.value = res.total || res || []
+    childData.value = []
     staticLoading.value = false
   }
 }
 
-const handleChartClick = (params: any) => {
+const handleChartClick = async (params: any) => {
   const name = params?.name
   if (!name || currentDrilldownKey.value) return
-  if (DRILLDOWN_KEYS.some((item) => item === name)) {
-    currentDrilldownKey.value = name
+  const target = totalChartData.value.find((item) => item.name === name)
+  const socClass = params?.data?.id ?? target?.id
+  if (DRILLDOWN_KEYS.some((item) => item === name) && socClass !== undefined) {
+    staticLoading.value = true
+    try {
+      const res = await IotSocSummaryApi.getSocSummaryStatisticsChild({
+        deptId: queryParams.deptId || userStore.user.deptId,
+        observationDate: queryParams.observationDate,
+        socClass
+      })
+
+      currentDrilldownKey.value = name
+      currentDrilldownId.value = socClass
+      childData.value = res?.total || res?.list || res || []
+    } finally {
+      staticLoading.value = false
+    }
   }
 }
 
 const resetDrilldown = () => {
   currentDrilldownKey.value = ''
+  currentDrilldownId.value = undefined
+  childData.value = []
 }
 
 /** 初始化 **/