<?php

namespace App\Models;

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

class ServiceRoleOrder extends BaseModel
{
    use HasFactory;

    public function role()
    {
        return $this->hasOne(ServiceRole::class, 'id', 'role_id');
    }

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

    public function reappraises()
    {
        return $this->hasMany(Reappraise::class, 'role_order_id', 'id');
    }

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