sn: appendApiSearchParams() 함수 적용

This commit is contained in:
2026-09-03 17:09:58 +09:00
parent 4cde9b67c1
commit 5da89e8ee7
7 changed files with 144 additions and 56 deletions
@@ -0,0 +1,66 @@
import { downloadExcelWithKoreanHeaders } from "@/shared/lib/download-excel";
export type FloodPredictionExcelRow = {
ctpvNm: string;
sggNm: string;
analysisDateTime: string;
rainBeginningDateTime: string;
observedAccumulatedRainfall: number | null;
predictionHour: number | null;
predictedAccumulatedRainfall: number | null;
exceededFrequencyYear: number | null;
floodRiskGrade: string;
};
const HEADERS: Record<string, string> = {
rowNo: "순번",
ctpvNm: "시도",
sggNm: "시군구",
analysisDateTime: "분석시각",
rainBeginningDateTime: "강우 시작시각",
observedAccumulatedRainfall: "관측 누적강수량(mm)",
predictionHour: "예측시간",
predictedAccumulatedRainfall: "예측 누적강수량(mm)",
exceededFrequencyYear: "초과빈도(년)",
floodRiskGrade: "침수위험도",
};
/** yyyyMMddHHmm 형식의 API 시각을 Excel 표시 문자열로 변환한다. */
const formatDateTime = (value: string) =>
/^\d{12}$/.test(value)
? `${value.slice(0, 4)}-${value.slice(4, 6)}-${value.slice(6, 8)} ${value.slice(8, 10)}:${value.slice(10, 12)}`
: value || "-";
/** 적용 조회조건의 전체 침수예측현황을 브라우저에서 한 시트 XLSX로 생성한다. */
export const downloadFloodPredictionStatusExcel = async (
rows: FloodPredictionExcelRow[],
) => {
const excelRows = rows.map((row, index) => ({
rowNo: index + 1,
ctpvNm: row.ctpvNm || "-",
sggNm: row.sggNm || "-",
analysisDateTime: formatDateTime(row.analysisDateTime),
rainBeginningDateTime: formatDateTime(row.rainBeginningDateTime),
observedAccumulatedRainfall:
row.observedAccumulatedRainfall == null
? "-"
: Number(row.observedAccumulatedRainfall.toFixed(1)),
predictionHour:
row.predictionHour == null ? "-" : `${row.predictionHour}시간 후`,
predictedAccumulatedRainfall:
row.predictedAccumulatedRainfall == null
? "-"
: Number(row.predictedAccumulatedRainfall.toFixed(1)),
exceededFrequencyYear:
row.exceededFrequencyYear == null ? "-" : row.exceededFrequencyYear,
floodRiskGrade: row.floodRiskGrade || "-",
}));
await downloadExcelWithKoreanHeaders({
data: excelRows as unknown as JSON[],
fileName: "침수예측현황.xlsx",
sheetNameArray: ["침수예측현황"],
headerRange: "A1:J1",
headerMapping: HEADERS,
});
};
@@ -8,8 +8,8 @@ import {
} from "@common/business_components";
import { Button } from "@common/components";
import { getBlobData, getData, getRawJson } from "@/components/lib/api";
import { fileDownload } from "@/components/lib/utils/fileDownload";
import { getData, getRawJson } from "@/components/lib/api";
import { appendApiSearchParams } from "@/shared/util/util";
import LoadingOverlay from "@/components/ui/LoadingBar";
import { windowPopupDefinitions } from "@/features/windowPopup/model/popupDefinitions";
import { WINDOW_POPUP_MESSAGE_TYPE } from "@/features/windowPopup/model/types";
@@ -18,6 +18,8 @@ import PopupSearchPanel from "@/features/windowPopup/ui/PopupSearchPanel";
import { useAlertStore } from "@/shared/lib";
import useStore from "@/store/useStore";
import { createPredictionAnalysisTimeRange } from "../model/analysisTimeRange";
import { fetchAllPages } from "@/shared/lib/fetch-all-pages";
import { downloadFloodPredictionStatusExcel } from "../lib/fldng-predc-prst-excel";
type ApiRow = {
sggCd: string;
@@ -55,6 +57,7 @@ type Filters = {
};
const PAGE_SIZE = 10;
const EXCEL_DOWNLOAD_PAGE_SIZE = 500;
const MAP_FREQUENCIES = new Set([30, 50, 80, 100]);
const FREQUENCY_OPTIONS = [30, 50, 80, 100] as const;
@@ -118,7 +121,7 @@ const buildInitialFilters = (): Filters => {
rainBgngTo: "",
analysisFrom: toDateTimeLocal(range.analysisFrom),
analysisTo: toDateTimeLocal(range.analysisTo),
frequencyYear: "",
frequencyYear: "0",
};
};
@@ -141,21 +144,21 @@ const formatNumber = (value: number | null, digits: number) =>
: Number(value).toFixed(digits);
/** 화면 조회조건을 목록·Excel API 쿼리로 변환한다. */
const buildQuery = (filters: Filters, page?: number) => {
const buildQuery = (filters: Filters, page?: number, size = PAGE_SIZE) => {
const params = new URLSearchParams();
if (page !== undefined) {
params.set("page", String(page - 1));
params.set("size", String(PAGE_SIZE));
params.set("size", String(size));
}
if (filters.ctpvCd !== "0") params.set("ctpvCd", filters.ctpvCd.slice(0, 2));
if (filters.sggCd !== "0") params.set("sggCd", filters.sggCd.slice(0, 5));
(
["rainBgngFrom", "rainBgngTo", "analysisFrom", "analysisTo"] as const
).forEach((key) => {
const value = apiDate(filters[key]);
if (value) params.set(key, value);
appendApiSearchParams(params, {
ctpvCd: filters.ctpvCd === "0" ? "0" : filters.ctpvCd.slice(0, 2),
sggCd: filters.sggCd === "0" ? "0" : filters.sggCd.slice(0, 5),
rainBgngFrom: apiDate(filters.rainBgngFrom),
rainBgngTo: apiDate(filters.rainBgngTo),
analysisFrom: apiDate(filters.analysisFrom),
analysisTo: apiDate(filters.analysisTo),
frequencyYear: filters.frequencyYear,
});
if (filters.frequencyYear) params.set("frequencyYear", filters.frequencyYear);
return params.toString();
};
@@ -176,7 +179,7 @@ const validateFilters = (filters: Filters) => {
return "분석시각의 시작은 종료보다 늦을 수 없습니다.";
}
if (
filters.frequencyYear &&
filters.frequencyYear != "0" &&
!FREQUENCY_OPTIONS.includes(
Number(filters.frequencyYear) as 30 | 50 | 80 | 100,
)
@@ -389,10 +392,25 @@ export default function FldngPredcPrstPopupContent() {
}
setLoading(true);
try {
const blob = await getBlobData(
`/sewerage-service/api/v1/fldng/predc/prst/excel?${buildQuery(appliedFilters)}`,
);
fileDownload(blob, "침수예측현황.xlsx");
const result = await fetchAllPages<
{ content: ApiRow[]; totalElements: number },
ApiRow
>({
batchSize: EXCEL_DOWNLOAD_PAGE_SIZE,
fetchPage: async (pageIndex, size) => {
const response = await getRawJson(
`/sewerage-service/api/v1/fldng/predc/prst?${buildQuery(appliedFilters, pageIndex + 1, size)}`,
);
const payload = response?.data ?? response;
return {
content: Array.isArray(payload?.content) ? payload.content : [],
totalElements: Number(payload?.totalElements ?? 0),
};
},
getItems: (response) => response.content,
getTotal: (response) => response.totalElements,
});
await downloadFloodPredictionStatusExcel(result.items);
} catch (error) {
console.error("침수예측현황 Excel 다운로드 중 오류:", error);
alert("침수예측현황 Excel 다운로드에 실패했습니다.");
@@ -519,7 +537,7 @@ export default function FldngPredcPrstPopupContent() {
}))
}
>
<option value="">전체</option>
<option value="0">전체</option>
{FREQUENCY_OPTIONS.map((frequency) => (
<option key={frequency} value={String(frequency)}>
{frequency}년