260507_01_ENH: SafetyInspectionTotal, SafetyInspectionDivDetail 페이지 구현
This commit is contained in:
@@ -0,0 +1,262 @@
|
||||
"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 { 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";
|
||||
|
||||
export const signStyleGapRow: SignStyle<TopChartTableRow> = {
|
||||
rowSelector: (row) => row.label === GAP_WORDING,
|
||||
display: "arrow",
|
||||
arrowWithNumber: true,
|
||||
paint: "text",
|
||||
};
|
||||
|
||||
const DIV_COLUMNS = [
|
||||
{ key: "-1", label: "전체" },
|
||||
{ key: "1", label: "서울" },
|
||||
{ key: "2", label: "경기" },
|
||||
{ key: "3", label: "부산" },
|
||||
{ key: "4", label: "대구" },
|
||||
{ key: "5", label: "대전" },
|
||||
{ key: "6", label: "광주" },
|
||||
] as const;
|
||||
|
||||
const METRICS: Array<{
|
||||
key: keyof Omit<CsiData, "wave">;
|
||||
label: ReactNode;
|
||||
group?: "direct" | "indirect";
|
||||
}> = [
|
||||
{ key: "csi", label: "고객만족지수(CSI)" },
|
||||
{
|
||||
key: "safeHelp",
|
||||
label: (
|
||||
<>
|
||||
① 현장 안전점검이
|
||||
<br />
|
||||
도움되는 정도
|
||||
</>
|
||||
),
|
||||
group: "direct",
|
||||
},
|
||||
{
|
||||
key: "recHelp",
|
||||
label: (
|
||||
<>
|
||||
② 위험 개선 권고사항이
|
||||
<br />
|
||||
도움되는 정도
|
||||
</>
|
||||
),
|
||||
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<CsiData, "wave">
|
||||
) => getDataByWave(dataMap, divKey, wave)?.[metricKey] ?? null;
|
||||
|
||||
const getGapValue = (
|
||||
dataMap: CsiDataMap | undefined,
|
||||
divKey: string,
|
||||
previousWave: number,
|
||||
currentWave: number,
|
||||
metricKey: keyof Omit<CsiData, "wave">
|
||||
) => {
|
||||
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[waves.length - 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) => ({
|
||||
...row,
|
||||
preAppendCells: getPreAppendCells(metric, metricIndex, rowIndex, selectedWaves.length + 1),
|
||||
labelClassName:
|
||||
metric.key === "csi"
|
||||
? "bg-red-100 text-center font-bold"
|
||||
: metric.group === "indirect"
|
||||
? "bg-green-200 text-center font-bold"
|
||||
: metric.group === "direct"
|
||||
? "bg-red-50 text-center font-bold"
|
||||
: "text-center 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 <div className="content-text-sm text-muted-foreground">데이터를 불러오는 중입니다.</div>;
|
||||
}
|
||||
|
||||
if (isError) {
|
||||
return <div className="content-text-sm text-destructive">고객만족도 데이터를 불러오지 못했습니다.</div>;
|
||||
}
|
||||
|
||||
if (rows.length === 0) {
|
||||
return <div className="content-text-sm text-muted-foreground">표시할 고객만족도 데이터가 없습니다.</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
<TopChartTable
|
||||
title="안전점검 특관관계자 만족도조사"
|
||||
categories={categories}
|
||||
rows={rows}
|
||||
subLabelHeader={baseDateLabel}
|
||||
minWidth={1120}
|
||||
chartHeight={260}
|
||||
chart={
|
||||
<VerticalBar
|
||||
data={verticalChartData}
|
||||
height="100%"
|
||||
paletteVarNames={["--chart-5", "--chart-7"]}
|
||||
valueFormatter={(value) => 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 },
|
||||
}}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default SafetyInspectionTotal;
|
||||
Reference in New Issue
Block a user