PartnershipController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. namespace App\Controllers;
  3. use App\Controllers\BaseController;
  4. use App\Models\VendorInfluencerPartnershipModel;
  5. use App\Models\UserModel;
  6. use App\Models\VendorModel;
  7. use CodeIgniter\HTTP\ResponseInterface;
  8. /**
  9. * 벤더사-인플루언서 파트너십 관리 컨트롤러 (완전 재설계)
  10. */
  11. class PartnershipController extends BaseController
  12. {
  13. protected $partnershipModel;
  14. protected $userModel;
  15. protected $vendorModel;
  16. public function __construct()
  17. {
  18. $this->partnershipModel = new VendorInfluencerPartnershipModel();
  19. $this->userModel = new UserModel();
  20. $this->vendorModel = new VendorModel();
  21. }
  22. /**
  23. * 벤더사의 인플루언서 요청 목록 조회
  24. * POST /api/vendor-influencer/requests
  25. */
  26. public function getInfluencerRequests()
  27. {
  28. try {
  29. $request = $this->request->getJSON();
  30. $vendorSeq = $request->vendorSeq ?? null;
  31. $status = $request->status ?? null;
  32. $keyword = $request->keyword ?? null;
  33. $page = $request->page ?? 1;
  34. $size = $request->size ?? 20;
  35. if (!$vendorSeq) {
  36. return $this->response->setStatusCode(400)->setJSON([
  37. 'success' => false,
  38. 'message' => '벤더사 SEQ가 필요합니다.'
  39. ]);
  40. }
  41. $filters = [];
  42. if ($status) $filters['status'] = $status;
  43. if ($keyword) $filters['keyword'] = $keyword;
  44. // 데이터 조회
  45. $items = $this->partnershipModel->getInfluencerRequestsForVendor($vendorSeq, $filters);
  46. // 페이징 처리
  47. $total = count($items);
  48. $offset = ($page - 1) * $size;
  49. $pagedItems = array_slice($items, $offset, $size);
  50. // 통계 조회
  51. $stats = $this->partnershipModel->getVendorStats($vendorSeq);
  52. return $this->response->setJSON([
  53. 'success' => true,
  54. 'message' => '요청 목록 조회 성공',
  55. 'data' => [
  56. 'items' => $pagedItems,
  57. 'total' => $total,
  58. 'page' => $page,
  59. 'totalPages' => ceil($total / $size),
  60. 'size' => $size,
  61. 'stats' => $stats
  62. ]
  63. ]);
  64. } catch (\Exception $e) {
  65. log_message('error', '인플루언서 요청 목록 조회 오류: ' . $e->getMessage());
  66. return $this->response->setStatusCode(500)->setJSON([
  67. 'success' => false,
  68. 'message' => '요청 목록을 불러오는 중 오류가 발생했습니다.',
  69. 'error' => ENVIRONMENT === 'development' ? $e->getMessage() : null
  70. ]);
  71. }
  72. }
  73. /**
  74. * 파트너십 승인/거부 처리
  75. * POST /api/vendor-influencer/approve
  76. */
  77. public function processInfluencerRequest()
  78. {
  79. try {
  80. $request = $this->request->getJSON();
  81. $mappingSeq = $request->mappingSeq ?? null;
  82. $action = $request->action ?? null; // APPROVE or REJECT
  83. $processedBy = $request->processedBy ?? null;
  84. $responseMessage = $request->responseMessage ?? '';
  85. if (!$mappingSeq || !$action) {
  86. return $this->response->setStatusCode(400)->setJSON([
  87. 'success' => false,
  88. 'message' => '필수 파라미터가 누락되었습니다.',
  89. 'debug' => [
  90. 'mappingSeq' => $mappingSeq,
  91. 'action' => $action,
  92. 'processedBy' => $processedBy,
  93. 'received_data' => $request
  94. ]
  95. ]);
  96. }
  97. // processedBy가 없으면 파트너십의 벤더 정보에서 기본값 설정
  98. if (!$processedBy) {
  99. $partnership = $this->partnershipModel->find($mappingSeq);
  100. if ($partnership) {
  101. $processedBy = $partnership['VENDOR_SEQ']; // 임시로 벤더 SEQ 사용
  102. }
  103. }
  104. if (!in_array($action, ['APPROVE', 'REJECT'])) {
  105. return $this->response->setStatusCode(400)->setJSON([
  106. 'success' => false,
  107. 'message' => '유효하지 않은 액션입니다. (APPROVE 또는 REJECT)'
  108. ]);
  109. }
  110. // 파트너십 존재 확인
  111. $partnership = $this->partnershipModel->find($mappingSeq);
  112. if (!$partnership) {
  113. return $this->response->setStatusCode(404)->setJSON([
  114. 'success' => false,
  115. 'message' => '파트너십 요청을 찾을 수 없습니다.'
  116. ]);
  117. }
  118. if ($partnership['STATUS'] !== 'PENDING') {
  119. return $this->response->setStatusCode(400)->setJSON([
  120. 'success' => false,
  121. 'message' => '대기 중인 요청만 처리할 수 있습니다.'
  122. ]);
  123. }
  124. // 처리자 검증
  125. $processor = $this->userModel->find($processedBy);
  126. if (!$processor) {
  127. $processor = $this->vendorModel->find($processedBy);
  128. }
  129. if (!$processor) {
  130. return $this->response->setStatusCode(400)->setJSON([
  131. 'success' => false,
  132. 'message' => '처리자 정보를 찾을 수 없습니다.'
  133. ]);
  134. }
  135. // 승인/거부 처리
  136. $result = false;
  137. if ($action === 'APPROVE') {
  138. $result = $this->partnershipModel->approvePartnership($mappingSeq, $processedBy, $responseMessage);
  139. $message = '파트너십이 승인되었습니다.';
  140. } else {
  141. $result = $this->partnershipModel->rejectPartnership($mappingSeq, $processedBy, $responseMessage);
  142. $message = '파트너십이 거부되었습니다.';
  143. }
  144. if (!$result) {
  145. return $this->response->setStatusCode(500)->setJSON([
  146. 'success' => false,
  147. 'message' => '처리 중 오류가 발생했습니다.'
  148. ]);
  149. }
  150. // 처리된 파트너십 정보 조회
  151. $updatedPartnership = $this->partnershipModel->find($mappingSeq);
  152. return $this->response->setJSON([
  153. 'success' => true,
  154. 'message' => $message,
  155. 'data' => [
  156. 'partnership' => $updatedPartnership,
  157. 'processedBy' => $processor['NAME'] ?? $processor['NICK_NAME'],
  158. 'processedAt' => date('Y-m-d H:i:s')
  159. ]
  160. ]);
  161. } catch (\Exception $e) {
  162. log_message('error', '파트너십 처리 오류: ' . $e->getMessage());
  163. return $this->response->setStatusCode(500)->setJSON([
  164. 'success' => false,
  165. 'message' => '처리 중 오류가 발생했습니다.',
  166. 'error' => ENVIRONMENT === 'development' ? $e->getMessage() : null
  167. ]);
  168. }
  169. }
  170. /**
  171. * 파트너십 해지 처리
  172. */
  173. public function terminatePartnership()
  174. {
  175. try {
  176. $mappingSeq = $this->request->getVar('mappingSeq');
  177. $terminatedBy = $this->request->getVar('terminatedBy');
  178. $responseMessage = $this->request->getVar('responseMessage');
  179. if (!$mappingSeq || !$terminatedBy) {
  180. return $this->response->setJSON([
  181. 'success' => false,
  182. 'message' => '필수 파라미터가 누락되었습니다.'
  183. ]);
  184. }
  185. // 처리자 정보 확인
  186. $userModel = new UserModel();
  187. $vendorModel = new VendorModel();
  188. $processor = $userModel->find($terminatedBy);
  189. if (!$processor) {
  190. $processor = $vendorModel->find($terminatedBy);
  191. }
  192. if (!$processor) {
  193. return $this->response->setJSON([
  194. 'success' => false,
  195. 'message' => '처리자 정보를 찾을 수 없습니다.'
  196. ]);
  197. }
  198. $partnershipModel = new VendorInfluencerPartnershipModel();
  199. // 현재 파트너십 상태 확인
  200. $partnership = $partnershipModel->find($mappingSeq);
  201. if (!$partnership) {
  202. return $this->response->setJSON([
  203. 'success' => false,
  204. 'message' => '파트너십 정보를 찾을 수 없습니다.'
  205. ]);
  206. }
  207. // 해지 처리 실행
  208. $result = $partnershipModel->terminatePartnership($mappingSeq, $terminatedBy, $responseMessage);
  209. if (!$result['success']) {
  210. log_message('error', 'Termination failed: ' . json_encode($result));
  211. return $this->response->setJSON([
  212. 'success' => false,
  213. 'message' => '해지 처리 중 오류가 발생했습니다.',
  214. 'debug' => $result['debug'] ?? null
  215. ]);
  216. }
  217. // 성공 응답
  218. return $this->response->setJSON([
  219. 'success' => true,
  220. 'message' => '파트너십이 해지되었습니다.',
  221. 'data' => $result['data']
  222. ]);
  223. } catch (\Exception $e) {
  224. log_message('error', '[terminatePartnership] Exception: ' . $e->getMessage());
  225. return $this->response->setJSON([
  226. 'success' => false,
  227. 'message' => '해지 처리 중 오류가 발생했습니다.',
  228. 'debug' => [
  229. 'error' => $e->getMessage(),
  230. 'file' => $e->getFile(),
  231. 'line' => $e->getLine()
  232. ]
  233. ]);
  234. }
  235. }
  236. /**
  237. * 인플루언서의 벤더사 검색
  238. * POST /api/vendor-influencer/search-vendors
  239. */
  240. public function searchVendorsForInfluencer()
  241. {
  242. try {
  243. $request = $this->request->getJSON();
  244. $influencerSeq = $request->influencerSeq ?? null;
  245. $keyword = $request->keyword ?? null;
  246. $category = $request->category ?? null;
  247. $page = $request->page ?? 1;
  248. $size = $request->size ?? 20;
  249. if (!$influencerSeq) {
  250. return $this->response->setStatusCode(400)->setJSON([
  251. 'success' => false,
  252. 'message' => '인플루언서 SEQ가 필요합니다.'
  253. ]);
  254. }
  255. $filters = [];
  256. if ($keyword) $filters['keyword'] = $keyword;
  257. if ($category) $filters['category'] = $category;
  258. // 벤더사 검색
  259. $items = $this->partnershipModel->searchVendorsForInfluencer($influencerSeq, $filters);
  260. // 페이징 처리
  261. $total = count($items);
  262. $offset = ($page - 1) * $size;
  263. $pagedItems = array_slice($items, $offset, $size);
  264. return $this->response->setJSON([
  265. 'success' => true,
  266. 'message' => '벤더사 검색 성공',
  267. 'data' => [
  268. 'items' => $pagedItems,
  269. 'pagination' => [
  270. 'total' => $total,
  271. 'page' => $page,
  272. 'totalPages' => ceil($total / $size),
  273. 'size' => $size
  274. ]
  275. ]
  276. ]);
  277. } catch (\Exception $e) {
  278. log_message('error', '벤더사 검색 오류: ' . $e->getMessage());
  279. return $this->response->setStatusCode(500)->setJSON([
  280. 'success' => false,
  281. 'message' => '검색 중 오류가 발생했습니다.',
  282. 'error' => ENVIRONMENT === 'development' ? $e->getMessage() : null
  283. ]);
  284. }
  285. }
  286. /**
  287. * 파트너십 요청 생성
  288. * POST /api/vendor-influencer/create-request
  289. */
  290. public function createPartnershipRequest()
  291. {
  292. try {
  293. $request = $this->request->getJSON();
  294. $vendorSeq = $request->vendorSeq ?? null;
  295. $influencerSeq = $request->influencerSeq ?? null;
  296. $requestMessage = $request->requestMessage ?? '';
  297. $commissionRate = $request->commissionRate ?? null;
  298. $specialConditions = $request->specialConditions ?? '';
  299. if (!$vendorSeq || !$influencerSeq) {
  300. return $this->response->setStatusCode(400)->setJSON([
  301. 'success' => false,
  302. 'message' => '벤더사 및 인플루언서 정보가 필요합니다.'
  303. ]);
  304. }
  305. // 중복 요청 확인
  306. $existingPartnership = $this->partnershipModel->getActivePartnership($vendorSeq, $influencerSeq);
  307. if ($existingPartnership && in_array($existingPartnership['STATUS'], ['PENDING', 'APPROVED'])) {
  308. return $this->response->setStatusCode(400)->setJSON([
  309. 'success' => false,
  310. 'message' => '이미 활성화된 파트너십 요청이 있습니다.'
  311. ]);
  312. }
  313. $partnershipData = [
  314. 'VENDOR_SEQ' => $vendorSeq,
  315. 'INFLUENCER_SEQ' => $influencerSeq,
  316. 'STATUS' => 'PENDING',
  317. 'REQUEST_TYPE' => 'NEW',
  318. 'REQUEST_MESSAGE' => $requestMessage,
  319. 'COMMISSION_RATE' => $commissionRate,
  320. 'SPECIAL_CONDITIONS' => $specialConditions,
  321. 'REQUESTED_BY' => $influencerSeq,
  322. 'IS_ACTIVE' => 'Y'
  323. ];
  324. $result = $this->partnershipModel->createPartnershipRequest($partnershipData);
  325. if (is_array($result) && isset($result['success']) && $result['success'] === false) {
  326. return $this->response->setStatusCode(500)->setJSON([
  327. 'success' => false,
  328. 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
  329. 'error' => $result
  330. ]);
  331. }
  332. // 생성된 파트너십 정보 조회
  333. $partnershipSeq = is_array($result) && isset($result['data']['SEQ']) ? $result['data']['SEQ'] : $result;
  334. $createdPartnership = $this->partnershipModel->find($partnershipSeq);
  335. return $this->response->setJSON([
  336. 'success' => true,
  337. 'message' => '파트너십 요청이 전송되었습니다.',
  338. 'data' => [
  339. 'partnership' => $createdPartnership,
  340. 'requestedAt' => date('Y-m-d H:i:s')
  341. ]
  342. ]);
  343. } catch (\Exception $e) {
  344. log_message('error', '파트너십 요청 생성 오류: ' . $e->getMessage());
  345. return $this->response->setStatusCode(500)->setJSON([
  346. 'success' => false,
  347. 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
  348. 'error' => $e->getMessage()
  349. ]);
  350. }
  351. }
  352. /**
  353. * 재승인 요청 생성
  354. * POST /api/vendor-influencer/reapply-request
  355. */
  356. public function createReapplyRequest()
  357. {
  358. try {
  359. $request = $this->request->getJSON();
  360. $vendorSeq = $request->vendorSeq ?? null;
  361. $influencerSeq = $request->influencerSeq ?? null;
  362. $requestMessage = $request->requestMessage ?? '';
  363. $requestedBy = $request->requestedBy ?? null;
  364. if (!$vendorSeq || !$influencerSeq || !$requestedBy) {
  365. return $this->response->setStatusCode(400)->setJSON([
  366. 'success' => false,
  367. 'message' => '필수 파라미터가 누락되었습니다.'
  368. ]);
  369. }
  370. // createReapplyRequest 호출 (새로 추가한 메서드)
  371. $result = $this->partnershipModel->createReapplyRequest($vendorSeq, $influencerSeq, $requestMessage, $requestedBy);
  372. // 에러 응답이면 그대로 반환
  373. if (is_array($result) && isset($result['success']) && $result['success'] === false) {
  374. return $this->response->setStatusCode(500)->setJSON([
  375. 'success' => false,
  376. 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
  377. 'error' => $result
  378. ]);
  379. }
  380. // 생성된 파트너십 정보 조회
  381. $partnershipSeq = is_array($result) && isset($result['data']['SEQ']) ? $result['data']['SEQ'] : $result;
  382. $createdPartnership = $this->partnershipModel->find($partnershipSeq);
  383. return $this->response->setJSON([
  384. 'success' => true,
  385. 'message' => '재승인 요청이 전송되었습니다.',
  386. 'data' => [
  387. 'partnership' => $createdPartnership,
  388. 'requestedAt' => date('Y-m-d H:i:s')
  389. ]
  390. ]);
  391. } catch (\Exception $e) {
  392. log_message('error', '재승인 요청 생성 오류: ' . $e->getMessage());
  393. return $this->response->setStatusCode(500)->setJSON([
  394. 'success' => false,
  395. 'message' => '재승인 요청 생성 중 오류가 발생했습니다.',
  396. 'error' => [
  397. 'db_error' => [
  398. 'code' => $e->getCode(),
  399. 'message' => $e->getMessage()
  400. ]
  401. ]
  402. ]);
  403. }
  404. }
  405. }