298 lines
7.6 KiB
TypeScript
298 lines
7.6 KiB
TypeScript
import { useState, useEffect } from "react";
|
|
import moment from "moment";
|
|
import { getData } from "@/components/lib/api";
|
|
|
|
export type TInftwtAnls = {
|
|
inftwtAnlsSn: number;
|
|
aplcnYm: string;
|
|
ctpvCd: string;
|
|
sggCd: string;
|
|
swtrmtFcltNm: string;
|
|
atflCd: string;
|
|
atflId: string;
|
|
atflInpNm: string;
|
|
atflHrPttnNm: string;
|
|
atflMpngNm: string;
|
|
atflOilqNm: string;
|
|
atflNm: string;
|
|
drtwSwtcRt: number;
|
|
anlsInfo: string;
|
|
scsYn: string;
|
|
};
|
|
|
|
export type TInftwtAnlsRslt = {
|
|
inftwtAnlsSn: number;
|
|
pplMngNo: string;
|
|
anlsTm: string;
|
|
predcOilq: number;
|
|
};
|
|
|
|
export type TRdiiAnls = {
|
|
rainInfwtAnlsSn: number;
|
|
aplcnYm: string;
|
|
ctpvCd: string;
|
|
sggCd: string;
|
|
swtrmtFcltNm: string;
|
|
atflCd: string;
|
|
atflId: string;
|
|
atflInpNm: string;
|
|
atflHrPttnNm: string;
|
|
atflMpngNm: string;
|
|
atflRainNm: string;
|
|
atflNm: string;
|
|
drtwSwtcRt: number;
|
|
anlsInfo: string;
|
|
scsYn: string;
|
|
srcNm: string;
|
|
};
|
|
|
|
export type TRdiiAnlsRslt = {
|
|
rainInfwtAnlsSn: number;
|
|
pplMngNo: string;
|
|
anlsTm: string;
|
|
predcOilq: number;
|
|
minq: number;
|
|
};
|
|
|
|
export type TOilqData = {
|
|
inftwtAnlsSn: number;
|
|
pplMngNo: string;
|
|
mtrngDt: string;
|
|
oilq: number;
|
|
};
|
|
|
|
export type TRainData = {
|
|
rainInfwtAnlsSn: number;
|
|
mtrngDt: string;
|
|
amrf: number;
|
|
};
|
|
|
|
function roundTo(n: number, digits: number): number {
|
|
const factor = Math.pow(10, digits);
|
|
return Math.round(n * factor) / factor;
|
|
}
|
|
|
|
const getOptionsAplcnYm = (yyyymms: string[]): { text: string; val: string; hidden?: boolean }[] => {
|
|
const mapped: { text: string; val: string; hidden?: boolean }[] = yyyymms.map((dateStr) => ({
|
|
text: `${dateStr.slice(0, 4)}-${dateStr.slice(4, 6)}`,
|
|
val: dateStr,
|
|
}));
|
|
mapped.unshift({ val: "0", text: "선택", hidden: true });
|
|
return mapped;
|
|
};
|
|
|
|
const selectAplcnYms = async (gubun: string) => {
|
|
if (!gubun) return [];
|
|
|
|
try {
|
|
const result = await getData(`v1/${gubun}/aplcnYm`);
|
|
if (Array.isArray(result) && result.length > 0) {
|
|
return getOptionsAplcnYm(result);
|
|
}
|
|
} catch {
|
|
return [];
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const useAplcnYms = (gubun: string) => {
|
|
const [data, setData] = useState<{ text: string; val: string; hidden?: boolean }[]>([]);
|
|
|
|
useEffect(() => {
|
|
if (!gubun) {
|
|
setData([]);
|
|
return;
|
|
}
|
|
|
|
const fetch = async () => {
|
|
const result = await selectAplcnYms(gubun);
|
|
setData(result ?? []);
|
|
};
|
|
|
|
void fetch();
|
|
}, [gubun]);
|
|
|
|
return data;
|
|
};
|
|
|
|
export const getOptionsInftwtAnlsInfo = (anlsArr: TInftwtAnls[]): { text: string; val: number; hidden?: boolean }[] => {
|
|
const options: { text: string; val: number; hidden?: boolean }[] = anlsArr.map((anls) => ({
|
|
text: anls.anlsInfo,
|
|
val: anls.inftwtAnlsSn,
|
|
}));
|
|
options.unshift({ val: 0, text: "선택", hidden: true });
|
|
return options;
|
|
};
|
|
|
|
export const selectInftwtAnls = async (aplcnYm: string) => {
|
|
try {
|
|
const queryString = aplcnYm ? `aplcnYmFrom=${aplcnYm}&aplcnYmTo=${aplcnYm}` : "";
|
|
const result = await getData(`v1/inftwtAnls${queryString ? `?${queryString}` : ""}`);
|
|
if (result && result.numberOfElements > 0) {
|
|
return result.content;
|
|
}
|
|
} catch {
|
|
return [];
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const getChartDataInftwtAnlsRslt = (arr: TInftwtAnlsRslt[]) => {
|
|
return arr.map((anlsRslt) => ({
|
|
label: moment(anlsRslt.anlsTm, "YYYYMMDDHHmmss").format("HH시"),
|
|
val: roundTo(anlsRslt.predcOilq, 2),
|
|
}));
|
|
};
|
|
|
|
export const selectInftwtAnlsRslt = async (inftwtAnlsSn: number, pplMngNo: string) => {
|
|
try {
|
|
if (!inftwtAnlsSn || !pplMngNo) return [];
|
|
const result = await getData(`v1/inftwtAnlsRslt/ppl/${inftwtAnlsSn}/${pplMngNo}`);
|
|
if (result && result.numberOfElements > 0) {
|
|
return result.content;
|
|
}
|
|
} catch {
|
|
return [];
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const getChartDataOilq = (arr: TOilqData[]) => {
|
|
return arr.map((anlsRslt) => ({
|
|
label: anlsRslt.mtrngDt,
|
|
val: roundTo(anlsRslt.oilq, 2),
|
|
}));
|
|
};
|
|
|
|
export const selectOilq = async (inftwtAnlsSn: number, pplMngNo: string) => {
|
|
try {
|
|
if (!inftwtAnlsSn || !pplMngNo) return [];
|
|
const result = await getData(`v1/inftwtAnlsRslt/oilqPpl/${inftwtAnlsSn}/${pplMngNo}`);
|
|
if (result && result.numberOfElements > 0) {
|
|
return result.content;
|
|
}
|
|
} catch {
|
|
return [];
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const selectPplInftwtQty = async (inftwtAnlsSn: number, pplMngNo: string) => {
|
|
try {
|
|
if (!inftwtAnlsSn || !pplMngNo) return 0;
|
|
const result = await getData(`v1/inftwtAnlsRslt/pplInftwtQty/${inftwtAnlsSn}/${pplMngNo}`);
|
|
if (Array.isArray(result) && result.length > 0) {
|
|
return result[0].avg_day_flow;
|
|
}
|
|
} catch {
|
|
return 0;
|
|
}
|
|
|
|
return 0;
|
|
};
|
|
|
|
const buildPplMngNoCandidates = (pplMngNo: string) => {
|
|
const normalized = String(pplMngNo ?? "").trim();
|
|
if (!normalized) return [];
|
|
const withoutPrefix = normalized.replace(/^L/, "");
|
|
const withPrefix = withoutPrefix ? `L${withoutPrefix}` : "";
|
|
return Array.from(new Set([normalized, withPrefix, withoutPrefix].filter(Boolean)));
|
|
};
|
|
|
|
export const selectPplRdiiQty = async (rainInfwtAnlsSn: number, pplMngNo: string) => {
|
|
if (!rainInfwtAnlsSn || !pplMngNo) return null;
|
|
|
|
for (const candidate of buildPplMngNoCandidates(pplMngNo)) {
|
|
try {
|
|
const result = await getData(`v1/rdiiAnlsRslt/pplRdiiQty/${rainInfwtAnlsSn}/${candidate}`);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
} catch {
|
|
// resultOnly mode fallback candidates should not raise console noise
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export const getOptionsRdiiAnlsInfo = (anlsArr: TRdiiAnls[]): { text: string; val: number; hidden?: boolean }[] => {
|
|
const options: { text: string; val: number; hidden?: boolean }[] = anlsArr.map((anls) => ({
|
|
text: anls.anlsInfo,
|
|
val: anls.rainInfwtAnlsSn,
|
|
}));
|
|
options.unshift({ val: 0, text: "선택", hidden: true });
|
|
return options;
|
|
};
|
|
|
|
export const selectRdiiAnls = async (aplcnYm: string) => {
|
|
try {
|
|
const queryString = aplcnYm ? `aplcnYmFrom=${aplcnYm}&aplcnYmTo=${aplcnYm}` : "";
|
|
const result = await getData(`v1/rdiiAnls${queryString ? `?${queryString}` : ""}`);
|
|
if (result && result.numberOfElements > 0) {
|
|
return result.content;
|
|
}
|
|
} catch {
|
|
return [];
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const getChartDataRdiiAnlsRslt = (arr: TRdiiAnlsRslt[]) => {
|
|
return arr.map((anlsRslt) => ({
|
|
label: moment(anlsRslt.anlsTm, "YYYYMMDDHHmmss").format("MM-DD HH시"),
|
|
lineVal0: roundTo(anlsRslt.predcOilq, 2),
|
|
barVal0: 0,
|
|
}));
|
|
};
|
|
|
|
export const getChartDataRdiiAnlsRsltMinq = (arr: TRdiiAnlsRslt[]) => {
|
|
return arr.map((anlsRslt) => ({
|
|
label: moment(anlsRslt.anlsTm, "YYYYMMDDHHmmss").format("MM-DD HH시"),
|
|
val: roundTo(anlsRslt.minq, 2),
|
|
}));
|
|
};
|
|
|
|
export const selectRdiiAnlsRslt = async (rainInfwtAnlsSn: number, pplMngNo: string) => {
|
|
if (!rainInfwtAnlsSn || !pplMngNo) return [];
|
|
|
|
for (const candidate of buildPplMngNoCandidates(pplMngNo)) {
|
|
try {
|
|
const result = await getData(`v1/rdiiAnlsRslt/ppl/${rainInfwtAnlsSn}/${candidate}`);
|
|
if (result && result.numberOfElements > 0) {
|
|
return result.content;
|
|
}
|
|
} catch {
|
|
// resultOnly mode fallback candidates should not raise console noise
|
|
}
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const selectRainData = async (rainInfwtAnlsSn: number) => {
|
|
try {
|
|
if (!rainInfwtAnlsSn) return [];
|
|
const result = await getData(`v1/rdiiAnlsRslt/rainData/${rainInfwtAnlsSn}`);
|
|
if (result && result.numberOfElements > 0) {
|
|
return result.content;
|
|
}
|
|
} catch {
|
|
return [];
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const getChartDataRainData = (arr: TRainData[]) => {
|
|
return arr.map((data) => ({
|
|
label: moment(data.mtrngDt, "YYYY-MM-DD HH:mm:ss").format("MM-DD HH시"),
|
|
lineVal0: 0,
|
|
barVal0: roundTo(data.amrf, 2),
|
|
}));
|
|
}; |