UserApplyCertifyJob.php 1.42 KB
<?php

namespace App\Jobs;

use App\Models\SystemRole;
use App\Models\User;
use App\Models\UserCertify;
use App\Notifications\UserCertifyApplyNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Notification;

class UserApplyCertifyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public UserCertify $userCertify;

    public function __construct(UserCertify $userCertify)
    {
        $this->userCertify = $userCertify;
    }


    public function handle(): void
    {
        $roleIds = SystemRole::query()
            ->whereRelation('permissions', fn($query) => $query->where('guard', 'Admin')->where('system_permissions.name', 'user-certify-audit'))
            ->where('system_roles.status', 1)
            ->pluck('system_roles.id')->toArray();

        $users = User::query()
            ->selectRaw('users.id,users.nick_name,users.official_id,users.phone,users.area_code')
            ->join('user_has_roles', 'user_has_roles.user_id', 'users.id')
            ->whereIn('user_has_roles.role_id', $roleIds)
            ->where('users.status', 1)->where('users.official_status', 1)
            ->get();

        Notification::send($users, new UserCertifyApplyNotification($this->userCertify));
    }
}