summaryrefslogtreecommitdiff
path: root/lib/procurement-items/service.ts
blob: c91959a9cb89e9a7ff2ae6b5a840b42fb16ff35f (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
// lib/procurement/items/service.ts
"use server"

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 } from "drizzle-orm"
import { GetProcurementItemsSchema, UpdateProcurementItemSchema, createProcurementItemSchema } from "./validations"
import { ProcurementItem, procurementItems } from "@/db/schema/items"
import {
  countProcurementItems,
  deleteProcurementItemById,
  deleteProcurementItemsByIds,
  findAllProcurementItems,
  insertProcurementItem,
  selectProcurementItems,
  updateProcurementItem
} from "./repository"

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

/**
 * 복잡한 조건으로 품목 목록을 조회 (+ pagination) 하고,
 * 총 개수에 따라 pageCount를 계산해서 리턴.
 * Next.js의 unstable_cache를 사용해 일정 시간 캐시.
 */
export async function getProcurementItems(input: GetProcurementItemsSchema) {
  const safePerPage = Math.min(input.perPage, 1000)

  return unstable_cache(
    async () => {
      try {
        const offset = (input.page - 1) * safePerPage

        const advancedWhere = filterColumns({
          table: procurementItems,
          filters: input.filters,
          joinOperator: input.joinOperator,
        })

        let globalWhere
        if (input.search) {
          const s = `%${input.search}%`
          globalWhere = or(
            ilike(procurementItems.itemCode, s),
            ilike(procurementItems.itemName, s),
            ilike(procurementItems.material, s),
            ilike(procurementItems.specification, s),
            ilike(procurementItems.unit, s),
          )
        }

        const finalWhere = and(advancedWhere, globalWhere)

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

        const { data, total } = await db.transaction(async (tx) => {
          const data = await selectProcurementItems(tx, {
            where: finalWhere,
            orderBy,
            offset,
            limit: safePerPage,
          })

          const total = await countProcurementItems(tx, finalWhere)
          return { data, total }
        })

        const pageCount = Math.ceil(total / safePerPage)
        return { data, pageCount }
      } catch (err) {
        console.error(err)
        return { data: [], pageCount: 0 }
      }
    },
    [JSON.stringify({...input, perPage: safePerPage})],
    {
      revalidate: 3600,
      tags: ["procurement-items"],
    }
  )()
}

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

export interface ProcurementItemCreateData {
  itemCode: string
  itemName: string
  material?: string | null
  specification?: string | null
  unit?: string | null
  isActive?: string | null
  createdBy?: string | null
}

/**
 * 품목 생성
 */
export async function createProcurementItem(input: ProcurementItemCreateData) {
  unstable_noStore()

  try {
    if (!input.itemCode || !input.itemName) {
      return {
        success: false,
        message: "품목코드와 품목명은 필수입니다",
        data: null,
        error: "필수 필드 누락"
      }
    }

    let result: any[] = []

    result = await db.transaction(async (tx) => {
      // 기존 품목 확인 (itemCode는 unique)
      const existingItem = await tx.query.procurementItems.findFirst({
        where: eq(procurementItems.itemCode, input.itemCode),
      })

      let txResult
      if (existingItem) {
        // 기존 품목 업데이트
        txResult = await updateProcurementItem(tx, existingItem.id, {
          itemName: input.itemName,
          material: input.material,
          specification: input.specification,
          unit: input.unit,
          isActive: input.isActive,
          createdBy: input.createdBy,
        })
      } else {
        // 새 품목 생성
        txResult = await insertProcurementItem(tx, {
          itemCode: input.itemCode,
          itemName: input.itemName,
          material: input.material,
          specification: input.specification,
          unit: input.unit,
          isActive: input.isActive || 'Y',
          createdBy: input.createdBy,
        })
      }

      return txResult
    })

    // 캐시 무효화
    revalidateTag("procurement-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)
    }
  }
}

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

