240 lines
7.9 KiB
TypeScript
240 lines
7.9 KiB
TypeScript
import { getFetchWithCount, postFectch, putFectch } from "@/components/lib/api";
|
|
|
|
import type { TableUpperProps } from "@/app/store/common/type/header";
|
|
import toast from "react-hot-toast";
|
|
import { z } from "zod";
|
|
import { makeMdfr, makeRstr } from "../bdstReg";
|
|
import {
|
|
htdflReuseBuildingSchema,
|
|
validationMessages
|
|
} from "../rainReg/type"; // Import baseReuseBuildingSchema, chgBuildingSchema, validationMessages
|
|
import { htdTableHeadList } from "./headList";
|
|
|
|
const hdtflObjSchema = z.object({
|
|
htdflFcltPk: z.number().nullish(),
|
|
bdstRuseFcltSn: z.number().nullish(),
|
|
etgtType: z
|
|
.string()
|
|
.min(1, { message: validationMessages.required("에너지원 종류") }),
|
|
fclCpct: z
|
|
.number()
|
|
.positive({ message: validationMessages.required("시설 용량") }),
|
|
plwtYn: z.enum(["Y", "N"]).default("Y"),
|
|
orgwtWtritSpt: z
|
|
.string()
|
|
.min(1, { message: validationMessages.required("원수 취수 지점") }),
|
|
orgwtOcrnProcs: z
|
|
.string()
|
|
.min(1, { message: validationMessages.required("원수 발생 공정") }),
|
|
wtritEntrcTp: z
|
|
.number()
|
|
.max(100, { message: "온도는 100이상이 될 수 없습니다." })
|
|
.default(0), // Changed max to 100 based on message
|
|
amtgnr: z.number().default(0),
|
|
prcsProcsYn: z.enum(["Y", "N"]).default("Y"),
|
|
prcsFcltCpct: z
|
|
.number()
|
|
.positive({ message: validationMessages.required("처리시설 용량") }),
|
|
fmYn: z.string().default(""),
|
|
utztnCpct: z
|
|
.number()
|
|
.positive({ message: validationMessages.required("이용 용량") }),
|
|
utztnUsg: z
|
|
.string()
|
|
.min(1, { message: validationMessages.required("이용 용도") }),
|
|
grssClfg: z.number().default(0), // Measurements, 0 is fine as default
|
|
combResdch: z.number().default(0), // Measurements, 0 is fine as default
|
|
trbt: z.number().default(0), // Measurements, 0 is fine as default
|
|
bod: z.number().default(0), // Measurements, 0 is fine as default
|
|
smll: z.number().default(0), // Measurements, 0 is fine as default
|
|
crmty: z.number().default(0), // Measurements, 0 is fine as default
|
|
tn: z.number().default(0), // Measurements, 0 is fine as default
|
|
tph: z.number().default(0), // Measurements, 0 is fine as default
|
|
ph: z.number().default(0), // Measurements, 0 is fine as default
|
|
clIonQnt: z.number().default(0), // Measurements, 0 is fine as default
|
|
cdtnv: z.number().default(0), // Measurements, 0 is fine as default
|
|
emsnExitTp: z.number()
|
|
.max(100, { message: "온도는 100이상이 될 수 없습니다." })
|
|
.default(0), // Changed max to 100 based on message, // Measurements, 0 is fine as default
|
|
rgtr: z.string().nullish().default(null),
|
|
mdfr: z.string().nullish().default(null),
|
|
});
|
|
|
|
export const hdtflZod = z.object({
|
|
bdstInfoSn: z.number().nullish(),
|
|
ctpvCd: z.number().default(0),
|
|
sggCd: z.number().default(0),
|
|
cityText: z.string().nullish(),
|
|
sggText: z.string().nullish(),
|
|
daddr: z.string().default(" "),
|
|
bdstNm: z.string().default(" "),
|
|
frstArchPrmsnDt: z.date().default(() => new Date()),
|
|
archChgPrmsnDt: z
|
|
.date()
|
|
.default(() => new Date())
|
|
.nullish(),
|
|
archArea: z.string().default(''),
|
|
archChgArea: z.number().nullish().default(0),
|
|
rgtr: z.string().nullish().default(null),
|
|
mdfr: z.string().nullish().default(null),
|
|
reuseBuilding: z
|
|
.array(
|
|
htdflReuseBuildingSchema.extend({
|
|
htdflObj: hdtflObjSchema,
|
|
})
|
|
)
|
|
.default([]),
|
|
//온배수는 건축물은 등록 하는것이 다름
|
|
// chgBuilding: z.array(chgBuildingSchema).nullish(), // Use imported chgBuildingSchema
|
|
});
|
|
export type customHtdflBdst = z.infer<typeof hdtflZod>;
|
|
export type htdflFacState = {
|
|
htdfl: {
|
|
isInit: boolean;
|
|
upHeadList: TableUpperProps[];
|
|
htdflRes: customHtdflBdst[];
|
|
htdflResCount:number
|
|
};
|
|
};
|
|
type htdflFacSActions = {
|
|
initHtdflReg: (code: string) => void;
|
|
htdflNmSearch: (code: string | number, fcltNm:string) => void;
|
|
htdflBdstUpdate: (data: customHtdflBdst, code: string | number) => void;
|
|
htdflBdstReg: (data: customHtdflBdst, code: string | number) => void;
|
|
getHtdflBdstData: (pageNation: number, code: string | number) => void;
|
|
};
|
|
|
|
const htdflRes: customHtdflBdst[] = [];
|
|
|
|
export type htdflFacType = htdflFacState & htdflFacSActions;
|
|
export const htdflFacStateExport: htdflFacState = {
|
|
htdfl: {
|
|
isInit: false,
|
|
upHeadList: htdTableHeadList,
|
|
htdflRes: htdflRes,
|
|
htdflResCount:1,
|
|
},
|
|
};
|
|
|
|
const initFetch = async (code: string) => {
|
|
|
|
let url = `/drain/facilityList/1/11`
|
|
|
|
url+=`/${code}`
|
|
|
|
|
|
const rswtRes = await getFetchWithCount("wr", url);
|
|
return rswtRes;
|
|
};
|
|
export const htdflFacActionsExport: (set: any) => htdflFacSActions = (
|
|
set: any
|
|
) => {
|
|
return {
|
|
getHtdflBdstData: async (pageNation: number, code: string | number) => {
|
|
try {
|
|
const htdflRes = await getFetchWithCount(
|
|
"wr",
|
|
`/drain/facilityList/${pageNation}/11/${code}`
|
|
);
|
|
return set((state: htdflFacState) => ({
|
|
...state,
|
|
htdfl: {
|
|
...state.htdfl,
|
|
htdflRes: htdflRes.data,
|
|
htdflResCount:htdflRes.count,
|
|
isInit: false,
|
|
},
|
|
}));
|
|
} catch (e) {
|
|
console.log(e);
|
|
}
|
|
},
|
|
initHtdflReg: async (code: string) => {
|
|
try {
|
|
const htdflRes = await initFetch( String(code));
|
|
set((state: htdflFacState) => {
|
|
return {
|
|
...state,
|
|
htdfl: {
|
|
...state.htdfl,
|
|
htdflRes: htdflRes.data,
|
|
htdflResCount:htdflRes.count,
|
|
isInit: false,
|
|
},
|
|
};
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
},
|
|
htdflBdstUpdate: async (data: customHtdflBdst, code: string | number) => {
|
|
try {
|
|
const chData = await makeMdfr(data);
|
|
const res = await putFectch("/drain/facility", chData);
|
|
if (res.status) {
|
|
toast.success("온배수 시설물 수정이 완료되었습니다.");
|
|
}
|
|
const htdflRes = await getFetchWithCount(
|
|
"wr",
|
|
`/drain/facilityList/1/11/${code}`
|
|
);
|
|
set((state: htdflFacState) => ({
|
|
...state,
|
|
htdfl: {
|
|
...state.htdfl,
|
|
htdflRes: htdflRes.data,
|
|
htdflResCount:htdflRes.count,
|
|
isInit: false,
|
|
},
|
|
}));
|
|
} catch (errorUpdate) {
|
|
toast.error("온배수 시설물 수정 오류입니다..");
|
|
console.log(errorUpdate);
|
|
}
|
|
},
|
|
htdflBdstReg: async (data: customHtdflBdst, code: string | number) => {
|
|
try {
|
|
// data는 단일 객체이므로 .map을 사용할 수 없습니다.
|
|
// 등록 시에는 건축물 변경 이력이 없으므로 chgBuilding을 빈 배열로 설정하고,
|
|
// API가 배열을 기대하므로 단일 객체를 배열로 감싸줍니다.
|
|
const chData = makeRstr(data);
|
|
const res = await postFectch("/drain/facility", chData);
|
|
if (res.status ===200) {
|
|
toast.success("온배수 시설 등록이 완료되었습니다.");
|
|
}
|
|
const htdflRes = await initFetch(String(code));
|
|
set((state: htdflFacState) => ({
|
|
...state,
|
|
htdfl: {
|
|
...state.htdfl,
|
|
htdflRes: htdflRes.data,
|
|
htdflResCount:htdflRes.count,
|
|
isInit: false,
|
|
},
|
|
}));
|
|
} catch (errorReg) {
|
|
toast.error("온배수 시설물 등록 오류입니다..");
|
|
console.log(errorReg);
|
|
|
|
}
|
|
},htdflNmSearch :async(code:string | number, fcltNm:string) =>{
|
|
try {
|
|
const htdflRes = await getFetchWithCount("wr", `/drain/facilityList/1/11/${code}/${fcltNm}`);
|
|
set((state: htdflFacState) => {
|
|
return {
|
|
...state,
|
|
htdfl: {
|
|
...state.htdfl,
|
|
htdflRes: htdflRes.data,
|
|
htdflResCount:htdflRes.count,
|
|
isInit: false,
|
|
},
|
|
};
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
},
|
|
};
|
|
};
|