106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import type { LvNowDmgMtncGnsvyResponseDto, NowDmgMtncGnsvyResponseDto } from "@/entities/dc";
|
|
|
|
type StageKey = "Dt1" | "Dt2" | "Dt3" | "Dt4" | "" | "Dc1" | "Dc2" | "Dc3" | "Dc4";
|
|
|
|
const STAGE_KEYS: StageKey[] = ["Dt1", "Dt2", "Dt3", "Dt4", "", "Dc1", "Dc2", "Dc3", "Dc4"];
|
|
|
|
function formatPlanCell(value: string | number | null | undefined, asYear = false): string {
|
|
if (value === null || value === undefined || value === "") return "-";
|
|
if (asYear) return `${value}년`;
|
|
if (typeof value === "number") return value.toLocaleString();
|
|
const num = Number(value);
|
|
if (!Number.isNaN(num) && value.trim() !== "" && /^-?\d+(\.\d+)?$/.test(value.trim()))
|
|
return num.toLocaleString();
|
|
return String(value);
|
|
}
|
|
|
|
function getStagedValue(
|
|
data: NowDmgMtncGnsvyResponseDto | null | undefined,
|
|
fieldPrefix: string,
|
|
stage: StageKey,
|
|
): string | number | null | undefined {
|
|
if (!data) return undefined;
|
|
const key = `${fieldPrefix}${stage}` as keyof NowDmgMtncGnsvyResponseDto;
|
|
return data[key] as string | number | null | undefined;
|
|
}
|
|
|
|
function getPlanYearCells(data: NowDmgMtncGnsvyResponseDto | null | undefined): string[] {
|
|
return STAGE_KEYS.map((stage) => formatPlanCell(getStagedValue(data, "planYr", stage), true));
|
|
}
|
|
|
|
function getMetricCells(
|
|
data: NowDmgMtncGnsvyResponseDto | null | undefined,
|
|
fieldPrefix: string,
|
|
): string[] {
|
|
return STAGE_KEYS.map((stage) => formatPlanCell(getStagedValue(data, fieldPrefix, stage)));
|
|
}
|
|
|
|
function collectNodesByLevel(
|
|
nodes: LvNowDmgMtncGnsvyResponseDto[] | undefined,
|
|
targetLevel: number,
|
|
acc: LvNowDmgMtncGnsvyResponseDto[] = [],
|
|
): LvNowDmgMtncGnsvyResponseDto[] {
|
|
if (!nodes) return acc;
|
|
for (const node of nodes) {
|
|
if (node.level === targetLevel) acc.push(node);
|
|
if (node.children?.length) collectNodesByLevel(node.children, targetLevel, acc);
|
|
}
|
|
return acc;
|
|
}
|
|
|
|
function extractYearOptions(tree: LvNowDmgMtncGnsvyResponseDto[] | undefined): string[] {
|
|
const years = collectNodesByLevel(tree, 3)
|
|
.map((node) => node.gnsvyYr)
|
|
.filter((year): year is string => Boolean(year));
|
|
return [...new Set(years)];
|
|
}
|
|
|
|
interface FacilityOption {
|
|
code: string;
|
|
name: string;
|
|
}
|
|
|
|
function extractFacilityOptions(
|
|
tree: LvNowDmgMtncGnsvyResponseDto[] | undefined,
|
|
gnsvyYr: string,
|
|
): FacilityOption[] {
|
|
const yearNodes = collectNodesByLevel(tree, 3).filter((node) => node.gnsvyYr === gnsvyYr);
|
|
const options: FacilityOption[] = [];
|
|
|
|
function pushFacility(node: LvNowDmgMtncGnsvyResponseDto) {
|
|
if (!node.swtrmtFcltCd) return;
|
|
options.push({
|
|
code: node.swtrmtFcltCd,
|
|
name: node.swtrmtFcltNm ?? node.swtrmtFcltCd,
|
|
});
|
|
}
|
|
|
|
if (yearNodes.length > 0) {
|
|
for (const yearNode of yearNodes) {
|
|
for (const child of yearNode.children ?? []) pushFacility(child);
|
|
}
|
|
return options;
|
|
}
|
|
|
|
// level=3 응답이 시설 리프만 내려주는 경우
|
|
function walk(nodes: LvNowDmgMtncGnsvyResponseDto[] | undefined) {
|
|
if (!nodes) return;
|
|
for (const node of nodes) {
|
|
pushFacility(node);
|
|
walk(node.children);
|
|
}
|
|
}
|
|
walk(tree);
|
|
return options;
|
|
}
|
|
|
|
export {
|
|
extractFacilityOptions,
|
|
extractYearOptions,
|
|
formatPlanCell,
|
|
getMetricCells,
|
|
getPlanYearCells,
|
|
STAGE_KEYS,
|
|
};
|
|
export type { FacilityOption };
|