summaryrefslogtreecommitdiff
path: root/lib/items-ship/service.ts
blob: 37b623c1d52b9e06cdb6670a85afb4c996486951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// src/lib/items-ship/service.ts
"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택)

import { revalidateTag, unstable_noStore } from "next/cache";
import db from "@/db/db";

import { filterColumns } from "@/lib/filter-columns";
import { unstable_cache } from "@/lib/unstable-cache";
import { getErrorMessage } from "@/lib/handle-error";

import { asc, desc, ilike, and, or, eq, count, inArray, sql } from "drizzle-orm";
import { GetItemsSchema, UpdateItemSchema, ShipbuildingItemCreateData, TypedItemCreateData } from "./validations";
import { Item, items, itemShipbuilding } from "@/db/schema/items";
import { deleteItemById, deleteItemsByIds, findAllItems, insertItem, updateItem } from "./repository";

/* -----------------------------------------------------
   1) 조회 관련 
----------------------------------------------------- */

/**
 * 복잡한 조건으로 Item 목록을 조회 (+ pagination) 하고,
 * 총 개수에 따라 pageCount를 계산해서 리턴.
 * Next.js의 unstable_cache를 사용해 일정 시간 캐시.
 */
export async function getShipbuildingItems(input: GetItemsSchema) {
  return unstable_cache(
    async () => {
      try {
        const offset = (input.page - 1) * input.perPage;

        // advancedTable 모드면 filterColumns()로 where 절 구성
        const advancedWhere = filterColumns({
          table: items,
          filters: input.filters,
          joinOperator: input.joinOperator,
        });

        let globalWhere;
        if (input.search) {
          const s = `%${input.search}%`;
          globalWhere = or(
            ilike(items.itemCode, s), 
            ilike(items.itemName, s),
            ilike(items.description, s)
          );
        }

        const finalWhere = and(
          advancedWhere,
          globalWhere
        );

        const where = finalWhere;

        const orderBy =
          input.sort.length > 0
            ? input.sort.map((item) =>
                item.desc ? desc(items[item.id]) : asc(items[item.id])
              )
            : [asc(items.createdAt)];

        // 조선 아이템 테이블과 기본 아이템 테이블 조인하여 조회
        const result = await db.select({
          id: itemShipbuilding.id,
          itemId: itemShipbuilding.itemId,
          workType: itemShipbuilding.workType,
          shipTypes: itemShipbuilding.shipTypes,
          itemCode: items.itemCode,
          itemName: items.itemName,
          description: items.description,
          createdAt: items.createdAt,
          updatedAt: items.updatedAt,
        })
        .from(itemShipbuilding)
        .innerJoin(items, eq(itemShipbuilding.itemId, items.id))
        .where(where)
        .orderBy(...orderBy)
        .offset(offset)
        .limit(input.perPage);
        
        // 전체 데이터 개수 조회
        const [{ count: total }] = await db.select({
          count: count()
        })
        .from(itemShipbuilding)
        .innerJoin(items, eq(itemShipbuilding.itemId, items.id))
        .where(where);
        
        const pageCount = Math.ceil(Number(total) / input.perPage);

        return { data: result, pageCount };
      } catch (err) {
        console.error("Error fetching shipbuilding items:", err);
        return { data: [], pageCount: 0 };
      }
    },
    [JSON.stringify(input)],
    {
      revalidate: 3600,
      tags: ["items"],
    }
  )();
}

/* -----------------------------------------------------
   2) 생성(Create) 
----------------------------------------------------- */

/**
 * Item 생성 - 아이템 타입에 따라 해당 테이블에 데이터 삽입
 */
