MemberRelease.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2020/9/10
  6. * Time: 6:19 PM
  7. */
  8. namespace app\console\Commands;
  9. use app\backend\modules\member\models\Member;
  10. use app\common\models\member\ChildrenOfMemberBack;
  11. use app\common\models\member\ParentOfMemberBack;
  12. use app\common\services\member\MemberRelation;
  13. use Illuminate\Console\Command;
  14. class MemberRelease extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'member:release {uniacid}';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = '导入会员关系';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. $this->info('=========start=========');
  45. $this->process();
  46. $this->info('=========end=========');
  47. }
  48. public function t1()
  49. {
  50. $bar = $this->output->createProgressBar(1000);
  51. $bar->setFormat(" %elapsed:6s%/%estimated:-6s% 内存消耗: %memory:6s%\n%current%/%max% [%bar%] %percent:3s%%");
  52. for ($i=1; $i<=1000; $i++){
  53. usleep(5000);
  54. $bar->advance();
  55. }
  56. $bar->finish();
  57. echo "\n";
  58. }
  59. public function process()
  60. {
  61. $this->info('=========公众号=========' . $this->argument('uniacid'));
  62. $parentMemberModle = new ParentOfMemberBack();
  63. $childMemberModel = new ChildrenOfMemberBack();
  64. $parentMemberModle->DeletedData($this->argument('uniacid'));
  65. $childMemberModel->DeletedData($this->argument('uniacid'));
  66. $pageSize = 2000;
  67. $total = Member::getAllMembersInfosByQueue($this->argument('uniacid'))->distinct()->count();
  68. $total_page = ceil($total/$pageSize);
  69. $this->info('------total-----' . $total);
  70. $this->info('------total_page-----' . $total_page);
  71. for ($curr_page = 1; $curr_page <= $total_page; $curr_page++) {
  72. $this->info('------curr_page-----' . $curr_page);
  73. \Log::info('--------record------', $curr_page);
  74. $offset = ($curr_page - 1) * $pageSize;
  75. $this->synRun($this->argument('uniacid'), $parentMemberModle, $childMemberModel, $pageSize, $offset);
  76. }
  77. }
  78. /**
  79. * @param $uniacid
  80. */
  81. public function synRun($uniacid, $parentMemberModle, $childMemberModel, $pageSize, $offset)
  82. {
  83. $memberModel = new Member();
  84. $memberModel->_allNodes = collect([]);
  85. $memberInfo = $memberModel->getTreeAllNodes($uniacid);
  86. if ($memberInfo->isEmpty()) {
  87. $this->info('----is empty-----');
  88. return;
  89. }
  90. foreach ($memberInfo as $item) {
  91. $memberModel->_allNodes->put($item->member_id, $item);
  92. }
  93. $member_info = Member::getAllMembersInfosByQueue($uniacid, $pageSize,
  94. $offset)->distinct()->get();
  95. $this->info('------queue member count-----' . $member_info->count());
  96. if (!$member_info->isEmpty()) {
  97. $this->info('-----queue member empty-----');
  98. }
  99. $this->info('--------queue synRun -----');
  100. foreach ($member_info as $key => $val) {
  101. $attr = [];
  102. $child_attr = [];
  103. $this->info('--------foreach start------' . $val->member_id);
  104. $memberModel->filter = [];
  105. $data = $memberModel->getNodeParents($uniacid, $val->member_id);
  106. if (!$data->isEmpty()) {
  107. $this->info('--------insert init------');
  108. foreach ($data as $k => $v) {
  109. if ($k != $val->member_id) {
  110. $attr[] = [
  111. 'uniacid' => $uniacid,
  112. 'parent_id' => $k,
  113. 'level' => $v['depth'] + 1,
  114. 'member_id' => $val->member_id,
  115. 'created_at' => time()
  116. ];
  117. $child_attr[] = [
  118. 'uniacid' => $uniacid,
  119. 'child_id' => $val->member_id,
  120. 'level' => $v['depth'] + 1,
  121. 'member_id' => $k,
  122. 'created_at' => time()
  123. ];
  124. } else {
  125. file_put_contents(storage_path("logs/" . date('Y-m-d') . "_batchparent.log"),
  126. print_r([$val->member_id, $v, 'insert'], 1), FILE_APPEND);
  127. }
  128. }
  129. $parentMemberModle->createData($attr);
  130. $childMemberModel->createData($child_attr);
  131. }
  132. }
  133. }
  134. }