user_id = $user_id; } /** * Execute the job. * * @return void */ public function handle() { try { $user = User::find($this->user_id); if (empty($user)) throw new \Exception("匹配用户不存在", 1); //是否已有大数据 $count = RecommendLinkingNew::where('id_users_left', $user->id)->count(); if ($count) { return false; } $i = 0; $un_user_ids = [$user->id]; while ($i == 0) { //获取最符合的用户 $other_user = $this->getConformUser($user, $un_user_ids); if (empty($other_user)) { return false; } $un_user_ids[] = $other_user->id; //获取他的大数据匹配 大于150分 $data = $this->getLinkData($other_user, $user); if (empty($data) && !is_array($data)) throw new \Exception("获取他的大数据匹配失败", 1); if (!count($data)) { continue; } $i++; } //复制大数据匹配 RecommendLinkingNew::insert($data); } catch (\Exception $e) { $this->getError($e); return false; } } public function getBase($user, $un_user_ids=[]) { $base_query = User::whereNotIn('id', $un_user_ids)->where('belief', $user->belief)->where('type', $user->type)->where('sex', $user->sex)->where('hidden_profile', 'NONE'); return $base_query; } public function getSameCity($user, $un_user_ids=[]) { $base_query = $this->getBase($user, $un_user_ids); //同城市 $base_city_query = $base_query->whereHas('profileCourtship', function($sql) use($user){ $sql->where(['province'=>$user->profileCourtship->province, 'city'=>$user->profileCourtship->city]); }); return $base_city_query; } public function getSameBirthday($user, $un_user_ids=[]) { $base_city_query = $this->getSameCity($user, $un_user_ids); $other_user = $base_city_query->whereHas('profileCourtship', function($sql) use($user){ $sql->where('birthday', $user->profileCourtship->birthday); })->first(); return $other_user; } public function getNearBirthday($user, $un_user_ids=[]) { try { $base_city_query = $this->getSameCity($user, $un_user_ids); $max_birthday = date('Y-m-d', strtotime('-2 year', strtotime($user->profileCourtship->birthday))); $min_birthday = date('Y-m-d', strtotime('+2 year', strtotime($user->profileCourtship->birthday))); $base_city_query = $base_city_query->whereHas('profileCourtship', function($sql) use($user, $max_birthday, $min_birthday){ $sql->where('birthday', '>=', $max_birthday)->where('birthday', '<=',$min_birthday); }); return $base_city_query->first(); } catch (\Exception $e) { $this->getError($e); return false; } } public function gerSameProvince($user, $un_user_ids=[]) { $base_query = $this->getBase($user, $un_user_ids); $base_province_query = $base_query->whereHas('profileCourtship', function($sql) use($user){ $sql->where(['province'=>$user->profileCourtship->province]); }); return $base_province_query; } public function getConformUser($user, $un_user_ids=[]) { try { if (empty($user->profileCourtship) || empty($user->profileCourtship->birthday) || empty($user->profileCourtship->province )|| empty($user->profileCourtship->city) || empty($user->sex) || empty($user->belief)) throw new \Exception("信息不全", 1); $base_city_query = $this->getSameCity($user, $un_user_ids); $other_user = $base_city_query->first(); if ($other_user) { //同年龄 $other_user = $this->getSameBirthday($user, $un_user_ids); if ($other_user) return $other_user; //相近年龄 $other_user = $this->getNearBirthday($user, $un_user_ids); if ($other_user) return $other_user; throw new \Exception("未找到同地区相似年龄", 1); }else{//同省份 $base_province_query = $this->gerSameProvince($user, $un_user_ids); $other_user = $base_province_query->first(); if ($other_user) { //同年龄 $other_user = $this->getSameBirthday($user, $un_user_ids); if ($other_user) return $other_user; //相近年龄 $other_user = $this->getNearBirthday($user, $un_user_ids); return $other_user; }else{ //同年龄 $other_user = $this->getSameBirthday($user, $un_user_ids); if ($other_user) return $other_user; //相近年龄 $other_user = $this->getNearBirthday($user, $un_user_ids); return $other_user; } } return false; } catch (\Exception $e) { $this->getError($e); return false; } } public function getLinkData($other_user, $user) { try { $result = RecommendLinkingNew::where('id_users_left', $other_user->id)->orderBy('score', 'desc')->orderBy('id','desc')->limit(1000)->get()->toArray(); foreach ($result as &$re) { $re['id_users_left'] = $user->id; $temp_num = 1000000000; $new_a = $re['id_users_right'] + $temp_num; $id = (string)$re['id_users_left'].substr($new_a, 1, 9); $re['id'] = $id; } return $result; } catch (\Exception $e) { $this->getError($e); return false; } } }