* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Jobs\Manage; use App\Mail\NoticeMail; use App\Repositories\Enums\Manage\DealStatusEnum; use App\Repositories\Models\Base\Admin; use App\Repositories\Models\Base\UserMessage; use App\Repositories\Models\Manage\Message; use Carbon\Carbon; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Mail; class SendTimeoutMessageJob implements ShouldQueue { use InteractsWithQueue; use Queueable; private $Id; /** * Create a new job instance. */ public function __construct($Id, $deal_limit_day) { $this->Id = $Id; $this->delay(Carbon::now()->addDays($deal_limit_day)); } /** * 确定任务应该超时的时间 * * @return \DateTime */ public function retryUntil() { return Carbon::now()->addHours(24); } /** * Execute the job. */ public function handle() { $model = Message::query()->where('id', $this->Id)->whereIn('status', [DealStatusEnum::WAIT, DealStatusEnum::IN])->first(); if (!$model) return false; $user = Admin::query()->where('id', $model->deal_admin_id)->first(); if (!$user) { $user = Admin::query()->where('id', $model->import_admin_id)->first(); } if (!$user) return false; Cache::remember('job:SendTimeoutMessageJob:' . $this->Id, Carbon::now()->addHour(), function () use ($model, $user) { $body = "【{$model->name}】【{$model->mobile}】【$model->body】处理超时了"; $this->sendSiteMsg('超时提醒', $body, Message::class, $model->id, $user); $admins = Admin::query()->whereHas('roles', function ($query) { return $query->where('name', 'admin'); })->select(['id', 'name'])->get(); foreach ($admins as $admin) { $deal_department_name = optional($model->deal_department)->name ?? '未知'; $body = "【{$deal_department_name}】【{$model->name}】【{$model->mobile}】【$model->body】处理超时了"; $this->sendSiteMsg('超时提醒', $body, Message::class, $model->id, $admin); } }); } /** * 发送站内信 * @param $model * @param $users * @return void */ private function sendSiteMsg($name, $body, $source_type, $source_id, $user = [], $type = 1) { UserMessage::query()->create([ 'user_id' => $user->id, 'user_type' => get_class($user), 'name' => $name, 'body' => $body, 'type' => $type, 'source_type' => $source_type, 'source_id' => $source_id, ]); } /** * 发送邮件 * @param $model * @param $users * @return void */ private function sendMailMsg($model, $users = []) { foreach ($users as $user) { if (!empty($user->email)) { Mail::to($user['email'])->send(new NoticeMail($model->title, $model->body)); } } } }