import type { TableUpperProps } from "../dashboard"; import { getFetch } from "@/app/components/api"; export type { TableUpperProps }; /**==================================== * Store - 타입 ====================================*/ /** * (1) 메인페이지 */ // 시설별 강수현황 export type SewageOccurProps = { bscFctCd : string, sidoNm : string, sggNm : string, lclgvrnmntNm : string, bscFctNm : string, daddr : string, fctCpc : number, waterUsage : number, swageChangePercent : number, estimationSewage : number, sewageInputYear : number, sewageInputChungchun : number, rgnNm : string, rvbsn : string, lgbsn : string, mdmbsn : string, msrDt : string, } export type modalTableType = { msrDt: string,waterUsage: number,swageChangePercent: number,estimationSewage: number}; export type SewageOccurDetailProps = { cnt : number, sum : number, yr:string, usege:number } // 검색단 const LabelArray:string[] = ['구분', '시도', '시군구','용량별', '시설명', '조회기간', '시설상태', '권역', '단위유역', '중권역', '환경청별']; export interface ExcelType { 시설코드: string, 권역:string|'', 단위유역:string|'', 중권역:string|'', 환경청:string|'', 시도: string, 시군구: string, 시설명: string, 주소: string, 시설용량: number, 물사용량: number | '', 오수전환율: number | '', 오수발생량추정: number | '', 연간하수유입량: number | '', 청천시하수유입량: number | '', } // 리스트 헤더 const upHeadList1:TableUpperProps[] = [ { id: "bscFctCd", title: "시설코드", row:0, rowSpan:2 }, { id: "A", title: "행정구역", row:0, colSpan:2 }, { id: "sidoNm", title: "시도", upName:"upChangeRe", upSequnce:1, row:1 }, { id: "sggNm", title: "시군구", upName:"upChangeRe",upSequnce:2, row:1 }, { id: "bscFctNm", title: "시설명", row:0, rowSpan:2 }, { id: "daddr", title: "주소", row:0, rowSpan:2 }, { id: "fctCpc", title: "시설용량(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "waterUsage", title: "물사용량(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "swageChangePercent", title: "오수전환율\n(%)", align:"right", row:0, rowSpan:2 }, { id: "estimationSewage", title: "오수발생량 추정\n(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "sewageInputYear", title: "연간 하수유입량\n(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "sewageInputChungchun", title: "청천시 하수유입량\n(m³/일)", align:"right", row:0, rowSpan:2 }, ]; const upHeadList2:TableUpperProps[] = [ { id: "bscFctCd", title: "시설코드", row:0, rowSpan:2 }, { id: "A", title: "유역", row:0, colSpan:3 }, { id: "rvbsn", title: "권역", upName:"upChangeRe", upSequnce:1, row:1 }, { id: "lgbsn", title: "단위유역", upName:"upChangeRe", upSequnce:2, row:1 }, { id: "mdmbsn", title: "중권역", upName:"upChangeRe", upSequnce:3, row:1 }, { id: "bscFctNm", title: "시설명", row:0, rowSpan:2 }, { id: "daddr", title: "주소", row:0, rowSpan:2 }, { id: "fctCpc", title: "시설용량(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "waterUsage", title: "물사용량(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "swageChangePercent", title: "오수전환율", align:"right", row:0, rowSpan:2 }, { id: "estimationSewage", title: "오수발생량 추정\n(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "sewageInputYear", title: "연간 하수유입량\n(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "sewageInputChungchun", title: "청천시 하수유입량\n(m³/일)", align:"right", row:0, rowSpan:2 }, ]; const upHeadList3:TableUpperProps[] = [ { id: "bscFctCd", title: "시설코드", row:0, rowSpan:2 }, { id: "A", title: "유역", row:0, colSpan:3 }, { id: "rvbsn", title: "권역", upName:"upChangeRe1", upSequnce:1, row:1 }, { id: "lgbsn", title: "단위유역", upName:"upChangeRe1", upSequnce:2, row:1 }, { id: "mdmbsn", title: "중권역", upName:"upChangeRe1", upSequnce:3, row:1 }, { id: "rgnNm", title: "환경청", row:0, rowSpan:2 }, { id: "B", title: "행정구역", row:0, colSpan:2 }, { id: "sidoNm", title: "시도", upName:"upChangeRe2", upSequnce:1, row:1 }, { id: "sggNm", title: "시군구", upName:"upChangeRe2",upSequnce:2, row:1 }, { id: "bscFctNm", title: "시설명", row:0, rowSpan:2 }, { id: "daddr", title: "주소", row:0, rowSpan:2 }, { id: "fctCpc", title: "시설용량(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "waterUsage", title: "물사용량(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "swageChangePercent", title: "오수전환율", align:"right", row:0, rowSpan:2 }, { id: "estimationSewage", title: "오수발생량 추정\n(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "sewageInputYear", title: "연간 하수유입량\n(m³/일)", align:"right", row:0, rowSpan:2 }, { id: "sewageInputChungchun", title: "청천시 하수유입량\n(m³/일)", align:"right", row:0, rowSpan:2 }, ]; const upHeadListSewageOccurDetail:TableUpperProps[] = [ { id: "msrDt", title: "년월" }, { id: "waterUsage", title: "물사용량(m³/일)" }, { id: "swageChangePercent", title: "오수전환율(%)" }, { id: "estimationSewage", title: "오수발생량 추정(m³/일)", row:0, rowSpan:2 }, ]; const sewageOccurList:SewageOccurProps[] = [] const tempSewageOccurList:modalTableType[] = [] /**==================================== * Store - API ====================================*/ const fetchSewageOccurList = async (pageNo: number, pageSize: number, searchYear: string, partCd?: string, nameCd?: string, fcltCpct?: string, operationValue?: string, fcltyNm?: string) => { // 20250701임시시 const tailStr = partCd ? '/'+ partCd + '/' + nameCd + '/' + fcltCpct + '/' + operationValue + (fcltyNm && fcltyNm !== '' ? '/' + fcltyNm : '') : ''; const url = '/fclt/flow/' + pageNo + '/' + pageSize + '/' + searchYear + tailStr; const res = await getFetch('fo', url); console.log("res", res.data); return { data: res.data, count: res.count } /* const tailStr = partCd ? '/'+ partCd + '/' + nameCd + '/' + fcltCpct + '/' + operationValue + (fcltyNm && fcltyNm !== '' ? '/' + fcltyNm : '') : ''; const url = '/fclt/qlty/' + pageNo + '/' + pageSize + '/' + searchYear + tailStr; const res = await getFetch('fo', url); return { data: res.data, count: res.count } */ } /**==================================== * Store - expaort ====================================*/ export type SewageOccurActions = { searchSewageOccurList: (pageNo: number, pageSize: number, searchYear: string, part?: string, nameCd?: string, fcltCpct?: string, operationValue?: string, fcltyNm?: string) => void; searchSewageOccurDetailList: (bscFctCd: string, searchYear: string, startYearMonth?: string, endYearMonth?: string) => void; } export type SewageOccurType = { SewageOccurSearch: { isInit: boolean; labelArray:string[]; }, SewageOccur : { isInit: boolean; upHeadList1: TableUpperProps[]; upHeadList2: TableUpperProps[]; upHeadList3: TableUpperProps[]; sewageOccurList: SewageOccurProps[]; // Changed from facilityAreaProps to ProcessAreaProps sewageOccurCount: number; excelList: ExcelType[]; isLoading: boolean; }, SewageOccurDetail : { isInit: boolean; upHeadListSewageOccurDetail: TableUpperProps[]; gridListSewageOccurDetail: modalTableType[]; } } export const sewageOccurInitState: SewageOccurType = { SewageOccurSearch: { isInit: false, labelArray: LabelArray, }, SewageOccur : { isInit : false, upHeadList1 : upHeadList1, upHeadList2 : upHeadList2, upHeadList3 : upHeadList3, sewageOccurList : sewageOccurList, sewageOccurCount : 2, excelList : [], isLoading : true, }, SewageOccurDetail : { isInit: false, upHeadListSewageOccurDetail: upHeadListSewageOccurDetail, gridListSewageOccurDetail: tempSewageOccurList, } } export type SewageOccurStore = SewageOccurType & SewageOccurActions; // Add type definitions for API responses export type FlowDataItem = { inflAmnt: string | number; infoRnDay: string | number; bscFctCd: string; msrDt: string; rainEffectDay?: number; } export type FlowResponse = { data: FlowDataItem[]; count: number; } export const sewageOccurReducer:(set:any)=>SewageOccurActions=(set: any) => { return { searchSewageOccurList: async (pageNo: number, pageSize: number, searchYear: string, part?: string, nameCd?: string, fcltCpct?: string, operationValue?: string, fcltyNm?: string) => { set((state: SewageOccurType) => ({ ...state, SewageOccur: { ...state.SewageOccur, isLoading: true, }, })); const res = await fetchSewageOccurList(pageNo, pageSize, searchYear, part, nameCd, fcltCpct, operationValue, fcltyNm); const data = res.data; // Promise.all을 사용하여 모든 비동기 작업이 완료될 때까지 기다림 const calData = await Promise.all(data.map(async (item: SewageOccurProps) => { item.sidoNm = item.lclgvrnmntNm.split(' ')[0]?.toString() || ''; item.sggNm = item.lclgvrnmntNm.split(' ')[1]?.toString() || ''; item.fctCpc = parseFloat(item.fctCpc.toString().replace(/,/g, '')); item.swageChangePercent = 80; const bscFctCd = item.bscFctCd; const res2 = await getFetch('fo', '/fclt/flow/' + bscFctCd + '/' + searchYear) as FlowResponse; let sumChungcunDay = 0; let chungcunSumFlow = 0; let totWaterUsage = 0; if(bscFctCd === '11200PB001R'){ console.log('res2.data : ',res2.data); } // 강우영향일 계산 (강우량이 3 초과이면 무조건 강우영향일) for(let idx = 0; idx < res2.data.length; idx++) { const item = res2.data[idx]; if(item){ if(item.infoRnDay && Number(item.infoRnDay.toString().trim()) >= 3) { item.rainEffectDay = 1; } else { item.rainEffectDay = 0; } } } // 이틀 전, 하루 전이 강우영향일(3 이상)인 경우 해당 날도 강우영향일로 설정 for(let idx = 0; idx < res2.data.length; idx++) { if(res2.data[idx]) { // 하루 전이 강우영향일(3 이상)인 경우 if(idx > 0 && res2.data[idx-1]?.infoRnDay && Number(res2.data[idx-1]?.infoRnDay.toString().trim()) >= 3) { res2.data[idx]!.rainEffectDay = 1; } // 이틀 전이 강우영향일(3 이상)인 경우 if(idx > 1 && res2.data[idx-2]?.infoRnDay && Number(res2.data[idx-2]?.infoRnDay.toString().trim()) >= 3) { res2.data[idx]!.rainEffectDay = 1; } } } for(let idx = 0; idx < res2.data.length; idx++) { if(res2.data[idx] && res2.data[idx]?.rainEffectDay === 1) { if(idx > 1){ if(res2.data[idx-1]?.infoRnDay && Number(res2.data[idx-1]?.infoRnDay.toString().trim()) >= 3) { res2.data[idx]!.infoRnDay = res2.data[idx-1]!.infoRnDay; } } if(idx > 2){ if(res2.data[idx-2]?.infoRnDay && Number(res2.data[idx-2]?.infoRnDay.toString().trim()) >= 3) { res2.data[idx]!.infoRnDay = res2.data[idx-2]!.infoRnDay; } } } } if(bscFctCd === '11200PB001R'){ console.log('calData : ',res2.data); } res2.data.forEach((item: FlowDataItem) => { if(Number(item.rainEffectDay) === 0){ chungcunSumFlow += Number(item.inflAmnt); sumChungcunDay += 1; } totWaterUsage += Number(item.inflAmnt); }); const avg = totWaterUsage /res2.count; item.waterUsage = parseFloat((avg * 0.65).toFixed(2)); item.sewageInputYear = parseFloat(avg.toFixed(2)); item.sewageInputChungchun = parseFloat((chungcunSumFlow===0?0:(chungcunSumFlow / sumChungcunDay)).toFixed(2)); item.estimationSewage = parseFloat((item.waterUsage * (item.swageChangePercent/100)).toFixed(2)); return item; })); // 조회 데이터 설정 set((state: any) => ({ ...state, SewageOccur: pageSize<5000?{ ...state.SewageOccur, sewageOccurList: calData, sewageOccurCount: res.count, isLoading : false, }:{ ...state.SewageOccur, excelList: [], isLoading : false, }, })) }, searchSewageOccurDetailList: async (bscFctCd: string, searchYear: string, startYearMonth?: string, endYearMonth?: string) => { const res = await getFetch('fo', '/fclt/flow/' + bscFctCd + '/' + searchYear); const monthData: SewageOccurDetailProps[] = []; let tot = 0, cnt= 0; console.log('startYearMonth : ', startYearMonth, 'endYearMonth : ', endYearMonth); if(startYearMonth) { res.data = res.data.filter((item: FlowDataItem) => { const month = item.msrDt?.substring(0, 6); return month >= startYearMonth; }); } if(endYearMonth) { res.data = res.data.filter((item: FlowDataItem) => { const month = item.msrDt?.substring(0, 6); return month <= endYearMonth; }); } res.data.forEach((item: FlowDataItem) => { const month = item.msrDt?.substring(4, 6); const monthIdx = month ? Number(month) - 1 : -1; if (monthIdx >= 0) { if (typeof monthData[monthIdx] === 'object') { monthData[monthIdx].sum += Number(item.inflAmnt); monthData[monthIdx].cnt++; } else { monthData[monthIdx] = {cnt:1, sum:Number(item.inflAmnt), yr:item.msrDt.substring(0,4), usege:0}; } } tot += Number(item.inflAmnt) cnt++ }) console.log('tot', tot, cnt, tot/cnt, tot/cnt*0.65); const tableData:modalTableType[] = [] let maxWaterUsage = 0; let minWaterUsage = 9999999999; let avgWaterUsage = 0; monthData.forEach((item, index) => { item.usege = item.sum/item.cnt *0.65 tableData.push({ msrDt: item.yr+'-'+(index+1>=10?String(index+1):'0'+(index+1)), waterUsage: Math.round(item.usege*100) /100, swageChangePercent: (Math.round((item.usege/item.usege*0.8) * 100) / 100) * 100, estimationSewage: Math.round(item.usege*0.8 * 100) / 100 }) if (item.usege > maxWaterUsage) { maxWaterUsage = item.usege; } if (item.usege < minWaterUsage) { minWaterUsage = item.usege; } }); avgWaterUsage = tot/cnt*0.65 const newData= [ { msrDt: '평균', waterUsage: Math.round(avgWaterUsage*100) /100, swageChangePercent: 80, estimationSewage: Math.round(avgWaterUsage*0.8*100) /100, }, { msrDt: '최대', waterUsage: Math.round(maxWaterUsage*100) /100, swageChangePercent: 80, estimationSewage: Math.round(maxWaterUsage*0.8*100) /100, }, { msrDt: '최소', waterUsage: Math.round(minWaterUsage*100) /100, swageChangePercent: 80, estimationSewage: Math.round(minWaterUsage*0.8*100) /100, }, ...tableData ] set((state: SewageOccurType) => ({ ...state, SewageOccurDetail: { ...state.SewageOccurDetail, gridListSewageOccurDetail: newData, } })) }, } }