FeedRecommendJob.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\PartnerModel;
  4. use App\Models\User\UserModel;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Queue\SerializesModels;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Support\Facades\Redis;
  11. class FeedRecommendJob implements ShouldQueue
  12. {
  13. use Dispatchable;
  14. use InteractsWithQueue;
  15. use Queueable;
  16. use SerializesModels;
  17. private $uid;
  18. /**
  19. * Create a new job instance.
  20. *
  21. * @param int $uid
  22. */
  23. public function __construct(int $uid)
  24. {
  25. $this->uid = $uid;
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $uid = $this->uid;
  35. try {
  36. if (Redis::exists("{fpdx:feed:recommend:{$uid}}")) {
  37. return;
  38. }
  39. $user = UserModel::findOrFail($uid);
  40. $hideList = Redis::zrange("charge_feed_{$user->uid}", 0, -1);
  41. empty($hideList) && $hideList = array();
  42. $last_login = time() - 86000 * 2;
  43. // 获取精选卡片用户
  44. $where = array(
  45. ['sex', $user->feed_sex],
  46. ['is_recommend', 1],
  47. ['is_sell', 1],
  48. ['is_push_feed', 1],
  49. ['score', '>', 0],
  50. ['login_at', '>', $last_login]
  51. );
  52. $partners = PartnerModel::where($where)->whereNotIn('id', $hideList)->get();
  53. $recommenduids = collect($partners)->pluck('uid')->toArray();
  54. $uids = $recommenduids;
  55. // 用户按距离排序
  56. if ($user->feed_sex == 1) {
  57. $key = "fpdx:user:locations:sell:boy";
  58. } else {
  59. $key = "fpdx:user:locations:sell:girl";
  60. }
  61. $redis_uids = Redis::geoRadius($key, $user->lng, $user->lat, 500, "km", array('ASC'));
  62. $whereuids = array_slice(array_intersect($redis_uids, $uids), 0, 100);
  63. // 获取uid-卡片id集合
  64. $p2uids = PartnerModel::whereIn('uid', $whereuids)->get(['id', 'uid']);
  65. $uid2ids = array();
  66. array_map(function ($value) use (&$uid2ids) {
  67. $uid2ids[$value['uid']] = $value['id'];
  68. }, collect($p2uids)->toArray());
  69. array_map(function ($value) use ($uid2ids, $uid) {
  70. Redis::rpush("{fpdx:feed:recommend:{$uid}}", $uid2ids[$value]);
  71. }, $whereuids);
  72. Redis::expire("{fpdx:feed:recommend:{$uid}}", mktime(0, 0, 0) + 86000 - time());
  73. } catch (\Exception $exception) {
  74. app('sentry')->captureException($exception);
  75. Redis::hset("session_msy_{$uid}", "init_recommend_queues", 0);
  76. }
  77. }
  78. }