NewUserPushMsg.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Jobs\Push;
  3. use App\Modules\Push\Models\QappNewUserPushTask;
  4. use App\Modules\Push\Models\QappNewUserPushTaskLog;
  5. use App\Modules\Push\Services\PushMessageService;
  6. use App\Modules\Trade\Models\Order;
  7. use App\Modules\User\Models\QappUserAddDestop;
  8. use App\Modules\User\Models\User;
  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. class NewUserPushMsg implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. const condition = [
  18. 'balance' => [
  19. ['name' => '低于500', 'value' => 'a', 'condition' => [0, 500]],
  20. ['name' => '500-2000', 'value' => 'b', 'condition' => [500, 2000]],
  21. ['name' => '2000-5000', 'value' => 'c', 'condition' => [2000, 5000]],
  22. ['name' => '5000以上', 'value' => 'd', 'condition' => [5000, 0]],
  23. ['name' => '不限', 'value' => 'z', 'condition' => []]
  24. ],
  25. 'paid' => [
  26. ['name' => '不限', 'value' => 'z', 'condition' => []],
  27. ['name' => '未付费', 'value' => 'a', 'condition' => ['unpaid']],
  28. ['name' => '已付费', 'value' => 'b', 'condition' => ['paid']],
  29. ['name' => 'VIP用户', 'value' => 'c', 'condition' => ['vip']],
  30. ],
  31. 'add_desktop' => [
  32. ['name' => '不限', 'value' => 'z', 'condition' => []],
  33. ['name' => '未加桌', 'value' => 'a', 'condition' => [0]],
  34. ['name' => '已加桌', 'value' => 'b', 'condition' => [1]],
  35. ]
  36. ];
  37. private $task;
  38. private $uid;
  39. /**
  40. * Create a new job instance.
  41. *
  42. * @return void
  43. */
  44. public function __construct(int $uid, QappNewUserPushTask $task)
  45. {
  46. $this->uid = $uid;
  47. $this->task = $task;
  48. }
  49. private function createLog(): QappNewUserPushTaskLog
  50. {
  51. return QappNewUserPushTaskLog::create([
  52. 'task_id' => $this->task->id,
  53. 'app_id' => '',
  54. 'uid' => $this->uid,
  55. 'status' => 1,
  56. ]);
  57. }
  58. /**
  59. * Execute the job.
  60. *
  61. * @return void
  62. */
  63. public function handle()
  64. {
  65. $log = $this->createLog();
  66. $filter = $this->fliterUser($this->task->push_filter, $this->uid);
  67. if ($filter) {
  68. $result = PushMessageService::pushMessageToUser($this->uid, $this->task->title, $this->task->content, $this->task->url);
  69. $log->push_time = now();
  70. if ($result) {
  71. $log->status = 3;
  72. $log->push_result = json_encode($result);
  73. } else {
  74. $log->status = 4;
  75. }
  76. } else {
  77. $log->status = 5;
  78. }
  79. $log->save();
  80. }
  81. private function findUserBalance(int $uid): int
  82. {
  83. $user = User::find($uid);
  84. return $user ? $user->balance : 0;
  85. }
  86. private function fliterUser(string $filter, int $uid)
  87. {
  88. $result = true;
  89. $conditions = json_decode($filter, true);
  90. if ($conditions && is_array($conditions)) {
  91. foreach ($conditions as $key => $value) {
  92. $condition = $this->getCondition($key, $value);
  93. if ($result && $condition) {
  94. switch ($key) {
  95. case 'balance':
  96. $balance = $this->findUserBalance($uid);
  97. $result = $condition[0] ? $balance >= $condition[0] && ($condition[1] ? $balance <= $condition[1] : true) : true;
  98. break;
  99. case 'paid':
  100. switch ($value) {
  101. case 'unpaid':
  102. $result = !Order::where('uid', $uid)->where('status', 'PAID')->exists();
  103. break;
  104. case 'paid':
  105. $result = Order::where('uid', $uid)->where('status', 'PAID')->exists();
  106. break;
  107. case 'vip':
  108. $result = Order::where('uid', $uid)->where('status', 'PAID')->where('order_type', 'YEAR')->exists();
  109. break;
  110. }
  111. break;
  112. case 'add_desktop':
  113. $result = ($value == QappUserAddDestop::where('uid', $uid)->where('status', 1)->exists());
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. return $result;
  120. }
  121. private function getCondition($key, $value)
  122. {
  123. if (array_key_exists($key, self::condition))
  124. foreach (self::condition[$key] as $item) {
  125. if ($item['value'] == $value) {
  126. return $item['condition'];
  127. }
  128. }
  129. return '';
  130. }
  131. }