MemberRelease.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\MemberChildren;
  12. use app\common\models\member\MemberParent;
  13. use app\common\models\member\ParentOfMemberBack;
  14. use app\common\models\MemberShopInfo;
  15. use app\common\models\Setting;
  16. use app\common\services\member\MemberRelation;
  17. use Carbon\Carbon;
  18. use Illuminate\Console\Command;
  19. use Illuminate\Support\Facades\DB;
  20. class MemberRelease extends Command
  21. {
  22. /**
  23. * The name and signature of the console command.
  24. *
  25. * @var string
  26. */
  27. protected $signature = 'member:release {uniacid}';
  28. /**
  29. * The console command description.
  30. *
  31. * @var string
  32. */
  33. protected $description = '导入会员关系';
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. $uniacid = $this->argument('uniacid');
  42. \YunShop::app()->uniacid = \Setting::$uniqueAccountId = $uniacid;
  43. $this->info('重置关系链开始时间:'.Carbon::now()->toDateTimeLocalString());
  44. $this->process();
  45. $this->info('重置关系链结束时间:'.Carbon::now()->toDateTimeLocalString());
  46. }
  47. private function process()
  48. {
  49. set_time_limit(-1);
  50. ini_set('memory_limit',-1);
  51. $parent_list = MemberShopInfo::pluck('parent_id','member_id')->toArray();
  52. DB::beginTransaction();
  53. try {
  54. MemberParent::uniacid()->delete();
  55. MemberChildren::uniacid()->delete();
  56. foreach (array_chunk($parent_list,10000,true) as $parent) {
  57. foreach ($parent as $member_id => $parent_id) {
  58. $current_parent_id = $parent_id;
  59. $i = 1;
  60. while ($current_parent_id != 0) {
  61. $member_parent[] = [
  62. 'member_id' => $member_id,
  63. 'parent_id' => $current_parent_id,
  64. 'level' => $i,
  65. 'uniacid' => \YunShop::app()->uniacid,
  66. ];
  67. $member_child[] = [
  68. 'member_id' => $current_parent_id,
  69. 'child_id' => $member_id,
  70. 'level' => $i,
  71. 'uniacid' => \YunShop::app()->uniacid,
  72. ];
  73. $current_parent_id = $parent_list[$current_parent_id];
  74. $i++;
  75. }
  76. }
  77. foreach (array_chunk($member_parent,10000) as $value) {
  78. MemberParent::insert($value);
  79. }
  80. foreach (array_chunk($member_child,10000) as $value) {
  81. MemberChildren::insert($value);
  82. }
  83. unset($member_parent);
  84. unset($member_child);
  85. }
  86. DB::commit();
  87. } catch (\Exception $e) {
  88. DB::rollBack();
  89. $this->info('重置关系链失败:'.$e->getMessage());
  90. }
  91. }
  92. }