From 33dccde38b365206cb14e1a0cb134e07dd8fe939 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Thu, 22 May 2025 09:52:21 +0000 Subject: (최겸) 기술영업 아이템 수정(itemCode reference) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/items-tech/service.ts | 60 +++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 39 deletions(-) (limited to 'lib/items-tech') diff --git a/lib/items-tech/service.ts b/lib/items-tech/service.ts index 02a5a901..62a66aaa 100644 --- a/lib/items-tech/service.ts +++ b/lib/items-tech/service.ts @@ -62,18 +62,17 @@ export async function getShipbuildingItems(input: GetItemsSchema) { // 조선 아이템 테이블과 기본 아이템 테이블 조인하여 조회 const result = await db.select({ id: itemShipbuilding.id, - itemId: itemShipbuilding.itemId, + itemCode: items.itemCode, workType: itemShipbuilding.workType, shipTypes: itemShipbuilding.shipTypes, itemList: itemShipbuilding.itemList, - itemCode: items.itemCode, itemName: items.itemName, description: items.description, createdAt: itemShipbuilding.createdAt, updatedAt: itemShipbuilding.updatedAt, }) .from(itemShipbuilding) - .innerJoin(items, eq(itemShipbuilding.itemId, items.id)) + .innerJoin(items, eq(itemShipbuilding.itemCode, items.itemCode)) .where(where) .orderBy(...orderBy) .offset(offset) @@ -84,7 +83,7 @@ export async function getShipbuildingItems(input: GetItemsSchema) { count: count() }) .from(itemShipbuilding) - .innerJoin(items, eq(itemShipbuilding.itemId, items.id)) + .innerJoin(items, eq(itemShipbuilding.itemCode, items.itemCode)) .where(where); const pageCount = Math.ceil(Number(total) / input.perPage); @@ -143,18 +142,17 @@ export async function getOffshoreTopItems(input: GetItemsSchema) { // 해양 TOP 아이템 테이블과 기본 아이템 테이블 조인하여 조회 const result = await db.select({ id: itemOffshoreTop.id, - itemId: itemOffshoreTop.itemId, + itemCode: items.itemCode, workType: itemOffshoreTop.workType, itemList: itemOffshoreTop.itemList, subItemList: itemOffshoreTop.subItemList, - itemCode: items.itemCode, itemName: items.itemName, description: items.description, createdAt: itemOffshoreTop.createdAt, updatedAt: itemOffshoreTop.updatedAt, }) .from(itemOffshoreTop) - .innerJoin(items, eq(itemOffshoreTop.itemId, items.id)) + .innerJoin(items, eq(itemOffshoreTop.itemCode, items.itemCode)) .where(where) .orderBy(...orderBy) .offset(offset) @@ -165,7 +163,7 @@ export async function getOffshoreTopItems(input: GetItemsSchema) { count: count() }) .from(itemOffshoreTop) - .innerJoin(items, eq(itemOffshoreTop.itemId, items.id)) + .innerJoin(items, eq(itemOffshoreTop.itemCode, items.itemCode)) .where(where); const pageCount = Math.ceil(Number(total) / input.perPage); @@ -224,18 +222,17 @@ export async function getOffshoreHullItems(input: GetItemsSchema) { // 해양 HULL 아이템 테이블과 기본 아이템 테이블 조인하여 조회 const result = await db.select({ id: itemOffshoreHull.id, - itemId: itemOffshoreHull.itemId, + itemCode: items.itemCode, workType: itemOffshoreHull.workType, itemList: itemOffshoreHull.itemList, subItemList: itemOffshoreHull.subItemList, - itemCode: items.itemCode, itemName: items.itemName, description: items.description, createdAt: itemOffshoreHull.createdAt, updatedAt: itemOffshoreHull.updatedAt, }) .from(itemOffshoreHull) - .innerJoin(items, eq(itemOffshoreHull.itemId, items.id)) + .innerJoin(items, eq(itemOffshoreHull.itemCode, items.itemCode)) .where(where) .orderBy(...orderBy) .offset(offset) @@ -246,7 +243,7 @@ export async function getOffshoreHullItems(input: GetItemsSchema) { count: count() }) .from(itemOffshoreHull) - .innerJoin(items, eq(itemOffshoreHull.itemId, items.id)) + .innerJoin(items, eq(itemOffshoreHull.itemCode, items.itemCode)) .where(where); const pageCount = Math.ceil(Number(total) / input.perPage); @@ -291,17 +288,15 @@ export async function createShipbuildingItem(input: TypedItemCreateData) { } const result = await db.transaction(async (tx) => { - // 1. itemCode 정규화해서 직접 쿼리 + // 1. itemCode로 직접 쿼리 const existRows = await tx.select().from(items) .where(eq(items.itemCode, input.itemCode)); const existingItem = existRows[0]; - let itemId: number; let itemResult: any; if (existingItem) { // 기존 아이템이 있으면 업데이트하지 않고 그대로 사용 - itemId = existingItem.id; itemResult = [existingItem]; // 배열 형태로 반환 } else { // 없으면 새로 생성 @@ -317,13 +312,11 @@ export async function createShipbuildingItem(input: TypedItemCreateData) { itemName: input.itemName, description: input.description, }).returning(); - - itemId = itemResult[0].id; } const shipData = input as ShipbuildingItemCreateData; const typeResult = await tx.insert(itemShipbuilding).values({ - itemId: itemId, + itemCode: input.itemCode, workType: shipData.workType ? (shipData.workType as '기장' | '전장' | '선실' | '배관' | '철의') : '기장', shipTypes: shipData.shipTypes || '', itemList: shipData.itemList || null, @@ -393,19 +386,17 @@ export async function createShipbuildingImportItem(input: { } const results = await db.transaction(async (tx) => { - // 1. itemCode 정규화해서 직접 쿼리 + // 1. itemCode로 직접 쿼리 const existRows = await tx.select().from(items) .where(eq(items.itemCode, input.itemCode)); const existingItem = existRows[0]; console.log('DB에서 직접 조회한 기존 아이템:', existingItem); - let itemId: number; + let itemResult: any; if (existingItem) { - // 기존 아이템이 있으면 업데이트하지 않고 그대로 사용 - itemId = existingItem.id; - console.log('기존 아이템 사용, id:', itemId); + console.log('기존 아이템 사용, itemCode:', input.itemCode); } else { // 없으면 새로 생성 // 현재 가장 큰 ID 값 가져오기 @@ -422,8 +413,7 @@ export async function createShipbuildingImportItem(input: { description: input.description, }).returning(); - itemId = insertResult[0].id; - console.log('새 아이템 생성 완료, id:', itemId); + console.log('새 아이템 생성 완료, itemCode:', input.itemCode); } const createdItems = []; @@ -432,13 +422,13 @@ export async function createShipbuildingImportItem(input: { const existShip = await tx.select().from(itemShipbuilding) .where( and( - eq(itemShipbuilding.itemId, itemId), + eq(itemShipbuilding.itemCode, input.itemCode), eq(itemShipbuilding.shipTypes, shipType) ) ); if (!existShip[0]) { const shipbuildingResult = await tx.insert(itemShipbuilding).values({ - itemId: itemId, + itemCode: input.itemCode, workType: input.workType, shipTypes: shipType, itemList: input.itemList || null, @@ -494,17 +484,15 @@ export async function createOffshoreTopItem(data: OffshoreTopItemCreateData) { // 트랜잭션 내에서 처리 const result = await db.transaction(async (tx) => { - // 1. itemCode 정규화해서 직접 쿼리 + // 1. itemCode로 직접 쿼리 const existRows = await tx.select().from(items) .where(eq(items.itemCode, data.itemCode)); const existingItem = existRows[0]; - let itemId: number; let itemResult: any; if (existingItem) { // 기존 아이템이 있으면 업데이트하지 않고 그대로 사용 - itemId = existingItem.id; itemResult = [existingItem]; // 배열 형태로 반환 } else { // 없으면 새로 생성 @@ -520,14 +508,12 @@ export async function createOffshoreTopItem(data: OffshoreTopItemCreateData) { itemName: data.itemName, description: data.description, }).returning(); - - itemId = itemResult[0].id; } const [offshoreTop] = await tx .insert(itemOffshoreTop) .values({ - itemId: itemId, + itemCode: data.itemCode, workType: data.workType, itemList: data.itemList, subItemList: data.subItemList, @@ -587,17 +573,15 @@ export async function createOffshoreHullItem(data: OffshoreHullItemCreateData) { // 트랜잭션 내에서 처리 const result = await db.transaction(async (tx) => { - // 1. itemCode 정규화해서 직접 쿼리 + // 1. itemCode로 직접 쿼리 const existRows = await tx.select().from(items) .where(eq(items.itemCode, data.itemCode)); const existingItem = existRows[0]; - let itemId: number; let itemResult: any; if (existingItem) { // 기존 아이템이 있으면 업데이트하지 않고 그대로 사용 - itemId = existingItem.id; itemResult = [existingItem]; // 배열 형태로 반환 } else { // 없으면 새로 생성 @@ -613,14 +597,12 @@ export async function createOffshoreHullItem(data: OffshoreHullItemCreateData) { itemName: data.itemName, description: data.description, }).returning(); - - itemId = itemResult[0].id; } const [offshoreHull] = await tx .insert(itemOffshoreHull) .values({ - itemId: itemId, + itemCode: data.itemCode, workType: data.workType, itemList: data.itemList, subItemList: data.subItemList, -- cgit v1.2.3