298 lines
13 KiB
TypeScript
298 lines
13 KiB
TypeScript
import { fetchType, getFetch, postFectch } from "@/components/lib/api";
|
|
import type { TableUpperProps } from "../../common/type/header";
|
|
import { monthSttnHeadListExport, pbnstPrfmncSttnHeadListExport, pbnstYearHeadListExport } from "./headList";
|
|
import type { FcPrfType, PrfType } from "../../prfmncReg/pbnstPrfmncReg/type";
|
|
import type { pubHasuType, selectLabel } from "../../ruseFcltMng/fcltReg/pbnstReg/type";
|
|
import type { ReturnType } from "@common/business_components";
|
|
import type { WaterReuseFacility } from "../../ruseFcltMng/fcltSttn/pbnstSttn/type";
|
|
|
|
type pbnstPrfmncSttnActions = {
|
|
tableHeadMaker: (startYear: number, endYear: number) => void;
|
|
// pbnstPrfmncInit: (page: string, sggOrCity: string) => void;
|
|
pbnstPrfmncSearch: (page: number, returnType: ReturnType, fcltNm: string, fromDate: string, toDate: string, fclts: string[]) => void;
|
|
getFlctsSttn: () => void;
|
|
}
|
|
|
|
const pbnstPrfmncRes: PrfType[] = [];
|
|
const prfmncRes: FcPrfType[] = [];
|
|
|
|
export type pbnstPrfmncSttnState = {
|
|
pbnstPrfmncSttn: {
|
|
pbnstPrfHeadList: TableUpperProps[];
|
|
pbnstPrfmncRes: PrfType[];
|
|
prfmncRes: any[];
|
|
isInit: boolean;
|
|
rawData: any[];
|
|
selectYearList: selectLabel[]
|
|
monthSttnHeadList: TableUpperProps[];
|
|
fclts: string[];
|
|
prfmncResCount: number;
|
|
}
|
|
}
|
|
|
|
export type pbnstPrfmncSttnType = pbnstPrfmncSttnState & pbnstPrfmncSttnActions;
|
|
|
|
export const pbnstPrfmncSttnExport: pbnstPrfmncSttnState = {
|
|
pbnstPrfmncSttn: {
|
|
pbnstPrfHeadList: [...pbnstPrfmncSttnHeadListExport],
|
|
pbnstPrfmncRes: pbnstPrfmncRes,
|
|
prfmncRes: prfmncRes,
|
|
isInit: false,
|
|
rawData: [],
|
|
selectYearList: [],
|
|
monthSttnHeadList: monthSttnHeadListExport,
|
|
fclts: [],
|
|
prfmncResCount: 1,
|
|
}
|
|
}
|
|
|
|
const hasuListFetch = async (
|
|
page: number,
|
|
returnType: ReturnType,
|
|
searchNm: string | undefined,
|
|
fromDate: string,
|
|
toDate: string,
|
|
flcts: string[]
|
|
) => {
|
|
let url = '';
|
|
let filteredFclt = flcts;
|
|
const filterData = returnType.sigunValue !== '0' ? returnType.sigunValue : returnType.sidoValue
|
|
|
|
if (filterData !== '0')
|
|
filteredFclt = flcts.filter((fclt: string) => {
|
|
return filterData.length == 2 ? fclt.slice(0, 2) === filterData : fclt.slice(0, 5) === filterData
|
|
})
|
|
if (returnType.partValue === "A") {
|
|
url += `${returnType.partValue}/${
|
|
// returnType.sidoValue ? returnType.sidoValue : returnType.sigunValue
|
|
returnType.sigunValue !== '0' ? returnType.sigunValue : returnType.sidoValue
|
|
}/0/1/SW`
|
|
|
|
if (filterData !== '0')
|
|
filteredFclt = flcts.filter((fclt: string) => {
|
|
return filterData.length == 2 ? fclt.slice(0, 2) === filterData : fclt.slice(0, 5) === filterData
|
|
})
|
|
} else if (returnType.partValue === "R") {
|
|
url += `${returnType.partValue}/${returnType.regionValue
|
|
? returnType.regionValue
|
|
: returnType.unitWaterShedValue
|
|
? returnType.unitWaterShedValue
|
|
: returnType.middleRegionValue
|
|
}/0/1/SW`
|
|
} else if (returnType.partValue === 'G') {
|
|
url += `${returnType.envPartValue}/${returnType.sigunValue !== '0' ? returnType.sigunValue : returnType.sidoValue}/0/1/SW`
|
|
if (filterData !== '0')
|
|
filteredFclt = flcts.filter((fclt: string) => {
|
|
return filterData.length == 2 ? fclt.slice(0, 2) === filterData : fclt.slice(0, 5) === filterData
|
|
})
|
|
} else {
|
|
url += `A/0/0/1/SW`
|
|
}
|
|
if (searchNm && searchNm?.length > 0) {
|
|
url += "/" + searchNm
|
|
}
|
|
const pbnList = await postFectch(`/prcsFclt/ruseInfo/1/${filteredFclt.length}/` + url, filteredFclt, fetchType.COMMON);
|
|
const checkRuseFcltList = searchNm && searchNm?.length > 0 ? pbnList.data.map((item) => item.bscFctCd) : filteredFclt
|
|
const getRuseList = await postFectch(
|
|
`/reuseFc/pubDetailPrfList/${fromDate}/${toDate}/${filterData}/${page}/5`,
|
|
checkRuseFcltList,
|
|
fetchType.WR
|
|
);
|
|
const checkWrRegListRes = getRuseList.data;
|
|
|
|
const data: WaterReuseFacility[] = checkWrRegListRes.map(
|
|
(item: FcPrfType) => {
|
|
if (!item.prfList || item.prfList.length === 0) {
|
|
return null;
|
|
}
|
|
const findData = pbnList.data.find(
|
|
(fclt: pubHasuType) =>
|
|
fclt.bscFctCd === item.pbnstRuseFcltId.substring(0, 11)
|
|
);
|
|
if (findData) {
|
|
return {
|
|
sido: findData?.sido || '-',
|
|
gugun: findData?.gugun || '-',
|
|
bscFctNm: findData?.bscFctNm || '-',
|
|
ruseUtztnFcltNm: item?.ruseUtztnFcltNm || '-',
|
|
pbnstRuseFcltId: item?.pbnstRuseFcltId || '-',
|
|
bscFctCd: findData?.bscFctCd || '-',
|
|
prfList: item.prfList,
|
|
}
|
|
}
|
|
}
|
|
).filter(Boolean);
|
|
return {data: data, count: getRuseList.count};
|
|
}
|
|
|
|
export const pbnstPrfmncSttnActionsExport: (set: any) => pbnstPrfmncSttnActions = (
|
|
set: any
|
|
) => {
|
|
return {
|
|
getFlctsSttn: async () => {
|
|
const fclts = await getFetch(fetchType.WR, '/reuseFc/pubFc')
|
|
return set((state: pbnstPrfmncSttnState) => ({
|
|
pbnstPrfmncSttn: {
|
|
...state.pbnstPrfmncSttn,
|
|
fclts: fclts
|
|
},
|
|
}));
|
|
},
|
|
tableHeadMaker: (startYear: number, endYear: number) => {
|
|
let yearHeader: TableUpperProps[] = [...pbnstPrfmncSttnHeadListExport];
|
|
const selectList = [] as unknown as selectLabel[]
|
|
for (let i = startYear; i <= endYear; i++) {
|
|
const yearHead = JSON.parse(JSON.stringify(pbnstYearHeadListExport));
|
|
if (yearHead) {
|
|
yearHead.forEach((item: { id?: string; }) => {
|
|
if (item.id) {
|
|
if (item.id === "year") { item.id = ""; return; }
|
|
item.id = i + item.id;
|
|
}
|
|
})
|
|
if (yearHead[0])
|
|
yearHead[0].title = i + "년도"
|
|
}
|
|
yearHeader = [...yearHeader, ...yearHead]
|
|
selectList.push({ text: i + "년도", val: String(i) })
|
|
}
|
|
set((state: pbnstPrfmncSttnState) => {
|
|
return {
|
|
...state,
|
|
pbnstPrfmncSttn: {
|
|
...state.pbnstPrfmncSttn,
|
|
pbnstPrfHeadList: [...yearHeader],
|
|
selectYearList: selectList
|
|
},
|
|
};
|
|
});
|
|
},
|
|
// pbnstPrfmncInit: async (page: string, sggOrCity: string) => {
|
|
// try {
|
|
// const prfmncRes = await hasuListFetch(page, sggOrCity);
|
|
// return set((state: pbnstPrfmncSttnState) => ({
|
|
// ...state,
|
|
// pbnstPrfmncSttn: {
|
|
// ...state.pbnstPrfmncSttn,
|
|
// prfmncRes: prfmncRes,
|
|
// isInit: true,
|
|
// },
|
|
// }));
|
|
// } catch (e) {
|
|
// console.log(e)
|
|
// }
|
|
// },
|
|
pbnstPrfmncSearch: async (page: number, returnType: ReturnType, fcltNm: string, fromDate: string, toDate: string, fclts: string[]) => {
|
|
try {
|
|
const prfmncRes = await hasuListFetch(
|
|
page,
|
|
returnType,
|
|
fcltNm,
|
|
fromDate,
|
|
toDate,
|
|
fclts);
|
|
|
|
let inUsageSum = 0;
|
|
let outUsageSum = 0;
|
|
let year = "";
|
|
const yearsData = [] as any
|
|
|
|
prfmncRes.data.forEach((item: any) => {
|
|
if (!item.prfList || item.prfList.length === 0) {
|
|
const year: string = ""; // 연도 정보가 없으면 빈 문자열, 필요시 item에서 추출.
|
|
const yearVal: { [key: string]: any } = { year: year, pbnstRuseFcltId: item.pbnstRuseFcltId ?? "", ruseUtztnFcltNm: item.ruseUtztnFcltNm ?? "" };
|
|
yearVal[`${year}inUsage`] = 0;
|
|
yearVal[`${year}outUsage`] = 0;
|
|
yearVal[`${year}total`] = 0;
|
|
yearsData.push(yearVal);
|
|
}
|
|
item.prfList?.forEach((acc: any) => {
|
|
year = acc.ym?.substring(0, 4) ?? "";
|
|
inUsageSum =
|
|
(acc.wshwtr ?? 0) + //조경용수
|
|
(acc.coolUswtr ?? 0) + //냉각수
|
|
(acc.cleanUswtr ?? 0) + //청소수
|
|
(acc.drwtUswtr ?? 0) + //식수대
|
|
(acc.dltnUswtr ?? 0) + //희석용수
|
|
(acc.ithlRswtr ?? 0) + //중수도
|
|
(acc.ithlEtcUswtr ?? 0) //기타
|
|
outUsageSum =
|
|
(acc.cleanToilUswtr ?? 0) + //청소화장실용수
|
|
(acc.resn ?? 0) + //세척살수용도
|
|
(acc.lndwtr ?? 0) + //조경용수
|
|
(acc.hydUswtr ?? 0) + //친수용수
|
|
(acc.ugwtChr ?? 0) + //지하수충전
|
|
(acc.farmUswtr ?? 0) + //농업용수
|
|
(acc.rvrKeepUswtr ?? 0) + //하천등유지용수
|
|
(acc.indstUswtr ?? 0); //공업용수
|
|
const yearData = yearsData.find(
|
|
(yearItem) =>
|
|
yearItem.year === year &&
|
|
yearItem.pbnstRuseFcltId == acc.pbnstRuseFcltId
|
|
);
|
|
|
|
if (yearData) {
|
|
yearData[`${year}inUsage`] += inUsageSum;
|
|
yearData[`${year}outUsage`] += outUsageSum;
|
|
yearData[`${year}total`] += (inUsageSum + outUsageSum);
|
|
} else {
|
|
const yearVal: { [key: string]: any } = { year: year, pbnstRuseFcltId: acc.pbnstRuseFcltId, ruseUtztnFcltNm: item.ruseUtztnFcltNm ?? "" };
|
|
yearVal[`${year}inUsage`] = inUsageSum;
|
|
yearVal[`${year}outUsage`] = outUsageSum;
|
|
yearVal[`${year}total`] = inUsageSum + outUsageSum;
|
|
yearsData.push(yearVal);
|
|
}
|
|
});
|
|
});
|
|
|
|
// 연도 필터링
|
|
const from = Number(fromDate);
|
|
const to = Number(toDate);
|
|
|
|
// 연도별 실적을 시설별로 병합
|
|
const mergedMap: { [key: string]: any } = {};
|
|
|
|
yearsData.forEach((acc) => {
|
|
const facilityId = acc.pbnstRuseFcltId;
|
|
const year = acc.year;
|
|
|
|
// 연도 필터 범위 안에만 포함
|
|
if (!year || Number(year) < from || Number(year) > to) return;
|
|
|
|
if (!mergedMap[facilityId]) {
|
|
mergedMap[facilityId] = {
|
|
pbnstRuseFcltId: facilityId,
|
|
ruseUtztnFcltNm: acc.ruseUtztnFcltNm,
|
|
};
|
|
}
|
|
|
|
// 해당 연도 데이터 추가
|
|
mergedMap[facilityId][`${year}inUsage`] = acc[`${year}inUsage`] ?? 0;
|
|
mergedMap[facilityId][`${year}outUsage`] = acc[`${year}outUsage`] ?? 0;
|
|
mergedMap[facilityId][`${year}total`] = acc[`${year}total`] ?? 0;
|
|
});
|
|
|
|
// prfmncRes 정보 merge
|
|
const resData = Object.values(mergedMap).map((item: any) => {
|
|
const match = prfmncRes.data.find((res) => res.pbnstRuseFcltId === item.pbnstRuseFcltId);
|
|
return match ? { ...item, ...match } : item;
|
|
});
|
|
|
|
return set((state: pbnstPrfmncSttnState) => ({
|
|
...state,
|
|
pbnstPrfmncSttn: {
|
|
...state.pbnstPrfmncSttn,
|
|
// prfmncRes: prfmncRes,
|
|
prfmncResCount: prfmncRes.count,
|
|
isInit: false,
|
|
prfmncRes: resData,
|
|
rawData: prfmncRes.data
|
|
},
|
|
}));
|
|
} catch (errorHasu) {
|
|
console.log(errorHasu);
|
|
}
|
|
},
|
|
|
|
}
|
|
} |