| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- namespace App\Controllers;
- use App\Controllers\BaseController;
- use App\Models\VendorInfluencerPartnershipModel;
- use App\Models\UserModel;
- use App\Models\VendorModel;
- use CodeIgniter\HTTP\ResponseInterface;
- /**
- * 벤더사-인플루언서 파트너십 관리 컨트롤러 (완전 재설계)
- */
- class PartnershipController extends BaseController
- {
- protected $partnershipModel;
- protected $userModel;
- protected $vendorModel;
-
- public function __construct()
- {
- $this->partnershipModel = new VendorInfluencerPartnershipModel();
- $this->userModel = new UserModel();
- $this->vendorModel = new VendorModel();
- }
- /**
- * 벤더사의 인플루언서 요청 목록 조회
- * POST /api/vendor-influencer/requests
- */
- public function getInfluencerRequests()
- {
- try {
- $request = $this->request->getJSON();
-
- $vendorSeq = $request->vendorSeq ?? null;
- $status = $request->status ?? null;
- $keyword = $request->keyword ?? null;
- $page = $request->page ?? 1;
- $size = $request->size ?? 20;
- if (!$vendorSeq) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '벤더사 SEQ가 필요합니다.'
- ]);
- }
- $filters = [];
- if ($status) $filters['status'] = $status;
- if ($keyword) $filters['keyword'] = $keyword;
- // 데이터 조회
- $items = $this->partnershipModel->getInfluencerRequestsForVendor($vendorSeq, $filters);
-
- // 페이징 처리
- $total = count($items);
- $offset = ($page - 1) * $size;
- $pagedItems = array_slice($items, $offset, $size);
- // 통계 조회
- $stats = $this->partnershipModel->getVendorStats($vendorSeq);
- return $this->response->setJSON([
- 'success' => true,
- 'message' => '요청 목록 조회 성공',
- 'data' => [
- 'items' => $pagedItems,
- 'total' => $total,
- 'page' => $page,
- 'totalPages' => ceil($total / $size),
- 'size' => $size,
- 'stats' => $stats
- ]
- ]);
- } catch (\Exception $e) {
- log_message('error', '인플루언서 요청 목록 조회 오류: ' . $e->getMessage());
-
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '요청 목록을 불러오는 중 오류가 발생했습니다.',
- 'error' => ENVIRONMENT === 'development' ? $e->getMessage() : null
- ]);
- }
- }
- /**
- * 파트너십 승인/거부 처리
- * POST /api/vendor-influencer/approve
- */
- public function processInfluencerRequest()
- {
- try {
- $request = $this->request->getJSON();
-
- $mappingSeq = $request->mappingSeq ?? null;
- $action = $request->action ?? null; // APPROVE or REJECT
- $processedBy = $request->processedBy ?? null;
- $responseMessage = $request->responseMessage ?? '';
- if (!$mappingSeq || !$action) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '필수 파라미터가 누락되었습니다.',
- 'debug' => [
- 'mappingSeq' => $mappingSeq,
- 'action' => $action,
- 'processedBy' => $processedBy,
- 'received_data' => $request
- ]
- ]);
- }
- // processedBy가 없으면 파트너십의 벤더 정보에서 기본값 설정
- if (!$processedBy) {
- $partnership = $this->partnershipModel->find($mappingSeq);
- if ($partnership) {
- $processedBy = $partnership['VENDOR_SEQ']; // 임시로 벤더 SEQ 사용
- }
- }
- if (!in_array($action, ['APPROVE', 'REJECT'])) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '유효하지 않은 액션입니다. (APPROVE 또는 REJECT)'
- ]);
- }
- // 파트너십 존재 확인
- $partnership = $this->partnershipModel->find($mappingSeq);
- if (!$partnership) {
- return $this->response->setStatusCode(404)->setJSON([
- 'success' => false,
- 'message' => '파트너십 요청을 찾을 수 없습니다.'
- ]);
- }
- if ($partnership['STATUS'] !== 'PENDING') {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '대기 중인 요청만 처리할 수 있습니다.'
- ]);
- }
- // 처리자 검증
- $processor = $this->userModel->find($processedBy);
- if (!$processor) {
- $processor = $this->vendorModel->find($processedBy);
- }
- if (!$processor) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '처리자 정보를 찾을 수 없습니다.'
- ]);
- }
- // 승인/거부 처리
- $result = false;
- if ($action === 'APPROVE') {
- $result = $this->partnershipModel->approvePartnership($mappingSeq, $processedBy, $responseMessage);
- $message = '파트너십이 승인되었습니다.';
- } else {
- $result = $this->partnershipModel->rejectPartnership($mappingSeq, $processedBy, $responseMessage);
- $message = '파트너십이 거부되었습니다.';
- }
- if (!$result) {
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '처리 중 오류가 발생했습니다.'
- ]);
- }
- // 처리된 파트너십 정보 조회
- $updatedPartnership = $this->partnershipModel->find($mappingSeq);
- return $this->response->setJSON([
- 'success' => true,
- 'message' => $message,
- 'data' => [
- 'partnership' => $updatedPartnership,
- 'processedBy' => $processor['NAME'] ?? $processor['NICK_NAME'],
- 'processedAt' => date('Y-m-d H:i:s')
- ]
- ]);
- } catch (\Exception $e) {
- log_message('error', '파트너십 처리 오류: ' . $e->getMessage());
-
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '처리 중 오류가 발생했습니다.',
- 'error' => ENVIRONMENT === 'development' ? $e->getMessage() : null
- ]);
- }
- }
- /**
- * 파트너십 해지 처리
- */
- public function terminatePartnership()
- {
- try {
- $mappingSeq = $this->request->getVar('mappingSeq');
- $terminatedBy = $this->request->getVar('terminatedBy');
- $responseMessage = $this->request->getVar('responseMessage');
- if (!$mappingSeq || !$terminatedBy) {
- return $this->response->setJSON([
- 'success' => false,
- 'message' => '필수 파라미터가 누락되었습니다.'
- ]);
- }
- // 처리자 정보 확인
- $userModel = new UserModel();
- $vendorModel = new VendorModel();
-
- $processor = $userModel->find($terminatedBy);
- if (!$processor) {
- $processor = $vendorModel->find($terminatedBy);
- }
-
- if (!$processor) {
- return $this->response->setJSON([
- 'success' => false,
- 'message' => '처리자 정보를 찾을 수 없습니다.'
- ]);
- }
- $partnershipModel = new VendorInfluencerPartnershipModel();
-
- // 현재 파트너십 상태 확인
- $partnership = $partnershipModel->find($mappingSeq);
- if (!$partnership) {
- return $this->response->setJSON([
- 'success' => false,
- 'message' => '파트너십 정보를 찾을 수 없습니다.'
- ]);
- }
- // 해지 처리 실행
- $result = $partnershipModel->terminatePartnership($mappingSeq, $terminatedBy, $responseMessage);
-
- if (!$result['success']) {
- log_message('error', 'Termination failed: ' . json_encode($result));
- return $this->response->setJSON([
- 'success' => false,
- 'message' => '해지 처리 중 오류가 발생했습니다.',
- 'debug' => $result['debug'] ?? null
- ]);
- }
- // 성공 응답
- return $this->response->setJSON([
- 'success' => true,
- 'message' => '파트너십이 해지되었습니다.',
- 'data' => $result['data']
- ]);
- } catch (\Exception $e) {
- log_message('error', '[terminatePartnership] Exception: ' . $e->getMessage());
- return $this->response->setJSON([
- 'success' => false,
- 'message' => '해지 처리 중 오류가 발생했습니다.',
- 'debug' => [
- 'error' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine()
- ]
- ]);
- }
- }
- /**
- * 인플루언서의 벤더사 검색
- * POST /api/vendor-influencer/search-vendors
- */
- public function searchVendorsForInfluencer()
- {
- try {
- $request = $this->request->getJSON();
-
- $influencerSeq = $request->influencerSeq ?? null;
- $keyword = $request->keyword ?? null;
- $category = $request->category ?? null;
- $page = $request->page ?? 1;
- $size = $request->size ?? 20;
- if (!$influencerSeq) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '인플루언서 SEQ가 필요합니다.'
- ]);
- }
- $filters = [];
- if ($keyword) $filters['keyword'] = $keyword;
- if ($category) $filters['category'] = $category;
- // 벤더사 검색
- $items = $this->partnershipModel->searchVendorsForInfluencer($influencerSeq, $filters);
-
- // 페이징 처리
- $total = count($items);
- $offset = ($page - 1) * $size;
- $pagedItems = array_slice($items, $offset, $size);
- return $this->response->setJSON([
- 'success' => true,
- 'message' => '벤더사 검색 성공',
- 'data' => [
- 'items' => $pagedItems,
- 'pagination' => [
- 'total' => $total,
- 'page' => $page,
- 'totalPages' => ceil($total / $size),
- 'size' => $size
- ]
- ]
- ]);
- } catch (\Exception $e) {
- log_message('error', '벤더사 검색 오류: ' . $e->getMessage());
-
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '검색 중 오류가 발생했습니다.',
- 'error' => ENVIRONMENT === 'development' ? $e->getMessage() : null
- ]);
- }
- }
- /**
- * 파트너십 요청 생성
- * POST /api/vendor-influencer/create-request
- */
- public function createPartnershipRequest()
- {
- try {
- $request = $this->request->getJSON();
-
- $vendorSeq = $request->vendorSeq ?? null;
- $influencerSeq = $request->influencerSeq ?? null;
- $requestMessage = $request->requestMessage ?? '';
- $commissionRate = $request->commissionRate ?? null;
- $specialConditions = $request->specialConditions ?? '';
- if (!$vendorSeq || !$influencerSeq) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '벤더사 및 인플루언서 정보가 필요합니다.'
- ]);
- }
- // 중복 요청 확인
- $existingPartnership = $this->partnershipModel->getActivePartnership($vendorSeq, $influencerSeq);
- if ($existingPartnership && in_array($existingPartnership['STATUS'], ['PENDING', 'APPROVED'])) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '이미 활성화된 파트너십 요청이 있습니다.'
- ]);
- }
- $partnershipData = [
- 'VENDOR_SEQ' => $vendorSeq,
- 'INFLUENCER_SEQ' => $influencerSeq,
- 'STATUS' => 'PENDING',
- 'REQUEST_TYPE' => 'NEW',
- 'REQUEST_MESSAGE' => $requestMessage,
- 'COMMISSION_RATE' => $commissionRate,
- 'SPECIAL_CONDITIONS' => $specialConditions,
- 'REQUESTED_BY' => $influencerSeq,
- 'IS_ACTIVE' => 'Y'
- ];
- $result = $this->partnershipModel->createPartnershipRequest($partnershipData);
- if (is_array($result) && isset($result['success']) && $result['success'] === false) {
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
- 'error' => $result
- ]);
- }
- // 생성된 파트너십 정보 조회
- $partnershipSeq = is_array($result) && isset($result['data']['SEQ']) ? $result['data']['SEQ'] : $result;
- $createdPartnership = $this->partnershipModel->find($partnershipSeq);
- return $this->response->setJSON([
- 'success' => true,
- 'message' => '파트너십 요청이 전송되었습니다.',
- 'data' => [
- 'partnership' => $createdPartnership,
- 'requestedAt' => date('Y-m-d H:i:s')
- ]
- ]);
- } catch (\Exception $e) {
- log_message('error', '파트너십 요청 생성 오류: ' . $e->getMessage());
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
- 'error' => $e->getMessage()
- ]);
- }
- }
- /**
- * 재승인 요청 생성
- * POST /api/vendor-influencer/reapply-request
- */
- public function createReapplyRequest()
- {
- try {
- $request = $this->request->getJSON();
-
- $vendorSeq = $request->vendorSeq ?? null;
- $influencerSeq = $request->influencerSeq ?? null;
- $requestMessage = $request->requestMessage ?? '';
- $requestedBy = $request->requestedBy ?? null;
- if (!$vendorSeq || !$influencerSeq || !$requestedBy) {
- return $this->response->setStatusCode(400)->setJSON([
- 'success' => false,
- 'message' => '필수 파라미터가 누락되었습니다.'
- ]);
- }
- // createReapplyRequest 호출 (새로 추가한 메서드)
- $result = $this->partnershipModel->createReapplyRequest($vendorSeq, $influencerSeq, $requestMessage, $requestedBy);
- // 에러 응답이면 그대로 반환
- if (is_array($result) && isset($result['success']) && $result['success'] === false) {
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
- 'error' => $result
- ]);
- }
- // 생성된 파트너십 정보 조회
- $partnershipSeq = is_array($result) && isset($result['data']['SEQ']) ? $result['data']['SEQ'] : $result;
- $createdPartnership = $this->partnershipModel->find($partnershipSeq);
- return $this->response->setJSON([
- 'success' => true,
- 'message' => '재승인 요청이 전송되었습니다.',
- 'data' => [
- 'partnership' => $createdPartnership,
- 'requestedAt' => date('Y-m-d H:i:s')
- ]
- ]);
- } catch (\Exception $e) {
- log_message('error', '재승인 요청 생성 오류: ' . $e->getMessage());
-
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
- 'error' => [
- 'db_error' => [
- 'code' => $e->getCode(),
- 'message' => $e->getMessage()
- ]
- ]
- ]);
- }
- }
- }
|