import { fetchType, getFetch, getFetchWithCount } from "@/components/lib/api"; import type { EnvPartSelectDataType, SelectDataType, } from "@common/business_components/ui"; import { bdstFilterYn, bdstIncenYn, chBuCd, ruseBizMthdCd, ruseSeCd, yn, } from "./basicCode"; import { normalizeSelectData } from "./normalizeSelectData"; export const btnClass = "p-4 m-2 rounded-sm text-lg font-medium text-primary-foreground "; const laeSecd = "41"; export const lwCds: Record = {}; const codeMApping = { WR001: "bdstUsgCd", WR002: "rswtBdstType", WR003: "rainFcltCd", WR004: "bdstGrntchClass", WR005: "rswtPrscCdMthdCd", WR006: "orgwtKndCd", WR007: "ruseMngMtd", WR008: "rusePrcsMthdCd", WR009: "erctUswtrPrcsMeth", WR010: "ruseFcltSpot", WR011: "pplQulity", WR012: "pplLenght", WR013: "bdstFilterYn", // 사용 불가 yn만 사용해야함으로 해당코드 사용 안함 WR014: "rainRswtMng", // 지도점검 등록 시변경 WR015: "rainFcltFlowRate", //지도점검 등록 시변경 WR016: "dmdUszCd", // 지도점검 등록 시변경 WR017: "sewageInFcltUse", //하수재이용 장내 WR018: "sewageOutFclttUse", // 하수재이용 장외 WR019: "htdPowerMthdCd", WR020: "htdUtztnUsgCd", }; export const getCommonUpCd = async (set: any) => { try { const upCd = await getFetchWithCount( fetchType.CS, "/cms-service/api/v1/up-codes?keyword=&upCd=&cdSe=&useYn=Y&siteId=WR&page=0&size=100&sort=string" ); const upWrCode: string[] = upCd.content .filter((item: any) => item.upCd.slice(0, 2) === "WR") .map((cd: any) => cd.upCd); await fetchCommonCodesByKeys(upWrCode); const lwCdCode = lwCds; set((state: comCodeState) => ({ code: { ...state.code, rainFcltCd: lwCdCode["rainFcltCd"] ?? [], bdstUsgCd: lwCdCode["bdstUsgCd"] ?? [], orgwtKndCd: lwCdCode["orgwtKndCd"] ?? [], pplQulity: lwCdCode["pplQulity"] ?? [], pplLenght: lwCdCode["pplLenght"] ?? [], dmdUszCd: lwCdCode["dmdUszCd"] ?? [], sewageOutFclttUse: lwCdCode["sewageOutFclttUse"] ?? [], rainRswtMng: lwCdCode["rainRswtMng"] ?? [], rainFcltFlowRate: lwCdCode["rainFcltFlowRate"] ?? [], ruseMngMtd: lwCdCode["ruseMngMtd"] ?? [], rusePrcsMthdCd: lwCdCode["rusePrcsMthdCd"] ?? [], erctUswtrPrcsMeth: lwCdCode["erctUswtrPrcsMeth"] ?? [], bdstGrntchClass: lwCdCode["bdstGrntchClass"] ?? [], htdPowerMthdCd: lwCdCode["htdPowerMthdCd"] ?? [], htdUtztnUsgCd: lwCdCode["htdUtztnUsgCd"] ?? [], rswtBdstType: lwCdCode["rswtBdstType"] ?? [], bdstTotalUsgType: lwCdCode["bdstUsgCd"]?.concat(lwCdCode["rswtBdstType"] || []) ?? [], }, })); } catch (e) { console.log("com code error ", e); } }; const fetchCommonCodesByKeys = async (keys: string[]) => { const targets = [...new Set(keys)].filter( (cd) => Boolean(codeMApping[cd as keyof typeof codeMApping]) ); await Promise.all( targets.map(async (cd) => { const mapping = codeMApping[cd as keyof typeof codeMApping]; if (!mapping) return; const lwCd = await getFetchWithCount( fetchType.CS, `/cms-service/api/v1/lwpo-cds/up-cd/${cd}` ); const list = Array.isArray(lwCd) ? lwCd : (lwCd.content ?? []); lwCds[mapping] = list.map((it: any) => ({ text: it.lwpoCdNm, val: it.lwpoCd, })); }) ); }; //물재이용 주소 코드: devU01TX0FVVEgyMDI1MDgyNTEwMzkwNDExNjEwNTM= export const buildingType = { RST: "RST", RNB: "RNB", HTB: "HTB", }; export const getSggCd = async (set: any) => { const sgg = await getFetch("common", "/addr/sgg"); /* const sggCd = sgg.map((item: any) => ({ val: item.adstDstrCd, text: item.gugun, })); */ const sggCd = [ // { text: "전체", val: '0' }, ...sgg.map((item: any) => ({ text: item.gugun || "", val: String(item.adstDstrCd) || "", })), ]; set((state: comCodeState) => ({ code: { ...state.code, sggCd: sggCd, }, })); }; export const getCityCd = async (set: any) => { const city = await getFetch("common", "/addr/city"); const cityCd = normalizeSelectData( city.map((item: any) => ({ val: item.adstDstrCd, text: item.sido, })), ); set((state: comCodeState) => ({ code: { ...state.code, cityCd: cityCd, }, })); }; // 권역 API 호출 함수 추가 export const getRegionCd = async (set: any) => { try { const rvbsn = await getFetch("common", "/addr/sarea"); const rvbsnCd = rvbsn.map((item: any) => ({ val: item.rvbsnCd, text: item.rvbsn, })); rvbsnCd.unshift({ val: "0", text: "전체" }); set((state: comCodeState) => ({ code: { ...state.code, rvbsnCd: rvbsnCd, }, })); } catch (error) { console.error("권역 데이터를 가져오는데 실패했습니다:", error); } }; // 중권역 API 호출 함수 추가 export const getMiddleRegionCd = async (set: any) => { try { const mdmbsn = await getFetch("common", "/addr/midArea"); const mdmbsnCd = mdmbsn.map((item: any) => ({ val: item.rvbsnCd, text: item.mdmbsn, })); mdmbsnCd.unshift({ val: "0", text: "전체" }); set((state: comCodeState) => ({ code: { ...state.code, mdmbsnCd: mdmbsnCd, }, })); } catch (error) { console.error("중권역 데이터를 가져오는데 실패했습니다:", error); } }; // 단위유역 API 호출 함수 추가 export const getUnitWaterShedCd = async (set: any) => { try { const lgbsn = await getFetch("common", "/addr/largeArea"); const lgbsnCd = lgbsn.map((item: any) => ({ val: item.rvbsnCd, text: item.lgbsn, })); lgbsnCd.unshift({ val: "0", text: "전체" }); set((state: comCodeState) => ({ code: { ...state.code, lgbsnCd: lgbsnCd, }, })); } catch (error) { console.error("대영역 데이터를 가져오는데 실패했습니다:", error); } }; // 환경청 데이터 export const getEnvPartCd = async (set: any) => { try { const lclgvrnmnt = await getFetch("common", "/addr/govArea"); const lclgvrnmntCd = lclgvrnmnt.map((item: any) => ({ val: item.lclgvrnmntCd, text: item.lclgvrnmntNm, adstDliStrCd: item.adstDliStrCd || "", })); lclgvrnmntCd.unshift({ val: "0", text: "전체", adstDliStrCd: "" }); set((state: comCodeState) => ({ code: { ...state.code, lclgvrnmntCd: lclgvrnmntCd, }, })); } catch (error) { console.error("환경청 데이터를 가져오는데 실패했습니다:", error); } }; export type comCodeState = { code: { isInit: boolean; // 초기화 여부 chBuCd: SelectDataType[]; // 변경사업코드 rainFcltCd: SelectDataType[]; // 빗물시설코드 bdstUsgCd: SelectDataType[]; // 건물용도코드 cityCd: SelectDataType[]; // 시도코드 sggCd: SelectDataType[]; // 시군구코드 rvbsnCd: SelectDataType[]; // 대권역코드 mdmbsnCd: SelectDataType[]; // 중권역코드 lgbsnCd: SelectDataType[]; // 단위유역(대영역)코드 lclgvrnmntCd: EnvPartSelectDataType[]; // 지방환경청코드 orgwtKndCd: SelectDataType[]; // 원수종류코드 yn: SelectDataType[]; // 여부코드 pplQulity: SelectDataType[]; // 관저질 pplLenght: SelectDataType[]; // 관연장 dmdUszCd: SelectDataType[]; // 용도별코드 sewageOutFclttUse: SelectDataType[]; // 하수재이용 장외코드 rainRswtMng: SelectDataType[]; // 운영관리형태코드 rainFcltFlowRate: SelectDataType[]; // 유량측정방법코드 buildingType: { RST: string; // 중수도시설 구분 RNB: string; // 빗물이용시설 구분 HTB: string; // 온배수시설 구분 }; // 시설구분 ruseMngMtd: SelectDataType[]; // 재이용관리방식코드 ruseSeCd: SelectDataType[]; // 재이용구분코드 ruseBizMthdCd: SelectDataType[]; // 재이용사업방식코드 rusePrcsMthdCd: SelectDataType[]; // 재이용처리공법코드 erctUswtrPrcsMeth: SelectDataType[]; // 하수처리공법코드 bdstFilterYn: SelectDataType[]; // 여과시설여부코드 bdstIncenYn: SelectDataType[]; // 인센티브여부코드 bdstGrntchClass: SelectDataType[]; // 보조금지원대상분류코드 htdPowerMthdCd: SelectDataType[]; // 온배수발전방식코드 htdUtztnUsgCd: SelectDataType[]; // 온배수이용용도코드 rswtBdstType: SelectDataType[]; // 중수도건물유형코드 bdstTotalUsgType: SelectDataType[]; // 전체건물용도유형코드 userSiSggCd: string; // 사용자 시군구코드 }; }; export type comCodeType = comCodeState & comCodeActions; export const comCodeStateExport: comCodeState = { code: { isInit: false, rainFcltCd: lwCds["rainFcltCd"] ?? [], bdstUsgCd: lwCds["bdstUsgCd"] ?? [], chBuCd: chBuCd, orgwtKndCd: lwCds["orgwtKndCd"] ?? [], cityCd: [], sggCd: [], rvbsnCd: [], mdmbsnCd: [], lgbsnCd: [], lclgvrnmntCd: [], pplQulity: lwCds["pplQulity"] ?? [], pplLenght: lwCds["pplLenght"] ?? [], yn: yn, dmdUszCd: lwCds["dmdUszCd"] ?? [], sewageOutFclttUse: lwCds["sewageOutFclttUse"] ?? [], rainRswtMng: lwCds["rainRswtMng"] ?? [], rainFcltFlowRate: lwCds["rainFcltFlowRate"] ?? [], buildingType: buildingType, ruseMngMtd: lwCds["ruseMngMtd"] ?? [], ruseSeCd: ruseSeCd, ruseBizMthdCd: ruseBizMthdCd, rusePrcsMthdCd: lwCds["rusePrcsMthdCd"] ?? [], erctUswtrPrcsMeth: lwCds["erctUswtrPrcsMeth"] ?? [], bdstFilterYn: bdstFilterYn, bdstIncenYn: bdstIncenYn, bdstGrntchClass: lwCds["bdstGrntchClass"] ?? [], // rswtPrscCdMthdCd: rswtPrscCdMthdCd, // 공단연락오면 바꿔야함 htdPowerMthdCd: lwCds["htdPowerMthdCd"] ?? [], htdUtztnUsgCd: lwCds["htdUtztnUsgCd"] ?? [], userSiSggCd: laeSecd, rswtBdstType: lwCds["rswtBdstType"] ?? [], bdstTotalUsgType: lwCds["bdstTotalUsgType"] ?? [], }, }; export type comCodeActions = { returnCodeText: (val: string, codeName: string) => string; returnTextCode: (text: string, codeName: string) => string; getCommonCodesByKeys: (keys: string[]) => Promise; initCommonCodes: () => Promise; }; export const codeActionExport: (set: any, get:any) => comCodeActions = (set: any, get:any) => { return { getCommonCodesByKeys: async (keys: string[]) => { try { await fetchCommonCodesByKeys(keys); const lwCdCode = lwCds; set((state: comCodeState) => ({ code: { ...state.code, rainFcltCd: lwCdCode["rainFcltCd"] ?? state.code.rainFcltCd, bdstUsgCd: lwCdCode["bdstUsgCd"] ?? state.code.bdstUsgCd, orgwtKndCd: lwCdCode["orgwtKndCd"] ?? state.code.orgwtKndCd, pplQulity: lwCdCode["pplQulity"] ?? state.code.pplQulity, pplLenght: lwCdCode["pplLenght"] ?? state.code.pplLenght, dmdUszCd: lwCdCode["dmdUszCd"] ?? state.code.dmdUszCd, sewageOutFclttUse: lwCdCode["sewageOutFclttUse"] ?? state.code.sewageOutFclttUse, rainRswtMng: lwCdCode["rainRswtMng"] ?? state.code.rainRswtMng, rainFcltFlowRate: lwCdCode["rainFcltFlowRate"] ?? state.code.rainFcltFlowRate, ruseMngMtd: lwCdCode["ruseMngMtd"] ?? state.code.ruseMngMtd, rusePrcsMthdCd: lwCdCode["rusePrcsMthdCd"] ?? state.code.rusePrcsMthdCd, erctUswtrPrcsMeth: lwCdCode["erctUswtrPrcsMeth"] ?? state.code.erctUswtrPrcsMeth, bdstGrntchClass: lwCdCode["bdstGrntchClass"] ?? state.code.bdstGrntchClass, htdPowerMthdCd: lwCdCode["htdPowerMthdCd"] ?? state.code.htdPowerMthdCd, htdUtztnUsgCd: lwCdCode["htdUtztnUsgCd"] ?? state.code.htdUtztnUsgCd, rswtBdstType: lwCdCode["rswtBdstType"] ?? state.code.rswtBdstType, bdstTotalUsgType: lwCdCode["bdstUsgCd"]?.concat(lwCdCode["rswtBdstType"] || []) ?? state.code.bdstTotalUsgType, }, })); } catch (e) { console.log("selected common code error ", e); } }, returnCodeText: (val: string, codeName: string) => { const codeList = get().code[codeName] || []; const found = codeList.find((item) => item.val === val); return found ? found.text : val; }, returnTextCode: (text: string, codeName: string) => { const codeList = get().code[codeName] || []; const found = codeList.find((item) => item.text === text); return found ? found.val : text; }, initCommonCodes: async () => { if (get().code.isInit) return; const response = await fetch("/api/common-codes", { method: "GET", headers: { accept: "application/json", }, }); if (!response.ok) { let errorMessage = `공통 코드 조회 실패: ${response.status}`; try { const errorBody = await response.json(); if (errorBody?.detail) { errorMessage += ` - ${errorBody.detail}`; } else if (errorBody?.message) { errorMessage += ` - ${errorBody.message}`; } } catch { const errorText = await response.text(); if (errorText) { errorMessage += ` - ${errorText}`; } } console.error("initCommonCodes failed:", errorMessage); throw new Error(errorMessage); } const data = await response.json(); lwCds["rainFcltCd"] = data.rainFcltCd ?? lwCds["rainFcltCd"] ?? []; lwCds["bdstUsgCd"] = data.bdstUsgCd ?? lwCds["bdstUsgCd"] ?? []; lwCds["orgwtKndCd"] = data.orgwtKndCd ?? lwCds["orgwtKndCd"] ?? []; lwCds["pplQulity"] = data.pplQulity ?? lwCds["pplQulity"] ?? []; lwCds["pplLenght"] = data.pplLenght ?? lwCds["pplLenght"] ?? []; lwCds["dmdUszCd"] = data.dmdUszCd ?? lwCds["dmdUszCd"] ?? []; lwCds["sewageOutFclttUse"] = data.sewageOutFclttUse ?? lwCds["sewageOutFclttUse"] ?? []; lwCds["rainRswtMng"] = data.rainRswtMng ?? lwCds["rainRswtMng"] ?? []; lwCds["rainFcltFlowRate"] = data.rainFcltFlowRate ?? lwCds["rainFcltFlowRate"] ?? []; lwCds["ruseMngMtd"] = data.ruseMngMtd ?? lwCds["ruseMngMtd"] ?? []; lwCds["rusePrcsMthdCd"] = data.rusePrcsMthdCd ?? lwCds["rusePrcsMthdCd"] ?? []; lwCds["erctUswtrPrcsMeth"] = data.erctUswtrPrcsMeth ?? lwCds["erctUswtrPrcsMeth"] ?? []; lwCds["bdstGrntchClass"] = data.bdstGrntchClass ?? lwCds["bdstGrntchClass"] ?? []; lwCds["htdPowerMthdCd"] = data.htdPowerMthdCd ?? lwCds["htdPowerMthdCd"] ?? []; lwCds["htdUtztnUsgCd"] = data.htdUtztnUsgCd ?? lwCds["htdUtztnUsgCd"] ?? []; lwCds["rswtBdstType"] = data.rswtBdstType ?? lwCds["rswtBdstType"] ?? []; lwCds["bdstTotalUsgType"] = data.bdstTotalUsgType ?? lwCds["bdstTotalUsgType"] ?? []; set((state) => ({ code: { ...state.code, sggCd: data.sggCd ?? state.code.sggCd, cityCd: data.cityCd ?? state.code.cityCd, rvbsnCd: data.rvbsnCd ?? state.code.rvbsnCd, mdmbsnCd: data.mdmbsnCd ?? state.code.mdmbsnCd, lgbsnCd: data.lgbsnCd ?? state.code.lgbsnCd, lclgvrnmntCd: data.lclgvrnmntCd ?? state.code.lclgvrnmntCd, rainFcltCd: data.rainFcltCd ?? state.code.rainFcltCd, bdstUsgCd: data.bdstUsgCd ?? state.code.bdstUsgCd, orgwtKndCd: data.orgwtKndCd ?? state.code.orgwtKndCd, pplQulity: data.pplQulity ?? state.code.pplQulity, pplLenght: data.pplLenght ?? state.code.pplLenght, dmdUszCd: data.dmdUszCd ?? state.code.dmdUszCd, sewageOutFclttUse: data.sewageOutFclttUse ?? state.code.sewageOutFclttUse, rainRswtMng: data.rainRswtMng ?? state.code.rainRswtMng, rainFcltFlowRate: data.rainFcltFlowRate ?? state.code.rainFcltFlowRate, ruseMngMtd: data.ruseMngMtd ?? state.code.ruseMngMtd, rusePrcsMthdCd: data.rusePrcsMthdCd ?? state.code.rusePrcsMthdCd, erctUswtrPrcsMeth: data.erctUswtrPrcsMeth ?? state.code.erctUswtrPrcsMeth, bdstGrntchClass: data.bdstGrntchClass ?? state.code.bdstGrntchClass, htdPowerMthdCd: data.htdPowerMthdCd ?? state.code.htdPowerMthdCd, htdUtztnUsgCd: data.htdUtztnUsgCd ?? state.code.htdUtztnUsgCd, rswtBdstType: data.rswtBdstType ?? state.code.rswtBdstType, bdstTotalUsgType: data.bdstTotalUsgType ?? state.code.bdstTotalUsgType, isInit: true, }, })); }, }; };