ThumbJob.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\PartnerModel;
  4. use App\Models\PraiseModel;
  5. use App\Models\User\UserModel;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Support\Facades\Redis;
  12. class ThumbJob implements ShouldQueue
  13. {
  14. use Dispatchable;
  15. use InteractsWithQueue;
  16. use Queueable;
  17. use SerializesModels;
  18. private $uid;
  19. private $partner_id;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @param int $uid
  24. * @param int $partner_id
  25. */
  26. public function __construct(int $uid, int $partner_id)
  27. {
  28. $this->uid = $uid;
  29. $this->partner_id = $partner_id;
  30. }
  31. /**
  32. * Execute the job.
  33. *
  34. * @return void
  35. */
  36. public function handle()
  37. {
  38. try {
  39. sleep(5);
  40. // 点赞者
  41. $user = UserModel::findOrFail($this->uid);
  42. // 被点赞者
  43. $puser = UserModel::where('partner_id', $this->partner_id)->firstOrFail();
  44. // 被点赞者被点赞池
  45. $datas = Redis::zrange("fpdx:feed:thumb:{$puser->uid}", 0, -1);
  46. foreach ($datas as $data) {
  47. try {
  48. // 点赞者卡片
  49. $partner = PartnerModel::findOrFail($data);
  50. $thumb = PraiseModel::where([
  51. ['uid', $partner->uid],
  52. ['partner_id', $puser->partner_id]
  53. ])->firstOrFail();
  54. $t = (time() - $thumb->created_at) / 3600 + 1;
  55. $score = 100 * exp(floatval(-1 / ($partner->praises * $t + 1))) * 0.5;
  56. Redis::zadd("fpdx:feed:thumb:{$puser->uid}", $score, $data);
  57. Redis::expire("fpdx:feed:thumb:{$puser->uid}", 86400 * 30);
  58. } catch (\Exception $exception) {
  59. }
  60. }
  61. // 点赞者卡片
  62. $partner = PartnerModel::findOrFail($user->partner_id);
  63. $score = ceil(100 * exp(floatval(-1 / ($partner->praises + 1))) * 0.5);
  64. Redis::zadd("fpdx:feed:thumb:{$puser->uid}", $score, $user->partner_id);
  65. Redis::expire("fpdx:feed:thumb:{$puser->uid}", 86400 * 30);
  66. } catch (\Exception $exception) {
  67. app('sentry')->captureException($exception);
  68. }
  69. }
  70. }