/** * ECC Mapper 공통 유틸리티 함수들 * bidding-and-pr-mapper.ts와 rfq-and-pr-mapper.ts에서 공통으로 사용하는 함수들 * * 1. 담당자 정보 조회 함수: 구매그룹코드로 id, email, phone 반환 * 2. 프로젝트 정보 조회 함수: 프로젝트 코드로 id, name 반환 * 3. 자재명 조회 함수: 자재코드로 자재명 반환 * 4. SAP 날짜/시간 파싱 함수: SAP 날짜/시간 문자열을 JavaScript Date로 변환 * 5. SAP 날짜를 YYYY-MM-DD 문자열로 변환 함수: SAP 날짜 문자열을 YYYY-MM-DD 문자열로 변환 * */ import { debugLog, debugSuccess, debugError } from '@/lib/debug-utils'; import db from '@/db/db'; import { users } from '@/db/schema'; import { projects } from '@/db/schema/projects'; import { EQUP_MASTER_MATL_CHARASGN } from '@/db/schema/MDG/mdg'; import { eq } from 'drizzle-orm'; /** * 담당자 정보 조회 함수 (EKGRP 기반) * EKGRP(구매그룹코드)를 users 테이블의 userCode와 매칭하여 사용자 정보 반환 */ export async function findUserInfoByEKGRP(EKGRP: string | null): Promise<{ userId: number; userName: string; userPhone: string | null; } | null> { try { debugLog('담당자 찾기 시작', { EKGRP }); if (!EKGRP) { debugError('EKGRP가 null 또는 undefined', { EKGRP }); return null; } // users 테이블에서 userCode로 직접 조회 const userResult = await db .select({ id: users.id, name: users.name, phone: users.phone }) .from(users) .where(eq(users.userCode, EKGRP)) .limit(1); if (userResult.length === 0) { debugError('EKGRP에 해당하는 사용자를 찾을 수 없음', { EKGRP }); return null; } const userInfo = { userId: userResult[0].id, userName: userResult[0].name, userPhone: userResult[0].phone }; debugSuccess('담당자 찾음', { EKGRP, userInfo }); return userInfo; } catch (error) { debugError('담당자 찾기 중 오류 발생', { EKGRP, error }); return null; } } /** * 프로젝트 정보 조회 함수 (PSPID 기반) * PSPID와 projects.code 매칭하여 프로젝트 ID와 이름 반환 */ export async function findProjectInfoByPSPID(PSPID: string | null): Promise<{ id: number; name: string } | null> { try { debugLog('프로젝트 정보 찾기 시작', { PSPID }); if (!PSPID) { debugError('PSPID가 null 또는 undefined', { PSPID }); return null; } const projectResult = await db .select({ id: projects.id, name: projects.name }) .from(projects) .where(eq(projects.code, PSPID)) .limit(1); if (projectResult.length === 0) { debugError('PSPID에 해당하는 프로젝트를 찾을 수 없음', { PSPID }); return null; } const projectInfo = { id: projectResult[0].id, name: projectResult[0].name }; debugSuccess('프로젝트 정보 찾음', { PSPID, projectInfo }); return projectInfo; } catch (error) { debugError('프로젝트 정보 찾기 중 오류 발생', { PSPID, error }); return null; } } /** * 자재명 조회 함수 (MATNR 기반) * MATNR을 기반으로 EQUP_MASTER_MATL_CHARASGN 테이블에서 ATWTB 조회 * * 주의: MATERIAL_MASTER_PART_MATL.ZZNAME이 아닌 EQUP_MASTER_MATL_CHARASGN.ATWTB를 사용해야 함 */ export async function findMaterialNameByMATNR(MATNR: string | null): Promise { try { debugLog('자재명 조회 시작', { MATNR }); if (!MATNR) { debugError('MATNR이 null 또는 undefined', { MATNR }); return null; } const materialResult = await db .select({ ATWTB: EQUP_MASTER_MATL_CHARASGN.ATWTB }) .from(EQUP_MASTER_MATL_CHARASGN) .where(eq(EQUP_MASTER_MATL_CHARASGN.MATNR, MATNR)) .limit(1); if (materialResult.length === 0) { debugError('MATNR에 해당하는 자재를 찾을 수 없음', { MATNR }); return null; } const materialName = materialResult[0].ATWTB; debugSuccess('자재명 조회 완료', { MATNR, materialName }); return materialName; } catch (error) { debugError('자재명 조회 중 오류 발생', { MATNR, error }); return null; } } /** * SAP 날짜/시간 파싱 함수 * SAP 형식 (YYYYMMDD + HHMMSS)을 JavaScript Date로 변환 */ export function parseSAPDateTime( dateStr: string | null, timeStr: string | null ): Date { let parsedDate = new Date(); if (dateStr && timeStr) { try { // SAP 날짜 형식 (YYYYMMDD) 파싱 if (dateStr.length === 8) { const year = parseInt(dateStr.substring(0, 4)); const month = parseInt(dateStr.substring(4, 6)) - 1; // 0-based const day = parseInt(dateStr.substring(6, 8)); const hour = parseInt(timeStr.substring(0, 2)); const minute = parseInt(timeStr.substring(2, 4)); const second = parseInt(timeStr.substring(4, 6)); parsedDate = new Date(year, month, day, hour, minute, second); } } catch (error) { debugError('SAP 날짜 파싱 오류', { date: dateStr, time: timeStr, error, }); } } return parsedDate; } /** * SAP 날짜를 YYYY-MM-DD 문자열로 변환 * YYYYMMDD 형식을 YYYY-MM-DD로 변환 */ export function parseSAPDateToString(dateStr: string | null): string | null { if (!dateStr) return null; try { if (dateStr.length === 8) { const year = dateStr.substring(0, 4); const month = dateStr.substring(4, 6); const day = dateStr.substring(6, 8); return `${year}-${month}-${day}`; } } catch (error) { debugError('SAP 날짜 문자열 파싱 오류', { date: dateStr, error }); } return null; } /** * 공통 타입 정의 */ export interface UserInfo { userId: number; userName: string; userPhone: string | null; } export interface ProjectInfo { id: number; name: string; }