| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace App\Controllers;
- use App\Controllers\BaseController;
- use App\Models\VendorInfluencerMappingModel;
- use App\Models\InfluencerModel;
- use CodeIgniter\HTTP\ResponseInterface;
- class DebugController extends BaseController
- {
- protected $vendorInfluencerModel;
- protected $influencerModel;
-
- public function __construct()
- {
- $this->vendorInfluencerModel = new VendorInfluencerMappingModel();
- $this->influencerModel = new InfluencerModel();
- }
-
- /**
- * 외래키 제약조건 디버깅용 메서드
- */
- public function debugForeignKey()
- {
- try {
- $mappingSeq = 2;
- $processedBy = 8;
-
- // 1. USER_LIST에서 SEQ 8번 사용자 확인
- $user = $this->influencerModel->where('SEQ', $processedBy)->first();
- $debugInfo = [
- 'user_exists' => !empty($user),
- 'user_data' => $user,
- 'user_count' => $this->influencerModel->where('SEQ', $processedBy)->countAllResults()
- ];
-
- // 2. VENDOR_INFLUENCER_MAPPING에서 SEQ 2번 레코드 확인
- $mapping = $this->vendorInfluencerModel->where('SEQ', $mappingSeq)->first();
- $debugInfo['mapping_exists'] = !empty($mapping);
- $debugInfo['mapping_data'] = $mapping;
-
- // 3. 현재 APPROVED_BY 필드 상태 확인
- if ($mapping) {
- $debugInfo['current_approved_by'] = $mapping['APPROVED_BY'];
- $debugInfo['current_status'] = $mapping['STATUS'];
- }
-
- // 4. 외래키 제약조건 확인
- $db = \Config\Database::connect();
- $foreignKeys = $db->query("
- SELECT
- CONSTRAINT_NAME,
- COLUMN_NAME,
- REFERENCED_TABLE_NAME,
- REFERENCED_COLUMN_NAME
- FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
- WHERE TABLE_NAME = 'VENDOR_INFLUENCER_MAPPING'
- AND TABLE_SCHEMA = DATABASE()
- AND REFERENCED_TABLE_NAME IS NOT NULL
- ")->getResultArray();
-
- $debugInfo['foreign_keys'] = $foreignKeys;
-
- // 5. 실제 업데이트 시도해보기 (트랜잭션 롤백)
- $db->transStart();
-
- try {
- $updateData = [
- 'STATUS' => 'APPROVED',
- 'APPROVED_BY' => $processedBy,
- 'RESPONSE_MESSAGE' => 'debug test',
- 'RESPONSE_DATE' => date('Y-m-d H:i:s')
- ];
-
- $result = $this->vendorInfluencerModel->update($mappingSeq, $updateData);
- $debugInfo['update_attempted'] = true;
- $debugInfo['update_result'] = $result;
- $debugInfo['update_error'] = null;
-
- } catch (\Exception $e) {
- $debugInfo['update_attempted'] = true;
- $debugInfo['update_result'] = false;
- $debugInfo['update_error'] = $e->getMessage();
- }
-
- // 항상 롤백
- $db->transRollback();
-
- // 6. 다른 사용자 SEQ들 확인
- $otherUsers = $this->influencerModel
- ->select('SEQ, NICK_NAME, EMAIL, IS_ACT, USER_TYPE')
- ->where('IS_ACT', 'Y')
- ->orderBy('SEQ')
- ->findAll(10);
-
- $debugInfo['sample_active_users'] = $otherUsers;
-
- return $this->response->setJSON([
- 'success' => true,
- 'debug_info' => $debugInfo
- ]);
-
- } catch (\Exception $e) {
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '디버깅 중 오류가 발생했습니다.',
- 'error' => $e->getMessage()
- ]);
- }
- }
-
- /**
- * 간단한 업데이트 테스트
- */
- public function testSimpleUpdate()
- {
- try {
- $mappingSeq = 2;
- $processedBy = 8;
-
- // 직접 SQL로 업데이트 시도
- $db = \Config\Database::connect();
-
- $sql = "UPDATE VENDOR_INFLUENCER_MAPPING SET APPROVED_BY = ? WHERE SEQ = ?";
-
- try {
- $result = $db->query($sql, [$processedBy, $mappingSeq]);
-
- return $this->response->setJSON([
- 'success' => true,
- 'message' => '직접 SQL 업데이트 성공',
- 'affected_rows' => $db->affectedRows()
- ]);
-
- } catch (\Exception $e) {
- return $this->response->setJSON([
- 'success' => false,
- 'message' => '직접 SQL 업데이트 실패',
- 'error' => $e->getMessage(),
- 'sql_state' => $db->error()
- ]);
- }
-
- } catch (\Exception $e) {
- return $this->response->setStatusCode(500)->setJSON([
- 'success' => false,
- 'message' => '테스트 중 오류가 발생했습니다.',
- 'error' => $e->getMessage()
- ]);
- }
- }
- /**
- * 벤더사 데이터 확인 (디버그용)
- */
- public function checkVendors()
- {
- $vendorModel = new \App\Models\VendorModel();
-
- // 전체 벤더사 수
- $totalVendors = $vendorModel->countAllResults(false);
-
- // 활성 벤더사 수
- $activeVendors = $vendorModel->where('IS_ACT', 'Y')->countAllResults(false);
-
- // 최근 5개 벤더사 데이터
- $recentVendors = $vendorModel
- ->where('IS_ACT', 'Y')
- ->orderBy('REG_DATE', 'DESC')
- ->limit(5)
- ->findAll();
-
- // 벤더사 상태별 분포
- $statusDistribution = $vendorModel
- ->select('IS_ACT, COUNT(*) as count')
- ->groupBy('IS_ACT')
- ->findAll();
-
- return $this->response->setJSON([
- 'success' => true,
- 'data' => [
- 'total_vendors' => $totalVendors,
- 'active_vendors' => $activeVendors,
- 'recent_vendors' => $recentVendors,
- 'status_distribution' => $statusDistribution,
- 'sample_fields' => array_keys($recentVendors[0] ?? [])
- ]
- ]);
- }
- }
|