442 lines
18 KiB
TypeScript
442 lines
18 KiB
TypeScript
import { useMemo, useRef, useState } from "react";
|
|
|
|
import { useAlertStore, useUserStore } from "@/shared/lib";
|
|
import { statisticsManagementApi } from "@/entities/statistics/api/statistics-management-service";
|
|
import { statisticsApi } from "@/entities/statistics/api/statistics-service";
|
|
import {
|
|
extractSewerageServiceLevelReviewList,
|
|
extractSewerageServiceLevelReviewStatsStatusInfo,
|
|
mapSewerageServiceLevelReviewRows,
|
|
type SewerageServiceLevelReviewRequestParams,
|
|
} from "@/entities/statistics/model/statistics-sewerage-service-level-review-interface";
|
|
import type { StatsStatusInfo } from "@/entities/statistics/model/statistics-sewage-pipe-review-interface";
|
|
import type {
|
|
ReviewerInfoRequest,
|
|
ReviewOpinionSaveRequest,
|
|
} from "@/entities/statistics/model/statistics-interface";
|
|
import { LoadingWithAnimation, Tabs, TabsContent, TabsList, TabsTrigger } from "@common/components";
|
|
|
|
import SubmitButton from "@/components/button/SumitButton";
|
|
import ReturnButton from "@/components/button/ReturnButton";
|
|
import ReviewOpinionButton from "@/components/button/ReviewOpinionButton";
|
|
import DataGrid from "@/components/module/DataGrid";
|
|
import SearchForm from "@/components/module/SearchForm";
|
|
import { ReturnReasonDialog } from "@/components/statistics/review/ReturnReasonDialog";
|
|
import { ReviewSubmitDialog } from "@/components/statistics/review/ReviewSubmitDialog";
|
|
import {
|
|
ReviewOpinionDialog,
|
|
type ReviewOpinionDialogContext,
|
|
} from "@/components/statistics/review/ReviewOpinionDialog";
|
|
import type {
|
|
ColumnConfig,
|
|
ColumnInfo,
|
|
ColumnInfoList,
|
|
} from "@/components/type/DataGrid.type";
|
|
import type { SearchFormValues } from "@/components/type/SearchForm.type";
|
|
import { SewerageServiceLevelInfoListRvw } from "@/components/util/column/review/SewerageServiceLevelRvwColumn";
|
|
import { toGridRows } from "@/components/util/grid/gridFunction";
|
|
import { buildReviewCellStyleCallbackByTab } from "@/components/util/grid/reviewEmptyCurrCellStyle";
|
|
import ReviewEmptyCurrLegend from "@/components/statistics/review/ReviewEmptyCurrLegend";
|
|
|
|
const firstTabKey = SewerageServiceLevelInfoListRvw[0]?.tabKey ?? "ST00130001";
|
|
|
|
async function fetchServiceLevelReviewTab(
|
|
tabKey: string,
|
|
searchParam: SewerageServiceLevelReviewRequestParams,
|
|
) {
|
|
if (tabKey === "ST00130001") {
|
|
return statisticsManagementApi.getSewageComplaintReviewList(searchParam);
|
|
}
|
|
if (tabKey === "ST00130002") {
|
|
return statisticsManagementApi.getResidentFriendlyFacilityReviewList(searchParam);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// 하수도 통계 검토 13. 하수도 서비스 수준
|
|
const SewerageServiceLevelRvw = () => {
|
|
const { setMessage: alert } = useAlertStore((state) => state);
|
|
const userInfo = useUserStore((state) => state.userInfo);
|
|
const [rowDataMap, setRowDataMap] = useState<Map<string, Record<string, string | number>[]>>(new Map());
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [activeTab, setActiveTab] = useState<string>(firstTabKey);
|
|
const [reviewOpinionOpen, setReviewOpinionOpen] = useState(false);
|
|
const [returnDialogOpen, setReturnDialogOpen] = useState(false);
|
|
const [submitDialogOpen, setSubmitDialogOpen] = useState(false);
|
|
const [lastSearchValues, setLastSearchValues] = useState<SearchFormValues | null>(null);
|
|
const [tabStatsStatusInfoMap, setTabStatsStatusInfoMap] = useState<
|
|
Map<string, StatsStatusInfo>
|
|
>(new Map());
|
|
|
|
const searchParamRef = useRef<SewerageServiceLevelReviewRequestParams | null>(null);
|
|
const searchDoneRef = useRef(false);
|
|
const lazyTabLoadedRef = useRef<Set<string>>(new Set());
|
|
|
|
const activeStatsStatusInfo = useMemo(
|
|
() => tabStatsStatusInfoMap.get(activeTab) ?? {},
|
|
[tabStatsStatusInfoMap, activeTab],
|
|
);
|
|
|
|
const columnInfo = useMemo(() => {
|
|
return SewerageServiceLevelInfoListRvw.map((tab: ColumnInfoList) => ({
|
|
...tab,
|
|
columns: (tab.columns ?? []).map((col: ColumnConfig) => ({
|
|
...col,
|
|
editable: false as const,
|
|
styleName: col.styleName ?? ("rg-cell-readonly" as const),
|
|
})),
|
|
}));
|
|
}, []);
|
|
|
|
const reviewCellStyleCallbackByTab = useMemo(
|
|
() => buildReviewCellStyleCallbackByTab(columnInfo, rowDataMap),
|
|
[columnInfo, rowDataMap],
|
|
);
|
|
|
|
const reviewOpinionContext = useMemo<ReviewOpinionDialogContext | null>(() => {
|
|
if (lastSearchValues == null) return null;
|
|
return {
|
|
statsYr: lastSearchValues.year ?? "",
|
|
sewerBzmnCd: lastSearchValues.bsnmCd ?? "",
|
|
statsCd: activeStatsStatusInfo.statsCd ?? "",
|
|
};
|
|
}, [activeStatsStatusInfo.statsCd, lastSearchValues]);
|
|
|
|
const latestReviewOpinion = activeStatsStatusInfo.splmntOpnnCn ?? "";
|
|
const latestProcessHistory = activeStatsStatusInfo.wrterOpnnCn ?? "";
|
|
|
|
const submitDialogContext = useMemo<ReviewerInfoRequest | null>(() => {
|
|
if (lastSearchValues == null) return null;
|
|
|
|
const statsYr = lastSearchValues.year ?? "";
|
|
const sewerBzmnCd = lastSearchValues.bsnmCd ?? "";
|
|
const statsCd = activeStatsStatusInfo.statsCd ?? "";
|
|
const instSeCd = userInfo?.instSeCd ?? "";
|
|
|
|
if (!statsYr || !sewerBzmnCd || !statsCd || !instSeCd) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
statsYr,
|
|
sewerBzmnCd,
|
|
statsCd,
|
|
instSeCd,
|
|
};
|
|
}, [activeStatsStatusInfo.statsCd, lastSearchValues, userInfo?.instSeCd]);
|
|
|
|
const { showReviewOpinionButton, showReturnButton, showSubmitButton } = useMemo(() => {
|
|
const statsStatusInfo = activeStatsStatusInfo;
|
|
const submit =
|
|
statsStatusInfo.submitStatusCd != null
|
|
? String(statsStatusInfo.submitStatusCd)
|
|
: "";
|
|
const prvn =
|
|
statsStatusInfo.prvnStatusCd != null ? String(statsStatusInfo.prvnStatusCd) : "";
|
|
const submitIs3 = submit === "3";
|
|
const reviewSubmitVisible = submitIs3 && (prvn === "0" || prvn === "1");
|
|
return {
|
|
showReviewOpinionButton: reviewSubmitVisible,
|
|
showSubmitButton: reviewSubmitVisible,
|
|
showReturnButton: submitIs3 && prvn !== "3",
|
|
};
|
|
}, [activeStatsStatusInfo]);
|
|
|
|
const showActionBar =
|
|
showReviewOpinionButton || showReturnButton || showSubmitButton;
|
|
|
|
const handleReset = () => {
|
|
setRowDataMap(new Map());
|
|
setActiveTab(firstTabKey);
|
|
setTabStatsStatusInfoMap(new Map());
|
|
setReviewOpinionOpen(false);
|
|
setReturnDialogOpen(false);
|
|
setSubmitDialogOpen(false);
|
|
setLastSearchValues(null);
|
|
searchParamRef.current = null;
|
|
searchDoneRef.current = false;
|
|
lazyTabLoadedRef.current.clear();
|
|
};
|
|
|
|
const handleSearch = async (values: SearchFormValues) => {
|
|
if (!values.sido || values.sido === "ALL") {
|
|
alert("시도를 선택해주세요.");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsLoading(true);
|
|
setLastSearchValues(values);
|
|
|
|
const searchParam: SewerageServiceLevelReviewRequestParams = {
|
|
statsYr: values.year,
|
|
sewerBzmnCd: values.bsnmCd,
|
|
ctpvNm: values.sido,
|
|
sggNm: values.gugun === "ALL" ? "" : values.gugun,
|
|
swtrmtFcltNm: values.fctName,
|
|
};
|
|
|
|
searchParamRef.current = searchParam;
|
|
searchDoneRef.current = true;
|
|
lazyTabLoadedRef.current.clear();
|
|
|
|
const response = await fetchServiceLevelReviewTab(activeTab, searchParam);
|
|
|
|
if (response?.status === "success" && response.data) {
|
|
const ssi = extractSewerageServiceLevelReviewStatsStatusInfo(response.data);
|
|
setTabStatsStatusInfoMap((prev) => {
|
|
const next = new Map(prev);
|
|
next.set(activeTab, ssi);
|
|
return next;
|
|
});
|
|
const listData = extractSewerageServiceLevelReviewList(activeTab, response.data);
|
|
setRowDataMap((prev) => {
|
|
const next = new Map(prev);
|
|
next.set(activeTab, toGridRows(mapSewerageServiceLevelReviewRows(listData)));
|
|
return next;
|
|
});
|
|
} else {
|
|
setTabStatsStatusInfoMap(new Map());
|
|
setRowDataMap(new Map());
|
|
alert(response?.message || "데이터 조회 중 오류가 발생했습니다.");
|
|
}
|
|
} catch {
|
|
setTabStatsStatusInfoMap(new Map());
|
|
setRowDataMap(new Map());
|
|
alert("데이터 조회 중 오류가 발생했습니다.");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleActiveTabChange = async (tabKey: string) => {
|
|
setActiveTab(tabKey);
|
|
|
|
const activeTabData = rowDataMap.get(tabKey);
|
|
const hasActiveTabData = Array.isArray(activeTabData) && activeTabData.length > 0;
|
|
|
|
const canLazyLoad =
|
|
searchParamRef.current &&
|
|
!hasActiveTabData &&
|
|
!lazyTabLoadedRef.current.has(tabKey) &&
|
|
searchDoneRef.current;
|
|
|
|
if (!canLazyLoad) return;
|
|
|
|
const searchParam = searchParamRef.current;
|
|
if (!searchParam) return;
|
|
|
|
try {
|
|
setIsLoading(true);
|
|
|
|
const response = await fetchServiceLevelReviewTab(tabKey, searchParam);
|
|
lazyTabLoadedRef.current.add(tabKey);
|
|
|
|
if (response?.status === "success" && response.data) {
|
|
const ssi = extractSewerageServiceLevelReviewStatsStatusInfo(response.data);
|
|
setTabStatsStatusInfoMap((prev) => {
|
|
const next = new Map(prev);
|
|
next.set(tabKey, ssi);
|
|
return next;
|
|
});
|
|
setRowDataMap((prev) => {
|
|
const next = new Map(prev);
|
|
const listData = extractSewerageServiceLevelReviewList(tabKey, response.data);
|
|
next.set(tabKey, toGridRows(mapSewerageServiceLevelReviewRows(listData)));
|
|
return next;
|
|
});
|
|
} else {
|
|
alert(response?.message || "데이터가 없습니다.");
|
|
}
|
|
} catch {
|
|
alert("데이터 조회 중 오류가 발생했습니다.");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const submitFunc = () => {
|
|
if (submitDialogContext == null) {
|
|
alert("조회 후 제출할 수 있습니다.");
|
|
return;
|
|
}
|
|
|
|
setSubmitDialogOpen(true);
|
|
};
|
|
|
|
const reviewOpinionFunc = async () => {
|
|
if (reviewOpinionContext == null) {
|
|
alert("조회 후 검토의견을 작성할 수 있습니다.");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const detailRes = await statisticsApi.getReviewOpinionDetail({
|
|
statsYr: reviewOpinionContext.statsYr,
|
|
sewerBzmnCd: reviewOpinionContext.sewerBzmnCd,
|
|
statsCd: reviewOpinionContext.statsCd,
|
|
});
|
|
|
|
if (detailRes.status !== "success") {
|
|
alert(detailRes.message || "검토의견 조회에 실패했습니다.");
|
|
return;
|
|
}
|
|
|
|
const row = detailRes.data?.[0];
|
|
setTabStatsStatusInfoMap((prev) => {
|
|
const next = new Map(prev);
|
|
next.set(activeTab, {
|
|
...(next.get(activeTab) ?? {}),
|
|
splmntOpnnCn: row?.splmntOpnnCn ?? "",
|
|
wrterOpnnCn: row?.wrterOpnnCn ?? "",
|
|
});
|
|
return next;
|
|
});
|
|
setReviewOpinionOpen(true);
|
|
} catch {
|
|
alert("검토의견 조회 중 오류가 발생했습니다.");
|
|
}
|
|
};
|
|
|
|
const saveReviewOpinion = async (param: ReviewOpinionSaveRequest) => {
|
|
try {
|
|
const response = await statisticsApi.saveReviewOpinion(param);
|
|
if (response.status !== "success") {
|
|
alert(response.message || "저장에 실패했습니다.");
|
|
return;
|
|
}
|
|
alert(response.message || "저장되었습니다.");
|
|
setTabStatsStatusInfoMap((prev) => {
|
|
const next = new Map(prev);
|
|
next.set(activeTab, {
|
|
...(next.get(activeTab) ?? {}),
|
|
splmntOpnnCn: param.splmntOpnnCn,
|
|
wrterOpnnCn: param.wrterOpnnCn,
|
|
});
|
|
return next;
|
|
});
|
|
} catch {
|
|
alert("저장 중 오류가 발생했습니다.");
|
|
}
|
|
};
|
|
|
|
const returnFunc = () => setReturnDialogOpen(true);
|
|
|
|
const handleSaveReturnReason = async () => {
|
|
if (lastSearchValues != null) {
|
|
await handleSearch(lastSearchValues);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="relative">
|
|
<SearchForm onSubmit={handleSearch} onReset={handleReset} />
|
|
|
|
{showActionBar && (
|
|
<div className="card !py-0">
|
|
<div className="flex justify-end items-center gap-[0.6rem] m-[0.6rem]">
|
|
{showReviewOpinionButton && (
|
|
<ReviewOpinionButton reviewOpinionFunc={reviewOpinionFunc} />
|
|
)}
|
|
{showReturnButton && <ReturnButton returnFunc={returnFunc} />}
|
|
{showSubmitButton && (
|
|
<SubmitButton submitFunc={() => submitFunc()} />
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{isLoading && (
|
|
<div className="absolute inset-0 z-50 [data-theme='black']:bg-transparent rounded-lg p-10 flex justify-center items-center">
|
|
<div className="relative size-[5rem]">
|
|
<LoadingWithAnimation color="primary" />
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex flex-col gap-[0.6rem] mx-[0.6rem]">
|
|
<ReviewEmptyCurrLegend />
|
|
<Tabs
|
|
value={activeTab}
|
|
onValueChange={(v) => void handleActiveTabChange(v)}
|
|
className="w-full"
|
|
variant="default"
|
|
>
|
|
<TabsList>
|
|
{columnInfo.map((info) => (
|
|
<TabsTrigger key={info.tabKey} value={info.tabKey}>
|
|
{info.tabLabel || info.tabKey}
|
|
</TabsTrigger>
|
|
))}
|
|
</TabsList>
|
|
{columnInfo.map((info) => {
|
|
const tabKey = info.tabKey;
|
|
if (!info.columns || !info.columnLayout) {
|
|
return null;
|
|
}
|
|
|
|
const rowData = rowDataMap.get(tabKey) ?? [];
|
|
const gridColumnInfo: ColumnInfo = {
|
|
columns: info.columns,
|
|
columnLayout: info.columnLayout,
|
|
};
|
|
|
|
return (
|
|
<TabsContent key={tabKey} value={tabKey}>
|
|
<DataGrid
|
|
columnInfo={gridColumnInfo}
|
|
rowStartNum={info.rowStartNum}
|
|
excelExportFileSuffix={info.tabLabel || tabKey}
|
|
rowData={rowData}
|
|
setRowData={(updater) => {
|
|
setRowDataMap((prev) => {
|
|
const next = new Map(prev);
|
|
const cur = next.get(tabKey) ?? [];
|
|
const n =
|
|
typeof updater === "function" ? updater(cur) : updater;
|
|
next.set(tabKey, n);
|
|
return next;
|
|
});
|
|
}}
|
|
isExcelImport={false}
|
|
isExcelExport
|
|
showHeaderCheckbox={false}
|
|
fixedColumns={info.fixedColumns}
|
|
enableSort={false}
|
|
cellStyleCallback={reviewCellStyleCallbackByTab.get(tabKey)}
|
|
/>
|
|
</TabsContent>
|
|
);
|
|
})}
|
|
</Tabs>
|
|
</div>
|
|
<ReviewOpinionDialog
|
|
open={reviewOpinionOpen}
|
|
onOpenChange={setReviewOpinionOpen}
|
|
context={reviewOpinionContext}
|
|
initialReviewOpinion={latestReviewOpinion}
|
|
initialProcessHistory={latestProcessHistory}
|
|
onSave={saveReviewOpinion}
|
|
/>
|
|
<ReviewSubmitDialog
|
|
open={submitDialogOpen}
|
|
onOpenChange={setSubmitDialogOpen}
|
|
context={submitDialogContext}
|
|
onSubmitted={async () => {
|
|
if (lastSearchValues != null) {
|
|
await handleSearch(lastSearchValues);
|
|
}
|
|
}}
|
|
/>
|
|
<ReturnReasonDialog
|
|
open={returnDialogOpen}
|
|
onOpenChange={setReturnDialogOpen}
|
|
context={reviewOpinionContext}
|
|
onSave={handleSaveReturnReason}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default SewerageServiceLevelRvw;
|