<?php
namespace App\Services\Admin;
use Illuminate\Support\Facades\DB;
use App\Services\Login\LoginService;

class UserService
{
    public function GetInfoList($arr){
        $q=DB::select("select a.id, a.cn_name as cname,a.username as uname,a.status,a.created_at,b.group_name from users as a left join `group` as b on a.`group` =b.id where a.status in(0,1) order by id desc limit ?,? ",[($arr['page']-1)*$arr['pagesize'],$arr['pagesize']]);
        $count=DB::select("select count(1) as c from users as a left join `group` as b on a.`group` =b.id where a.status in(0,1)");
        $result['list']=$q;
        $result['count']=$count[0]->c;
        return $result;
    }
    public function Save($arr){
        if($arr['info']['id']){
            $c=DB::table('users')->where(['username'=>$arr['info']['uname']])->get();
            if(count($c)){
                $result['status']='no';
                $result['msg']='用户名已存在';
                return $result;
            }
            $query=DB::table('users')->where(['id'=>$arr['info']['id']])->update([
                'group' => $arr['info']['groupId'],
                'cn_name' => $arr['info']['cname'],
                'username' => $arr['info']['uname'],
                'status'=>$arr['info']['status'],
            ]);
            if($query){
                $result['status']='ok';
                $result['msg']='操作成功';
            }else{
                $result['status']='no';
                $result['msg']='操作失败';
            }
        }else{
            $hash = password_hash('B123465a', PASSWORD_DEFAULT);
            $c=DB::table('users')->where(['username'=>$arr['info']['uname']])->get();
            if(count($c)){
                $result['status']='no';
                $result['msg']='用户名已存在';
                return $result;
            }
            DB::beginTransaction();
            try {
                $id=DB::table('users')->insertGetId([
                    'group' => $arr['info']['groupId'],
                    'cn_name' => $arr['info']['cname'],
                    'username' => $arr['info']['uname'],
                    'pwd' => $hash,
                    'status'=>1
                ]);
                if($arr['info']['groupId']==7){  //如果是新建体检机构，则在体检机构表插入数据
                    DB::table('medical_institution')->insert([
                        'org_name' => $arr['info']['cname'],
                        'link_user_id'=>$id,
                        'status'=>1
                    ]);
                }
                DB::commit(); // 手动提交事务
                if($id){
                    $result['status']='ok';
                    $result['msg']='操作成功';
                }
            } catch (\Exception $e) {
                DB::rollback(); // 发生异常时手动回滚事务
                $result['status']='no';
                $result['msg']='操作失败';
            }

        }

        return $result;
    }
    public function GetDetail($arr){
        $c=DB::table('users')->select(['id','cn_name','username','status','group','img','xtsign_userid','xtsign_username','ukey_sfuid'])->where(['id'=>$arr['id']])->whereIn('status',[0,1])->get();
        if(count($c)){
            $result['info']=$c;
            $result['status']='ok';
            $result['msg']='成功';
        }else{
            $result['status']='no';
            $result['msg']='获取详情失败';
        }
        return $result;
    }
    public function ChangePwd($arr){
        $result=array();
        $s=app()->make(LoginService::class);
        $check=$s->CheckPwd(['userid'=>$arr['id'],'password'=>$arr['oldpwd']]);
        if($check['status']){
            $hash = password_hash($arr['newpwd'], PASSWORD_DEFAULT);
            $u=DB::table('users')->where(['id'=>$arr['id'],'status'=>1])->update(['pwd'=>$hash]);
            if($u){
                $result['status']='ok';
            }else{
                $result['status']='no';
                $result['msg']='修改密码失败';
            }
        }else{
            $result=$check;
        }
        return $result;
    }

    //检查用户是否有某个目录的权限
    //参数['userid'=>$userid,'group'=>$group,'url'=>$url]
    public function CheckMenuAuth($arr){
        $list=['index','dashboard'];
        $q=DB::select("select * from users where id=? and `group` =?",[$arr['userid'],$arr['group']]);
        if(count($q)==1){
            if(in_array($arr['url'],$list)){
                return \Yz::Return(true,'',[]);
            }
            $check=DB::select("select * from (select menu_id from group_menu where group_id=? ) as a inner JOIN (select id from menu where url = ? ) as b on a.menu_id=b.id
",[$arr['group'],$arr['url']]);
            if(count($check)>0){
               return \Yz::Return(true,'',[]);
            }else{
                return  \Yz::echoError1('暂无权限');
            }
        }else{
            return \Yz::echoError1('权限不匹配');
        }
    }
}
