VendorInfluencerMappingModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Models;
  3. use CodeIgniter\Model;
  4. class VendorInfluencerMappingModel extends Model
  5. {
  6. protected $table = 'VENDOR_INFLUENCER_MAPPING';
  7. protected $primaryKey = 'SEQ';
  8. protected $useAutoIncrement = true;
  9. protected $returnType = 'array';
  10. protected $useSoftDeletes = false;
  11. protected $allowedFields = [
  12. 'VENDOR_SEQ',
  13. 'INFLUENCER_SEQ',
  14. 'REQUEST_TYPE',
  15. 'STATUS',
  16. 'REQUEST_MESSAGE',
  17. 'RESPONSE_MESSAGE',
  18. 'REQUESTED_BY',
  19. 'APPROVED_BY',
  20. 'COMMISSION_RATE',
  21. 'SPECIAL_CONDITIONS',
  22. 'EXPIRED_DATE',
  23. 'REQUEST_DATE',
  24. 'RESPONSE_DATE',
  25. 'REG_DATE',
  26. 'MOD_DATE',
  27. 'IS_ACT'
  28. ];
  29. protected $useTimestamps = true;
  30. protected $createdField = 'REG_DATE';
  31. protected $updatedField = 'MOD_DATE';
  32. protected $validationRules = [
  33. 'VENDOR_SEQ' => 'required|integer',
  34. 'INFLUENCER_SEQ' => 'required|integer',
  35. 'REQUEST_TYPE' => 'required|in_list[INFLUENCER_REQUEST,VENDOR_REQUEST]',
  36. 'STATUS' => 'required|in_list[PENDING,APPROVED,REJECTED,CANCELLED,EXPIRED]',
  37. 'REQUESTED_BY' => 'required|integer',
  38. ];
  39. protected $validationMessages = [
  40. 'VENDOR_SEQ' => [
  41. 'required' => '벤더 SEQ는 필수입니다.',
  42. 'integer' => '벤더 SEQ는 숫자여야 합니다.'
  43. ],
  44. 'INFLUENCER_SEQ' => [
  45. 'required' => '인플루언서 SEQ는 필수입니다.',
  46. 'integer' => '인플루언서 SEQ는 숫자여야 합니다.'
  47. ],
  48. 'REQUEST_TYPE' => [
  49. 'required' => '요청 타입은 필수입니다.',
  50. 'in_list' => '유효하지 않은 요청 타입입니다.'
  51. ],
  52. 'STATUS' => [
  53. 'required' => '상태는 필수입니다.',
  54. 'in_list' => '유효하지 않은 상태입니다.'
  55. ],
  56. 'REQUESTED_BY' => [
  57. 'required' => '요청자는 필수입니다.',
  58. 'integer' => '요청자는 숫자여야 합니다.'
  59. ]
  60. ];
  61. protected $skipValidation = false;
  62. protected $cleanValidationRules = true;
  63. /**
  64. * 만료된 요청들을 처리
  65. */
  66. public function processExpiredRequests()
  67. {
  68. return $this->where('STATUS', 'PENDING')
  69. ->where('EXPIRED_DATE <', date('Y-m-d H:i:s'))
  70. ->where('IS_ACT', 'Y')
  71. ->set([
  72. 'STATUS' => 'EXPIRED',
  73. 'MOD_DATE' => date('Y-m-d H:i:s')
  74. ])
  75. ->update();
  76. }
  77. /**
  78. * 특정 벤더-인플루언서 조합의 활성 요청 확인
  79. */
  80. public function getActiveRequest($vendorSeq, $influencerSeq)
  81. {
  82. return $this->where('VENDOR_SEQ', $vendorSeq)
  83. ->where('INFLUENCER_SEQ', $influencerSeq)
  84. ->where('STATUS', 'PENDING')
  85. ->where('IS_ACT', 'Y')
  86. ->first();
  87. }
  88. /**
  89. * 사용자의 요청 목록 조회
  90. */
  91. public function getUserRequests($userSeq, $asInfluencer = true, $status = null)
  92. {
  93. $field = $asInfluencer ? 'INFLUENCER_SEQ' : 'VENDOR_SEQ';
  94. $builder = $this->where($field, $userSeq)
  95. ->where('IS_ACT', 'Y');
  96. if ($status) {
  97. $builder->where('STATUS', $status);
  98. }
  99. return $builder->findAll();
  100. }
  101. /**
  102. * 벤더사별 승인요청 통계
  103. */
  104. public function getVendorRequestStats($vendorSeq)
  105. {
  106. $builder = $this->where('VENDOR_SEQ', $vendorSeq)
  107. ->where('IS_ACT', 'Y');
  108. return [
  109. 'pending' => $builder->where('STATUS', 'PENDING')->countAllResults(false),
  110. 'approved' => $builder->where('STATUS', 'APPROVED')->countAllResults(false),
  111. 'rejected' => $builder->where('STATUS', 'REJECTED')->countAllResults(false),
  112. 'total' => $builder->countAllResults()
  113. ];
  114. }
  115. /**
  116. * 인플루언서별 요청 통계
  117. */
  118. public function getInfluencerRequestStats($influencerSeq)
  119. {
  120. $builder = $this->where('INFLUENCER_SEQ', $influencerSeq)
  121. ->where('IS_ACT', 'Y');
  122. return [
  123. 'pending' => $builder->where('STATUS', 'PENDING')->countAllResults(false),
  124. 'approved' => $builder->where('STATUS', 'APPROVED')->countAllResults(false),
  125. 'rejected' => $builder->where('STATUS', 'REJECTED')->countAllResults(false),
  126. 'total' => $builder->countAllResults()
  127. ];
  128. }
  129. }