<?php

namespace App\Http\Controllers\API\His;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use JWT;

class UserController extends Controller
{
    public function GetUserList(){
        $data = [];

        $His = new HisController();
        $res = $His::Get("查询人员列表", $data);
        $up_count=0;
        if ($res['code'] == 200) {
            $res_data = $res['data'];
            foreach ($res_data as $data_k => $data_v) {
                if( $data_v['emplCode']=='admin') continue;
                $user = DB::table('users')->where(['cas_code' => $data_v['emplCode']])->first();
                $dept = DB::table('s_department')->where(['department_number' => $data_v['deptCode']])->first();
                $group=null;
                if($data_v['posiCode']=='300A'){
                    $group=6;
                }else{
                    if($data_v['emplType']=='N'){ //护士
                        $group=3;
                    }
                    if($data_v['emplType']=='D'){ //医生
                        $group=7;
                    }
                }
                if (!$user) {
                    //插入用户表
                    $u= DB::table('users')->insert([
                        'cas_code' => $data_v['emplCode'],
                        'cn_name' => $data_v['emplName'],
                        'username' => $data_v['emplCode'],
                        'pwd' =>'$2y$10$HlgBHem3knR9JGcPWWxfhuj/oQzGouQaWQ8BIDbSxliJ39G6pOj/K',//默认密码：111111
                        'department_id' => isset($dept->id)?$dept->id:null,
                        'status' => 1,
                        'group' => $group,
                    ]);
                }else{
                    $u= DB::table('users')->where('id', $user->id)->update([
                        'cn_name' => $data_v['emplName'],
                        'department_id' => isset($dept->id)?$dept->id:null,
                        'group' => $user->group_locked==1?$user->group:$group,//如果分组被手动调整过，那就维持手动的分组不变
                    ]);
                }
                if($u){
                    $up_count++;
                }
            }
            return \Yz::JsonReturn(true,"调用His接口完成，更新".$up_count."条数据",['up_count'=>$up_count]);
        }
        return \Yz::JsonError("调用His接口失败");
    }

    //用于his 医生直接跳转过来，查看开单记录
    public function AutoLogin(){
        $usercode = request('usercode');
        $deptcode = request('deptcode');
        if(empty($usercode) || empty($deptcode)){
            return \Yz::JsonError("参数不全");
        }
        $dept=DB::table('s_department')->where(['department_number' => $deptcode])->first();
        if(!$dept) return \Yz::JsonError("科室不存在");

        $user=DB::table('users')->where('username', $usercode)->first();
        if(!$user) return \Yz::JsonError("无效用户");
        $u_user=DB::table('users')->where(['username' => $usercode])->update([
            'department_id'=>$dept->id,
            'updated_at'=>date('Y-m-d H:i:s')
        ]);
        $jwt= new JWT();
        $accessTimeout = $jwt -> GetGetSecretTimeOut();
        $refreshTimeout = $jwt -> GetRefreshTokenTimeOut();
        $access_token = $jwt->BuildJWT('yz','access',$user->id,$user->group,$accessTimeout);
        $refresh_token = $jwt->BuildJWT('yz','refresh',$user->id,'',$refreshTimeout);
        DB::table('users')->where(['id'=>$user->id,'status'=>1])->update(['token'=>md5($refresh_token)]);
        return \Yz::JsonReturn(true,'登录成功',['access_token'=>$access_token,'refresh_token'=>$refresh_token]);
    }
}
