SendEmailsCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Events\DormantAccountNotify;
  4. use App\Services\CallApiService;
  5. use App\Services\DormantService;
  6. use Carbon\Carbon;
  7. use Carbon\CarbonImmutable;
  8. use Illuminate\Console\Command;
  9. class SendEmailsCommand extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'dormant:send-email';
  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(): int
  42. {
  43. $now = CarbonImmutable::now()->toDateTimeLocalString();
  44. file_put_contents(storage_path('logs/sample.log'), $now . PHP_EOL, FILE_APPEND);
  45. $this->dormantService->setMainToken();
  46. $dormantDefault = $this->dormantService->getSetup();
  47. $formDt = Carbon::now()->startOfDay();
  48. $toDt = Carbon::now()->endOfDay();
  49. $toDt->subMonths($dormantDefault['ActiveMonth'])->addDays($dormantDefault['AdvancedNoticeDays']);
  50. $formDt->subMonths($dormantDefault['ActiveMonth']);
  51. $memberPage = $this->callApiService->callApi([
  52. 'url' => 'member-page',
  53. 'data' => [
  54. 'PageVars' => [
  55. 'Query' => "(last_login_on between $formDt->timestamp and $toDt->timestamp) and dormant_mail_on = 0",
  56. 'Asc' => 'last_login_on',
  57. 'Limit' => 999999
  58. ]
  59. ],
  60. 'headers' => [
  61. 'GateToken' => $this->dormantService->getGateToken('main')
  62. ]
  63. ]);
  64. $memberList = collect($memberPage['Page'])->map(function ($member) {
  65. $member['LastLoginAt'] = Carbon::createFromTimestamp($member['LastLoginOn']);
  66. return $member;
  67. });
  68. // $dormantList = [];
  69. // while ($formDt->timestamp <= $toDt->timestamp) {
  70. // $dormantList[] = collect($memberList)->filter(function ($member) use ($formDt) {
  71. // return $member['LastLoginAt']->format('Y-m-d') === $formDt->format('Y-m-d');
  72. // })->toArray();
  73. //
  74. // $formDt->addDays($dormantDefault['NoticeIntervalDays']);
  75. // }
  76. //
  77. // $dormantList = collect($dormantList)->collapse()->toArray();
  78. $dormantList = $memberList->toArray();
  79. $result = [];
  80. // dd($dormantList);
  81. foreach ($dormantList ?? [] as $member) {
  82. $result[] = event(new DormantAccountNotify([
  83. 'email' => $member['Email'],
  84. 'due_date' => Carbon::createFromTimestamp($member['LastLoginOn'])->addMonths($dormantDefault['ActiveMonth'])->format('Y-m-d'),
  85. ]));
  86. }
  87. $mailSendList = collect($result)->collapse()->collapse()->toArray();
  88. $mailSendPage = collect($dormantList)->map(function ($member) use ($mailSendList) {
  89. if ($mailSendList[$member['Email']]) {
  90. $dormantMailOn = Carbon::now()->timestamp;
  91. } else {
  92. $dormantMailOn = -1;
  93. }
  94. return ['Id' => $member['Id'], 'DormantMailOn' => $dormantMailOn];
  95. })->toArray();
  96. if (empty($mailSendPage)) {
  97. return 0;
  98. }
  99. $memberAct = $this->callApiService->callApi([
  100. 'url' => 'member-act',
  101. 'data' => [
  102. 'Page' => $mailSendPage
  103. ],
  104. 'headers' => [
  105. 'GateToken' => $this->dormantService->getGateToken('main')
  106. ]
  107. ]);
  108. return 0;
  109. }
  110. }