import type { rainChkZodType } from "@/app/store/prfmncReg/chkgdnReg/rainChkgdn"; import type { rswtChkZodType } from "@/app/store/prfmncReg/chkgdnReg/rswrtChkgdn"; import type { TableUpperProps } from "@/app/store/ruseFcltMng/fcltReg/rainReg"; import { Checkbox, Input, TableCell, TableRow } from "@common/components/ui"; export const rainChkTableData = (chkData: rainChkZodType[]) => { // 1. chkgdnFk + utztnUsgCd 기준 누계 정보 만들기 const maketotal = chkData.map((item: rainChkZodType) => { const selectTotal = item.guideDetails.reduce((acc: any, cur: any) => { const code = cur.utztnUsgCd; const fk = cur.chkgdnFk; const key = `${fk}_${code}`; acc[key] = acc[key] || { totalSplyUswtrUtztnQnt: 0, totalRuseFcltUtztnQnt: 0, }; acc[key].totalSplyUswtrUtztnQnt += cur.splyUswtrUtztnQnt; acc[key].totalRuseFcltUtztnQnt += cur.ruseFcltUtztnQnt; return acc; }, {}); return selectTotal; // 모든 키: `${chkgdnFk}_${utztnUsgCd}` 형태 }); // 2. 누계 정보들을 하나의 객체로 합치기 (평탄화) const flatTotalMap = Object.assign({}, ...maketotal); // 3. rainyDetails에 누계 정보 주입 const updatedRainyDetails: any[] = []; chkData.forEach((rd: rainChkZodType) => { rd.rainyDetails.forEach((rainyItem: any) => { const key = `${rainyItem.chkgdnFk}_${rainyItem.utztnUsgCd}`; const totals = flatTotalMap[key] || { totalSplyUswtrUtztnQnt: 0, totalRuseFcltUtztnQnt: 0, }; updatedRainyDetails.push({ ...rd, ...rd.building, ...rainyItem, ...totals, rowspan: rd.rainyDetails?.length, }); }); }); return updatedRainyDetails; }; export const rswtChkTableData = (chkData: rswtChkZodType[]) => { // 1. chkgdnFk + utztnUsgCd 기준 누계 정보 만들기 const maketotal = makeTotalChk(chkData) // 2. 누계 정보들을 하나의 객체로 합치기 (평탄화) const flatTotalMap = Object.assign({}, ...maketotal); // 3. rainyDetails에 누계 정보 주입 const updatedRainyDetails: any[] = []; // chkData.forEach((rd: rswtChkZodType) => { // rd.rswtrDetails.forEach((rswtItem: any) => { // const keyGuide = `${rswtItem.chkgdnFk}_${rswtItem.utztnUsgCd}_guide`; // const keyPolution = `${rswtItem.chkgdnFk}_${rswtItem.utztnUsgCd}_poliution`; // const totalsGuide = flatTotalMap[keyGuide] || { // totalSplyUswtrUtztnQnt: 0, // totalRuseFcltUtztnQnt: 0, // }; // updatedRainyDetails.push({ // ...rd, // ...rd.building, // ...rswtItem, // ...totalsGuide, // averCombResdch:totalsGuide.averCombResdch/rswtItem // totalsGuide.averTrbt:0, // totalsGuide.averBod:0, // totalsGuide.averSmll:0, // totalsGuide.averCrmty:0, // totalsGuide.averTph:0, // totalsGuide.averTn:0, // totalsGuide.averPh:0, // totalsGuide.averClIonQnt:0, // rowspan: rd.rswtrDetails?.length, // }); // }); // }); return updatedRainyDetails; }; const makeTotalChk=(chkData: rswtChkZodType[])=>{ const maketotal = chkData.map((item: rswtChkZodType) => { const selectTotalWater = item.guideDetails.reduce((acc: any, cur: any) => { const code = cur.utztnUsgCd; const fk = cur.chkgdnFk; const flg = 'guide' const key = `${fk}_${code}_${flg}`; acc[key] = acc[key] || { totalSplyUswtrUtztnQnt: 0, totalRuseFcltUtztnQnt: 0, }; acc[key].totalSplyUswtrUtztnQnt += cur.splyUswtrUtztnQnt; acc[key].totalRuseFcltUtztnQnt += cur.ruseFcltUtztnQnt; return acc; }, {}); const selectAverPolution = item.rswtrPolutions.reduce((acc: any, cur: any) => { const code = cur.utztnUsgCd; const fk = cur.chkgdnFk; const flg = 'poliution' const key = `${fk}_${code}_${flg}`; acc[key] = acc[key] || { averCombResdch:0, averTrbt:0, averBod:0, averSmll:0, averCrmty:0, averTph:0, averTn:0, averPh:0, averClIonQnt:0, node:0 }; acc[key].averCombResdch+=cur.combResdch acc[key].averTrbt+=cur.trbt acc[key].averBod+=cur.bod acc[key].averSmll+=cur.smll acc[key].averCrmty+=cur.crmty acc[key].averTph+=cur.tph acc[key].averTn+=cur.tn acc[key].averPh+=cur.ph acc[key].averClIonQnt+=cur.clIonQnt acc[key].nod+=1 return acc; }, {}); return {...selectTotalWater, ...selectAverPolution}; // 모든 키: `${chkgdnFk}_${utztnUsgCd}` 형태 }); return maketotal; } export const chRowspanTable = ( makeTableData: any[], headList: TableUpperProps[], handleChange: ( rowIndex: number, field: keyof (typeof makeTableData)[0], value: string | number ) => void, toggleSelection: (pk: number, checked: boolean) => void, selectedIds: number[], handleCellClick: (index: number, headId: string) => void ) => { // rowspan된 셀 렌더링 여부 추적용 const renderedMap = new Map>(); return ( <> {makeTableData.map((row, rowIndex) => ( {headList.map((head) => { const key = head.id; const isMerged = head.rowSpan; const value = row[key]; // 병합 대상이면 중복 렌더링 여부 판단 if (isMerged) { const renderedSet = renderedMap.get(key) ?? new Set(); if (renderedSet.has(rowIndex)) return null; // 이미 렌더링된 셀은 skip const rowspan = row.rowspan ?? 1; for (let i = 0; i < rowspan; i++) { renderedSet.add(rowIndex + i); } renderedMap.set(key, renderedSet); return ( {key === "order" ? ( toggleSelection(row.chkgdnPk, checked === true) } > ) : ( value )} ); } // 기본 셀 return ( handleCellClick(rowIndex, head.id)} > {key === "totalSplyUswtrUtztnQnt" || key === "totalRuseFcltUtztnQnt" || key === "utztnUsgCd" ? ( `${value}` ) : ( { handleChange(rowIndex, head.id, e.target.value); }} value={value} /> )} ); })} ))} ); };