"use client"; import { type ReactNode, useMemo } from "react"; import type { ChartData } from "@/components/chart/types"; import TopChartTable from "@/components/combo/TopChartTable"; import type { TopChartTableRow } from "@/components/combo/types"; import {cn, formatKoreanBaseDate} from "@/lib/utils"; import { useCsiDataQuery } from "@/query/statistics"; import type { CsiData, CsiDataMap } from "@/types/kfpa"; import VerticalBar from "@/components/chart/VerticalBar"; import {SignStyle} from "@/components/table/types"; import {GAP_WORDING} from "@/const/kfpa.const"; import {DIV_KEYS, REGION_KEY_MAP} from "@/config/kfpaConfig"; export const signStyleGapRow: SignStyle = { rowSelector: (row) => row.label === GAP_WORDING, display: "arrow", arrowWithNumber: true, paint: "text", }; const DIV_COLUMNS = DIV_KEYS.map((key) => ({ key, label: key === "-1" ? "전체" : REGION_KEY_MAP[key], })); const METRICS: Array<{ key: keyof Omit; label: ReactNode; group?: "direct" | "indirect"; }> = [ { key: "csi", label: "고객만족지수(CSI)" }, { key: "safeHelp", label: ( <> ① 현장 안전점검이
도움되는 정도 ), group: "direct", }, { key: "recHelp", label: ( <> ② 위험 개선 권고사항이
도움되는 정도 ), group: "direct", }, { key: "staffProf", label: "③ 점검직원 전문성", group: "indirect" }, { key: "staffAtt", label: "④ 점검직원 태도", group: "indirect" }, ]; const getWaveLabel = (wave: number) => { const text = String(wave); return `${wave >= 100 ? text.slice(-2) : text}년`; }; const getDataByWave = (dataMap: CsiDataMap | undefined, divKey: string, wave: number) => dataMap?.[divKey]?.find((item) => item.wave === wave); const getMetricValue = ( dataMap: CsiDataMap | undefined, divKey: string, wave: number, metricKey: keyof Omit ) => getDataByWave(dataMap, divKey, wave)?.[metricKey] ?? null; const getGapValue = ( dataMap: CsiDataMap | undefined, divKey: string, previousWave: number, currentWave: number, metricKey: keyof Omit ) => { const previousValue = getMetricValue(dataMap, divKey, previousWave, metricKey); const currentValue = getMetricValue(dataMap, divKey, currentWave, metricKey); if (previousValue == null || currentValue == null) return null; return currentValue - previousValue; }; const getPreAppendCells = ( metric: (typeof METRICS)[number], metricIndex: number, waveIndex: number, rowCountPerMetric: number ): TopChartTableRow["preAppendCells"] => { if (waveIndex !== 0) return undefined; if (metric.key === "csi") { return [ { content: "고객만족지수(CSI)", colSpan: 2, rowSpan: rowCountPerMetric, width: 240, className: "bg-red-100 text-center align-middle font-bold content-text-base", }, ]; } const cells: TopChartTableRow["preAppendCells"] = []; if (metric.group === "direct" && metricIndex === 1) { cells.push({ content: "직접 요소", rowSpan: rowCountPerMetric * 2, width: 60, className: "bg-red-50 text-center align-middle font-bold content-text-base", }); } if (metric.group === "indirect" && metricIndex === 3) { cells.push({ content: "간접 요소", rowSpan: rowCountPerMetric * 2, width: 60, className: "bg-green-200 text-center align-middle font-bold content-text-base", }); } cells.push({ content: metric.label, rowSpan: rowCountPerMetric, width: 170, className: metric.group === "indirect" ? "bg-green-200 text-left align-middle font-bold content-text-base" : "bg-red-50 text-left align-middle font-bold content-text-base", }); return cells; }; const SafetyInspectionTotal = () => { const { data, isLoading, isError } = useCsiDataQuery(); const baseDateLabel = useMemo(() => formatKoreanBaseDate(), []); const { categories, rows, verticalChartData } = useMemo(() => { const waves = Array.from( new Set( Object.values(data ?? {}) .flat() .map((item) => item.wave) .filter((wave) => Number.isFinite(wave)) ) ).sort((a, b) => a - b); const latestWave = waves.at(-1); const selectedWaves = latestWave == null ? [] : [latestWave - 1, latestWave]; const categories = DIV_COLUMNS.map((div) => div.label); const rows: TopChartTableRow[] = selectedWaves.length === 0 ? [] : METRICS.flatMap((metric, metricIndex) => [ { label: GAP_WORDING, values: DIV_COLUMNS.map((div) => getGapValue(data, div.key, selectedWaves[0], selectedWaves[1], metric.key) ), }, ...selectedWaves.map((wave) => ({ label: getWaveLabel(wave), values: DIV_COLUMNS.map((div) => getMetricValue(data, div.key, wave, metric.key)), })), ].map((row, rowIndex) => { let labelColorClassName: string | undefined; if (metric.key === "csi") { labelColorClassName = "bg-red-100"; } else if (metric.group === "indirect") { labelColorClassName = "bg-green-200"; } else if (metric.group === "direct") { labelColorClassName = "bg-red-50"; } return { ...row, preAppendCells: getPreAppendCells(metric, metricIndex, rowIndex, selectedWaves.length + 1), labelClassName: cn( labelColorClassName, "text-center", row.label !== GAP_WORDING && "font-bold" ), labelWidth: 90, }; }) ); const verticalChartData: ChartData = { categories, series: selectedWaves.map((wave) => ({ name: getWaveLabel(wave), data: DIV_COLUMNS.map((div) => getDataByWave(data, div.key, wave)?.csi ?? null), })), }; return { categories, rows, verticalChartData, }; }, [data]); if (isLoading) { return
데이터를 불러오는 중입니다.
; } if (isError) { return
고객만족도 데이터를 불러오지 못했습니다.
; } if (rows.length === 0) { return
표시할 고객만족도 데이터가 없습니다.
; } return (
value.toFixed(2)} barMaxWidth={30} showFocusLabel="all" labelOptions={{ position: "top", distance: 4 }} grid={{ left: 0, right: 0, top: 40, bottom: 5 }} xAxis={{ type: "category", data: categories, axisLabel: { show: false }, axisTick: { show: false }, axisLine: { show: false }, }} yAxis={{ type: "value", min: 0, max: 5, axisLabel: { show: false }, axisTick: { show: false }, axisLine: { show: false }, splitLine: { show: false }, }} /> } />
); }; export default SafetyInspectionTotal;