import type { rainChkZodType } from "@/app/store/prfmncReg/chkgdnReg/rainChkgdn"; import { fetchType, getFetch, getFetchWithCount } from "@/components/lib/api"; import { type customRainBdst, } from "../../fcltReg/rainReg"; import { excelRainSttnParsing } from "./excel"; import { rainSttHeadListExport, sttnHeadListType, rainSttNodeListExport, rainSttnDetailHeadListExport, rainSttnDetailNodeListExport, } from "./headList"; import type { z } from "zod"; import type { CommonTableProps } from "@/app/store/common/type/header"; import type { SelectDataType } from "@common/business_components/ui"; // 테이블 헤드 리스트 const rainSttHeadList = rainSttHeadListExport; const rainSttNodeList = rainSttNodeListExport; const rainSttnDetailNodeList = rainSttnDetailNodeListExport; const rainSttnDetailHeadList = rainSttnDetailHeadListExport; const rainSttnRes: customRainBdst[] = []; const rainChkRes: rainChkZodType = {} as rainChkZodType const visibleHeadList = (falseHeads: CommonTableProps[], orgNodeList: CommonTableProps[]) => { const visibleHeads = orgNodeList.map((node) => { const copyNode = { ...node } falseHeads.forEach((head) => { if (copyNode.id === head.id) copyNode.visible = head.visible }) return copyNode; }) return visibleHeads } const getColspanReductionMap = (falseHeads: CommonTableProps[], orgList: CommonTableProps[]): Record => { const reduceMap: Record = {}; falseHeads.forEach(head => { const node = orgList.find(n => n.id === head.id); if (node?.upName) { reduceMap[node.upName] = (reduceMap[node.upName] || 0) + 1; if (node.row && node.row >= 3) { const upperUpName = orgList.find(n => n.id === node.upName)?.upName; if (upperUpName) { reduceMap[upperUpName] = (reduceMap[upperUpName] || 0) + 1; } } } }); return reduceMap; }; // colSpan 반영 및 visible 처리 const applyColspanReduction = ( orgList: CommonTableProps[], reduceMap: Record ): CommonTableProps[] => { return orgList.map(node => { const newNode: CommonTableProps = { ...node }; const reduceCount = reduceMap[node.id]; if ( newNode.colSpan !== undefined && reduceCount !== undefined && typeof newNode.colSpan === "number" ) { newNode.colSpan -= reduceCount; if (newNode.colSpan <= 0) { newNode.visible = false; } } return newNode; }); }; // 일단 헤더가 3개 이상인경우 visible 처리를 위해서 공통함수로 작업 export const controlHeadView = (headList: CommonTableProps[], orgNodeList: CommonTableProps[]) => { const falseHeads = headList.filter((item) => item.visible == false); const viewHeadList = visibleHeadList(falseHeads, orgNodeList) const reduceMap = getColspanReductionMap(falseHeads, viewHeadList) return applyColspanReduction(viewHeadList, reduceMap); } export type rainSttnState = { rainSttn: { rainSttnRes: customRainBdst[]; rainSttHeadList: CommonTableProps[]; rainSttNodeList: CommonTableProps[]; rainSttnDetailHeadList: CommonTableProps[] rainSttnDetailNodeList: CommonTableProps[] rainChkRes: rainChkZodType excelList: []; isInit: boolean; rainSttnResCount: number; }; }; type rainSttnActions = { rainSttnInit: (isExcel: boolean, page: number, total: string, con: string, txt: string, sidoCode: SelectDataType[], sggCode: SelectDataType[]) => void rainSttnSearch: (isExcel: boolean, page: number, total: string, con: string, txt: string, sidoCode: SelectDataType[], sggCode: SelectDataType[]) => void; rainChSttnHead: ( headList: z.infer, ) => void; rainSttnChkSearch: (rusePk: string) => void; }; export type rainSttnType = rainSttnState & rainSttnActions; export const rainSttnStateExport: rainSttnState = { rainSttn: { rainSttHeadList: rainSttHeadList, rainSttNodeList: rainSttNodeList, rainSttnDetailHeadList: rainSttnDetailHeadList, rainSttnDetailNodeList: rainSttnDetailNodeList, rainSttnRes: rainSttnRes, rainChkRes: rainChkRes, excelList: [], isInit: false, rainSttnResCount: 1, }, }; export const rainSttnActionsExport: (set: any) => rainSttnActions = ( set: any ) => { return { rainSttnInit: async (isExcel: boolean, page: number, total: string, con: string, txt: string, sidoCode: SelectDataType[], sggCode: SelectDataType[]) => { try { const rainSttnRes = await getFetchWithCount( fetchType.WR, `/reuseFc/rainyInfoList` + (page ? `/${page}` : "/1") + (total ? `/${total}` : "/20") + (con ? `/${con}` : "/0") + (txt ? `/${txt}` : "") ); if (isExcel) { const rainExcel = excelRainSttnParsing(rainSttnRes, sidoCode, sggCode); set((state: rainSttnState) => { return { ...state, rainSttn: { ...state.rainSttn, excelList: rainExcel, }, }; }); } else { set((state: rainSttnState) => { return { ...state, rainSttn: { ...state.rainSttn, rainSttnRes: rainSttnRes.data, isInit: false, }, }; }); } } catch (e) { console.log(e) } }, rainSttnSearch: async (isExcel: boolean, page: number, total: string, con: string, txt: string, sidoCode: SelectDataType[], sggCode: SelectDataType[]) => { try { const rainSttnRes = await getFetchWithCount( fetchType.WR, `/reuseFc/rainyInfoList` + (page ? `/${page}` : "/1") + (total ? `/${total}` : "/20") + (con ? `/${con}` : "/0") + (txt ? `/${txt}` : "") ); if (isExcel) { const rainExcel = excelRainSttnParsing(rainSttnRes, sidoCode, sggCode); set((state: rainSttnState) => { return { ...state, rainSttn: { ...state.rainSttn, excelList: rainExcel, rainSttnResCount: rainSttnRes.count, }, }; }); } else set((state: rainSttnState) => { return { ...state, rainSttn: { ...state.rainSttn, rainSttnRes: rainSttnRes.data, rainSttnResCount: rainSttnRes.count, isInit: false, }, }; }); } catch (erTxt) { console.log("error", erTxt); } }, rainChSttnHead: async ( headList: z.infer ) => { const changeNodeList = controlHeadView(headList, rainSttNodeList) return set((state: rainSttnState) => ({ rainSttn: { ...state.rainSttn, rainSttHeadList: headList, rainSttNodeList: changeNodeList, }, })); }, rainSttnChkSearch: async ( rusePk: string ) => { const rainChkRes = await getFetch( fetchType.WR, `/reuseChk/rainy/${rusePk}` ); return set((state: rainSttnState) => ({ rainSttn: { ...state.rainSttn, rainChkRes: rainChkRes }, })) } }; };