/** 단건 업데이트 */
export async function modifyProcurementItem(input: UpdateProcurementItemSchema & { id: number }) {
  unstable_noStore()
  try {
    await db.transaction(async (tx) => {
      await updateProcurementItem(tx, input.id, {
        itemCode: input.itemCode,
        itemName: input.itemName,
        material: input.material,
        specification: input.specification,
        unit: input.unit,
        isActive: input.isActive,
      })
    })

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

/** 단건 삭제 */
export async function removeProcurementItem(input: { id: number }) {
  unstable_noStore()
  try {
    await db.transaction(async (tx) => {
      await deleteProcurementItemById(tx, input.id)
    })

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

/** 복수 삭제 */
export async function removeProcurementItems(input: { ids: number[] }) {
  unstable_noStore()
  try {
    await db.transaction(async (tx) => {
      await deleteProcurementItemsByIds(tx, input.ids)
    })

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

export async function getAllProcurementItems(): Promise<ProcurementItem[]> {
  try {
    return await findAllProcurementItems()
  } catch (err) {
    throw new Error("Failed to get procurement items")
  }
}

// 품목 검색 함수
export async function searchProcurementItems(query: string): Promise<{ itemCode: string; itemName: string }[]> {
  unstable_noStore()

  try {
    // 검색어가 없으면 상위 50개 반환
    if (!query || query.trim().length < 1) {
      const results = await db
        .select({
          itemCode: procurementItems.itemCode,
          itemName: procurementItems.itemName,
        })
        .from(procurementItems)
        .where(eq(procurementItems.isActive, 'Y'))
        .limit(50)
        .orderBy(asc(procurementItems.itemCode))
      
      return results
    }

    const searchQuery = `%${query.trim()}%`

    const results = await db
      .select({
        itemCode: procurementItems.itemCode,
        itemName: procurementItems.itemName,
      })
      .from(procurementItems)
      .where(
        and(
          or(
            ilike(procurementItems.itemCode, searchQuery),
            ilike(procurementItems.itemName, searchQuery)
          ),
          // 활성화된 품목만
          eq(procurementItems.isActive, 'Y')
        )
      )
      .limit(50)
      .orderBy(asc(procurementItems.itemCode))

    return results
  } catch (err) {
    console.error("품목 검색 오류:", err)
    return []
  }
}

/* -----------------------------------------------------
   4) Excel Import
----------------------------------------------------- */

export interface ImportResult {
  success: boolean
  importedCount: number
  errorCount: number
  errors: string[]
  message: string
}

/**
 * Excel 파일에서 품목 데이터 대량 가져오기
 */
export async function importProcurementItemsFromExcel(excelData: any[]): Promise<ImportResult> {
  unstable_noStore()

  try {
    if (!Array.isArray(excelData)) {
      return {
        success: false,
        importedCount: 0,
        errorCount: 0,
        errors: ["데이터 배열이 필요합니다."],
        message: "데이터 배열이 필요합니다."
      }
    }

    let importedCount = 0
    let errorCount = 0
    const errors: string[] = []

    // 배치 처리 (한 번에 너무 많은 데이터를 처리하지 않도록)
    const batchSize = 50
    for (let i = 0; i < excelData.length; i += batchSize) {
      const batch = excelData.slice(i, i + batchSize)

      for (const itemData of batch) {
        try {
          // 데이터 검증
          const cleanedData = {
            itemCode: itemData.itemCode?.toString().trim() || "",
            itemName: itemData.itemName?.toString().trim() || "",
            material: itemData.material?.toString().trim() || "",
            specification: itemData.specification?.toString().trim() || "",
            unit: itemData.unit?.toString().trim() || "",
            isActive: itemData.isActive?.toString().trim() || 'Y',
          }
          const validatedData = createProcurementItemSchema.parse(cleanedData)

          // 품목 생성 또는 업데이트
          const result = await createProcurementItem({
            itemCode: validatedData.itemCode,
            itemName: validatedData.itemName,
            material: validatedData.material || "",
            specification: validatedData.specification || "",
            unit: validatedData.unit || "",
            isActive: validatedData.isActive || 'Y',
          })

          if (result.success) {
            importedCount++
          } else {
            errorCount++
            errors.push(`${validatedData.itemCode}: ${result.error}`)
          }
        } catch (validationError) {
          errorCount++
          errors.push(`${itemData.itemCode || '알 수 없음'}: 검증 오류`)
        }
      }
    }

    return {
      success: true,
      importedCount,
      errorCount,
      errors: errors.slice(0, 10), // 너무 많은 오류 메시지를 보내지 않도록 제한
      message: `${importedCount}개 품목이 성공적으로 가져오기를 완료했습니다.${errorCount > 0 ? ` ${errorCount}개 오류 발생.` : ''}`
    }

  } catch (error) {
    console.error("품목 가져오기 오류:", error)
    return {
      success: false,
      importedCount: 0,
      errorCount: 0,
      errors: ["서버 내부 오류가 발생했습니다."],
      message: "서버 내부 오류가 발생했습니다."
    }
  }
}