224 lines
10 KiB
PHP
224 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
use CodeIgniter\CLI\CLI;
|
|
|
|
// 헬퍼 로드 (app/Helpers/log_helper.php 가 있어야 함 autoload 설정 넣어놓았음)
|
|
|
|
class NaverWorker extends BaseCommand
|
|
{
|
|
protected $group = 'Workers';
|
|
protected $name = 'naver:worker';
|
|
protected $description = 'Redis에서 데이터를 꺼내 DB에 저장하고 네이버 API를 호출합니다.';
|
|
|
|
public function run(array $params)
|
|
{
|
|
$redis = new \Redis();
|
|
try {
|
|
$redis->connect('redis', 6379);
|
|
$redis->select(9);
|
|
CLI::write(CLI::color('🟢 Naver Worker running...', 'green'));
|
|
} catch (\Exception $e) {
|
|
CLI::error("Redis 연결 불가: " . $e->getMessage());
|
|
return;
|
|
}
|
|
|
|
while (true) {
|
|
try {
|
|
// 1. Redis에서 데이터를 꺼냄
|
|
$result = $redis->brPop(['naver:raw_queue'], 30);
|
|
|
|
if ($result) {
|
|
$rawData = $result[1];
|
|
try {
|
|
$responseJson = json_decode($rawData, true);
|
|
$payload = $responseJson['request_data'] ?? [];
|
|
if ( empty($payload) ) {
|
|
throw new \Exception( date("Y-m-d H:i:s") . "::빈 페이로드 데이터: " . $rawData);
|
|
}
|
|
CLI::write("Processing: " . json_encode($payload, JSON_UNESCAPED_UNICODE));
|
|
|
|
$requestType = $payload['requestType'] ?? '';
|
|
if (!in_array($requestType, ['REG', 'MOD', 'CNC', 'FIN'])) {
|
|
throw new \Exception( date("Y-m-d H:i:s") . "::잘못된 요청 유형: " . json_encode($payload, JSON_UNESCAPED_UNICODE));
|
|
}
|
|
// 2. 실제 작업 수행
|
|
/*
|
|
REG : 수동검증요청 ,
|
|
MOD 매물정보수정 - 매물제공업체의 매물정보 수정으로 재검증요청 1차 실패 후 재검증 요청 ,
|
|
CNC : 사용자취소 ,
|
|
FIN : 서비스 노출/대기 검수완료 상태(필요한 상태인지 확인 필요)
|
|
*/
|
|
switch ($requestType) {
|
|
case 'REG':
|
|
$this->insertVrfc($payload);
|
|
break;
|
|
case 'MOD':
|
|
// 수동검증요청 및 매물정보수정 처리
|
|
break;
|
|
case 'CNC':
|
|
// 사용자취소 처리
|
|
break;
|
|
case 'FIN':
|
|
// 서비스 노출/대기 검수완료 상태 처리
|
|
break;
|
|
}
|
|
|
|
|
|
CLI::write("✅ Success: " . ($payload['articleNumber'] ?? 'Unknown'), 'cyan');
|
|
|
|
} catch (\Exception $e) {
|
|
// 3. 실패 시: Helper 함수 사용
|
|
$errorMsg = $e->getMessage();
|
|
|
|
CLI::error("❌ Task Failed: $errorMsg");
|
|
|
|
// 공통 헬퍼 함수 호출
|
|
write_custom_log("FAILED_DATA | Error: $errorMsg | Data: $rawData", 'ERROR', 'failed');
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
CLI::error("Worker Loop Error: " . $e->getMessage());
|
|
sleep(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function insertVrfc($payload)
|
|
{
|
|
// 1. 필수 데이터 검증
|
|
if (empty($payload['articleNumber']) || empty($payload['requestType'])) {
|
|
throw new \Exception("필수 파라미터 누락");
|
|
}
|
|
|
|
$articleNumber = $payload['articleNumber'];
|
|
$requestType = $payload['requestType'];
|
|
$requestDatetime = date('Y-m-d H:i:s', strtotime($payload['requestDatetime']));
|
|
|
|
// 2. 네이버 API 클라이언트 초기화
|
|
$naverClient = new \App\Libraries\NaverApiClient();
|
|
|
|
// 매물 정보 조회
|
|
$articleInfojson = $naverClient->getArticleInfo($articleNumber);
|
|
// if (!$articleInfojson || !isset($articleInfojson['data']) || empty($articleInfojson['code'] !== 'success')) {
|
|
// throw new \Exception("매물 정보 조회 실패: $articleNumber ::: message : " . ($articleInfojson['message'] ?? 'No response'));
|
|
// }
|
|
|
|
if (!$articleInfojson || !isset($articleInfojson['data']) || $articleInfojson['code'] !== 'success') {
|
|
$msg = $articleInfojson['message'] ?? 'No message';
|
|
throw new \Exception("네이버 API 응답 에러: $articleNumber | 메시지: $msg");
|
|
}
|
|
|
|
$articleInfo = $articleInfojson['data'];
|
|
|
|
// 받아온 정보 로그 기록
|
|
write_custom_log("ARTICLE_INFO | ArticleNumber: $articleNumber | Info: " . json_encode($articleInfo , JSON_UNESCAPED_UNICODE), 'INFO', 'service');
|
|
CLI::write("Fetched Article Info: " . json_encode($articleInfo));
|
|
|
|
/**
|
|
* $articleInfo['verificationTypeCode']
|
|
* S : 현장확인매물
|
|
* D : 홍보확인서
|
|
* N : 신홍보확인서
|
|
* M : 모바일
|
|
* T : 전화
|
|
* O : 모바일확인V2
|
|
*
|
|
* S - reciept , result 테이블 사용
|
|
* D,N,M,T,O - v2_vrfc_req , v2_article_info , v2_article_info_etc , v2_article_fail 테이블 사용
|
|
*/
|
|
|
|
// {"code":"success","message":"","data":{"articleNumber":"2500000001","realEstateType":"빌라/연립","realEstateTypeCode":"C02","cpId":"toad","cpArticleNumber":"15882606","tradeType":"전세","tradeTypeCode":"B1","statusTypeCode":"E12","verificationTypeCode":"M","isUnregisteredVerificationRequested":false,"isBuildingRegisterAreaCheckRequested":false,"isAutoVerificationRequested":false,"exposureStartDateTime":"2025-01-02 09:12:02","facilities":{"roomCount":2,"bathroomCount":1},"address":{"legalDivision":{"cityNumber":"1100000000","divisionNumber":"1168000000","sectorNumber":"1168010700","legalDivisionAddress":"서울특별시 강남구 신사동"},"isVirtualAddress":false,"correspondenceFloorCount":2,"longitude":0.0,"latitude":0.0},"space":{"totalSpace":34.5,"groundSpace":34.5,"buildingSpace":34.5,"supplySpace":34.5,"exclusiveSpace":34.5},"price":{"dealAmount":777777777,"warrantyAmount":2999999999,"leaseAmount":7777777},"floor":{"correspondenceFloorCount":2,"totalFloorCount":3,"undergroundFloorCount":null}}}
|
|
$comp_sq = '2';
|
|
$rcpt_rating = '3';
|
|
|
|
$files = $articleInfo['files'] ?? [];
|
|
$certRegister = [];
|
|
$confirm_doc_img_url = [];
|
|
$referenceFileUrl = [];
|
|
foreach ($files as $file) {
|
|
// 파일 처리 로직
|
|
$fileTypeCode = $file['fileTypeCode'];
|
|
if ( $fileTypeCode == 'RCDOC' ) {
|
|
$certRegister[] = $file['fileUrl'];
|
|
} elseif ( $fileTypeCode == 'ADDOC' ) {
|
|
$confirm_doc_img_url[] = $file['fileUrl'];
|
|
} elseif ( $fileTypeCode == 'REFER') {
|
|
$referenceFileUrl[] = $file['fileUrl'];
|
|
}
|
|
}
|
|
|
|
$vrfc_params = [
|
|
'reqSeq' => '',
|
|
'atcl_no' => $articleInfo['articleNumber'],
|
|
'step' => '',
|
|
'cpid' => $articleInfo['cpId'],
|
|
'cp_atcl_id' => $articleInfo['cpArticleNumber'],
|
|
'trade_type' => $articleInfo['tradeTypeCode'],
|
|
'realtor_nm' => $articleInfo['realtor']['realtorName'],
|
|
'realtor_tel_no' => $articleInfo['realtor']['representativeCellphoneNumber'],
|
|
'seller_tel_no' => $articleInfo['seller']['sellerTelephoneNumber'],
|
|
'vrfc_type' => $articleInfo['verificationTypeCode'],
|
|
'rgbk_confirm' => $articleInfo['isUnregisteredVerificationRequested'] ? 'Y' : 'N',
|
|
'req_type' => '',
|
|
'rdate' => $requestDatetime ?? db_now('Y-m-d H:i:s'),
|
|
'cpTelNo' => $articleInfo['seller']['sellerTelephoneNumber'],
|
|
'stat_cd' => '10',
|
|
'try_cnt' => '0',
|
|
'insert_user' => 'admin',
|
|
'insert_tm' => db_now(),
|
|
'memo' => '',
|
|
'contact_fail_cnt' => '0',
|
|
'sync_yn' => 'N',
|
|
'reg_try_cnt' => '0',
|
|
'tel_fail_cause' => null,
|
|
'rgbk_confirm_owner_nm' => $articleInfo['seller']['ownerName'] ?? null,
|
|
'direct_trad_yn' => $articleInfo['seller']['isDirectTrade'] === true ? 'Y' : 'N',
|
|
'confirm_doc_img_url' => json_encode($confirm_doc_img_url, JSON_UNESCAPED_UNICODE),
|
|
'confirm_doc_owner_check_yn' => '',
|
|
'owner_verifiable' => null,
|
|
'vrfc_cmpl_type' => null,
|
|
'rgbk_doc_img_url' => null,
|
|
'certRegister' => json_encode($certRegister, JSON_UNESCAPED_UNICODE),
|
|
'referenceFileUrl' => json_encode($referenceFileUrl, JSON_UNESCAPED_UNICODE),
|
|
];
|
|
|
|
write_custom_log("VRFC_PARAMS | " . json_encode($vrfc_params , JSON_UNESCAPED_UNICODE), 'INFO', 'service');
|
|
|
|
|
|
// if ($articleInfo['verificationTypeCode'] == 'S') { // 현장확인매물
|
|
// // 현장확인매물 처리 로직
|
|
|
|
// } else { // 그외 일반매물
|
|
// // V2 매물 처리 로직
|
|
// $v2ArticleModel = model(\App\Models\V2ArticleModel::class);
|
|
// // 매물 번호로 중복 체크
|
|
// $existingV2 = $v2ArticleModel->where('atcl_no', $articleInfo['articleNumber'])->first();
|
|
// if ($existingV2) {
|
|
// throw new \Exception("중복 매물 번호: " . $articleInfo['articleNumber']);
|
|
// }
|
|
|
|
|
|
// $v2ArticleModel->insert($vrfc_params);
|
|
// $vr_sq = $v2ArticleModel->getInsertID(); // 방금 삽입한 vr_sq 값 가져오기
|
|
// CLI::write("Inserted V2 Article with vr_sq: " . $vr_sq);
|
|
// // 오류 처리
|
|
// if ($v2ArticleModel->errors()) {
|
|
// $errorMessages = implode(', ', $v2ArticleModel->errors());
|
|
// throw new \Exception("V2 매물 삽입 오류: " . $errorMessages);
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CLI::write("Processing: " . ($payload['articleNumber'] ?? 'Unknown'));
|
|
}
|
|
} |