From bac0228d21b7195065e9cddcc327ae33659c7bcc Mon Sep 17 00:00:00 2001 From: dujinkim Date: Sun, 1 Jun 2025 13:52:21 +0000 Subject: (대표님) 20250601까지 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/items-tech/service.ts | 225 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) (limited to 'lib/items-tech/service.ts') diff --git a/lib/items-tech/service.ts b/lib/items-tech/service.ts index be985adb..158fce13 100644 --- a/lib/items-tech/service.ts +++ b/lib/items-tech/service.ts @@ -1076,3 +1076,228 @@ export async function getAllOffshoreItems(): Promise<(ItemOffshoreHull | ItemOff throw new Error("Failed to get items"); } } + + +// ----------------------------------------------------------- +// 기술영업을 위한 로직 +// ----------------------------------------------------------- + +// 조선 공종 타입 +export type WorkType = '기장' | '전장' | '선실' | '배관' | '철의' + +// 조선 아이템 with 공종 정보 +export interface ShipbuildingItem { + id: number + itemCode: string + itemName: string + description: string | null + workType: WorkType + itemList: string | null // 실제 아이템명 + shipTypes: string + createdAt: Date + updatedAt: Date +} + +// 공종별 아이템 조회 +export async function getShipbuildingItemsByWorkType(workType?: WorkType, shipType?: string) { + try { + const query = db + .select({ + id: itemShipbuilding.id, + itemCode: itemShipbuilding.itemCode, + itemName: items.itemName, + description: items.description, + workType: itemShipbuilding.workType, + itemList: itemShipbuilding.itemList, + shipTypes: itemShipbuilding.shipTypes, + createdAt: itemShipbuilding.createdAt, + updatedAt: itemShipbuilding.updatedAt, + }) + .from(itemShipbuilding) + .leftJoin(items, eq(itemShipbuilding.itemCode, items.itemCode)) + + const conditions = [] + if (workType) { + conditions.push(eq(itemShipbuilding.workType, workType)) + } + if (shipType) { + conditions.push(eq(itemShipbuilding.shipTypes, shipType)) + } + + if (conditions.length > 0) { + query.where(and(...conditions)) + } + + const result = await query + + return { + data: result as ShipbuildingItem[], + error: null + } + } catch (error) { + console.error("조선 아이템 조회 오류:", error) + return { + data: null, + error: error instanceof Error ? error.message : "알 수 없는 오류" + } + } +} + +// 아이템 검색 +export async function searchShipbuildingItems(searchQuery: string, workType?: WorkType, shipType?: string) { + try { + const searchConditions = [ + ilike(itemShipbuilding.itemCode, `%${searchQuery}%`), + ilike(items.itemName, `%${searchQuery}%`), + ilike(items.description, `%${searchQuery}%`), + ilike(itemShipbuilding.itemList, `%${searchQuery}%`) + ] + + let whereCondition = or(...searchConditions) + + const filterConditions = [] + if (workType) { + filterConditions.push(eq(itemShipbuilding.workType, workType)) + } + if (shipType) { + filterConditions.push(eq(itemShipbuilding.shipTypes, shipType)) + } + + if (filterConditions.length > 0) { + whereCondition = and( + and(...filterConditions), + or(...searchConditions) + ) + } + + const result = await db + .select({ + id: itemShipbuilding.id, + itemCode: itemShipbuilding.itemCode, + itemName: items.itemName, + description: items.description, + workType: itemShipbuilding.workType, + itemList: itemShipbuilding.itemList, + shipTypes: itemShipbuilding.shipTypes, + createdAt: itemShipbuilding.createdAt, + updatedAt: itemShipbuilding.updatedAt, + }) + .from(itemShipbuilding) + .leftJoin(items, eq(itemShipbuilding.itemCode, items.itemCode)) + .where(whereCondition) + + return { + data: result as ShipbuildingItem[], + error: null + } + } catch (error) { + console.error("조선 아이템 검색 오류:", error) + return { + data: null, + error: error instanceof Error ? error.message : "알 수 없는 오류" + } + } +} + +// 모든 공종 목록 조회 +export async function getWorkTypes() { + return [ + { code: '기장' as WorkType, name: '기장', description: '기계 장치' }, + { code: '전장' as WorkType, name: '전장', description: '전기 장치' }, + { code: '선실' as WorkType, name: '선실', description: '선실' }, + { code: '배관' as WorkType, name: '배관', description: '배관' }, + { code: '철의' as WorkType, name: '철의', description: '선체 강재' }, + ] +} + +// 특정 아이템 코드들로 아이템 조회 +export async function getShipbuildingItemsByCodes(itemCodes: string[]) { + try { + const result = await db + .select({ + id: itemShipbuilding.id, + itemCode: itemShipbuilding.itemCode, + itemName: items.itemName, + description: items.description, + workType: itemShipbuilding.workType, + itemList: itemShipbuilding.itemList, + shipTypes: itemShipbuilding.shipTypes, + createdAt: itemShipbuilding.createdAt, + updatedAt: itemShipbuilding.updatedAt, + }) + .from(itemShipbuilding) + .leftJoin(items, eq(itemShipbuilding.itemCode, items.itemCode)) + .where( + or(...itemCodes.map(code => eq(itemShipbuilding.itemCode, code))) + ) + + return { + data: result as ShipbuildingItem[], + error: null + } + } catch (error) { + console.error("조선 아이템 코드별 조회 오류:", error) + return { + data: null, + error: error instanceof Error ? error.message : "알 수 없는 오류" + } + } +} + +// 전체 조선 아이템 조회 (캐싱용) +export async function getAllShipbuildingItemsForCache() { + try { + const result = await db + .select({ + id: itemShipbuilding.id, + itemCode: itemShipbuilding.itemCode, + itemName: items.itemName, + description: items.description, + workType: itemShipbuilding.workType, + itemList: itemShipbuilding.itemList, + shipTypes: itemShipbuilding.shipTypes, + createdAt: itemShipbuilding.createdAt, + updatedAt: itemShipbuilding.updatedAt, + }) + .from(itemShipbuilding) + .leftJoin(items, eq(itemShipbuilding.itemCode, items.itemCode)) + + return { + data: result as ShipbuildingItem[], + error: null + } + } catch (error) { + console.error("전체 조선 아이템 조회 오류:", error) + return { + data: null, + error: error instanceof Error ? error.message : "알 수 없는 오류" + } + } +} + +// 선종 목록 가져오기 +export async function getShipTypes() { + try { + const result = await db + .selectDistinct({ + shipTypes: itemShipbuilding.shipTypes + }) + .from(itemShipbuilding) + .orderBy(itemShipbuilding.shipTypes) + + return { + data: result.map(item => item.shipTypes), + error: null + } + } catch (error) { + console.error("선종 목록 조회 오류:", error) + return { + data: null, + error: error instanceof Error ? error.message : "알 수 없는 오류" + } + } +} + +// ----------------------------------------------------------- +// 기술영업을 위한 로직 끝 +// ----------------------------------------------------------- \ No newline at end of file -- cgit v1.2.3