<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ServiceRoleUser extends BaseModel
{
    use HasFactory;

    public function serviceUser()
    {
        return $this->hasOne(ServiceUser::class, 'user_id', 'user_id');
    }

    public function insertRoleUserData($service_user,$role_id){
        $insertRoleUserData = [];
        $insertRoleUserData['created_at'] = $insertRoleUserData['updated_at'] = date('Y-m-d H:i:s');
        $roleUser = self::where('user_id',$service_user->user_id)
            ->where('role_id',$role_id)
            ->exists();
        if(!$roleUser){
            $insertRoleUserData['user_id'] = $service_user->user_id??0;
            $insertRoleUserData['role_id'] = $role_id??0;
            $insertRoleUserData['s_user_id'] = $service_user->id;
            self::insert($insertRoleUserData);
        }
    }
}
