SendDormantTableCommand.php 7.2 KB

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