/* ============================================================================= * M01_MasterData_Machine_Api.ts * M01 기계 화면 서버 호출(PLAN 821-2) — 양식 목록 · 시간당 경비 · 쓰는 곳 · 저장 전 영향(848-5) · * 비슷한 기계 찾기 · 한 줄. * 서버 = `M01_MasterData_Machine.py` · 시스템 관리자만. * ========================================================================== */ import { API_BASE_URL } from "@config/config_frontend"; import type { MachinePick } from "../common_util/common_util_machine_sheet"; import type { FileChanges, Row } from "./M01_MasterData_Api_Fetch"; export interface MachineLists { fuels: MachinePick[]; jobs: MachinePick[]; power: number; hour: number; books: string[]; /** 구분 → 원문 */ groups: Record; } export interface MachineHourly { 노무비?: number; 재료비?: number; 경비?: number; 계?: number; reason?: string; } async function get(path: string, params: Record = {}): Promise { const query = new URLSearchParams(params); const res = await fetch(`${API_BASE_URL}/m01/machine${path}?${query}`, { credentials: "include", }); const body = (await res.json()) as T & { detail?: unknown }; if (!res.ok) throw new Error(String(body.detail ?? `HTTP ${res.status}`)); return body; } let lists: Promise | null = null; /** 한 번 받아 둠(페이지 다시 그리면 다시 받음) */ export const fetchMachineLists = (fresh = false): Promise => { if (!lists || fresh) lists = get("/lists").catch((e) => ((lists = null), Promise.reject(e))); return lists; }; export const fetchMachineHourly = (keys: string[]): Promise> => keys.length ? get("/hourly", { keys: keys.join(",") }) : Promise.resolve({}); /** 단가산출 층별 수(이름 없음 — 남의 층 이름 안 냄) */ export type LayerCounts = Record<"마스터" | "회사" | "개인" | "프로젝트", number>; /** 쓰는 곳(848-5 · 수식 문서 기준) — uses = 직접 쓰는 기초 글 */ export interface MachineUses { uses: string[]; 직접: number; 기본: number; 사슬: number; 고침: { 기초: number; 단가산출: number; 위층: number }; 단가산출: LayerCounts; } export const fetchMachineUses = (key: string): Promise => get("/uses", { key }); /** 저장 전 영향(848-5) — 기계마다 수 · 금액 바뀐 기초 수 · 크게 바뀐 다섯 */ export interface MachineImpact { 기계: { 키: string; 이름: string; 규격: string; 칸: string[]; 직접: number; 사슬: number; 고침: { 기초: number; 단가산출: number; 위층: number }; 단가산출: LayerCounts; }[]; 바뀜: number; 셈: number; 위: { 키: string; 이름: string; 단위: string; 전: number | null; 후: number | null }[]; } export async function fetchMachineImpact(files: FileChanges[]): Promise { const res = await fetch(`${API_BASE_URL}/m01/machine/impact`, { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ files }), }); const body = (await res.json()) as MachineImpact & { detail?: unknown }; if (!res.ok) throw new Error(String(body.detail ?? `HTTP ${res.status}`)); return body; } export const findMachines = async (q: string): Promise => (await get<{ rows: Row[] }>("/find", { q })).rows; /** 쓰는 계산식(822-2) — 방법 = 목록 · 꼴 · 그 기계 식 · 까닭 = 값이 모자란 칸 */ export interface MachineFormula { 키?: string; 이름?: string; 방법: "목록" | "꼴" | "그 기계 식"; 까닭: string; } export async function fetchMachineFormula(row: Row): Promise { const res = await fetch(`${API_BASE_URL}/m01/machine/formula`, { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify(row), }); const body = (await res.json()) as MachineFormula & { detail?: unknown }; if (!res.ok) throw new Error(String(body.detail ?? `HTTP ${res.status}`)); return body; } export const fetchMachineRow = (key: string): Promise<{ version: string; row: Row }> => get("/row", { key });