Files
frontend/apps/wr/app/store/stats/rainStat/index.tsx
T

186 lines
6.6 KiB
TypeScript

import { fetchType, getFetch } from "@/components/lib/api";
import type { TableUpperProps } from "../../common/type/header";
import { excelRainStat } from "./excel";
import { rainStatHeadListExport } from "./headList";
export type RainStatProps = {
cityText: string
ctpvCd: string //시도
sggCd: string //시군구
areaNm: string //행정구역명
bdstNm: string //시설명
bdstUsgCd: string //건축물용도코드
daddr: string //주소
lawTrgtYn: string //법적의무대상여부
instlCmptnDt: string //설치완료일자
instlCst: number //설치비용
pntfColKnd: string //접수면종류
filtrTrtmfctYn: string //여과등처리시설유무
archArea: number //건축연면적
dtfcltCpct: number //처리용량(저류조용량)
wtrUsqty: number //연간사용량
utztnUsgCd: string //이용용도코드
anlOperCst: number //연간운영비
annRnwtQty: number //물사용량
jungsudo: number //중수도​이용량
dachae: number //대체수​이용량
lndwtr: number //조경용수
cleanToilUswtr: number //청소화장실용수
resn: number //세척살수용수
farmUswtr: number //농업용수
indstUswtr: number //공업용수
hydUswtr: number //친수용수
ithlEtcUswtr: number //기타
tmp: number //개소
guideStats: GuideStatProps[]
}
export type GuideStatProps = {
utztnUsgCd: string
splyUswtrUtztnQnt : number,
ruseFcltUtztnQnt: number
}
type ExcelType = {
[key: string]: string | number | undefined;
};
const excelDownList: ExcelType[] = [];
const fetchRainStatList = async (year: string) => {
const res = await getFetch(fetchType.WR, `/reuseChk/rainy/stat/${year}`)
return res;
}
type rainStatActions = {
searchRainStatList: (year: string) => void
}
export type rainStatState = {
rainStat: {
rainStatHeadList: TableUpperProps[],
rainStatList: RainStatProps[],
excelList: ExcelType[]
rainDetailList : [][]
}
}
export type rainStatType = rainStatState & rainStatActions;
export const rainStatExport: rainStatState = {
rainStat: {
rainStatHeadList: rainStatHeadListExport,
rainStatList: [],
excelList: [],
rainDetailList : []
}
}
export const rainStatActionsExport = (
set: (fn: (state: rainStatState) => rainStatState) => void
) => {
return {
searchRainStatList: async (year: string) => {
const res = await fetchRainStatList(year);
const cityArr: any[] = [];
res.forEach((item: RainStatProps) => {
let obj = cityArr.find((e) => {
return e.ctpvCd == item.ctpvCd
})
if (obj) {
obj.instlCst += item.instlCst
obj.dtfcltCpct += item.dtfcltCpct
obj.wtrUsqty += item.wtrUsqty
obj.anlOperCst += item.anlOperCst
obj.tmp += 1
} else {
obj = {};
obj.ctpvCd = item.ctpvCd
obj.instlCst = item.instlCst
obj.tmp = 1
obj.dtfcltCpct = item.dtfcltCpct
obj.wtrUsqty = item.wtrUsqty
obj.anlOperCst = item.anlOperCst
cityArr.push(obj)
}
if (item.guideStats && item.guideStats.length > 0) {
item.guideStats.forEach((guide: GuideStatProps) => {
rainStatHeadListExport.forEach((headItem: TableUpperProps) => {
if (headItem.title == guide.utztnUsgCd) {
if (!obj[headItem.id] || typeof obj[headItem.id] !== "number") obj[headItem.id] = 0;
if (typeof obj[headItem.id] === "number") {
obj[headItem.id] += guide.ruseFcltUtztnQnt;
}
}
});
})
}
})
cityArr.forEach(item => {
excelDownList.push({
ctpvCd: item.ctpvCd,
tmp: item.tmp,
instlCst: item.instlCst, //설치비
dtfcltCpct: item.dtfcltCpct, //저류조용량
bdstUsgCd: item.bdstUsgCd, //연간사용량
lndwtr: item.lndwtr, //조경용수
cleanToilUswtr: item.cleanToilUswtr, //청소화장실용수
resn: item.resn, //세척살수용수
farmUswtr: item.farmUswtr, //농업용수
indstUswtr: item.indstUswtr, //공업용수
hydUswtr: item.hydUswtr, //친수용수
ithlEtcUswtr: item.ithlEtcUswtr, //기타
anlOperCst: item.anlOperCst, //연간운영비
})
})
//상세
const rainDetailList : RainStatProps[] = [];
res.forEach((item:RainStatProps) => {
const utztnUsgArray = new Array<string>();
item.guideStats.forEach((guideStat) => {
utztnUsgArray.push(guideStat.utztnUsgCd);
})
rainDetailList.push({
...item,
utztnUsgCd : utztnUsgArray.join(', '),
})
})
const excelDetailData = excelRainStat(rainDetailList);
/* const rainStatHeadList = data.map((item:RainStatProps) => ({
...item,
ctpvCd:item.ctpvCd,
sggCd:item.sggCd,
areaNm:item.areaNm,
archArea:item.archArea,
dtfcltCpct:item.dtfcltCpct,
annRnwtQty:item.annRnwtQty,
jungsudo:item.jungsudo,
dachae:item.dachae,
way1:item.way1,
way2:item.way2,
way3:item.way3,
way4:item.way4,
way5:item.way5,
way6:item.way6,
way7:item.way7,
yearMoney:item.yearMoney,
})) */
set((state: rainStatState) => ({
rainStat: {
...state.rainStat,
rainDetailList: excelDetailData as [][],
rainStatList: cityArr,
excelList: excelDownList
}
}));
}
}
}