params = $params; } /** * 生成用户裂变等级关系 * * @return string|void */ public function handle() { try { $this->makRepo(); $level = 1; //一级 $res = $this->relationRepo->create(['relation_uid' => $this->params['invite_id'], 'uid' => $this->params['invited_id'], 'relation_level' => $level]); if (!$res) { logger('CreateUserRelationJob', [$this->params]); return ''; } $this->generateSuperiorRelation($this->params, $level); } catch (\Exception $exception) { logger('CreateUserRelationJob', [$exception->getMessage()]); } } /** * make对象 */ private function makRepo() { $this->relationRepo = make(UserLevelRelationRepository::class); $this->userRepo = make(UserRepository::class); } /** * 递归生成用户关系等级(最多生成四级) * * @param $data * @param $level */ private function generateSuperiorRelation($data, &$level) { if ($level < 4) { $userParentUid = $this->userRepo->findBy('code', $data['invite_id'], 'parent_uid'); if (!empty($userParentUid['parent_uid'])) { $level++; $this->relationRepo->create(['relation_uid' => $userParentUid['parent_uid'], 'uid' => $data['invited_id'], 'relation_level' => $level]); $data['invite_id'] = $userParentUid['parent_uid']; $this->generateSuperiorRelation($data, $level); } } } }