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
|
"use server"
import { revalidatePath } from 'next/cache'
import { eq, and, or, desc, asc, count, ilike, SQL, gte, lte } from 'drizzle-orm'
import db from '@/db/db'
import { techSalesContactPossibleItems } from "@/db/schema/techSales"
import { techVendors, techVendorContacts, techVendorPossibleItems } from "@/db/schema/techVendors"
import { itemShipbuilding, itemOffshoreTop, itemOffshoreHull } from "@/db/schema/items"
import { filterColumns } from '@/lib/filter-columns'
import { GetContactPossibleItemsSchema } from "./validations"
// 담당자별 아이템 상세 타입 정의 (뷰 기반)
export interface ContactPossibleItemDetail {
id: number
contactId: number
vendorPossibleItemId: number
createdAt: Date
updatedAt: Date
// 벤더 정보
vendorId: number
vendorName: string | null
vendorCode: string | null
vendorEmail: string | null
vendorPhone: string | null
vendorCountry: string | null
vendorStatus: string | null
techVendorType: string | null
// 연락처 정보
contactName: string | null
contactPosition: string | null
contactTitle: string | null
contactEmail: string | null
contactPhone: string | null
contactCountry: string | null
isPrimary: boolean | null
// 아이템 정보
workType: string | null
shipTypes: string | null
itemCode: string | null
itemList: string | null
subItemList: string | null
}
/**
* 담당자별 아이템 목록 조회 (간단한 필터, 정렬, 검색 지원)
*/
export async function getContactPossibleItems(input: GetContactPossibleItemsSchema) {
try {
const offset = (input.page - 1) * input.perPage
// ✅ 1) 고급 필터 조건
let advancedWhere: SQL<unknown> | undefined = undefined
if (input.filters && input.filters.length > 0) {
advancedWhere = filterColumns({
table: techSalesContactPossibleItems,
filters: input.filters as any,
joinOperator: input.joinOperator || 'and',
})
}
// ✅ 2) 기본 필터 조건들
const basicConditions: SQL<unknown>[] = []
if (input.vendorName) {
basicConditions.push(ilike(techVendors.vendorName, `%${input.vendorName}%`))
}
if (input.contactName) {
basicConditions.push(ilike(techVendorContacts.contactName, `%${input.contactName}%`))
}
if (input.vendorCode) {
basicConditions.push(ilike(techVendors.vendorCode, `%${input.vendorCode}%`))
}
// 아이템 코드와 작업 유형은 조인된 테이블에서 검색
if (input.itemCode) {
const itemCodeConditions = [
ilike(itemShipbuilding.itemCode, `%${input.itemCode}%`),
ilike(itemOffshoreTop.itemCode, `%${input.itemCode}%`),
ilike(itemOffshoreHull.itemCode, `%${input.itemCode}%`)
]
basicConditions.push(or(...itemCodeConditions))
}
if (input.workType) {
const workTypeConditions = [
ilike(itemShipbuilding.workType, `%${input.workType}%`),
ilike(itemOffshoreTop.workType, `%${input.workType}%`),
ilike(itemOffshoreHull.workType, `%${input.workType}%`)
]
basicConditions.push(or(...workTypeConditions))
}
const basicWhere = basicConditions.length > 0 ? and(...basicConditions) : undefined
// ✅ 3) 글로벌 검색 조건
let globalWhere: SQL<unknown> | undefined = undefined
if (input.search) {
const s = `%${input.search}%`
const searchConditions = [
ilike(techVendors.vendorName, s),
ilike(techVendorContacts.contactName, s),
ilike(techVendors.vendorCode, s),
ilike(techVendors.email, s),
ilike(techVendorContacts.contactEmail, s),
]
globalWhere = or(...searchConditions)
}
// ✅ 4) 최종 WHERE 조건
const whereConditions: SQL<unknown>[] = []
if (advancedWhere) whereConditions.push(advancedWhere)
if (basicWhere) whereConditions.push(basicWhere)
if (globalWhere) whereConditions.push(globalWhere)
const finalWhere = whereConditions.length > 0 ? and(...whereConditions) : undefined
// ✅ 5) 전체 개수 조회 (단순 조인으로 변경)
const totalResult = await db
.select({ count: count() })
.from(techSalesContactPossibleItems)
.leftJoin(techVendorContacts, eq(techSalesContactPossibleItems.contactId, techVendorContacts.id))
.leftJoin(techVendorPossibleItems, eq(techSalesContactPossibleItems.vendorPossibleItemId, techVendorPossibleItems.id))
.leftJoin(techVendors, eq(techVendorContacts.vendorId, techVendors.id))
.where(finalWhere)
const total = totalResult[0]?.count || 0
if (total === 0) {
return { data: [], pageCount: 0, total: 0 }
}
console.log("Total contact possible items:", total)
// ✅ 6) 정렬 및 페이징
const orderByColumns: any[] = []
for (const sort of input.sort) {
const column = sort.id
// techSalesContactPossibleItems 테이블의 컬럼들
if (column in techSalesContactPossibleItems) {
const contactItemColumn = techSalesContactPossibleItems[column as keyof typeof techSalesContactPossibleItems]
orderByColumns.push(sort.desc ? desc(contactItemColumn) : asc(contactItemColumn))
}
// techVendors 테이블의 컬럼들
else if (column === 'vendorName' || column === 'vendorCode' || column === 'email') {
const vendorColumn = techVendors[column as keyof typeof techVendors]
orderByColumns.push(sort.desc ? desc(vendorColumn) : asc(vendorColumn))
}
// techVendorContacts 테이블의 컬럼들
else if (column === 'contactName' || column === 'contactEmail') {
const contactColumn = techVendorContacts[column as keyof typeof techVendorContacts]
orderByColumns.push(sort.desc ? desc(contactColumn) : asc(contactColumn))
}
// 조인된 테이블의 컬럼들 (실제로는 계산된 값이므로 기본 정렬 사용)
else if (column === 'itemCode' || column === 'workType') {
// 조인된 테이블의 컬럼은 직접 정렬할 수 없으므로 기본 정렬 사용
orderByColumns.push(desc(techSalesContactPossibleItems.createdAt))
}
}
if (orderByColumns.length === 0) {
orderByColumns.push(desc(techSalesContactPossibleItems.createdAt))
}
// ✅ 7) 메인 쿼리 (단순 조인으로 변경)
const basicItems = await db
.select({
// 기본 매핑 정보
id: techSalesContactPossibleItems.id,
contactId: techSalesContactPossibleItems.contactId,
vendorPossibleItemId: techSalesContactPossibleItems.vendorPossibleItemId,
createdAt: techSalesContactPossibleItems.createdAt,
updatedAt: techSalesContactPossibleItems.updatedAt,
// 벤더 정보
vendorId: techVendors.id,
vendorName: techVendors.vendorName,
vendorCode: techVendors.vendorCode,
vendorEmail: techVendors.email,
vendorPhone: techVendors.phone,
vendorCountry: techVendors.country,
vendorStatus: techVendors.status,
techVendorType: techVendors.techVendorType,
// 연락처 정보
contactName: techVendorContacts.contactName,
contactPosition: techVendorContacts.contactPosition,
contactTitle: techVendorContacts.contactTitle,
contactEmail: techVendorContacts.contactEmail,
contactPhone: techVendorContacts.contactPhone,
contactCountry: techVendorContacts.contactCountry,
isPrimary: techVendorContacts.isPrimary,
// 벤더 가능 아이템 ID 정보
shipbuildingItemId: techVendorPossibleItems.shipbuildingItemId,
offshoreTopItemId: techVendorPossibleItems.offshoreTopItemId,
offshoreHullItemId: techVendorPossibleItems.offshoreHullItemId,
})
.from(techSalesContactPossibleItems)
.leftJoin(techVendorContacts, eq(techSalesContactPossibleItems.contactId, techVendorContacts.id))
.leftJoin(techVendorPossibleItems, eq(techSalesContactPossibleItems.vendorPossibleItemId, techVendorPossibleItems.id))
.leftJoin(techVendors, eq(techVendorContacts.vendorId, techVendors.id))
.where(finalWhere)
.orderBy(...orderByColumns)
.offset(offset)
.limit(input.perPage)
// ✅ 8) 각 아이템의 상세 정보를 별도로 조회하여 합치기
const items = await Promise.all(basicItems.map(async (item) => {
let itemCode = null;
let workType = null;
let shipTypes = null;
let itemList = null;
let subItemList = null;
if (item.shipbuildingItemId) {
const shipItem = await db
.select({
itemCode: itemShipbuilding.itemCode,
workType: itemShipbuilding.workType,
shipTypes: itemShipbuilding.shipTypes,
itemList: itemShipbuilding.itemList,
})
.from(itemShipbuilding)
.where(eq(itemShipbuilding.id, item.shipbuildingItemId))
.limit(1);
if (shipItem.length > 0) {
itemCode = shipItem[0].itemCode;
workType = shipItem[0].workType;
shipTypes = shipItem[0].shipTypes;
itemList = shipItem[0].itemList;
}
} else if (item.offshoreTopItemId) {
const topItem = await db
.select({
itemCode: itemOffshoreTop.itemCode,
workType: itemOffshoreTop.workType,
itemList: itemOffshoreTop.itemList,
subItemList: itemOffshoreTop.subItemList,
})
.from(itemOffshoreTop)
.where(eq(itemOffshoreTop.id, item.offshoreTopItemId))
.limit(1);
if (topItem.length > 0) {
itemCode = topItem[0].itemCode;
workType = topItem[0].workType;
itemList = topItem[0].itemList;
subItemList = topItem[0].subItemList;
}
} else if (item.offshoreHullItemId) {
const hullItem = await db
.select({
itemCode: itemOffshoreHull.itemCode,
workType: itemOffshoreHull.workType,
itemList: itemOffshoreHull.itemList,
subItemList: itemOffshoreHull.subItemList,
})
.from(itemOffshoreHull)
.where(eq(itemOffshoreHull.id, item.offshoreHullItemId))
.limit(1);
if (hullItem.length > 0) {
itemCode = hullItem[0].itemCode;
workType = hullItem[0].workType;
itemList = hullItem[0].itemList;
subItemList = hullItem[0].subItemList;
}
}
return {
...item,
itemCode,
workType,
shipTypes,
itemList,
subItemList,
};
}))
// ✅ 9) 페이지 수 계산
const pageCount = Math.ceil(total / input.perPage)
return { data: items as ContactPossibleItemDetail[], pageCount, total }
} catch (err) {
console.error("Error fetching contact possible items:", err)
return { data: [], pageCount: 0, total: 0 }
}
}
/**
* 담당자별 아이템 삭제
*/
export async function deleteContactPossibleItem(id: number) {
try {
await db
.delete(techSalesContactPossibleItems)
.where(eq(techSalesContactPossibleItems.id, id))
revalidatePath("/evcp/contact-possible-items")
return { success: true }
} catch (error) {
console.error("담당자별 아이템 삭제 오류:", error)
return { success: false, error: "담당자별 아이템 삭제에 실패했습니다." }
}
}
/**
* 여러 담당자별 아이템 삭제
*/
export async function deleteContactPossibleItems(ids: number[]) {
try {
await db
.delete(techSalesContactPossibleItems)
.where(
or(...ids.map(id => eq(techSalesContactPossibleItems.id, id)))
)
revalidatePath("/evcp/contact-possible-items")
return { success: true }
} catch (error) {
console.error("담당자별 아이템 일괄 삭제 오류:", error)
return { success: false, error: "담당자별 아이템 삭제에 실패했습니다." }
}
}
|