123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Jobs;
- use App\Models\Company;
- use App\Models\Member;
- use App\Models\EmailBatch;
- use App\Models\Email;
- use Illuminate\Bus\Queueable;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- class EmailBatchJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * @var EmailBatch
- */
- private $EmailBatch;
- /**
- * EmailBatchJob constructor.
- * @param EmailBatch $EmailBatch
- */
- public function __construct(EmailBatch $EmailBatch)
- {
- $this->EmailBatch = $EmailBatch;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $status = $this->getRuleStatus($this->EmailBatch->template->alias);
- if ($status) {
- if ($this->EmailBatch->emails) {
- $emails=explode(",", $this->EmailBatch->emails);
- foreach ($emails as $emails) {
- if (validator_check($emails, ['email'])) {
- $this->dispatchSms($emails);
- $this->EmailBatch->target_num++;
- }
- $this->EmailBatch->save();
- }
- } else {
- $emails=$this->getEmails($this->EmailBatch->accept_member, $this->EmailBatch->accept_type);
- if (!empty($emails)) {
- foreach ($emails as $emails) {
- if (validator_check($emails, ['email'])) {
- $this->dispatchSms($emails);
- $this->EmailBatch->target_num++;
- }
- $this->EmailBatch->save();
- }
- }
- }
- }
- if ($this->EmailBatch->target_num == 0) {
- $this->EmailBatch->status=1;
- $this->EmailBatch->save();
- }
- }
- protected function getEmails($accept_member, $accept_type)
- {
- $where=[];
- if ($accept_type) {
- $where[]=["last_login_time",'<',time()-($accept_type==1?7:30)*24*60*60];
- }
- $memberEmail=Member::where($where)->pluck('email');
- $companyEmail=Company::where($where)->pluck('email');
- if ($accept_member == 0) {
- return $memberEmail->concat($companyEmail);
- } else {
- return $accept_member==1?$companyEmail:$memberEmail;
- }
- }
- protected function dispatchSms($emails)
- {
- $emailJob=new EmailJob($emails, $this->EmailBatch->template->alias, [], []);
- $emailJob->setBatchId($this->EmailBatch->id);
- dispatch($emailJob);
- }
- protected function getRuleStatus($alias)
- {
- $result = Email::where(['alias'=>$alias])->first();
- if ($result) {
- if ($result->status) {
- return true;
- }
- return false;
- } else {
- return false;
- }
- }
- }
|