VendorController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace App\Controllers;
  3. use CodeIgniter\RESTful\ResourceController;
  4. use App\Models\VendorInfluencerMappingModel;
  5. use App\Models\VendorInfluencerStatusHistoryModel;
  6. use App\Models\VendorPartnershipModel;
  7. use App\Models\VendorModel;
  8. use App\Models\InfluencerModel;
  9. class VendorController extends ResourceController
  10. {
  11. protected $modelName = 'App\Models\VendorInfluencerMappingModel';
  12. protected $format = 'json';
  13. protected $vendorInfluencerModel;
  14. protected $vendorPartnershipModel;
  15. protected $statusHistoryModel;
  16. protected $vendorModel;
  17. protected $influencerModel;
  18. public function __construct()
  19. {
  20. $this->vendorInfluencerModel = new VendorInfluencerMappingModel();
  21. $this->vendorPartnershipModel = new VendorPartnershipModel();
  22. $this->statusHistoryModel = new VendorInfluencerStatusHistoryModel();
  23. $this->vendorModel = new VendorModel();
  24. $this->influencerModel = new InfluencerModel();
  25. }
  26. /**
  27. * 벤더사의 인플루언서 요청 목록 조회 (히스토리 테이블 기반)
  28. */
  29. public function getInfluencerRequests()
  30. {
  31. try {
  32. $request = $this->request->getJSON();
  33. $vendorSeq = $request->vendorSeq ?? null;
  34. $status = $request->status ?? null;
  35. $page = $request->page ?? 1;
  36. $size = $request->size ?? 20;
  37. log_message('debug', 'getInfluencerRequests 호출: ' . json_encode([
  38. 'vendorSeq' => $vendorSeq,
  39. 'status' => $status,
  40. 'page' => $page,
  41. 'size' => $size
  42. ]));
  43. if (!$vendorSeq) {
  44. return $this->response->setStatusCode(400)->setJSON([
  45. 'success' => false,
  46. 'message' => '벤더사 SEQ는 필수입니다.'
  47. ]);
  48. }
  49. $result = $this->vendorPartnershipModel->getVendorRequests($vendorSeq, $page, $size, $status);
  50. // 통계 계산 (히스토리 테이블이 없을 경우를 대비한 안전장치)
  51. $statsFormatted = [
  52. 'pending' => 0,
  53. 'approved' => 0,
  54. 'rejected' => 0,
  55. 'total' => 0
  56. ];
  57. try {
  58. $stats = $this->statusHistoryModel->getStatusStatsByVendor($vendorSeq);
  59. foreach ($stats as $stat) {
  60. $statsFormatted['total'] += $stat['count'];
  61. switch ($stat['STATUS']) {
  62. case 'PENDING':
  63. $statsFormatted['pending'] = $stat['count'];
  64. break;
  65. case 'APPROVED':
  66. $statsFormatted['approved'] = $stat['count'];
  67. break;
  68. case 'REJECTED':
  69. $statsFormatted['rejected'] = $stat['count'];
  70. break;
  71. }
  72. }
  73. } catch (\Exception $statsError) {
  74. log_message('warning', '통계 조회 실패 (히스토리 테이블 없음?): ' . $statsError->getMessage());
  75. // 히스토리 테이블이 없으면 메인 테이블에서 대략적인 통계 계산
  76. try {
  77. $mainStats = $this->vendorInfluencerModel
  78. ->where('VENDOR_SEQ', $vendorSeq)
  79. ->where('IS_ACT', 'Y')
  80. ->countAllResults();
  81. $statsFormatted['total'] = $mainStats;
  82. $statsFormatted['pending'] = $mainStats; // 히스토리가 없으면 모두 PENDING으로 가정
  83. } catch (\Exception $mainStatsError) {
  84. log_message('error', '메인 테이블 통계도 실패: ' . $mainStatsError->getMessage());
  85. }
  86. }
  87. log_message('debug', 'API 응답 데이터: ' . json_encode([
  88. 'items_count' => count($result['data']),
  89. 'pagination' => $result['pagination'],
  90. 'stats' => $statsFormatted
  91. ]));
  92. // 프론트엔드에서 기대하는 응답 구조에 맞춤
  93. return $this->response->setJSON([
  94. 'success' => true,
  95. 'data' => [
  96. 'items' => $result['data'], // 프론트엔드에서 data.items로 접근
  97. 'total' => $result['pagination']['total'],
  98. 'page' => $result['pagination']['currentPage'],
  99. 'totalPages' => $result['pagination']['totalPages'],
  100. 'size' => $result['pagination']['limit'],
  101. 'stats' => $statsFormatted
  102. ]
  103. ]);
  104. } catch (\Exception $e) {
  105. log_message('error', '인플루언서 요청 목록 조회 오류: ' . $e->getMessage());
  106. log_message('error', '스택 트레이스: ' . $e->getTraceAsString());
  107. return $this->response->setStatusCode(500)->setJSON([
  108. 'success' => false,
  109. 'message' => '요청 목록 조회 중 오류가 발생했습니다.',
  110. 'error' => $e->getMessage()
  111. ]);
  112. }
  113. }
  114. /**
  115. * 인플루언서 요청 승인/거절 처리 (히스토리 테이블 기반)
  116. */
  117. public function processInfluencerRequest()
  118. {
  119. try {
  120. $request = $this->request->getJSON();
  121. $mappingSeq = $request->mappingSeq ?? null;
  122. $action = $request->action ?? null; // 'approve' or 'reject'
  123. $processedBy = $request->processedBy ?? null;
  124. $responseMessage = $request->responseMessage ?? '';
  125. log_message('debug', '승인 처리 요청: ' . json_encode([
  126. 'mappingSeq' => $mappingSeq,
  127. 'action' => $action,
  128. 'processedBy' => $processedBy,
  129. 'responseMessage' => $responseMessage
  130. ]));
  131. if (!$mappingSeq || !$action || !$processedBy) {
  132. return $this->response->setStatusCode(400)->setJSON([
  133. 'success' => false,
  134. 'message' => '필수 파라미터가 누락되었습니다. (mappingSeq, action, processedBy 필요)'
  135. ]);
  136. }
  137. // action 검증
  138. if (!in_array($action, ['approve', 'reject'])) {
  139. return $this->response->setStatusCode(400)->setJSON([
  140. 'success' => false,
  141. 'message' => 'action은 approve 또는 reject만 가능합니다.'
  142. ]);
  143. }
  144. // 매핑 정보와 현재 상태 확인
  145. $mapping = $this->vendorInfluencerModel->getWithCurrentStatus($mappingSeq);
  146. if (!$mapping) {
  147. return $this->response->setStatusCode(404)->setJSON([
  148. 'success' => false,
  149. 'message' => '요청을 찾을 수 없습니다.'
  150. ]);
  151. }
  152. // 현재 상태가 PENDING인지 확인
  153. if ($mapping['CURRENT_STATUS'] !== 'PENDING') {
  154. return $this->response->setStatusCode(400)->setJSON([
  155. 'success' => false,
  156. 'message' => '이미 처리된 요청입니다. 현재 상태: ' . $mapping['CURRENT_STATUS']
  157. ]);
  158. }
  159. // 처리자 확인
  160. $processingUser = $this->validateProcessor($processedBy);
  161. if (!$processingUser['success']) {
  162. return $this->response->setStatusCode(400)->setJSON($processingUser);
  163. }
  164. // 상태 변경
  165. $newStatus = ($action === 'approve') ? 'APPROVED' : 'REJECTED';
  166. $statusMessage = $responseMessage ?: ($action === 'approve' ? '승인 처리됨' : '거부 처리됨');
  167. log_message('debug', "상태 변경: {$mapping['CURRENT_STATUS']} → {$newStatus}");
  168. // 히스토리 테이블에 상태 변경 기록
  169. $this->statusHistoryModel->changeStatus($mappingSeq, $newStatus, $statusMessage, $processedBy);
  170. // 메인 테이블 업데이트 (응답 관련 정보)
  171. $this->vendorInfluencerModel->update($mappingSeq, [
  172. 'RESPONSE_MESSAGE' => $responseMessage,
  173. 'RESPONSE_DATE' => date('Y-m-d H:i:s'),
  174. 'APPROVED_BY' => $processedBy
  175. ]);
  176. // 승인인 경우 파트너십 시작일 설정
  177. if ($action === 'approve') {
  178. $this->vendorInfluencerModel->update($mappingSeq, [
  179. 'PARTNERSHIP_START_DATE' => date('Y-m-d H:i:s')
  180. ]);
  181. }
  182. log_message('debug', "승인 처리 완료: action={$action}, newStatus={$newStatus}");
  183. return $this->response->setJSON([
  184. 'success' => true,
  185. 'message' => $action === 'approve' ? '요청이 승인되었습니다.' : '요청이 거부되었습니다.',
  186. 'data' => [
  187. 'mappingSeq' => $mappingSeq,
  188. 'action' => $action,
  189. 'status' => $newStatus,
  190. 'processedBy' => $processingUser['data']['name'],
  191. 'responseMessage' => $responseMessage
  192. ]
  193. ]);
  194. } catch (\Exception $e) {
  195. log_message('error', '승인 처리 중 예외 발생: ' . $e->getMessage());
  196. log_message('error', '승인 처리 스택 트레이스: ' . $e->getTraceAsString());
  197. return $this->response->setStatusCode(500)->setJSON([
  198. 'success' => false,
  199. 'message' => '요청 처리 중 오류가 발생했습니다.',
  200. 'error' => $e->getMessage()
  201. ]);
  202. }
  203. }
  204. /**
  205. * 처리자 검증 (벤더사 또는 사용자)
  206. */
  207. private function validateProcessor($processedBy)
  208. {
  209. // 1. 먼저 USER_LIST에서 확인 (인플루언서)
  210. $user = $this->influencerModel
  211. ->where('SEQ', $processedBy)
  212. ->where('IS_ACT', 'Y')
  213. ->first();
  214. if ($user) {
  215. return [
  216. 'success' => true,
  217. 'data' => [
  218. 'type' => 'user',
  219. 'seq' => $user['SEQ'],
  220. 'name' => $user['NICK_NAME'] ?: $user['NAME']
  221. ]
  222. ];
  223. }
  224. // 2. VENDOR_LIST에서 확인 (벤더사)
  225. $vendor = $this->vendorModel
  226. ->where('SEQ', $processedBy)
  227. ->where('IS_ACT', 'Y')
  228. ->first();
  229. if ($vendor) {
  230. return [
  231. 'success' => true,
  232. 'data' => [
  233. 'type' => 'vendor',
  234. 'seq' => $vendor['SEQ'],
  235. 'name' => $vendor['COMPANY_NAME'] . ' (벤더사)'
  236. ]
  237. ];
  238. }
  239. return [
  240. 'success' => false,
  241. 'message' => "처리자 SEQ {$processedBy}는 USER_LIST나 VENDOR_LIST에서 찾을 수 없습니다."
  242. ];
  243. }
  244. /**
  245. * 파트너십 해지 (히스토리 테이블 기반)
  246. */
  247. public function terminatePartnership()
  248. {
  249. try {
  250. $request = $this->request->getJSON();
  251. $mappingSeq = $request->mappingSeq ?? null;
  252. $terminatedBy = $request->terminatedBy ?? null;
  253. $terminationReason = $request->terminationReason ?? '';
  254. if (!$mappingSeq || !$terminatedBy) {
  255. return $this->response->setStatusCode(400)->setJSON([
  256. 'success' => false,
  257. 'message' => '필수 파라미터가 누락되었습니다.'
  258. ]);
  259. }
  260. // 매핑 정보와 현재 상태 확인
  261. $mapping = $this->vendorInfluencerModel->getWithCurrentStatus($mappingSeq);
  262. if (!$mapping) {
  263. return $this->response->setStatusCode(404)->setJSON([
  264. 'success' => false,
  265. 'message' => '파트너십을 찾을 수 없습니다.'
  266. ]);
  267. }
  268. // 현재 상태가 APPROVED인지 확인
  269. if ($mapping['CURRENT_STATUS'] !== 'APPROVED') {
  270. return $this->response->setStatusCode(400)->setJSON([
  271. 'success' => false,
  272. 'message' => '승인된 파트너십만 해지할 수 있습니다. 현재 상태: ' . $mapping['CURRENT_STATUS']
  273. ]);
  274. }
  275. // 처리자 확인
  276. $processingUser = $this->validateProcessor($terminatedBy);
  277. if (!$processingUser['success']) {
  278. return $this->response->setStatusCode(400)->setJSON($processingUser);
  279. }
  280. // 상태를 TERMINATED로 변경
  281. $statusMessage = '파트너십 해지: ' . $terminationReason;
  282. $this->statusHistoryModel->changeStatus($mappingSeq, 'TERMINATED', $statusMessage, $terminatedBy);
  283. // 해지 날짜 업데이트
  284. $this->vendorInfluencerModel->update($mappingSeq, [
  285. 'PARTNERSHIP_END_DATE' => date('Y-m-d H:i:s')
  286. ]);
  287. return $this->response->setJSON([
  288. 'success' => true,
  289. 'message' => '파트너십이 해지되었습니다.',
  290. 'data' => [
  291. 'mappingSeq' => $mappingSeq,
  292. 'status' => 'TERMINATED',
  293. 'terminatedBy' => $processingUser['data']['name']
  294. ]
  295. ]);
  296. } catch (\Exception $e) {
  297. log_message('error', '파트너십 해지 오류: ' . $e->getMessage());
  298. return $this->response->setStatusCode(500)->setJSON([
  299. 'success' => false,
  300. 'message' => '파트너십 해지 중 오류가 발생했습니다.',
  301. 'error' => $e->getMessage()
  302. ]);
  303. }
  304. }
  305. /**
  306. * 벤더사 상태 통계 조회
  307. */
  308. public function getStatusStats()
  309. {
  310. try {
  311. $request = $this->request->getJSON();
  312. $vendorSeq = $request->vendorSeq ?? null;
  313. if (!$vendorSeq) {
  314. return $this->response->setStatusCode(400)->setJSON([
  315. 'success' => false,
  316. 'message' => '벤더사 SEQ는 필수입니다.'
  317. ]);
  318. }
  319. $stats = $this->statusHistoryModel->getStatusStatsByVendor($vendorSeq);
  320. return $this->response->setJSON([
  321. 'success' => true,
  322. 'data' => $stats
  323. ]);
  324. } catch (\Exception $e) {
  325. log_message('error', '상태 통계 조회 오류: ' . $e->getMessage());
  326. return $this->response->setStatusCode(500)->setJSON([
  327. 'success' => false,
  328. 'message' => '상태 통계 조회 중 오류가 발생했습니다.',
  329. 'error' => $e->getMessage()
  330. ]);
  331. }
  332. }
  333. }