-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOcrService.theirs.java
More file actions
74 lines (61 loc) · 6.23 KB
/
Copy pathOcrService.theirs.java
File metadata and controls
74 lines (61 loc) · 6.23 KB
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
package com.quicktax.demo.service.ocr;
import com.quicktax.demo.common.ApiException;
import com.quicktax.demo.common.ErrorCode;
import com.quicktax.demo.domain.cases.TaxCase;
import com.quicktax.demo.domain.ocr.OcrJob;
import com.quicktax.demo.domain.ocr.OcrJobStatus;
import com.quicktax.demo.domain.ocr.OcrResult;
import com.quicktax.demo.domain.ocr.OcrResultId;
import com.quicktax.demo.dto.OcrConfirmRequest;
import com.quicktax.demo.dto.OcrConfirmRequest.OcrYearData;
import com.quicktax.demo.repo.TaxCaseRepository;
import com.quicktax.demo.repo.ocr.OcrJobRepository;
import com.quicktax.demo.repo.ocr.OcrResultRepository;
import com.quicktax.demo.service.calc.CalcService; // ?뮕 怨꾩궛 ?붿쭊 ?쒕퉬???꾪룷??import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
@RequiredArgsConstructor
public class OcrService {
private final TaxCaseRepository taxCaseRepository;
private final OcrJobRepository ocrJobRepository;
private final OcrResultRepository ocrResultRepository;
private final CalcService calcService; // ?뮕 ?덈줈 留뚮뱺 怨꾩궛 ?붿쭊 二쇱엯
/**
* OCR ?뺤젙 諛??대? 怨꾩궛 ?ㅽ뻾
*/
@Transactional
public void confirmOcrDataAndCalculate(Long cpaId, Long caseId, OcrConfirmRequest request) {
// 1. Case 諛?沅뚰븳 寃利? TaxCase taxCase = taxCaseRepository.findById(caseId)
.orElseThrow(() -> new ApiException(ErrorCode.COMMON404, "議댁옱?섏? ?딅뒗 Case ID?낅땲??"));
Long ownerCpaId = taxCase.getCustomer().getTaxCompany().getCpaId();
if (!cpaId.equals(ownerCpaId)) {
throw new ApiException(ErrorCode.AUTH403, "沅뚰븳??議댁옱?섏? ?딆뒿?덈떎.");
}
// 2. OCR ?묒뾽 ?곹깭 ?뺤씤
OcrJob ocrJob = ocrJobRepository.findById(caseId)
.orElseThrow(() -> new ApiException(ErrorCode.COMMON404, "OCR ?붿껌 ?댁뿭??議댁옱?섏? ?딆뒿?덈떎."));
if (ocrJob.getStatus() == OcrJobStatus.FAILED) {
throw new ApiException(ErrorCode.COMMON500, "OCR 遺꾩꽍???ㅽ뙣?덉뒿?덈떎.");
}
if (ocrJob.getStatus() != OcrJobStatus.READY) {
throw new ApiException(ErrorCode.OCR409, "OCR 遺꾩꽍???꾩쭅 ?꾨즺?섏? ?딆븯?듬땲??");
}
// 3. ?곕룄蹂??곗씠?????諛?怨꾩궛 ?ㅽ뻾
for (OcrYearData data : request.getOcrData()) {
Integer year = data.getCaseYear();
log.info("OCR ?뺤젙 ?곗씠?????諛?怨꾩궛 ?쒖옉: CaseId={}, Year={}", caseId, year);
// 3-1. OCR 寃곌낵 ????낅뜲?댄듃
OcrResultId resultId = new OcrResultId(caseId, year);
OcrResult ocrResult = ocrResultRepository.findById(resultId)
.orElseGet(() -> new OcrResult(taxCase, year));
ocrResult.updateData(data);
ocrResultRepository.save(ocrResult);
// 3-2. ?? [?듭떖 ?곌껐] ?곗씠?곌? ??λ맂 吏곹썑 諛붾줈 怨꾩궛 ?붿쭊 媛?? // 媛??곕룄蹂꾨줈 猷⑦봽 ?덉뿉???몄텧?섏뿬, 理쒖떊?붾맂 OCR ?곗씠?곕? 湲곕컲?쇰줈 怨꾩궛???섑뻾?⑸땲??
calcService.runCalculation(caseId, year);
}
log.info("Case ID: {} 紐⑤뱺 ?곕룄?????OCR ?뺤젙 諛??대? 怨꾩궛 ?꾨즺.", caseId);
}
}