api
This commit is contained in:
@@ -3,89 +3,180 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Libraries\NaverApiClient;
|
||||
use App\Models\Entities\V2ArticleModel;
|
||||
use App\Models\Entities\VrfcReqModel;
|
||||
use App\Models\Entities\NaverWorkerLogModel; // 새로 만든 테이블용 모델
|
||||
use App\Models\Entities\V2stdailyModel;
|
||||
use App\Models\Entities\V2chgstatModel;
|
||||
use App\Models\Entities\V2chghistoryModel;
|
||||
|
||||
class NaverService
|
||||
{
|
||||
protected $naverClient;
|
||||
protected $VrfcReqModel;
|
||||
protected $naverClient, $VrfcReqModel, $V2stdailyModel, $V2chgstatModel, $V2chghistoryModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->naverClient = new NaverApiClient();
|
||||
$this->VrfcReqModel = model(VrfcReqModel::class);
|
||||
helper('log'); // 헬퍼 로드
|
||||
$this->V2stdailyModel = model(V2stdailyModel::class);
|
||||
$this->V2chgstatModel = model(V2chgstatModel::class);
|
||||
$this->V2chghistoryModel = model(V2chghistoryModel::class);
|
||||
helper('log');
|
||||
}
|
||||
|
||||
/**
|
||||
* 매물 정보를 처리하고 DB에 저장하는 메인 함수
|
||||
* @param array $payload 큐에서 꺼낸 원본 데이터
|
||||
* requestType
|
||||
* REG:수동검증요청
|
||||
* MOD:매물정보수정-매물제공업체의 매물정보수정으로 재검증요청1차실패 후재검증요청
|
||||
* CNC:사용자취소
|
||||
* FIN 서비스-서비스 노출/대기검수완료상태,필요한상태인지확인필요
|
||||
* 메인 프로세스: 요청 타입에 따른 분기 처리
|
||||
*/
|
||||
|
||||
public function processArticle(array $payload)
|
||||
{
|
||||
$articleNumber = $payload['articleNumber'];
|
||||
$requestDatetime = date('Y-m-d H:i:s', strtotime($payload['requestDatetime'] ?? 'now'));
|
||||
$requestType = $payload['requestType'] ?? ''; // REG:등록 ,
|
||||
$requestType = $payload['requestType'] ?? '';
|
||||
|
||||
// 1. 네이버 API 호출
|
||||
$response = $this->naverClient->getArticleInfo($articleNumber);
|
||||
|
||||
if (!$response || !isset($response['data']) || $response['code'] !== 'success') {
|
||||
$msg = $response['message'] ?? 'No message';
|
||||
throw new \Exception("네이버 API 응답 에러: $articleNumber | 메시지: $msg");
|
||||
if (!$response || $response['code'] !== 'success') {
|
||||
throw new \Exception("네이버 API 응답 에러: $articleNumber");
|
||||
}
|
||||
|
||||
$articleInfo = $response['data'];
|
||||
// 로그 기록
|
||||
write_custom_log("ARTICLE_INFO | ArticleNumber: $articleNumber", 'INFO', 'service');
|
||||
// 2. 가공 로직 (insertVrfc에 있던 긴 배열 생성 로직)
|
||||
$vrfcParams = $this->mapToDatabaseParams($articleInfo, $payload);
|
||||
write_custom_log("VRFC_PARAMS | " . json_encode($vrfcParams, JSON_UNESCAPED_UNICODE), 'INFO', 'service');
|
||||
$vrfcParams = $this->mapToDatabaseParams($response['data'], $payload);
|
||||
write_custom_log("PROCESS_START | Type: $requestType | Atcl: $articleNumber", 'INFO', 'service');
|
||||
|
||||
switch ($requestType) {
|
||||
case 'REG':
|
||||
$vrfcParams['req_type'] = '10';
|
||||
return $this->handleRegistration($articleNumber, $vrfcParams);
|
||||
case 'REG': // 신규 등록
|
||||
$vr_sq = $this->insertVrfcReq($articleNumber, $vrfcParams);
|
||||
if ($vr_sq) $this->V2stdailyModel->set_v2_st_daily(null, $vrfcParams['cpid'], $vrfcParams['vrfc_type'] . '0103', '1', 'add');
|
||||
break;
|
||||
case 'MOD':
|
||||
$vrfcParams['req_type'] = '20';
|
||||
return $this->handleModification($articleNumber, $vrfcParams);
|
||||
|
||||
case 'MOD': // 수정
|
||||
$vr_sq = $this->updateVrfcReq($articleNumber, $vrfcParams);
|
||||
if ($vr_sq) $this->V2stdailyModel->set_v2_st_daily(null, $vrfcParams['cpid'], $vrfcParams['vrfc_type'] . '0102', '1', 'add');
|
||||
break;
|
||||
case 'CNC':
|
||||
$vrfcParams['req_type'] = '30';
|
||||
return $this->handleStatusChange($articleNumber, $vrfcParams);
|
||||
|
||||
case 'CNC': // 취소
|
||||
$vr_sq = $this->deleteVrfcReq($articleNumber, $vrfcParams);
|
||||
if ($vr_sq) $this->V2stdailyModel->set_v2_st_daily(null, $vrfcParams['cpid'], 'A0101', '1', 'add');
|
||||
break;
|
||||
case 'FIN':
|
||||
$vrfcParams['req_type'] = '40';
|
||||
return $this->handleStatusChange($articleNumber, $vrfcParams);
|
||||
|
||||
case 'FIN': // 완료
|
||||
$vr_sq = $this->finVrfcReq($articleNumber, $vrfcParams);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception("알 수 없는 requestType: " . $requestType);
|
||||
}
|
||||
|
||||
// 중복 체크
|
||||
$existing = $this->VrfcReqModel->where('atcl_no', $articleNumber)
|
||||
|
||||
->first();
|
||||
if ($existing) {
|
||||
throw new \Exception("중복 요청: " . $articleNumber . " / " . $vrfcParams['req_type']);
|
||||
throw new \Exception("알 수 없는 requestType: $requestType");
|
||||
}
|
||||
|
||||
// 3. DB 저장
|
||||
if (!$this->VrfcReqModel->insert($vrfcParams)) {
|
||||
$errorMessages = implode(', ', $this->VrfcReqModel->errors());
|
||||
throw new \Exception("V2 매물 삽입 오류: " . $errorMessages);
|
||||
return ['vr_sq' => $vr_sq, 'articleNumber' => $articleNumber];
|
||||
}
|
||||
|
||||
/**
|
||||
* [REG] 신규 등록
|
||||
*/
|
||||
private function insertVrfcReq($articleNumber, $params)
|
||||
{
|
||||
$existing = $this->VrfcReqModel->where('atcl_no', $articleNumber)->first();
|
||||
if ($existing) throw new \Exception("중복 등록 시도: $articleNumber");
|
||||
|
||||
$params['stat_cd'] = '10';
|
||||
$params['insert_user'] = '0';
|
||||
$params['req_type'] = 'C';
|
||||
|
||||
if (!$this->VrfcReqModel->insert($params)) {
|
||||
$sql = (string)$this->VrfcReqModel->getLastQuery();
|
||||
write_custom_log("INSERT_FAILED | Atcl: $articleNumber | SQL: $sql", 'ERROR', 'failed');
|
||||
throw new \Exception("신규 등록 실패");
|
||||
}
|
||||
|
||||
return $this->VrfcReqModel->getInsertID();
|
||||
$vr_sq = $this->VrfcReqModel->getInsertID();
|
||||
$this->recordStatusAndHistory($vr_sq, '10', 'C9', "신규접수 : 10");
|
||||
|
||||
return $vr_sq;
|
||||
}
|
||||
|
||||
/**
|
||||
* [MOD] 수정 처리
|
||||
*/
|
||||
private function updateVrfcReq($articleNumber, $params)
|
||||
{
|
||||
$existing = $this->findExisting($articleNumber);
|
||||
if (!$existing) return $this->insertVrfcReq($articleNumber, $params);
|
||||
|
||||
$params['stat_cd'] = '30';
|
||||
$params['req_type'] = 'U';
|
||||
$params['insert_tm'] = db_now();
|
||||
|
||||
return $this->updateProcess($existing, $params, 'MOD', "재접수 상태변경: {$existing['stat_cd']} => 30");
|
||||
}
|
||||
|
||||
/**
|
||||
* [CNC] 취소 처리
|
||||
*/
|
||||
private function deleteVrfcReq($articleNumber, $params)
|
||||
{
|
||||
$existing = $this->findExisting($articleNumber);
|
||||
$params['stat_cd'] = '19';
|
||||
$params['req_type'] = 'D';
|
||||
|
||||
return $this->updateProcess($existing, $params, 'CNC', "취소 처리: {$existing['stat_cd']} => 19");
|
||||
}
|
||||
|
||||
/**
|
||||
* [FIN] 완료 처리
|
||||
*/
|
||||
private function finVrfcReq($articleNumber, $params)
|
||||
{
|
||||
$existing = $this->findExisting($articleNumber);
|
||||
$params['stat_cd'] = '60';
|
||||
$params['req_type'] = 'F';
|
||||
|
||||
return $this->updateProcess($existing, $params, 'FIN', "완료 처리: {$existing['stat_cd']} => 60");
|
||||
}
|
||||
|
||||
// --- 내부 공통 유틸리티 함수 ---
|
||||
|
||||
private function findExisting($articleNumber) {
|
||||
$existing = $this->VrfcReqModel->where('atcl_no', $articleNumber)->first();
|
||||
if (!$existing) throw new \Exception("해당 매물 없음: $articleNumber");
|
||||
return $existing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 공통 업데이트 및 이력 기록 로직 (Lock 최소화)
|
||||
*/
|
||||
private function updateProcess($existing, $params, $type, $memo)
|
||||
{
|
||||
$vr_sq = $existing['vr_sq'];
|
||||
|
||||
if (!$this->VrfcReqModel->update($vr_sq, $params)) {
|
||||
$sql = (string)$this->VrfcReqModel->getLastQuery();
|
||||
write_custom_log("UPDATE_FAILED | Type: $type | vr_sq: $vr_sq | SQL: $sql", 'ERROR', 'failed');
|
||||
throw new \Exception("[$type] 업데이트 실패");
|
||||
}
|
||||
|
||||
$this->recordStatusAndHistory($vr_sq, $params['stat_cd'], 'C9', $memo);
|
||||
return $vr_sq;
|
||||
}
|
||||
|
||||
/**
|
||||
* 상태 및 이력 테이블 기록 (독립적 에러 처리)
|
||||
*/
|
||||
private function recordStatusAndHistory($vr_sq, $stat_cd, $chg_type, $memo)
|
||||
{
|
||||
// 1. 상태(stat) 저장
|
||||
try {
|
||||
$this->V2chgstatModel->saveChgstat([
|
||||
'vr_sq' => $vr_sq, 'stat_cd' => $stat_cd, 'insert_user' => '0', 'insert_tm' => db_now()
|
||||
], 'I');
|
||||
} catch (\Exception $e) {
|
||||
write_custom_log("STAT_SAVE_ERR | vr_sq: $vr_sq | Msg: " . $e->getMessage(), 'ERROR', 'failed');
|
||||
}
|
||||
|
||||
// 2. 이력(history) 저장
|
||||
try {
|
||||
$this->V2chghistoryModel->v2_savehistory([
|
||||
'vr_sq' => $vr_sq, 'stat_cd' => $stat_cd, 'chg_type' => $chg_type,
|
||||
'memo' => $memo, 'insert_id' => 'SYSTEM', 'insert_tm' => db_now()
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
write_custom_log("HIST_SAVE_ERR | vr_sq: $vr_sq | Msg: " . $e->getMessage(), 'ERROR', 'failed');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,6 +188,7 @@ class NaverService
|
||||
$certRegister = [];
|
||||
$confirm_doc_img_url = [];
|
||||
$referenceFileUrl = [];
|
||||
$requestDatetime = date('YmdHis', strtotime($payload['requestDatetime'] ?? 'now'));
|
||||
|
||||
foreach ($files as $file) {
|
||||
$fileTypeCode = $file['fileTypeCode'];
|
||||
@@ -124,13 +216,13 @@ class NaverService
|
||||
'req_type' => '',
|
||||
'rdate' => $requestDatetime ?? db_now('Y-m-d H:i:s'),
|
||||
'cpTelNo' => $articleInfo['seller']['sellerTelephoneNumber'],
|
||||
'stat_cd' => '10',
|
||||
'stat_cd' => '',
|
||||
'try_cnt' => '0',
|
||||
'insert_user' => 'admin',
|
||||
'insert_user' => '',
|
||||
'insert_tm' => db_now(),
|
||||
'memo' => '',
|
||||
'contact_fail_cnt' => '0',
|
||||
'sync_yn' => 'N',
|
||||
'sync_yn' => 'Y',
|
||||
'reg_try_cnt' => '0',
|
||||
'tel_fail_cause' => null,
|
||||
'rgbk_confirm_owner_nm' => $articleInfo['seller']['ownerName'] ?? null,
|
||||
@@ -144,65 +236,11 @@ class NaverService
|
||||
'referenceFileUrl' => empty($referenceFileUrl) ? null : json_encode($referenceFileUrl, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
|
||||
|
||||
return $vrfc_params;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [REG] 신규 등록 처리
|
||||
*/
|
||||
private function handleRegistration($articleNumber, $params)
|
||||
{
|
||||
$existing = $this->VrfcReqModel->where('atcl_no', $articleNumber)->first();
|
||||
if ($existing) {
|
||||
// 이미 존재한다면 업데이트로 돌리거나 예외 처리
|
||||
return $this->handleModification($articleNumber, $params);
|
||||
}
|
||||
|
||||
if (!$this->VrfcReqModel->insert($params)) {
|
||||
throw new \Exception("등록 실패: " . implode(', ', $this->VrfcReqModel->errors()));
|
||||
}
|
||||
return $this->VrfcReqModel->getInsertID();
|
||||
}
|
||||
|
||||
/**
|
||||
* [MOD] 수정 처리
|
||||
*/
|
||||
private function handleModification($articleNumber, $params)
|
||||
{
|
||||
// 기존 기록이 있는지 확인
|
||||
$existing = $this->VrfcReqModel->where('atcl_no', $articleNumber)->first();
|
||||
|
||||
if (!$existing) {
|
||||
// 수정 요청인데 데이터가 없으면 새로 등록
|
||||
return $this->handleRegistration($articleNumber, $params);
|
||||
}
|
||||
|
||||
// 기존 데이터 업데이트 (PK인 reqSeq 기준으로 업데이트)
|
||||
if (!$this->VrfcReqModel->update($existing['reqSeq'], $params)) {
|
||||
throw new \Exception("수정 실패: " . implode(', ', $this->VrfcReqModel->errors()));
|
||||
}
|
||||
return $existing['reqSeq'];
|
||||
}
|
||||
|
||||
/**
|
||||
* [CNC / FIN] 상태값 변경 처리
|
||||
*/
|
||||
private function handleStatusChange($articleNumber, $statusCode, $memo)
|
||||
{
|
||||
$existing = $this->VrfcReqModel->where('atcl_no', $articleNumber)->first();
|
||||
if (!$existing) {
|
||||
throw new \Exception("상태 변경 실패: 해당 매물 없음 ($articleNumber)");
|
||||
}
|
||||
|
||||
$updateData = [
|
||||
'stat_cd' => $statusCode,
|
||||
'memo' => $existing['memo'] . " | " . $memo . "(" . date('Y-m-d H:i:s') . ")"
|
||||
];
|
||||
|
||||
if (!$this->VrfcReqModel->update($existing['reqSeq'], $updateData)) {
|
||||
throw new \Exception("상태 업데이트 실패: " . $articleNumber);
|
||||
}
|
||||
return $existing['reqSeq'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user