DebugController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Controllers;
  3. use App\Controllers\BaseController;
  4. use App\Models\VendorInfluencerMappingModel;
  5. use App\Models\InfluencerModel;
  6. use CodeIgniter\HTTP\ResponseInterface;
  7. class DebugController extends BaseController
  8. {
  9. protected $vendorInfluencerModel;
  10. protected $influencerModel;
  11. public function __construct()
  12. {
  13. $this->vendorInfluencerModel = new VendorInfluencerMappingModel();
  14. $this->influencerModel = new InfluencerModel();
  15. }
  16. /**
  17. * 외래키 제약조건 디버깅용 메서드
  18. */
  19. public function debugForeignKey()
  20. {
  21. try {
  22. $mappingSeq = 2;
  23. $processedBy = 8;
  24. // 1. USER_LIST에서 SEQ 8번 사용자 확인
  25. $user = $this->influencerModel->where('SEQ', $processedBy)->first();
  26. $debugInfo = [
  27. 'user_exists' => !empty($user),
  28. 'user_data' => $user,
  29. 'user_count' => $this->influencerModel->where('SEQ', $processedBy)->countAllResults()
  30. ];
  31. // 2. VENDOR_INFLUENCER_MAPPING에서 SEQ 2번 레코드 확인
  32. $mapping = $this->vendorInfluencerModel->where('SEQ', $mappingSeq)->first();
  33. $debugInfo['mapping_exists'] = !empty($mapping);
  34. $debugInfo['mapping_data'] = $mapping;
  35. // 3. 현재 APPROVED_BY 필드 상태 확인
  36. if ($mapping) {
  37. $debugInfo['current_approved_by'] = $mapping['APPROVED_BY'];
  38. $debugInfo['current_status'] = $mapping['STATUS'];
  39. }
  40. // 4. 외래키 제약조건 확인
  41. $db = \Config\Database::connect();
  42. $foreignKeys = $db->query("
  43. SELECT
  44. CONSTRAINT_NAME,
  45. COLUMN_NAME,
  46. REFERENCED_TABLE_NAME,
  47. REFERENCED_COLUMN_NAME
  48. FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
  49. WHERE TABLE_NAME = 'VENDOR_INFLUENCER_MAPPING'
  50. AND TABLE_SCHEMA = DATABASE()
  51. AND REFERENCED_TABLE_NAME IS NOT NULL
  52. ")->getResultArray();
  53. $debugInfo['foreign_keys'] = $foreignKeys;
  54. // 5. 실제 업데이트 시도해보기 (트랜잭션 롤백)
  55. $db->transStart();
  56. try {
  57. $updateData = [
  58. 'STATUS' => 'APPROVED',
  59. 'APPROVED_BY' => $processedBy,
  60. 'RESPONSE_MESSAGE' => 'debug test',
  61. 'RESPONSE_DATE' => date('Y-m-d H:i:s')
  62. ];
  63. $result = $this->vendorInfluencerModel->update($mappingSeq, $updateData);
  64. $debugInfo['update_attempted'] = true;
  65. $debugInfo['update_result'] = $result;
  66. $debugInfo['update_error'] = null;
  67. } catch (\Exception $e) {
  68. $debugInfo['update_attempted'] = true;
  69. $debugInfo['update_result'] = false;
  70. $debugInfo['update_error'] = $e->getMessage();
  71. }
  72. // 항상 롤백
  73. $db->transRollback();
  74. // 6. 다른 사용자 SEQ들 확인
  75. $otherUsers = $this->influencerModel
  76. ->select('SEQ, NICK_NAME, EMAIL, IS_ACT, USER_TYPE')
  77. ->where('IS_ACT', 'Y')
  78. ->orderBy('SEQ')
  79. ->findAll(10);
  80. $debugInfo['sample_active_users'] = $otherUsers;
  81. return $this->response->setJSON([
  82. 'success' => true,
  83. 'debug_info' => $debugInfo
  84. ]);
  85. } catch (\Exception $e) {
  86. return $this->response->setStatusCode(500)->setJSON([
  87. 'success' => false,
  88. 'message' => '디버깅 중 오류가 발생했습니다.',
  89. 'error' => $e->getMessage()
  90. ]);
  91. }
  92. }
  93. /**
  94. * 간단한 업데이트 테스트
  95. */
  96. public function testSimpleUpdate()
  97. {
  98. try {
  99. $mappingSeq = 2;
  100. $processedBy = 8;
  101. // 직접 SQL로 업데이트 시도
  102. $db = \Config\Database::connect();
  103. $sql = "UPDATE VENDOR_INFLUENCER_MAPPING SET APPROVED_BY = ? WHERE SEQ = ?";
  104. try {
  105. $result = $db->query($sql, [$processedBy, $mappingSeq]);
  106. return $this->response->setJSON([
  107. 'success' => true,
  108. 'message' => '직접 SQL 업데이트 성공',
  109. 'affected_rows' => $db->affectedRows()
  110. ]);
  111. } catch (\Exception $e) {
  112. return $this->response->setJSON([
  113. 'success' => false,
  114. 'message' => '직접 SQL 업데이트 실패',
  115. 'error' => $e->getMessage(),
  116. 'sql_state' => $db->error()
  117. ]);
  118. }
  119. } catch (\Exception $e) {
  120. return $this->response->setStatusCode(500)->setJSON([
  121. 'success' => false,
  122. 'message' => '테스트 중 오류가 발생했습니다.',
  123. 'error' => $e->getMessage()
  124. ]);
  125. }
  126. }
  127. /**
  128. * 벤더사 데이터 확인 (디버그용)
  129. */
  130. public function checkVendors()
  131. {
  132. $vendorModel = new \App\Models\VendorModel();
  133. // 전체 벤더사 수
  134. $totalVendors = $vendorModel->countAllResults(false);
  135. // 활성 벤더사 수
  136. $activeVendors = $vendorModel->where('IS_ACT', 'Y')->countAllResults(false);
  137. // 최근 5개 벤더사 데이터
  138. $recentVendors = $vendorModel
  139. ->where('IS_ACT', 'Y')
  140. ->orderBy('REG_DATE', 'DESC')
  141. ->limit(5)
  142. ->findAll();
  143. // 벤더사 상태별 분포
  144. $statusDistribution = $vendorModel
  145. ->select('IS_ACT, COUNT(*) as count')
  146. ->groupBy('IS_ACT')
  147. ->findAll();
  148. return $this->response->setJSON([
  149. 'success' => true,
  150. 'data' => [
  151. 'total_vendors' => $totalVendors,
  152. 'active_vendors' => $activeVendors,
  153. 'recent_vendors' => $recentVendors,
  154. 'status_distribution' => $statusDistribution,
  155. 'sample_fields' => array_keys($recentVendors[0] ?? [])
  156. ]
  157. ]);
  158. }
  159. }