<?php

namespace App\Http\Controllers\API\His;

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

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();
                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' => $dept->id,
                        'status' => 1,
                    ]);
                }else{
                    $u= DB::table('users')->where('id', $user->id)->update([
                        'cn_name' => $data_v['emplName'],
                        'department_id' => $dept->id,
                    ]);
                }
                if($u){
                    $up_count++;
                }
            }
            return \Yz::JsonReturn(true,"调用His接口完成，更新".$up_count."条数据",['up_count'=>$up_count]);
        }
        return \Yz::JsonError("调用His接口失败");
    }
}
