VendorInfluencerStatusHistoryModel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Models;
  3. use CodeIgniter\Model;
  4. class VendorInfluencerStatusHistoryModel extends Model
  5. {
  6. protected $table = 'VENDOR_INFLUENCER_STATUS_HISTORY';
  7. protected $primaryKey = 'SEQ';
  8. protected $useAutoIncrement = true;
  9. protected $returnType = 'array';
  10. protected $useSoftDeletes = false;
  11. protected $protectFields = true;
  12. protected $allowedFields = [
  13. 'MAPPING_SEQ',
  14. 'STATUS',
  15. 'PREVIOUS_STATUS',
  16. 'STATUS_MESSAGE',
  17. 'CHANGED_BY',
  18. 'CHANGED_DATE',
  19. 'IS_CURRENT',
  20. 'REG_DATE'
  21. ];
  22. // Dates
  23. protected $useTimestamps = false;
  24. protected $dateFormat = 'datetime';
  25. protected $createdField = 'REG_DATE';
  26. protected $updatedField = '';
  27. protected $deletedField = '';
  28. // Validation
  29. protected $validationRules = [
  30. 'MAPPING_SEQ' => 'required|integer',
  31. 'STATUS' => 'required|in_list[PENDING,APPROVED,REJECTED,CANCELLED,EXPIRED,TERMINATED]',
  32. 'CHANGED_BY' => 'required|integer',
  33. 'IS_CURRENT' => 'required|in_list[Y,N]'
  34. ];
  35. protected $validationMessages = [
  36. 'MAPPING_SEQ' => [
  37. 'required' => '매핑 SEQ는 필수입니다.',
  38. 'integer' => '매핑 SEQ는 정수여야 합니다.'
  39. ],
  40. 'STATUS' => [
  41. 'required' => '상태는 필수입니다.',
  42. 'in_list' => '유효하지 않은 상태입니다.'
  43. ],
  44. 'CHANGED_BY' => [
  45. 'required' => '변경자는 필수입니다.',
  46. 'integer' => '변경자 SEQ는 정수여야 합니다.'
  47. ]
  48. ];
  49. protected $skipValidation = false;
  50. protected $cleanValidationRules = true;
  51. // Callbacks
  52. protected $allowCallbacks = true;
  53. protected $beforeInsert = ['beforeInsert'];
  54. protected $afterInsert = [];
  55. protected $beforeUpdate = [];
  56. protected $afterUpdate = [];
  57. protected $beforeFind = [];
  58. protected $afterFind = [];
  59. protected $beforeDelete = [];
  60. protected $afterDelete = [];
  61. /**
  62. * 상태 변경 전 처리
  63. */
  64. protected function beforeInsert(array $data)
  65. {
  66. // REG_DATE 자동 설정
  67. if (!isset($data['data']['REG_DATE'])) {
  68. $data['data']['REG_DATE'] = date('Y-m-d H:i:s');
  69. }
  70. // CHANGED_DATE 자동 설정
  71. if (!isset($data['data']['CHANGED_DATE'])) {
  72. $data['data']['CHANGED_DATE'] = date('Y-m-d H:i:s');
  73. }
  74. return $data;
  75. }
  76. /**
  77. * 특정 매핑의 현재 상태 조회
  78. */
  79. public function getCurrentStatus($mappingSeq)
  80. {
  81. return $this->where('MAPPING_SEQ', $mappingSeq)
  82. ->where('IS_CURRENT', 'Y')
  83. ->first();
  84. }
  85. /**
  86. * 특정 매핑의 상태 히스토리 조회
  87. */
  88. public function getStatusHistory($mappingSeq, $limit = 10)
  89. {
  90. return $this->where('MAPPING_SEQ', $mappingSeq)
  91. ->orderBy('CHANGED_DATE', 'DESC')
  92. ->limit($limit)
  93. ->findAll();
  94. }
  95. /**
  96. * 상태 변경 (트랜잭션 포함)
  97. */
  98. public function changeStatus($mappingSeq, $newStatus, $statusMessage = '', $changedBy = null)
  99. {
  100. $db = \Config\Database::connect();
  101. $db->transStart();
  102. try {
  103. // 1. 현재 상태 조회
  104. $currentStatus = $this->getCurrentStatus($mappingSeq);
  105. $previousStatus = $currentStatus ? $currentStatus['STATUS'] : null;
  106. // 2. 기존 현재 상태를 이전 상태로 변경
  107. if ($currentStatus) {
  108. $this->update($currentStatus['SEQ'], ['IS_CURRENT' => 'N']);
  109. }
  110. // 3. 새로운 상태 히스토리 추가
  111. $historyData = [
  112. 'MAPPING_SEQ' => $mappingSeq,
  113. 'STATUS' => $newStatus,
  114. 'PREVIOUS_STATUS' => $previousStatus,
  115. 'STATUS_MESSAGE' => $statusMessage,
  116. 'CHANGED_BY' => $changedBy,
  117. 'IS_CURRENT' => 'Y'
  118. ];
  119. $result = $this->insert($historyData);
  120. // 4. 메인 테이블의 MOD_DATE 업데이트
  121. $mappingModel = new VendorInfluencerMappingModel();
  122. $mappingModel->update($mappingSeq, ['MOD_DATE' => date('Y-m-d H:i:s')]);
  123. $db->transComplete();
  124. if ($db->transStatus() === false) {
  125. throw new \Exception('상태 변경 트랜잭션 실패');
  126. }
  127. return $result;
  128. } catch (\Exception $e) {
  129. $db->transRollback();
  130. log_message('error', '상태 변경 실패: ' . $e->getMessage());
  131. throw $e;
  132. }
  133. }
  134. /**
  135. * 특정 상태의 매핑 목록 조회
  136. */
  137. public function getMappingsByStatus($status, $isActive = true)
  138. {
  139. $builder = $this->builder();
  140. $builder->select('VENDOR_INFLUENCER_STATUS_HISTORY.*, VENDOR_INFLUENCER_MAPPING.*')
  141. ->join('VENDOR_INFLUENCER_MAPPING',
  142. 'VENDOR_INFLUENCER_MAPPING.SEQ = VENDOR_INFLUENCER_STATUS_HISTORY.MAPPING_SEQ')
  143. ->where('VENDOR_INFLUENCER_STATUS_HISTORY.STATUS', $status)
  144. ->where('VENDOR_INFLUENCER_STATUS_HISTORY.IS_CURRENT', 'Y');
  145. if ($isActive) {
  146. $builder->where('VENDOR_INFLUENCER_MAPPING.IS_ACT', 'Y');
  147. }
  148. return $builder->get()->getResultArray();
  149. }
  150. /**
  151. * 벤더사별 상태 통계
  152. */
  153. public function getStatusStatsByVendor($vendorSeq)
  154. {
  155. $builder = $this->builder();
  156. return $builder->select('VENDOR_INFLUENCER_STATUS_HISTORY.STATUS, COUNT(*) as count')
  157. ->join('VENDOR_INFLUENCER_MAPPING',
  158. 'VENDOR_INFLUENCER_MAPPING.SEQ = VENDOR_INFLUENCER_STATUS_HISTORY.MAPPING_SEQ')
  159. ->where('VENDOR_INFLUENCER_MAPPING.VENDOR_SEQ', $vendorSeq)
  160. ->where('VENDOR_INFLUENCER_STATUS_HISTORY.IS_CURRENT', 'Y')
  161. ->where('VENDOR_INFLUENCER_MAPPING.IS_ACT', 'Y')
  162. ->groupBy('VENDOR_INFLUENCER_STATUS_HISTORY.STATUS')
  163. ->get()
  164. ->getResultArray();
  165. }
  166. /**
  167. * 인플루언서별 상태 통계
  168. */
  169. public function getStatusStatsByInfluencer($influencerSeq)
  170. {
  171. $builder = $this->builder();
  172. return $builder->select('VENDOR_INFLUENCER_STATUS_HISTORY.STATUS, COUNT(*) as count')
  173. ->join('VENDOR_INFLUENCER_MAPPING',
  174. 'VENDOR_INFLUENCER_MAPPING.SEQ = VENDOR_INFLUENCER_STATUS_HISTORY.MAPPING_SEQ')
  175. ->where('VENDOR_INFLUENCER_MAPPING.INFLUENCER_SEQ', $influencerSeq)
  176. ->where('VENDOR_INFLUENCER_STATUS_HISTORY.IS_CURRENT', 'Y')
  177. ->where('VENDOR_INFLUENCER_MAPPING.IS_ACT', 'Y')
  178. ->groupBy('VENDOR_INFLUENCER_STATUS_HISTORY.STATUS')
  179. ->get()
  180. ->getResultArray();
  181. }
  182. }