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
|
"use server"
import db from "@/db/db"
import { vendors, contracts, contractItems, forms,formsPlant,formEntriesPlant, formEntries, formMetas, tags,tagsPlant, tagClasses, tagClassAttributes, projects } from "@/db/schema"
import { eq, and, inArray } from "drizzle-orm"
import { getEditableFieldsByTag } from "./services"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
interface VendorFormStatus {
vendorId: number
vendorName: string
formCount: number // 벤더가 가진 form 개수
tagCount: number // 벤더가 가진 tag 개수
totalFields: number // 입력해야 하는 총 필드 개수
completedFields: number // 입력 완료된 필드 개수
completionRate: number // 완료율 (%)
}
export interface FormStatusByVendor {
tagCount: number;
totalFields: number;
completedFields: number;
completionRate: number;
upcomingCount: number; // 7일 이내 임박한 개수
overdueCount: number; // 지연된 개수
}
export async function getProjectsWithContracts() {
try {
const projectList = await db
.selectDistinct({
id: projects.id,
projectCode: projects.code,
projectName: projects.name,
})
.from(projects)
.innerJoin(contracts, eq(contracts.projectId, projects.id))
.orderBy(projects.code)
return projectList
} catch (error) {
console.error('Error getting projects with contracts:', error)
throw new Error('계약이 있는 프로젝트 조회 중 오류가 발생했습니다.')
}
}
export async function getVendorFormStatus(projectId?: number): Promise<VendorFormStatus[]> {
try {
// 1. 벤더 조회 쿼리 수정
const vendorList = projectId
? await db
.selectDistinct({
vendorId: vendors.id,
vendorName: vendors.vendorName,
})
.from(vendors)
.innerJoin(contracts, eq(contracts.vendorId, vendors.id))
.where(eq(contracts.projectId, projectId))
: await db
.selectDistinct({
vendorId: vendors.id,
vendorName: vendors.vendorName,
})
.from(vendors)
.innerJoin(contracts, eq(contracts.vendorId, vendors.id))
const vendorStatusList: VendorFormStatus[] = []
for (const vendor of vendorList) {
let vendorFormCount = 0
let vendorTagCount = 0
let vendorTotalFields = 0
let vendorCompletedFields = 0
const uniqueTags = new Set<string>()
// 2. 계약 조회 시 projectId 필터 추가
const vendorContracts = projectId
? await db
.select({
id: contracts.id,
projectId: contracts.projectId
})
.from(contracts)
.where(
and(
eq(contracts.vendorId, vendor.vendorId),
eq(contracts.projectId, projectId)
)
)
: await db
.select({
id: contracts.id,
projectId: contracts.projectId
})
.from(contracts)
.where(eq(contracts.vendorId, vendor.vendorId))
for (const contract of vendorContracts) {
// 3. 계약별 contractItems 조회
const contractItemsList = await db
.select({
id: contractItems.id
})
.from(contractItems)
.where(eq(contractItems.contractId, contract.id))
for (const contractItem of contractItemsList) {
// 4. contractItem별 forms 조회
const formsList = await db
.select({
id: forms.id,
formCode: forms.formCode,
contractItemId: forms.contractItemId
})
.from(forms)
.where(eq(forms.contractItemId, contractItem.id))
vendorFormCount += formsList.length
// 5. formEntries 조회
const entriesList = await db
.select({
id: formEntries.id,
formCode: formEntries.formCode,
data: formEntries.data
})
.from(formEntries)
.where(eq(formEntries.contractItemId, contractItem.id))
// 6. TAG별 편집 가능 필드 조회
const editableFieldsByTag = await getEditableFieldsByTag(contractItem.id, contract.projectId)
for (const entry of entriesList) {
// formMetas에서 해당 formCode의 columns 조회
const metaResult = await db
.select({
columns: formMetas.columns
})
.from(formMetas)
.where(
and(
eq(formMetas.formCode, entry.formCode),
eq(formMetas.projectId, contract.projectId)
)
)
.limit(1)
if (metaResult.length === 0) continue
const metaColumns = metaResult[0].columns as any[]
// shi가 'IN' 또는 'BOTH'인 필드 찾기
const inputRequiredFields = metaColumns
.filter(col => col.shi === 'IN' || col.shi === 'BOTH')
.map(col => col.key)
// entry.data 분석 (배열로 가정)
const dataArray = Array.isArray(entry.data) ? entry.data : []
for (const dataItem of dataArray) {
if (typeof dataItem !== 'object' || !dataItem) continue
const tagNo = dataItem.TAG_NO
if (tagNo) {
uniqueTags.add(tagNo)
// TAG별 편집 가능 필드 가져오기
const tagEditableFields = editableFieldsByTag.get(tagNo) || []
// 최종 입력 필요 필드 = shi 기반 필드 + TAG 기반 편집 가능 필드
const allRequiredFields = inputRequiredFields.filter(field =>
tagEditableFields.includes(field)
)
// 각 필드별 입력 상태 체크
for (const fieldKey of allRequiredFields) {
vendorTotalFields++
const fieldValue = dataItem[fieldKey]
// 값이 있고, 빈 문자열이 아니고, null이 아니면 입력 완료
if (fieldValue !== undefined && fieldValue !== null && fieldValue !== '') {
vendorCompletedFields++
}
}
}
}
}
}
}
// 완료율 계산
const completionRate = vendorTotalFields > 0
? Math.round((vendorCompletedFields / vendorTotalFields) * 100 * 10) / 10
: 0
vendorStatusList.push({
vendorId: vendor.vendorId,
vendorName: vendor.vendorName || '이름 없음',
formCount: vendorFormCount,
tagCount: uniqueTags.size,
totalFields: vendorTotalFields,
completedFields: vendorCompletedFields,
completionRate
})
}
return vendorStatusList
} catch (error) {
console.error('Error getting vendor form status:', error)
throw new Error('벤더별 Form 입력 현황 조회 중 오류가 발생했습니다.')
}
}
export async function getFormStatusByVendor(projectId: number, projectCode: string, packageCode: string, formCode: string): Promise<FormStatusByVendor[]> {
try {
const session = await getServerSession(authOptions)
if (!session?.user?.id) {
throw new Error("인증이 필요합니다.")
}
let vendorFormCount = 0
let vendorTagCount = 0
let vendorTotalFields = 0
let vendorCompletedFields = 0
let vendorUpcomingCount = 0 // 7일 이내 임박한 개수
let vendorOverdueCount = 0 // 지연된 개수
const uniqueTags = new Set<string>()
const processedTags = new Set<string>() // 중복 처리 방지용
// 현재 날짜와 7일 후 날짜 계산
const today = new Date()
today.setHours(0, 0, 0, 0) // 시간 부분 제거
const sevenDaysLater = new Date(today)
sevenDaysLater.setDate(sevenDaysLater.getDate() + 7)
// 4. contractItem별 forms 조회
const formsList = await db
.select({
id: formsPlant.id,
formCode: formsPlant.formCode,
contractItemId: formsPlant.contractItemId
})
.from(formsPlant)
.where(
and(
eq(formsPlant.projectCode, projectCode),
eq(formsPlant.packageCode, packageCode),
eq(formsPlant.formCode, formCode)
)
)
vendorFormCount += formsList.length
// 5. formEntries 조회
const entriesList = await db
.select({
id: formEntriesPlant.id,
formCode: formEntriesPlant.formCode,
data: formEntriesPlant.data
})
.from(formEntriesPlant)
.where(
and(
eq(formEntriesPlant.packageCode, packageCode),
eq(formEntriesPlant.projectCode, projectCode),
eq(formEntriesPlant.formCode, formCode)
)
)
// 6. TAG별 편집 가능 필드 조회
const editableFieldsByTag = await getEditableFieldsByTag(projectCode,packageCode, projectId)
const vendorStatusList: VendorFormStatus[] = []
for (const entry of entriesList) {
const metaResult = await db
.select({
columns: formMetas.columns
})
.from(formMetas)
.where(
and(
eq(formMetas.formCode, entry.formCode),
eq(formMetas.projectId, projectId)
)
)
.limit(1)
if (metaResult.length === 0) continue
const metaColumns = metaResult[0].columns as any[]
const inputRequiredFields = metaColumns
.filter(col => col.shi === 'IN' || col.shi === 'BOTH')
.map(col => col.key)
const dataArray = Array.isArray(entry.data) ? entry.data : []
for (const dataItem of dataArray) {
if (typeof dataItem !== 'object' || !dataItem) continue
const tagNo = dataItem.TAG_NO
if (tagNo) {
uniqueTags.add(tagNo)
// TAG별 편집 가능 필드 가져오기
const tagEditableFields = editableFieldsByTag.get(tagNo) || []
const allRequiredFields = inputRequiredFields.filter(field =>
tagEditableFields.includes(field)
)
// 해당 TAG의 필드 완료 상태 체크
let tagHasIncompleteFields = false
for (const fieldKey of allRequiredFields) {
vendorTotalFields++
const fieldValue = dataItem[fieldKey]
if (fieldValue !== undefined && fieldValue !== null && fieldValue !== '') {
vendorCompletedFields++
} else {
tagHasIncompleteFields = true
}
}
// 미완료 TAG에 대해서만 날짜 체크 (TAG당 한 번만 처리)
if (!processedTags.has(tagNo) && tagHasIncompleteFields) {
processedTags.add(tagNo)
const targetDate = dataItem.DUE_DATE
if (targetDate) {
const target = new Date(targetDate)
target.setHours(0, 0, 0, 0) // 시간 부분 제거
if (target < today) {
// 미완료이면서 지연된 경우 (오늘보다 이전)
vendorOverdueCount++
} else if (target >= today && target <= sevenDaysLater) {
// 미완료이면서 7일 이내 임박한 경우
vendorUpcomingCount++
}
}
}
}
}
}
// 완료율 계산
const completionRate = vendorTotalFields > 0
? Math.round((vendorCompletedFields / vendorTotalFields) * 100 * 10) / 10
: 0
vendorStatusList.push({
tagCount: uniqueTags.size,
totalFields: vendorTotalFields,
completedFields: vendorCompletedFields,
completionRate,
upcomingCount: vendorUpcomingCount,
overdueCount: vendorOverdueCount
})
return vendorStatusList
} catch (error) {
console.log('Error getting vendor form status:', error)
throw new Error('벤더별 Form 입력 현황 조회 중 오류가 발생했습니다.')
}
}
|