FeedMockPushJob.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Jobs;
  3. use App\Jobs\GrowingIO\FeedMockReportJob;
  4. use App\Models\Log\FeedLogModel;
  5. use App\Models\PartnerModel;
  6. use App\Models\User\UserModel;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Database\Eloquent\Collection;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. use Illuminate\Support\Facades\Redis;
  14. /**
  15. * feed流mock数据
  16. * Class FeedMockPushJob
  17. * @package App\Jobs
  18. */
  19. class FeedMockPushJob implements ShouldQueue
  20. {
  21. use Dispatchable;
  22. use InteractsWithQueue;
  23. use Queueable;
  24. use SerializesModels;
  25. private $uid;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @param int $uid
  30. */
  31. public function __construct(int $uid)
  32. {
  33. $this->uid = $uid;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. $user = UserModel::findOrFail($this->uid);
  43. /** @var UserModel $user */
  44. // 滑动的性别
  45. if (in_array($user->feed_sex, [1, 2])) {
  46. $sxo = $user->feed_sex;
  47. } else {
  48. $sxo = 2;
  49. }
  50. $partner_id = Redis::lpop("feed:mock:sex:{$sxo}");
  51. if ($partner_id) {
  52. $partner = PartnerModel::whereId($partner_id)->orderBy('last_three_day_feed', 'ASC')->first([
  53. 'id',
  54. 'last_three_day_feed'
  55. ]);
  56. } else {
  57. if (Redis::exists("feed:mock:lock:{$sxo}")) {
  58. $partner = null;
  59. } else {
  60. $partners = PartnerModel::where([['is_sell', 1], ['sex', $sxo]])->orderBy(
  61. 'last_three_day_feed',
  62. 'ASC'
  63. )->take(500)->get(['id', 'last_three_day_feed']);
  64. $partner = $partners->pop();
  65. foreach ($partners as $value) {
  66. /** @var PartnerModel $value */
  67. Redis::rpush("feed:mock:sex:{$sxo}", [$value->id]);
  68. }
  69. }
  70. }
  71. if ($partner) {
  72. FeedLogModel::create([
  73. 'uid' => $this->uid,
  74. 'partner_id' => $partner->id,
  75. 'ismock' => 1
  76. ]);
  77. FeedMockReportJob::dispatch($this->uid, $partner->id, $partner->last_three_day_feed);
  78. } else {
  79. Redis::setex("feed:mock:lock:{$sxo}", 3600 * 5, true);
  80. }
  81. }
  82. }