EmailBatchJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Company;
  4. use App\Models\Member;
  5. use App\Models\EmailBatch;
  6. use App\Models\Email;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. class EmailBatchJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. /**
  16. * @var EmailBatch
  17. */
  18. private $EmailBatch;
  19. /**
  20. * EmailBatchJob constructor.
  21. * @param EmailBatch $EmailBatch
  22. */
  23. public function __construct(EmailBatch $EmailBatch)
  24. {
  25. $this->EmailBatch = $EmailBatch;
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $status = $this->getRuleStatus($this->EmailBatch->template->alias);
  35. if ($status) {
  36. if ($this->EmailBatch->emails) {
  37. $emails=explode(",", $this->EmailBatch->emails);
  38. foreach ($emails as $emails) {
  39. if (validator_check($emails, ['email'])) {
  40. $this->dispatchSms($emails);
  41. $this->EmailBatch->target_num++;
  42. }
  43. $this->EmailBatch->save();
  44. }
  45. } else {
  46. $emails=$this->getEmails($this->EmailBatch->accept_member, $this->EmailBatch->accept_type);
  47. if (!empty($emails)) {
  48. foreach ($emails as $emails) {
  49. if (validator_check($emails, ['email'])) {
  50. $this->dispatchSms($emails);
  51. $this->EmailBatch->target_num++;
  52. }
  53. $this->EmailBatch->save();
  54. }
  55. }
  56. }
  57. }
  58. if ($this->EmailBatch->target_num == 0) {
  59. $this->EmailBatch->status=1;
  60. $this->EmailBatch->save();
  61. }
  62. }
  63. protected function getEmails($accept_member, $accept_type)
  64. {
  65. $where=[];
  66. if ($accept_type) {
  67. $where[]=["last_login_time",'<',time()-($accept_type==1?7:30)*24*60*60];
  68. }
  69. $memberEmail=Member::where($where)->pluck('email');
  70. $companyEmail=Company::where($where)->pluck('email');
  71. if ($accept_member == 0) {
  72. return $memberEmail->concat($companyEmail);
  73. } else {
  74. return $accept_member==1?$companyEmail:$memberEmail;
  75. }
  76. }
  77. protected function dispatchSms($emails)
  78. {
  79. $emailJob=new EmailJob($emails, $this->EmailBatch->template->alias, [], []);
  80. $emailJob->setBatchId($this->EmailBatch->id);
  81. dispatch($emailJob);
  82. }
  83. protected function getRuleStatus($alias)
  84. {
  85. $result = Email::where(['alias'=>$alias])->first();
  86. if ($result) {
  87. if ($result->status) {
  88. return true;
  89. }
  90. return false;
  91. } else {
  92. return false;
  93. }
  94. }
  95. }