DeleteDormantMemberCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\CallApiService;
  4. use App\Services\DormantService;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Str;
  9. class DeleteDormantMemberCommand extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'dormant:delete-member';
  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['DormantMonth']);
  47. $appName = $dormantDefault['GuestApp'];
  48. $memberDormantPage = $this->callApiService->callAppApi([
  49. 'url' => 'member-dormant-page',
  50. 'data' => [
  51. 'PageVars' => [
  52. 'Query' => "created_on <= $dt->timestamp",
  53. 'Limit' => 999999
  54. ]
  55. ],
  56. ], $this->dormantService->getGateToken($appName), $appName);
  57. if ($this->callApiService->verifyApiError($memberDormantPage)) {
  58. return 0;
  59. }
  60. $actData = [];
  61. foreach ($memberDormantPage['Page'] ?? [] as $memberDormant) {
  62. sleep($dormantDefault['SchedulerSleepSecond']);
  63. $actData['member-dormant-act'][] = [ 'Id' => $memberDormant['Id'] * -1 ];
  64. $member = $this->callApiService->callApi([
  65. 'url' => 'member-pick',
  66. 'data' => [
  67. 'Page' => [
  68. [ 'Id' => $memberDormant['Id'] ]
  69. ]
  70. ],
  71. 'headers' => [
  72. 'GateToken' => $this->dormantService->getGateToken('main')
  73. ]
  74. ])['Page'][0];
  75. $actData['member-act'][] = [
  76. 'Id' => $memberDormant['Id'],
  77. 'Email' => 'Unknown-' . Str::of( Hash::make($member['Email'] . $member['Id'] . $member['ActivateCode']) )
  78. ->after('$2y$10$')->limit(10, ''),
  79. 'Status' => '6'
  80. ];
  81. }
  82. if (empty($actData)) {
  83. return 0;
  84. }
  85. if (! $this->useActApi($actData, $dormantDefault['GuestApp'])) {
  86. return 0;
  87. }
  88. return 0;
  89. }
  90. public function useActApi($actData, $appName)
  91. {
  92. $memberDormantAct = $this->callApiService->callAppApi([
  93. 'url' => 'member-dormant-act',
  94. 'data' => [
  95. 'Page' => $actData['member-dormant-act']
  96. ],
  97. ], $this->dormantService->getGateToken($appName), $appName);
  98. if ($this->callApiService->verifyApiError($memberDormantAct)) {
  99. return false;
  100. }
  101. $memberAct = $this->callApiService->callApi([
  102. 'url' => 'member-act',
  103. 'data' => [
  104. 'Page' => $actData['member-act']
  105. ],
  106. 'headers' => [
  107. 'GateToken' => $this->dormantService->getGateToken('main')
  108. ]
  109. ]);
  110. if ($this->callApiService->verifyApiError($memberAct)) {
  111. return false;
  112. }
  113. return true;
  114. }
  115. }