| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- declare(strict_types=1);
- namespace App\Jobs;
- use App\Repositories\UserLevelRelationRepository;
- use App\Repositories\UserRepository;
- use Hyperf\AsyncQueue\Job;
- class CreateUserRelationJob extends Job
- {
- public $params;
- private $relationRepo;
- private $userRepo;
- public function __construct($params)
- {
- // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
- $this->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);
- }
- }
- }
- }
|