<?php
namespace App\Services\mH5;
use DateTime;
use Illuminate\Support\Facades\DB;

class PersonService
{
    //检测是否注册过
    public function checkRegister($arr){
        $openid=$arr['openid'];
        $c=DB::table('persons')->where(['openid'=>$openid])->get();
        if(count($c)){
            return true;
        }else{
            return  false;
        }
    }
    //注册
    public function Register($arr){
        $result=array();
        $c= $this->checkRegister(['openid'=>$arr['openid']]);
        if($c){
            $result['status']=false;
            $result['msg']='已注册过，无需进行此操作';
        }else{
            //HSM加密
            $HSM_sfz =\App\Lib\HSM::HsmEncrypt($arr['info']['sfz']);
            if($HSM_sfz['status']!=true){
                return \Yz::echoError1('调用HSM加密失败');
            }
            $arr['info']['sfz']=$HSM_sfz['data'];
            if(isset($arr['info']['tel'])){
                $HSM_tel =\App\Lib\HSM::HsmEncrypt($arr['info']['tel']);
                if($HSM_tel['status']!=true){
                    return \Yz::echoError1('调用HSM加密失败');
                }
                $arr['info']['tel']=$HSM_tel['data'];
            }

            $i=DB::table('persons')->insert([
                'name' => $arr['info']['name'],
                'sex' => $arr['info']['sex'],
                'id_card_num' => $arr['info']['sfz'],
                'tel' => $arr['info']['tel'],
                'openid' => $arr['openid'],
                'status'=>1
            ]);
            if($i){
                $result['status']=true;
                $result['msg']='创建成功';
            }else{
                $result['status']=false;
                $result['msg']='操作失败';
            }
        }
        return $result;
    }

    //检测是否进行过免费体检
    public function GetPersonRecode($arr,$type=1){
        $result=array();
        $id_num='';

        if(isset($arr['group']) and $arr['group']=='mH5user'){  //如果是本地项目请求（非对外接口）

            $openid=$arr['openid'];
            $c=DB::table('persons')->select(['id_card_num'])->where(['openid'=>$openid,'status'=>1])->get();
            if(count($c)>0){
                $id_num=$c[0]->id_card_num;
            }else{
                $result['status']=false;
                $result['msg']='未找到有效用户';
                return  $result;
            }
        }else{ //对外接口，直接获取证件IdNum
            $id_num=$arr['id_num'];
        }

        if(strlen($id_num)>0){
            date_default_timezone_set('PRC');
            $currentYear = date('Y');
            $firstDay = date('Y-01-01', strtotime($currentYear));
            $lastDay = date('Y-12-31', strtotime($currentYear));
            //查询体检预约记录

            //查询体检记录表 免费体检记录
            $c=DB::table('examination_records as a') ->join('medical_institution as b', 'a.institution_id', '=', 'b.id')
                ->select(['a.id','a.institution_id','a.id_card_num','a.tijian_num','a.tijian_time','a.created_at as insertime','b.org_name'])->where([['a.id_card_num','=',$id_num],['a.created_at','>=',$firstDay],['a.created_at','<=',$lastDay],'a.fee_type'=>0,'a.is_del'=>0])->get();
            if(count($c)>0){
                $result['status']=false;
                $result['msg']='已体检过';
                $result['info']=$c;
            }else{
                $result['status']=true;
                $result['msg']='可以继续,本年度无免费体检记录';
                $result['info']=$c;
            }
        }else{
            $result['status']=false;
            $result['msg']='用户证件号未传';
        }
        return $result;
    }

    //获取个人预约记录详情
    public function GetAppointmentRecord($arr){
        //HSM加密
        $HSM_sfz =\App\Lib\HSM::HsmEncrypt($arr['id_num']);
        if($HSM_sfz['status']!=true){
            return \Yz::echoError1('调用HSM加密失败');
        }
        $arr['id_num']=$HSM_sfz['data'];
        $cha = DB::select("SELECT a.*,b.sex,b.tel,b.status,c.org_name from  appointment_record as a INNER JOIN
    persons as b on a.person_id=b.id INNER JOIN medical_institution as c on a.org_id=c.id   where a.id_card_num=?
    and a.status in (1,2) and is_del=0 order by a.id desc",[$arr['id_num']]);

//dd($cha);
        if(count($cha)>0){

            //HSM解密
            $HSM_tel =\App\Lib\HSM::HsmDecrypt($cha[0]->tel);

            if($HSM_tel['status']!=true){
                return \Yz::echoError1('调用HSM解密失败');
            }
            $cha[0]->tel=$HSM_tel['data'];

            $HSM_sfz =\App\Lib\HSM::HsmDecrypt($cha[0]->id_card_num);
            if($HSM_sfz['status']!=true){
                return \Yz::echoError1('调用HSM解密失败');
            }
            $cha[0]->id_card_num=$HSM_sfz['data'];

            $cha[0]->bind_check_type=null;
            $cha[0]->bind_unit_id=null;
            $cha[0]->bind_group_id=null;
            $cha[0]->bind_batch_id=null;
            $cha[0]->start_time=substr($cha[0]->time, 0,5);
            $cha[0]->end_time=substr($cha[0]->time,-5);
            $org_seting=DB::table('institution_seting')->where(['institution_id'=>$cha[0]->org_id,'small_check_type'=>$cha[0]->doc_type_name])->first();
            if(!!$org_seting){
                $cha[0]->bind_check_type=$org_seting->bind_check_type;
                $cha[0]->bind_unit_id=$org_seting->bind_unit_id;
                $cha[0]->bind_group_id=$org_seting->bind_group_id;
                $cha[0]->bind_batch_id=$org_seting->bind_batch_id;
                $cha[0]->bind_user_type=$org_seting->bind_user_type;
            }
            $cha_img =DB::table('appointment_img')->where('appointment_record_id',$cha[0]->id)->get();
           //env('APP_URL')
            foreach ($cha_img as $key=>$item){
                $cha_img[$key]->imgurl=env('ZHUAN_WANG').$item->imgurl;
            }
            $cha[0]->imgs=$cha_img;
        }

        if(!count($cha)>0) return \Yz::echoError1("未找到预约记录");
        return \Yz::Return(true,'',$cha);
    }


}
