You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
3.6 KiB
Java
88 lines
3.6 KiB
Java
package com.vmis.interfaceapp.service;
|
|
|
|
import com.vmis.interfaceapp.client.GovernmentApi;
|
|
import com.vmis.interfaceapp.config.ApiConstant;
|
|
import com.vmis.interfaceapp.util.ExceptionDetailUtil;
|
|
import com.vmis.interfaceapp.model.basic.BasicRequest;
|
|
import com.vmis.interfaceapp.model.basic.BasicResponse;
|
|
import com.vmis.interfaceapp.model.basic.CarBassMatterInqireVO;
|
|
import com.vmis.interfaceapp.model.common.Envelope;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
/**
|
|
* 자동차 기본 사항 조회 서비스
|
|
*
|
|
* <p>API 호출 정보를 관리하는 서비스 클래스입니다.</p>
|
|
* <ul>
|
|
* <li>최초 요청: createInitialRequest() - 시퀀스로 ID 생성 후 INSERT</li>
|
|
* <li>결과 업데이트: updateResponse() - 응답 데이터 UPDATE</li>
|
|
* </ul>
|
|
*/
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class CarBassMatterInqireService {
|
|
|
|
private final GovernmentApi governmentApi;
|
|
private final RequestEnricher enricher;
|
|
private final CarBassMatterInqireLogService logService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 자동차 기본사항 조회: 보강 -> 최초요청로그 -> 외부호출 -> 응답로그.
|
|
*/
|
|
@Transactional
|
|
public ResponseEntity<Envelope<BasicResponse>> basic(Envelope<BasicRequest> envelope) {
|
|
// 1) 요청 보강
|
|
enricher.enrichBasic(envelope);
|
|
|
|
String generatedId = null;
|
|
try {
|
|
// 2) 최초 요청 로그 저장 (첫 번째 데이터 기준)
|
|
if (envelope.getData() != null && !envelope.getData().isEmpty()) {
|
|
BasicRequest req = envelope.getData().get(0);
|
|
CarBassMatterInqireVO logEntity = CarBassMatterInqireVO.fromRequest(req);
|
|
generatedId = logService.createInitialRequestNewTx(logEntity);
|
|
}
|
|
|
|
// 3) 외부 API 호출
|
|
ResponseEntity<Envelope<BasicResponse>> response = governmentApi.callBasic(envelope);
|
|
|
|
// 4) 응답 로그 업데이트
|
|
// 원본 소스, 정상적인 호출, 리턴(에러 리턴포함) 일 경우에만 에러 로그 남김
|
|
if (generatedId != null && response.getBody() != null) {
|
|
CarBassMatterInqireVO update = CarBassMatterInqireVO.fromResponse(generatedId, response.getBody());
|
|
if (update != null) {
|
|
logService.updateResponseNewTx(update);
|
|
}
|
|
}
|
|
|
|
return response;
|
|
} catch (Exception e) {
|
|
// 5) 오류 로그 업데이트
|
|
if (generatedId != null) {
|
|
try {
|
|
String detail = ExceptionDetailUtil.buildForLog(e);
|
|
CarBassMatterInqireVO errorLog = CarBassMatterInqireVO.builder()
|
|
.carBassMatterInqireId(generatedId) // 자동차기본사항조회 ID (PK)
|
|
.cntcResultCode(ApiConstant.CNTC_RESULT_CODE_ERROR) // 연계결과코드 (에러)
|
|
.cntcResultDtls(detail) // 연계결과상세 (에러 메시지)
|
|
.build();
|
|
logService.updateResponseNewTx(errorLog);
|
|
log.error("[BASIC-ERR-LOG] API 호출 에러 정보 저장 완료(별도TX) - ID: {}, detail: {}", generatedId, detail, e);
|
|
} catch (Exception ignore) {
|
|
log.error("[BASIC-ERR-LOG] 에러 로그 저장 실패 - ID: {}", generatedId, ignore);
|
|
}
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
}
|