summaryrefslogtreecommitdiff
path: root/components/common/discipline/discipline-service.ts
blob: cb6edb0ef2b8a3a67dc34e227e80f0147729414b (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
"use server"

import db from '@/db/db'
import { cmctbCd } from '@/db/schema/NONSAP/nonsap'
import { eq, or, ilike, and } from 'drizzle-orm'

// 설계공종코드 타입 정의
export interface DisciplineCode {
  CD: string // 설계공종코드
  USR_DF_CHAR_18: string // 설계공종명
}

// 설계공종코드 검색 옵션
export interface DisciplineSearchOptions {
  searchTerm?: string
  limit?: number
}

/**
 * 설계공종코드 목록 조회
 * 내부 PostgreSQL DB의 cmctbCd 테이블에서 CD_CLF = 'PLJP43' 조건으로 조회
 */
export async function getDisciplineCodes(options: DisciplineSearchOptions = {}): Promise<{
  success: boolean
  data: DisciplineCode[]
  error?: string
}> {
  try {
    const { searchTerm, limit = 100 } = options

    // 검색어가 있는 경우와 없는 경우를 분리해서 처리
    let result;
    
    if (searchTerm && searchTerm.trim()) {
      const term = `%${searchTerm.trim().toUpperCase()}%`
      result = await db
        .select({
          CD: cmctbCd.CD,
          USR_DF_CHAR_18: cmctbCd.USR_DF_CHAR_18
        })
        .from(cmctbCd)
        .where(
          and(
            eq(cmctbCd.CD_CLF, 'PLJP43'),
            or(
              ilike(cmctbCd.CD, term),
              ilike(cmctbCd.USR_DF_CHAR_18, term)
            )
          )
        )
        .orderBy(cmctbCd.CD)
        .limit(limit > 0 ? limit : 100)
    } else {
      result = await db
        .select({
          CD: cmctbCd.CD,
          USR_DF_CHAR_18: cmctbCd.USR_DF_CHAR_18
        })
        .from(cmctbCd)
        .where(eq(cmctbCd.CD_CLF, 'PLJP43'))
        .orderBy(cmctbCd.CD)
        .limit(limit > 0 ? limit : 100)
    }

    // null 값 처리
    const cleanedResult = result
      .filter(item => item.CD && item.USR_DF_CHAR_18)
      .map(item => ({
        CD: item.CD!,
        USR_DF_CHAR_18: item.USR_DF_CHAR_18!
      }))

    return {
      success: true,
      data: cleanedResult
    }
  } catch (error) {
    console.error('Error fetching discipline codes:', error)
    return {
      success: false,
      data: [],
      error: '설계공종코드를 조회하는 중 오류가 발생했습니다.'
    }
  }
}

/**
 * 특정 설계공종코드 조회
 * 내부 PostgreSQL DB의 cmctbCd 테이블에서 조회
 */
export async function getDisciplineCodeByCode(code: string): Promise<DisciplineCode | null> {
  if (!code.trim()) {
    return null
  }

  try {
    const result = await db
      .select({
        CD: cmctbCd.CD,
        USR_DF_CHAR_18: cmctbCd.USR_DF_CHAR_18
      })
      .from(cmctbCd)
      .where(
        and(
          eq(cmctbCd.CD_CLF, 'PLJP43'),
          eq(cmctbCd.CD, code.trim().toUpperCase())
        )
      )
      .limit(1)

    if (result.length > 0 && result[0].CD && result[0].USR_DF_CHAR_18) {
      return {
        CD: result[0].CD,
        USR_DF_CHAR_18: result[0].USR_DF_CHAR_18
      }
    }

    return null
  } catch (error) {
    console.error('Error fetching discipline code by code:', error)
    return null
  }
}