<?php
namespace app\controller;

use app\BaseController;
use app\common\array2xml;
use think\facade\Config;
use think\facade\Db;
use think\facade\Cache;

class Index extends BaseController
{
    public function index()
    {
        $this->getPdf('12206060840002');
//        phpinfo();
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
    public function getReport()
    {
        $code = $_REQUEST['code'];
        $type = $_REQUEST['type'];
        libxml_disable_entity_loader(false);
//        $client = new \SoapClient("http://report.dalabs.cn/RasClientDetail.asmx?wsdl");
        $client = new \SoapClient("http://115.238.253.166/RasClientDetail.asmx?wsdl");
        $data = [
            'ClientID'=>Config::get('app')['dian']['ClientID'],
            'ClientGUID'=>Config::get('app')['dian']['ClientGUID'],
            'hospBarcode'=>$code,
            'SelectType'=>$type
        ];   //请求参数
        Db::table('api_log')->insert(['type'=>1,'barcode'=>$code]);
        try{
            $result = $client->GetDetailDataByHospBarcode3($data);   //请求获取接口
            Db::table('api_log')->insert(['type'=>2,'barcode'=>$code]);
            $data = ((array)$result)['GetDetailDataByHospBarcode3Result'];
            $obj = simplexml_load_string("<data>".$data."</data>","SimpleXMLElement", LIBXML_NOCDATA);
            if($obj)
            {
                $info = json_decode(json_encode($obj),true);
                $returnCode = $info['Error']['Code'];
                if($returnCode !== '0')
                {
                    return '报告获取失败！迪安回传信息:'.$info['Error']['Descript'];
                }
            }
            $report = json_encode($obj);
            DB::table('dianapiinfo')->where('Barcode',$code)->update(['result_xml'=>$report]);
            $this->getPdf($code);
            return 'success';
        }catch(\SoapFault $e){
            return $e->getMessage();
        }
    }

    public function getPdf($code)
    {
        $type = 1;
        libxml_disable_entity_loader(false);
        $client = new \SoapClient("https://r.dalabs.cn/ReportService.asmx?wsdl");
//        $data = [
//            'ClientID'=>Config::get('app')['dian']['ClientID'],
//            'Key'=>Config::get('app')['dian']['ClientGUID'],
//            'PageNO'=>1,
//            'Condition'=>[
//                'ClinicID'=>$code,
//                'StartDate'=>'',
//                'EndDate'=>''
//            ],
//            'Model'=>1
//        ];   //请求参数
//        try{
//            $a2x = new array2xml();
//            $data = $a2x->toXml($data);
//            var_dump(htmlspecialchars($data));
//            var_dump($data);
            $result = $client->QueryReports2(array('ReqInfo'=>[
                'PageNo'=>1,
                'ClientID'=>Config::get('app')['dian']['ClientID'],
                'Key'=>Config::get('app')['dian']['ClientGUID'],
                'Condition'=>[
                    'ClinicID'=>$code,
                    'StartDate'=>'',
                    'EndDate'=>''
                ]]));   //请求获取接口
            Db::table('dianapiinfo')->where('Barcode',$code)->update(['pdf_xml'=>$result]);
            $data = ((array)$result)['QueryReports2Result'];
            var_dump(json_decode(json_encode($data),true));die;
            $obj = simplexml_load_string("<data>".$data."</data>","SimpleXMLElement", LIBXML_NOCDATA);
            Db::table('api_log')->insert(['type'=>4,'barcode'=>$code]);
            if($obj)
            {
                $info = json_decode(json_encode($obj),true);
                var_dump($info);die;
                $returnCode = $info['Msg']['Code'];
                if($returnCode !== '1')
                {
                    return '报告PDF获取失败！迪安回传信息:'.$info['Msg']['Descript'];
                }
                if(isset($info['NewDataSet']['Table1']['BARCODE']))
                {
                    $pdf = $info['NewDataSet']['Table1']['PDFREPORTURL'];
                    $name = $info['NewDataSet']['Table1']['BARCODE'];
                    $this->downImgRar($pdf,$name,'pdf',$info['NewDataSet']['Table1']['BARCODE']);
                }else{
                    foreach ($info['NewDataSet']['Table1'] as $k=>$v)
                    {
                        $pdf = $v['PDFREPORTURL'];
                        $name = $v['BARCODE'].'_'.($k+1);
                        $this->downImgRar($pdf,$name,'pdf',$v['BARCODE']);
                    }
                }

            }
            return 'success';
//        }catch(\SoapFault $e){
//            return $e->getMessage();
//        }
    }

    function downImgRar($url,$rename,$ext,$barCode){
        switch ($ext) {
            case 'png':
            case 'jpg':    //下载图片
                $file_path = '/images/';
                break;
            case 'pdf':    //下载PDF
                $file_path = '/pdf/';
                break;
            case 'zip':
            case 'rar':    //下载压缩包
                $file_path = '/rar/';
                break;
            default:
                $file_path = '/files/';
                break;
        }
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $rawdata=curl_exec ($ch);
        curl_close ($ch);
        // 使用中文文件名需要转码
        file_put_contents($_SERVER['DOCUMENT_ROOT'].$file_path.$rename.'.'.$ext,$rawdata);
        // 返回路径
        $path = $_SERVER['DOCUMENT_ROOT'].$file_path.$rename.".".$ext;
        $ins = ['type'=>$ext,'url'=>$path,'barcode'=>$barCode];
        Db::table('annex')->insert($ins);
        $patientId = Db::table('dianapiinfo')->where('Barcode',$barCode)->value('patientId');
        Db::table('phy_lis_reports')->where('patientId',$patientId)->update(['is_report_url'=>1]);
        return 'success';
    }
}
