ModifyRelationJob.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingran
  5. * Date: 2018/7/27
  6. * Time: 下午4:40
  7. */
  8. namespace app\Jobs;
  9. use app\common\events\member\RegisterByAgent;
  10. use app\common\models\MemberShopInfo;
  11. use Illuminate\Bus\Queueable;
  12. use Illuminate\Contracts\Queue\ShouldQueue;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Queue\SerializesModels;
  15. use Yunshop\Commission\models\Agents;
  16. class ModifyRelationJob implements ShouldQueue
  17. {
  18. use InteractsWithQueue, Queueable, SerializesModels;
  19. /**
  20. * 修改关系里的会员id
  21. *
  22. * @var
  23. */
  24. private $uid;
  25. /**
  26. * 新的会员父级关系链
  27. * @var
  28. */
  29. private $member_relation;
  30. /**
  31. * 分销插件是否开启
  32. * @var
  33. */
  34. private $plugin_commission;
  35. /**
  36. * 当前公众号
  37. * @var
  38. */
  39. private $uniacid;
  40. public function __construct($uid, $member_relation, $plugin_commission)
  41. {
  42. $this->uid = $uid;
  43. $this->member_relation = $member_relation;
  44. $this->plugin_commission = $plugin_commission;
  45. }
  46. public function handle()
  47. {
  48. $level = count($this->member_relation);
  49. \Log::debug('-----------relation-----------', $this->member_relation);
  50. if ($level > 1) {
  51. $first_relation = $this->uid . ',' . $this->member_relation[0] . ',' . $this->member_relation[1];
  52. $second_relation = $this->uid . ',' . $this->member_relation[0];
  53. } else {
  54. if ($this->member_relation[0] != 0) {
  55. $first_relation = $this->uid . ',' . $this->member_relation[0];
  56. $second_relation = $this->uid;
  57. } else {
  58. $first_relation = $this->uid;
  59. $second_relation = $this->uid;
  60. }
  61. }
  62. $this->ChangeFirstMember($this->uid, $first_relation, $this->plugin_commission);
  63. $this->ChangeSecondMember($this->uid, $second_relation, $this->plugin_commission);
  64. }
  65. /**
  66. * @param $uid
  67. * @param $relation
  68. * @param $open_plugin
  69. */
  70. private function ChangeFirstMember($uid, $relation, $open_plugin)
  71. {
  72. \Log::debug('----------ChangeFirstMember uid-------', $uid);
  73. $memberModel = $this->getMemberModel($uid, 1);
  74. \Log::debug('----------ChangeFirstMember-------', count($memberModel));
  75. if (!$memberModel->isEmpty()) {
  76. foreach ($memberModel as $key => $model) {
  77. $model->relation = $relation;
  78. if ($model->save() && $open_plugin) {
  79. $this->changeAgentRelation($model);
  80. }
  81. }
  82. }
  83. }
  84. private function ChangeSecondMember($uid, $relation, $open_plugin)
  85. {
  86. \Log::debug('----------ChangeSecondMember uid-------', $uid);
  87. $memberModel = $this->getMemberModel($uid, 2);
  88. \Log::debug('----------ChangeSecondMember-------', count($memberModel));
  89. if (!$memberModel->isEmpty()) {
  90. foreach ($memberModel as $key => $model) {
  91. if ($model->parent_id !== 0) {
  92. $model->relation = $model->parent_id . ',' .$relation;
  93. if ($model->save() && $open_plugin) {
  94. $this->changeAgentRelation($model);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. private function getMemberModel($uid, $pos)
  101. {
  102. $memberModel = MemberShopInfo::getSubLevelMember($uid, $pos);
  103. return $memberModel;
  104. }
  105. private function changeAgentRelation($model)
  106. {
  107. $agents = Agents::getAgentByMemberId($model->member_id)->first();
  108. if (!is_null($agents)) {
  109. $agents->parent_id = $model->parent_id;
  110. $agents->parent = $model->relation;
  111. $agents->save();
  112. }
  113. $agent_data = [
  114. 'member_id' => $model->member_id,
  115. 'parent_id' => $model->parent_id,
  116. 'parent' => $model->relation
  117. ];
  118. // event(new RegisterByAgent($agent_data));
  119. }
  120. }