<?php

namespace App\Models;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;


class ProjectUser extends BaseModel
{
    public function projectRole()
    {
        return $this->belongsTo('App\Models\ProjectRole', 'project_role_id');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }

    public function project()
    {
        return $this->belongsTo('App\Models\Project', 'project_id');
    }

    public function getValidator(Request $request, $type)
    {
        if($type == 'store') {
            $validator = Validator::make($request->input('data'), [
                'user_id' => 'required'
            ], [
                'user_id.required' => '用户必选'
            ]);
        } else {
            $validator = Validator::make($request->input('data'), [
                'project_role_id' => 'required'
            ], [
                'project_role_id.required' => '角色必选'
            ]);
        }

        return $validator;
    }
    public static function getProjectManagerUser(){
        $user_list = DB::table('project_users')->select('user_id')->where('project_role_id','=',4)->distinct()->get()->toArray();
        $list = json_encode($user_list,true);
        $new_user_list = json_decode($list,true);
        foreach ($new_user_list as &$value){
            $name = User::where('id',$value['user_id'])->value('name');
            $value['name'] = $name;
        }
        return $new_user_list;
    }
}
