NoticeJob.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Deed\InvitationCardModel;
  4. use App\Models\PartnerModel;
  5. use App\Models\PraiseModel;
  6. use App\Models\User\SuperLikeModel;
  7. use App\Models\User\UserModel;
  8. use App\Models\User\UserSysTagModel;
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Contracts\Queue\ShouldQueue;
  13. use Illuminate\Foundation\Bus\Dispatchable;
  14. /**
  15. * Class NoticeJob
  16. * @package App\Jobs
  17. */
  18. class NoticeJob implements ShouldQueue
  19. {
  20. use Dispatchable;
  21. use InteractsWithQueue;
  22. use Queueable;
  23. use SerializesModels;
  24. private $dlt = 0.0003;
  25. private $t2limit = 80;
  26. private $basescore = 60;
  27. private $minscore = 89;
  28. private $less60m_prescore = 3;
  29. // 累计超级心动分数
  30. private $total_superlike_prescore = 2;
  31. // 累计心动邀请分数
  32. private $total_invite_prescore = 4;
  33. // 累计心动分数
  34. private $total_thumb_prescore = 1;
  35. // 当前超级心动分数
  36. private $superlike_prescore = 20;
  37. // 当前心动邀请分数
  38. private $invite_prescore = 40;
  39. // 当前心动分数
  40. private $thumb_prescore = 5;
  41. private $uid;
  42. private $callback;
  43. private $args;
  44. /**
  45. * Create a new job instance.
  46. *
  47. * @param int $uid 通知用户
  48. * @param $callback
  49. * @param $args
  50. */
  51. public function __construct(int $uid, $callback, $args)
  52. {
  53. $this->uid = $uid;
  54. $this->callback = $callback;
  55. $this->args = $args;
  56. array_push($this->args, false);
  57. }
  58. /**
  59. * Execute the job.
  60. *
  61. * @return bool
  62. */
  63. public function handle()
  64. {
  65. sleep(3);
  66. /** @var UserSysTagModel $systag */
  67. $systag = UserSysTagModel::firstOrCreate(['uid' => $this->uid], [
  68. 'supvip_days' => 0,
  69. 'last_be_supvip_at' => 0,
  70. 'supvip_endat' => 0,
  71. 'last_send_notice_at' => 0,
  72. 'after_notice_less_h_into_cnt' => 0,
  73. 'notice_send_cnt' => 0
  74. ]);
  75. $last_send_notice_at = $systag->last_send_notice_at;
  76. 0 == $systag->last_send_notice_at && $systag->last_send_notice_at = time() - 18 * 3600;
  77. $user = UserModel::findOrFail($this->uid);
  78. $partner = PartnerModel::findOrFail($user->partner_id);
  79. if ($user->login_at < $last_send_notice_at && time() < $last_send_notice_at + 86400) {
  80. return true;
  81. }
  82. // 当前超级心动分数
  83. $superlike_cnt = SuperLikeModel::where([
  84. ['partner_id', $user->partner_id],
  85. ['created_at', '>', $user->login_at]
  86. ])->count();
  87. $superlike_score = $superlike_cnt * $this->superlike_prescore;
  88. // 当前心动邀请分数
  89. $invite_cnt = InvitationCardModel::where([
  90. ['invite_uid', $user->uid],
  91. ['created_at', '>', $user->login_at]
  92. ])->count();
  93. $invite_score = $invite_cnt * $this->invite_prescore;
  94. // 当前心动分数
  95. $thumb_cnt = PraiseModel::where([
  96. ['partner_id', $user->partner_id],
  97. ['create_at', '>', $user->login_at]
  98. ])->count();
  99. $thumb_score = $thumb_cnt * $this->thumb_prescore;
  100. $total_at = $last_send_notice_at;
  101. 0 == $total_at && $total_at = $user->login_at;
  102. // 累计超级心动分数
  103. $total_superlike_cnt = SuperLikeModel::where([
  104. ['partner_id', $user->partner_id],
  105. ['created_at', '<', $total_at]
  106. ])->count();
  107. $total_superlike_score = $total_superlike_cnt * $this->total_superlike_prescore;
  108. // 累计心动邀请分数
  109. $total_invite_cnt = InvitationCardModel::where([
  110. ['invite_uid', $user->uid],
  111. ['created_at', '<', $total_at]
  112. ])->count();
  113. $total_invite_score = $total_invite_cnt * $this->total_invite_prescore;
  114. // 累计心动分数
  115. $total_thumb_cnt = max($partner->praises - PraiseModel::where([
  116. ['partner_id', $user->partner_id],
  117. ['create_at', '>', $total_at]
  118. ])->count(), 0);
  119. $total_thumb_score = $total_thumb_cnt * $this->total_thumb_prescore;
  120. $sbf = ceil(min(
  121. $this->minscore,
  122. exp(-1 * $this->dlt * (time() - $partner->created_at) / 3600) * ($this->basescore + $total_superlike_score + $total_invite_score + $total_thumb_score - $systag->after_notice_less_h_into_cnt * $this->less60m_prescore)
  123. ));
  124. $now = ceil($superlike_score + $invite_score + $thumb_score + min(
  125. ceil((time() - $systag->last_send_notice_at) / 3600),
  126. 24
  127. ) * ($this->t2limit / 24));
  128. if ($now >= $sbf) {
  129. // 发送
  130. if (call_user_func_array($this->callback, $this->args)) {
  131. UserSysTagModel::where('uid', $user->uid)->update([
  132. 'last_send_notice_at' => time(),
  133. 'notice_send_cnt' => $systag->notice_send_cnt + 1
  134. ]);
  135. }
  136. return true;
  137. } else {
  138. return true;
  139. }
  140. }
  141. }