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
|
// src/lib/vendors/repository.ts
import { eq, inArray, count, desc, not, isNull, and } from "drizzle-orm";
import db from '@/db/db';
import { SQL } from "drizzle-orm";
import { techVendors, techVendorContacts, techVendorPossibleItems, type TechVendor, type TechVendorContact, type TechVendorWithAttachments, techVendorAttachments } from "@/db/schema/techVendors";
import { itemShipbuilding, itemOffshoreTop, itemOffshoreHull } from "@/db/schema/items";
export type NewTechVendorContact = typeof techVendorContacts.$inferInsert
export type NewTechVendorItem = typeof techVendorPossibleItems.$inferInsert
type PaginationParams = {
offset: number;
limit: number;
};
// 메인 벤더 목록 조회 (첨부파일 정보 포함)
export async function selectTechVendorsWithAttachments(
tx: any,
params: {
where?: SQL<unknown>;
orderBy?: SQL<unknown>[];
} & PaginationParams
) {
const query = tx
.select({
id: techVendors.id,
vendorName: techVendors.vendorName,
vendorCode: techVendors.vendorCode,
taxId: techVendors.taxId,
address: techVendors.address,
country: techVendors.country,
phone: techVendors.phone,
email: techVendors.email,
website: techVendors.website,
status: techVendors.status,
techVendorType: techVendors.techVendorType,
representativeName: techVendors.representativeName,
representativeEmail: techVendors.representativeEmail,
representativePhone: techVendors.representativePhone,
representativeBirth: techVendors.representativeBirth,
countryEng: techVendors.countryEng,
countryFab: techVendors.countryFab,
agentName: techVendors.agentName,
agentPhone: techVendors.agentPhone,
agentEmail: techVendors.agentEmail,
items: techVendors.items,
createdAt: techVendors.createdAt,
updatedAt: techVendors.updatedAt,
})
.from(techVendors);
// where 조건이 있는 경우
if (params.where) {
query.where(params.where);
}
// 정렬 조건이 있는 경우
if (params.orderBy && params.orderBy.length > 0) {
query.orderBy(...params.orderBy);
} else {
// 기본 정렬: 생성일 기준 내림차순
query.orderBy(desc(techVendors.createdAt));
}
// 페이지네이션 적용
query.offset(params.offset).limit(params.limit);
const vendors = await query;
// 첨부파일 정보 가져오기
const vendorsWithAttachments = await Promise.all(
vendors.map(async (vendor: TechVendor) => {
const attachments = await tx
.select({
id: techVendorAttachments.id,
fileName: techVendorAttachments.fileName,
filePath: techVendorAttachments.filePath,
})
.from(techVendorAttachments)
.where(eq(techVendorAttachments.vendorId, vendor.id));
// 벤더의 worktype 조회
const workTypes = await getVendorWorkTypes(tx, vendor.id, vendor.techVendorType);
return {
...vendor,
hasAttachments: attachments.length > 0,
attachmentsList: attachments,
workTypes: workTypes.join(', '), // 콤마로 구분해서 저장
} as TechVendorWithAttachments;
})
);
return vendorsWithAttachments;
}
// 메인 벤더 목록 수 조회 (첨부파일 정보 포함)
export async function countTechVendorsWithAttachments(
tx: any,
where?: SQL<unknown>
) {
const query = tx.select({ count: count() }).from(techVendors);
if (where) {
query.where(where);
}
const result = await query;
return result[0].count;
}
// 기술영업 벤더 조회
export async function selectTechVendors(
tx: any,
params: {
where?: SQL<unknown>;
orderBy?: SQL<unknown>[];
} & PaginationParams
) {
const query = tx.select().from(techVendors);
if (params.where) {
query.where(params.where);
}
if (params.orderBy && params.orderBy.length > 0) {
query.orderBy(...params.orderBy);
} else {
query.orderBy(desc(techVendors.createdAt));
}
query.offset(params.offset).limit(params.limit);
return query;
}
// 기술영업 벤더 수 카운트
export async function countTechVendors(tx: any, where?: SQL<unknown>) {
const query = tx.select({ count: count() }).from(techVendors);
if (where) {
query.where(where);
}
const result = await query;
return result[0].count;
}
// 벤더 상태별 카운트
export async function groupByTechVendorStatus(tx: any) {
const result = await tx
.select({
status: techVendors.status,
count: count(),
})
.from(techVendors)
.groupBy(techVendors.status);
return result;
}
// 벤더 상세 정보 조회
export async function getTechVendorById(id: number) {
const result = await db
.select()
.from(techVendors)
.where(eq(techVendors.id, id));
return result.length > 0 ? result[0] : null;
}
// 벤더 연락처 정보 조회
export async function getTechVendorContactsById(id: number) {
const result = await db
.select()
.from(techVendorContacts)
.where(eq(techVendorContacts.id, id));
return result.length > 0 ? result[0] : null;
}
// 신규 벤더 생성
export async function insertTechVendor(
tx: any,
data: Omit<TechVendor, "id" | "createdAt" | "updatedAt">
) {
return tx
.insert(techVendors)
.values({
...data,
createdAt: new Date(),
updatedAt: new Date(),
})
.returning();
}
// 벤더 정보 업데이트 (단일)
export async function updateTechVendor(
tx: any,
id: string | number,
data: Partial<TechVendor>
) {
return tx
.update(techVendors)
.set({
...data,
updatedAt: new Date(),
})
.where(eq(techVendors.id, Number(id)))
.returning();
}
// 벤더 정보 업데이트 (다수)
export async function updateTechVendors(
tx: any,
ids: (string | number)[],
data: Partial<TechVendor>
) {
return tx
.update(techVendors)
.set({
...data,
updatedAt: new Date(),
})
.where(inArray(techVendors.id, ids.map(id => Number(id))))
.returning();
}
// 벤더 연락처 조회
export async function selectTechVendorContacts(
tx: any,
params: {
where?: SQL<unknown>;
orderBy?: SQL<unknown>[];
} & PaginationParams
) {
const query = tx.select().from(techVendorContacts);
if (params.where) {
query.where(params.where);
}
if (params.orderBy && params.orderBy.length > 0) {
query.orderBy(...params.orderBy);
} else {
query.orderBy(desc(techVendorContacts.createdAt));
}
query.offset(params.offset).limit(params.limit);
return query;
}
// 벤더 연락처 수 카운트
export async function countTechVendorContacts(tx: any, where?: SQL<unknown>) {
const query = tx.select({ count: count() }).from(techVendorContacts);
if (where) {
query.where(where);
}
const result = await query;
return result[0].count;
}
// 연락처 생성
export async function insertTechVendorContact(
tx: any,
data: Omit<TechVendorContact, "id" | "createdAt" | "updatedAt">
) {
return tx
.insert(techVendorContacts)
.values({
...data,
createdAt: new Date(),
updatedAt: new Date(),
})
.returning();
}
// 아이템 목록 조회 (새 스키마용)
export async function selectTechVendorPossibleItems(
tx: any,
params: {
where?: SQL<unknown>;
orderBy?: SQL<unknown>[];
} & PaginationParams
) {
const query = tx.select({
id: techVendorPossibleItems.id,
vendorId: techVendorPossibleItems.vendorId,
shipbuildingItemId: techVendorPossibleItems.shipbuildingItemId,
offshoreTopItemId: techVendorPossibleItems.offshoreTopItemId,
offshoreHullItemId: techVendorPossibleItems.offshoreHullItemId,
createdAt: techVendorPossibleItems.createdAt,
updatedAt: techVendorPossibleItems.updatedAt,
}).from(techVendorPossibleItems);
if (params.where) {
query.where(params.where);
}
if (params.orderBy && params.orderBy.length > 0) {
query.orderBy(...params.orderBy);
} else {
query.orderBy(desc(techVendorPossibleItems.createdAt));
}
query.offset(params.offset).limit(params.limit);
return query;
}
// 아이템 수 카운트 (새 스키마용)
export async function countTechVendorPossibleItems(tx: any, where?: SQL<unknown>) {
const query = tx.select({ count: count() }).from(techVendorPossibleItems);
if (where) {
query.where(where);
}
const result = await query;
return result[0].count;
}
// 아이템 생성
export async function insertTechVendorItem(
tx: any,
data: Omit<NewTechVendorItem, "id" | "createdAt" | "updatedAt">
) {
return tx
.insert(techVendorPossibleItems)
.values({
...data,
createdAt: new Date(),
updatedAt: new Date(),
})
.returning();
}
// 벤더의 worktype 조회
export async function getVendorWorkTypes(
tx: any,
vendorId: number,
vendorType: string
): Promise<string[]> {
try {
const workTypes: string[] = [];
// 벤더 타입에 따라 해당하는 아이템 테이블에서 worktype 조회
if (vendorType.includes('조선')) {
// 조선 아이템들의 workType 조회
const shipWorkTypes = await tx
.select({ workType: itemShipbuilding.workType })
.from(techVendorPossibleItems)
.leftJoin(itemShipbuilding, eq(techVendorPossibleItems.shipbuildingItemId, itemShipbuilding.id))
.where(and(
eq(techVendorPossibleItems.vendorId, vendorId),
not(isNull(techVendorPossibleItems.shipbuildingItemId))
));
workTypes.push(...shipWorkTypes.map((item: { workType: string | null }) => item.workType).filter(Boolean));
}
if (vendorType.includes('해양TOP')) {
// 해양 TOP 아이템들의 workType 조회
const topWorkTypes = await tx
.select({ workType: itemOffshoreTop.workType })
.from(techVendorPossibleItems)
.leftJoin(itemOffshoreTop, eq(techVendorPossibleItems.offshoreTopItemId, itemOffshoreTop.id))
.where(and(
eq(techVendorPossibleItems.vendorId, vendorId),
not(isNull(techVendorPossibleItems.offshoreTopItemId))
));
workTypes.push(
...topWorkTypes
.map((item: { workType: string | null }) => item.workType)
.filter(Boolean) as string[]
);
}
if (vendorType.includes('해양HULL')) {
// 해양 HULL 아이템들의 workType 조회
const hullWorkTypes = await tx
.select({ workType: itemOffshoreHull.workType })
.from(techVendorPossibleItems)
.leftJoin(itemOffshoreHull, eq(techVendorPossibleItems.offshoreHullItemId, itemOffshoreHull.id))
.where(and(
eq(techVendorPossibleItems.vendorId, vendorId),
not(isNull(techVendorPossibleItems.offshoreHullItemId))
));
workTypes.push(...hullWorkTypes.map((item: { workType: string | null }) => item.workType).filter(Boolean));
}
// 중복 제거 후 반환
const uniqueWorkTypes = [...new Set(workTypes)];
return uniqueWorkTypes;
} catch (error) {
console.error('getVendorWorkTypes 오류:', error);
return [];
}
}
|