export async function createShipbuildingItem(input: TypedItemCreateData) {
  unstable_noStore()
  
  try {
    if (!input.itemCode || !input.itemName) {
      return { 
        success: false, 
        message: "아이템 코드와 아이템 명은 필수입니다",
        data: null, 
        error: "필수 필드 누락" 
      }
    }

    let result: unknown[] = []
    
    result = await db.transaction(async (tx) => {
      const existingItem = await tx.query.items.findFirst({
        where: eq(items.itemCode, input.itemCode),
      })

      let itemId: number
      let itemResult

      if (existingItem) {
        itemResult = await updateItem(tx, existingItem.id, {
          itemName: input.itemName,
          description: input.description,
        })
        itemId = existingItem.id
      } else {
        itemResult = await insertItem(tx, {
          itemCode: input.itemCode,
          itemName: input.itemName,
          description: input.description,
        })
        itemId = itemResult[0].id
      }
      
      const shipData = input as ShipbuildingItemCreateData;
      const typeResult = await tx.insert(itemShipbuilding).values({
        itemId: itemId,
        workType: shipData.workType ? (shipData.workType as '기장' | '전장' | '선실' | '배관' | '철의') : '기장',
        shipTypes: shipData.shipTypes || ''
      }).returning();
      
      return [...itemResult, ...typeResult]
    })

    revalidateTag("items")

    return {
      success: true,
      data: result[0] || null,
      error: null
    }
  } catch (err) {
    console.error("아이템 생성/업데이트 오류:", err)
    
    if (err instanceof Error && err.message.includes("unique constraint")) {
      return {
        success: false,
        message: "이미 존재하는 아이템 코드입니다",
        data: null,
        error: "중복 키 오류"
      }
    }

    return {
      success: false,
      message: getErrorMessage(err),
      data: null,
      error: getErrorMessage(err)
    }
  }
}

/**
 * Excel import를 위한 조선 아이템 생성 함수
 * 하나의 아이템 코드에 대해 여러 선종을 처리 (1:N 관계)
 */
export async function createShipbuildingImportItem(input: {
  itemCode: string;
  itemName: string;
  workType: '기장' | '전장' | '선실' | '배관' | '철의';
  description?: string | null;
  shipTypes: Record<string, boolean>;
}) {
  unstable_noStore();

  try {

    if (!input.itemCode || !input.itemName) {
      return {
        success: false,
        message: "아이템 코드와 아이템 명은 필수입니다",
        data: null,
        error: "필수 필드 누락"
      }
    }
    let results: any[] = []
    results = await db.transaction(async (tx) => {
      // 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;

      if (existingItem) {
        // 이미 있으면 업데이트
        await updateItem(tx, existingItem.id, {
          itemName: input.itemName,
          description: input.description,
        });
        itemId = existingItem.id;
        console.log('기존 아이템 업데이트, id:', itemId);
      } else {
        // 없으면 새로 생성
        // 현재 가장 큰 ID 값 가져오기
        const maxIdResult = await tx.select({ maxId: sql`MAX(id)` }).from(items);
        const maxId = maxIdResult[0]?.maxId || 0;
        const newId = Number(maxId) + 1;
        console.log('새 아이템 생성을 위한 ID 계산:', { maxId, newId });

        // 새 ID로 아이템 생성
        const insertResult = await tx.insert(items).values({
          id: newId,
          itemCode: input.itemCode,
          itemName: input.itemName,
          description: input.description,
        }).returning();
        
        itemId = insertResult[0].id;
        console.log('새 아이템 생성 완료, id:', itemId);
      }

      const createdItems = [];
      for (const shipType of Object.keys(input.shipTypes)) {
        // 그대로 선종명 string으로 저장
        const existShip = await tx.select().from(itemShipbuilding)
          .where(
            and(
              eq(itemShipbuilding.itemId, itemId),
              eq(itemShipbuilding.shipTypes, shipType)
            )
          );
        if (!existShip[0]) {
          const shipbuildingResult = await tx.insert(itemShipbuilding).values({
            itemId: itemId,
            workType: input.workType,
            shipTypes: shipType
          }).returning();
          createdItems.push({
            ...shipbuildingResult[0]
          });
          console.log('조선아이템 생성:', shipType, shipbuildingResult[0]);
        } else {
          console.log('이미 존재하는 조선아이템:', shipType);
        }
      }
      return createdItems;
    });

    revalidateTag("items");

    return {
      success: true,
      data: results,
      error: null
    }
  } catch (err) {
    // DB에 실제로 존재하는 itemCode 목록도 함께 출력
    const allCodes = await db.select({ code: items.itemCode }).from(items);
    console.error("아이템 import 오류:", err);
    console.error("DB에 존재하는 모든 itemCode:", allCodes.map(x => x.code));
    return {
      success: false,
      message: getErrorMessage(err),
      data: null,
      error: getErrorMessage(err)
    }
  }
}

