summaryrefslogtreecommitdiff
path: root/lib/evaluation/service.ts
blob: 19e41dffadcde1c2bcc581b85dda223465da938d (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
'use server'

import db from "@/db/db"
import { 
  evaluationSubmissions,
  periodicEvaluationsView, 
  type PeriodicEvaluationView 
} from "@/db/schema"
import { 
  and, 
  asc, 
  count, 
  desc, 
  ilike, 
  or, sql , eq, avg, 
  type SQL 
} from "drizzle-orm"
import { filterColumns } from "@/lib/filter-columns"
import { GetEvaluationTargetsSchema } from "../evaluation-target-list/validation";

export async function getPeriodicEvaluations(input: GetEvaluationTargetsSchema) {
    try {

      const offset = (input.page - 1) * input.perPage;
  
      // ✅ getEvaluationTargets 방식과 동일한 필터링 처리
      // 1) 고급 필터 조건
      let advancedWhere: SQL<unknown> | undefined = undefined;
      if (input.filters && input.filters.length > 0) {
        advancedWhere = filterColumns({
          table: periodicEvaluationsView,
          filters: input.filters,
          joinOperator: input.joinOperator || 'and',
        });
      }
  
      // 2) 기본 필터 조건
      let basicWhere: SQL<unknown> | undefined = undefined;
      if (input.basicFilters && input.basicFilters.length > 0) {
        basicWhere = filterColumns({
          table: periodicEvaluationsView,
          filters: input.basicFilters,
          joinOperator: input.basicJoinOperator || 'and',
        });
      }
  
      // 3) 글로벌 검색 조건
      let globalWhere: SQL<unknown> | undefined = undefined;
      if (input.search) {
        const s = `%${input.search}%`;
  
        const validSearchConditions: SQL<unknown>[] = [];
  
        // 벤더 정보로 검색
        const vendorCodeCondition = ilike(periodicEvaluationsView.vendorCode, s);
        if (vendorCodeCondition) validSearchConditions.push(vendorCodeCondition);
  
        const vendorNameCondition = ilike(periodicEvaluationsView.vendorName, s);
        if (vendorNameCondition) validSearchConditions.push(vendorNameCondition);
  
        // 평가 관련 코멘트로 검색
        const evaluationNoteCondition = ilike(periodicEvaluationsView.evaluationNote, s);
        if (evaluationNoteCondition) validSearchConditions.push(evaluationNoteCondition);
  
        const adminCommentCondition = ilike(periodicEvaluationsView.evaluationTargetAdminComment, s);
        if (adminCommentCondition) validSearchConditions.push(adminCommentCondition);
  
        const consolidatedCommentCondition = ilike(periodicEvaluationsView.evaluationTargetConsolidatedComment, s);
        if (consolidatedCommentCondition) validSearchConditions.push(consolidatedCommentCondition);
  
        // 최종 확정자 이름으로 검색
        const finalizedByUserNameCondition = ilike(periodicEvaluationsView.finalizedByUserName, s);
        if (finalizedByUserNameCondition) validSearchConditions.push(finalizedByUserNameCondition);
  
        if (validSearchConditions.length > 0) {
          globalWhere = or(...validSearchConditions);
        }
      }
  
      // ✅ getEvaluationTargets 방식과 동일한 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;
  
      // ✅ getEvaluationTargets 방식과 동일한 전체 데이터 수 조회
      const totalResult = await db
        .select({ count: count() })
        .from(periodicEvaluationsView)
        .where(finalWhere);
  
      const total = totalResult[0]?.count || 0;
  
      if (total === 0) {
        return { data: [], pageCount: 0, total: 0 };
      }
  
      console.log("Total periodic evaluations:", total);
  
      // ✅ getEvaluationTargets 방식과 동일한 정렬 및 페이징 처리된 데이터 조회
      const orderByColumns = input.sort.map((sort) => {
        const column = sort.id as keyof typeof periodicEvaluationsView.$inferSelect;
        return sort.desc ? desc(periodicEvaluationsView[column]) : asc(periodicEvaluationsView[column]);
      });
  
      if (orderByColumns.length === 0) {
        orderByColumns.push(desc(periodicEvaluationsView.createdAt));
      }
  
      const periodicEvaluationsData = await db
        .select()
        .from(periodicEvaluationsView)
        .where(finalWhere)
        .orderBy(...orderByColumns)
        .limit(input.perPage)
        .offset(offset);
  
      const pageCount = Math.ceil(total / input.perPage);

      console.log(periodicEvaluationsData,"periodicEvaluationsData")
  
      return { data: periodicEvaluationsData, pageCount, total };
    } catch (err) {
      console.error("Error in getPeriodicEvaluations:", err);
      // ✅ getEvaluationTargets 방식과 동일한 에러 반환 (total 포함)
      return { data: [], pageCount: 0, total: 0 };
    }
  }

  export interface PeriodicEvaluationsStats {
    total: number
    pendingSubmission: number
    submitted: number
    inReview: number
    reviewCompleted: number
    finalized: number
    averageScore: number | null
    completionRate: number
    averageFinalScore: number | null
    documentsSubmittedCount: number
    documentsNotSubmittedCount: number
    reviewProgress: {
      totalReviewers: number
      completedReviewers: number
      pendingReviewers: number
      reviewCompletionRate: number
    }
  }
  
  export async function getPeriodicEvaluationsStats(evaluationYear: number): Promise<PeriodicEvaluationsStats> {
    try {
      // 기본 WHERE 조건: 해당 연도의 평가만
      const baseWhere = eq(periodicEvaluationsView.evaluationYear, evaluationYear)
  
      // 1. 전체 통계 조회
      const totalStatsResult = await db
        .select({
          total: count(),
          averageScore: avg(periodicEvaluationsView.totalScore),
          averageFinalScore: avg(periodicEvaluationsView.finalScore),
        })
        .from(periodicEvaluationsView)
        .where(baseWhere)
  
      const totalStats = totalStatsResult[0] || { 
        total: 0, 
        averageScore: null, 
        averageFinalScore: null 
      }
  
      // 2. 상태별 카운트 조회
      const statusStatsResult = await db
        .select({
          status: periodicEvaluationsView.status,
          count: count(),
        })
        .from(periodicEvaluationsView)
        .where(baseWhere)
        .groupBy(periodicEvaluationsView.status)
  
      // 상태별 카운트를 객체로 변환
      const statusCounts = statusStatsResult.reduce((acc, item) => {
        acc[item.status] = item.count
        return acc
      }, {} as Record<string, number>)
  
      // 3. 문서 제출 상태 통계
      const documentStatsResult = await db
        .select({
          documentsSubmitted: periodicEvaluationsView.documentsSubmitted,
          count: count(),
        })
        .from(periodicEvaluationsView)
        .where(baseWhere)
        .groupBy(periodicEvaluationsView.documentsSubmitted)
  
      const documentCounts = documentStatsResult.reduce((acc, item) => {
        if (item.documentsSubmitted) {
          acc.submitted = item.count
        } else {
          acc.notSubmitted = item.count
        }
        return acc
      }, { submitted: 0, notSubmitted: 0 })
  
      // 4. 리뷰어 진행 상황 통계
      const reviewProgressResult = await db
        .select({
          totalReviewers: sql<number>`SUM(${periodicEvaluationsView.totalReviewers})`.as('total_reviewers'),
          completedReviewers: sql<number>`SUM(${periodicEvaluationsView.completedReviewers})`.as('completed_reviewers'),
          pendingReviewers: sql<number>`SUM(${periodicEvaluationsView.pendingReviewers})`.as('pending_reviewers'),
        })
        .from(periodicEvaluationsView)
        .where(baseWhere)
  
      const reviewProgress = reviewProgressResult[0] || {
        totalReviewers: 0,
        completedReviewers: 0,
        pendingReviewers: 0,
      }
  
      // 5. 완료율 계산
      const finalizedCount = statusCounts['FINALIZED'] || 0
      const totalCount = totalStats.total
      const completionRate = totalCount > 0 ? Math.round((finalizedCount / totalCount) * 100) : 0
  
      // 6. 리뷰 완료율 계산
      const reviewCompletionRate = reviewProgress.totalReviewers > 0 
        ? Math.round((reviewProgress.completedReviewers / reviewProgress.totalReviewers) * 100) 
        : 0
  
      // 7. 평균 점수 포맷팅 (소수점 1자리)
      const formatScore = (score: string | number | null): number | null => {
        if (score === null || score === undefined) return null
        return Math.round(Number(score) * 10) / 10
      }
  
      return {
        total: totalCount,
        pendingSubmission: statusCounts['PENDING_SUBMISSION'] || 0,
        submitted: statusCounts['SUBMITTED'] || 0,
        inReview: statusCounts['IN_REVIEW'] || 0,
        reviewCompleted: statusCounts['REVIEW_COMPLETED'] || 0,
        finalized: finalizedCount,
        averageScore: formatScore(totalStats.averageScore),
        averageFinalScore: formatScore(totalStats.averageFinalScore),
        completionRate,
        documentsSubmittedCount: documentCounts.submitted,
        documentsNotSubmittedCount: documentCounts.notSubmitted,
        reviewProgress: {
          totalReviewers: reviewProgress.totalReviewers,
          completedReviewers: reviewProgress.completedReviewers,
          pendingReviewers: reviewProgress.pendingReviewers,
          reviewCompletionRate,
        },
      }
  
    } catch (error) {
      console.error('Error in getPeriodicEvaluationsStats:', error)
      // 에러 발생 시 기본값 반환
      return {
        total: 0,
        pendingSubmission: 0,
        submitted: 0,
        inReview: 0,
        reviewCompleted: 0,
        finalized: 0,
        averageScore: null,
        averageFinalScore: null,
        completionRate: 0,
        documentsSubmittedCount: 0,
        documentsNotSubmittedCount: 0,
        reviewProgress: {
          totalReviewers: 0,
          completedReviewers: 0,
          pendingReviewers: 0,
          reviewCompletionRate: 0,
        },
      }
    }
  }



  interface RequestDocumentsData {
    periodicEvaluationId: number
    companyId: number
    evaluationYear: number
    evaluationRound: string
    message: string
  }
  
  export async function requestDocumentsFromVendors(data: RequestDocumentsData[]) {
    try {
      // 각 평가에 대해 evaluationSubmissions 레코드 생성
      const submissions = await Promise.all(
        data.map(async (item) => {
          // 이미 해당 periodicEvaluationId와 companyId로 생성된 submission이 있는지 확인
          const existingSubmission = await db.query.evaluationSubmissions.findFirst({
            where: and(
              eq(evaluationSubmissions.periodicEvaluationId, item.periodicEvaluationId),
              eq(evaluationSubmissions.companyId, item.companyId)
            )
          })
  
          if (existingSubmission) {
            // 이미 존재하면 reviewComments만 업데이트
            const [updated] = await db
              .update(evaluationSubmissions)
              .set({
                reviewComments: item.message,
                updatedAt: new Date()
              })
              .where(eq(evaluationSubmissions.id, existingSubmission.id))
              .returning()
            
            return updated
          } else {
            // 새로 생성
            const [created] = await db
              .insert(evaluationSubmissions)
              .values({
                periodicEvaluationId: item.periodicEvaluationId,
                companyId: item.companyId,
                evaluationYear: item.evaluationYear,
                evaluationRound: item.evaluationRound,
                submissionStatus: 'draft', // 기본값
                reviewComments: item.message,
                // 진행률 관련 필드들은 기본값 0으로 설정됨
                totalGeneralItems: 0,
                completedGeneralItems: 0,
                totalEsgItems: 0,
                completedEsgItems: 0,
                isActive: true
              })
              .returning()
            
            return created
          }
        })
      )
  
      
      return {
        success: true,
        message: `${submissions.length}개 업체에 자료 요청이 완료되었습니다.`,
        submissions
      }
  
    } catch (error) {
      console.error("Error requesting documents from vendors:", error)
      return {
        success: false,
        message: "자료 요청 중 오류가 발생했습니다.",
        error: error instanceof Error ? error.message : "Unknown error"
      }
    }
  }
  
  // 기존 요청 상태 확인 함수 추가
  export async function checkExistingSubmissions(periodicEvaluationIds: number[]) {
    try {
      const existingSubmissions = await db.query.evaluationSubmissions.findMany({
        where: (submissions) => {
          // periodicEvaluationIds 배열에 포함된 ID들을 확인
          return periodicEvaluationIds.length === 1 
            ? eq(submissions.periodicEvaluationId, periodicEvaluationIds[0])
            : periodicEvaluationIds.length > 1
            ? or(...periodicEvaluationIds.map(id => eq(submissions.periodicEvaluationId, id)))
            : eq(submissions.id, -1) // 빈 배열인 경우 결과 없음
        },
        columns: {
          id: true,
          periodicEvaluationId: true,
          companyId: true,
          createdAt: true,
          reviewComments: true
        }
      })
  
      return existingSubmissions
    } catch (error) {
      console.error("Error checking existing submissions:", error)
      return []
    }
  }