SendDormantTableCommand.php-- 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\CallApiService;
  4. use App\Services\DormantService;
  5. use Carbon\Carbon;
  6. use Carbon\CarbonImmutable;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Hash;
  9. class SendDormantTableCommand extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'dormant:send-table';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '휴면테이블로 보내기';
  23. private $callApiService;
  24. private $dormantService;
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct(CallApiService $callApiService, DormantService $dormantService)
  31. {
  32. parent::__construct();
  33. $this->callApiService = $callApiService;
  34. $this->dormantService = $dormantService;
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. $this->dormantService->setMainToken();
  44. $dormantDefault = $this->dormantService->getSetup();
  45. $dt = Carbon::now()->endOfDay();
  46. $dt->subMonths($dormantDefault['ActiveMonth']);
  47. $startOfDay = $dt->startOfDay()->timestamp;
  48. $endOfDay = $dt->endOfDay()->timestamp;
  49. $memberPage = $this->callApiService->callApi([
  50. 'url' => 'member-page',
  51. 'data' => [
  52. 'PageVars' => [
  53. 'Query' => "(last_login_on between $startOfDay and $endOfDay) and status != 5",
  54. 'Asc' => 'last_login_on',
  55. 'Limit' => 999999
  56. ]
  57. ],
  58. 'headers' => [
  59. 'GateToken' => $this->dormantService->getGateToken('main')
  60. ]
  61. ]);
  62. // dd($memberPage);
  63. // $a = collect($memberPage['Page'])->map(function ($member) {
  64. // return [$member['Email'], Carbon::createFromTimestamp($member['LastLoginOn'])->format('Y-m-d')];
  65. // });
  66. foreach ($memberPage['Page'] ?? [] as $member) {
  67. if (! $this->goToDormantAccount($member['Id'], $dormantDefault['GuestApp'])) {
  68. $now = CarbonImmutable::now()->toDateTimeLocalString();
  69. file_put_contents(storage_path('logs/sample.log'), $now . PHP_EOL, FILE_APPEND);
  70. }
  71. }
  72. return 0;
  73. }
  74. public function goToDormantAccount($id, $appName)
  75. {
  76. $memberDormantPick = $this->callApiService->callAppApi([
  77. 'url' => 'member-dormant-pick',
  78. 'data' => [
  79. 'Page' => [
  80. [ 'Id' => (int)$id ]
  81. ]
  82. ],
  83. ], $this->dormantService->getGateToken($appName), $appName);
  84. // 이미 휴면 상태이면 false 반환
  85. if (! $this->callApiService->verifyApiError($memberDormantPick)) {
  86. return false;
  87. }
  88. $memberPick = $this->callApiService->callApi([
  89. 'url' => 'member-pick',
  90. 'data' => [
  91. 'Page' => [
  92. [ 'Id' => (int)$id ]
  93. ]
  94. ],
  95. 'headers' => [
  96. 'GateToken' => $this->dormantService->getGateToken('main')
  97. ]
  98. ]);
  99. $member = $memberPick['Page'][0];
  100. $member['Status'] = '5';
  101. $member['SgroupIdRecom'] = $member['SgroupId'];
  102. unset($member['SgroupId']);
  103. $memberExtPick = $this->callApiService->callApi([
  104. 'url' => 'member-ext-pick',
  105. 'data' => [
  106. 'Page' => [
  107. [ 'Id' => (int)$id ]
  108. ]
  109. ],
  110. 'headers' => [
  111. 'GateToken' => $this->dormantService->getGateToken('main')
  112. ]
  113. ]);
  114. $page = array_merge($member, $memberExtPick['Page'][0]);
  115. $page['CreatedOn'] = Carbon::now()->timestamp;
  116. unset($page['UpdatedOn']);
  117. $memberDormantAct = $this->callApiService->callAppApi([
  118. 'url' => 'member-dormant-act',
  119. 'data' => [
  120. 'Page' => [ $page ]
  121. ],
  122. ], $this->dormantService->getGateToken($appName), $appName);
  123. if ($this->callApiService->verifyApiError($memberDormantAct)) {
  124. return false;
  125. }
  126. $this->initMemberExtDb((int)$member['Id']);
  127. $this->initMemberDb($member);
  128. }
  129. function initMemberExtDb($member_id)
  130. {
  131. $memberExtAct = $this->callApiService->callApi([
  132. 'url' => 'member-ext-act',
  133. 'data' => [
  134. 'Page' => [
  135. [ 'Id' => (int)$member_id, 'MobileNo' => '' ]
  136. ]
  137. ],
  138. 'headers' => [
  139. 'GateToken' => $this->dormantService->getGateToken('main')
  140. ]
  141. ]);
  142. }
  143. function initMemberDb($member)
  144. {
  145. $memberStructure = [
  146. 'LastSeenOn' => 'INT', 'LastLoginOn' => 'INT', 'MemberDate' => 'STRING', 'Email' => 'UNIQUE',
  147. 'LoginId' => 'STRING', 'SsoBrand' => 'STRING', 'SsoSub' => 'STRING',
  148. 'NickName' => 'STRING', 'FirstName' => 'STRING', 'SurName' => 'STRING', 'IsActivated' => 'CHAR', 'IsGuest' => 'CHAR',
  149. 'ProfileImg' => 'STRING', 'ProfileWeb' => 'STRING', 'ProfileText' => 'STRING', 'CreatedIp' => 'STRING', 'Sort' => 'CHAR', 'BuyerId' => 'INT',
  150. 'SgroupId' => 'INT', 'SgroupCode' => 'STRING', 'LastloginIp' => 'STRING', 'IsWithdrawn' => 'CHAR', 'SsoSubId' => 'INT',
  151. 'IsMemberApp' => 'CHAR'
  152. ];
  153. $memInitPage = ['Id' => (int)$member['Id'], 'Status' => '5'];
  154. foreach ($memberStructure as $key => $value) {
  155. switch ($value) {
  156. case 'UNIQUE':
  157. $memInitPage[$key] = base64_encode($member['Email']);
  158. break;
  159. case 'STRING':
  160. $memInitPage[$key] = '';
  161. break;
  162. case 'CHAR':
  163. $memInitPage[$key] = '0';
  164. break;
  165. case 'INT':
  166. $memInitPage[$key] = 0;
  167. break;
  168. }
  169. }
  170. $memberAct = $this->callApiService->callApi([
  171. 'url' => 'member-act',
  172. 'data' => [
  173. 'Page' => [ $memInitPage ]
  174. ],
  175. 'headers' => [
  176. 'GateToken' => $this->dormantService->getGateToken('main')
  177. ]
  178. ]);
  179. if ($this->callApiService->verifyApiError($memberAct)) {
  180. return false;
  181. }
  182. }
  183. }