/* -----------------------------------------------------
   3) 업데이트
----------------------------------------------------- */

// 업데이트 타입 정의 인터페이스
interface UpdateShipbuildingItemInput extends UpdateItemSchema {
  id: number;
  workType?: string;
  shipTypes?: string;
  itemCode?: string;
  itemName?: string;
  description?: string;
}

/** 단건 업데이트 */
export async function modifyShipbuildingItem(input: UpdateShipbuildingItemInput) {
  unstable_noStore();
  try {
    await db.transaction(async (tx) => {
      // 기본 아이템 테이블 업데이트
      const [item] = await updateItem(tx, input.id, {
        itemCode: input.itemCode,
        itemName: input.itemName,
        description: input.description,
      });
      
      // 조선 아이템 테이블 업데이트
      if (input.workType || input.shipTypes) {
        await tx.update(itemShipbuilding)
          .set({ 
            workType: input.workType as '기장' | '전장' | '선실' | '배관' | '철의',
            shipTypes: input.shipTypes
          })
          .where(eq(itemShipbuilding.itemId, item.id));
      }
      
      return item;
    });

    revalidateTag("items");
    return { data: null, error: null };
  } catch (err) {
    return { data: null, error: getErrorMessage(err) };
  }
}

/* -----------------------------------------------------
   4) 삭제
----------------------------------------------------- */

// 삭제 타입 정의 인터페이스
interface DeleteItemInput {
  id: number;
}

interface DeleteItemsInput {
  ids: number[];
}

/** 단건 삭제 */
export async function removeShipbuildingItem(input: DeleteItemInput) {
  unstable_noStore();
  try {
    await db.transaction(async (tx) => {
      const item = await tx.query.items.findFirst({
        where: eq(items.id, input.id),
      });
      
      if (!item) {
        throw new Error("아이템을 찾을 수 없습니다.");
      }
      
      // 조선 아이템 테이블에서 먼저 삭제
      await tx.delete(itemShipbuilding)
        .where(eq(itemShipbuilding.itemId, input.id));
      
      // 기본 아이템 테이블에서 삭제
      await deleteItemById(tx, input.id);
    });

    revalidateTag("items");

    return { data: null, error: null };
  } catch (err) {
    return { data: null, error: getErrorMessage(err) };
  }
}

/** 복수 삭제 */
export async function removeShipbuildingItems(input: DeleteItemsInput) {
  unstable_noStore();
  try {
    await db.transaction(async (tx) => {
      if (input.ids.length > 0) {
        // 조선 아이템 테이블에서 먼저 삭제
        await tx.delete(itemShipbuilding)
          .where(inArray(itemShipbuilding.itemId, input.ids));
        
        // 기본 아이템 테이블에서 삭제
        await deleteItemsByIds(tx, input.ids);
      }
    });

    revalidateTag("items");

    return { data: null, error: null, success: true, message: "아이템이 성공적으로 삭제되었습니다." };
  } catch (err) {
    return { data: null, error: getErrorMessage(err), success: false, message: "아이템 삭제 중 오류가 발생했습니다." };
  }
}

export async function getAllShipbuildingItems(): Promise<Item[]> {
  try {
    return await findAllItems(); 
  } catch (error) {
    console.error("Failed to get items:", error);
    throw new Error("Failed to get items");
  }
}