<?php
function SoapToXml($xml)
{
    //api测试先关闭
    $startStr=strrpos($xml,"<![CDATA[");
    $endStr=strpos($xml,"]]>");
    $xml=substr($xml,$startStr,$endStr-$startStr);
    $xml = str_replace("<![CDATA[>","",$xml);
    $xml = str_replace("<![CDATA[","",$xml);
    $xml = str_replace("]]]]>","",$xml);
    $xml = str_replace("]]>","",$xml);
    $xml = str_replace("<ILLEGAL VALUE>","",$xml);
    return $xml;
}

date_default_timezone_set('PRC');
$callback = isset($_GET['callback']) ? trim($_GET['callback']) : '';

//==============获得前台请求==============
$req['HospitalId'] = $_GET['hospitalId'];
$req['startDate'] = $_GET['startDate'];
$req['endDate'] = $_GET['endDate'];
$req['deptId'] = $_GET['deptId'];
if (isset($_GET['doctorId'])) {
    $req['doctorId'] = $_GET['doctorId'];
}

$day = $_GET['day']?$_GET['day']:1;

$arr = array();
$TradeCode = "1004";
$ExtOrgCode = "南方医务通";
$ExtUserID = "NFYWT";
$ClientType ="";
$HospitalId =$req['HospitalId'];
$DeptType = "";
$SessType = "";
$StartDate = $req['startDate'];
$EndDate = date('Y-m-d',(strtotime($req['startDate'])+$day*3600*24));
$RBASSessionCode = "";
$ServiceCode = "";
$StopScheduleFlag = "";
$DepartmentCode = $req['deptId'];
$DoctorCode= empty($req['doctorId'])?'':$req['doctorId'];
$callback = isset($_GET['callback']) ? trim($_GET['callback']) : ''; 
$SearchCode = "";


$postData = "<Request><HospitalId>$HospitalId</HospitalId><ExtOrgCode>$ExtOrgCode</ExtOrgCode><ExtUserID>NFYWT</ExtUserID><ClientType></ClientType><TradeCode>1004</TradeCode><DeptType></DeptType><DoctorCode></DoctorCode><SessType></SessType><StartDate>$StartDate</StartDate><EndDate>$EndDate</EndDate><RBASSessionCode></RBASSessionCode><ServiceCode></ServiceCode><DoctorCode></DoctorCode><StopScheduleFlag></StopScheduleFlag><DepartmentCode>$DepartmentCode</DepartmentCode><SearchCode></SearchCode></Request>";
$postData = str_replace(' ','%20',$postData);
libxml_disable_entity_loader(false);
$wsdl = "yygh1.wsdl";

try{
    $client = new SoapClient($wsdl);
    $param = array(array("Input" => $postData));
    $result = $client->__soapCall('QuerySchedule',$param);
    $res = reset($result);
} catch (SoapFault $e) {
    var_dump($e);exit;
}

//$res = SoapToXml($result);

$res = simplexml_load_string($res);
$res = json_encode($res);
$res = json_decode($res,true);

$times = array();
for($i = $day-1; $i >=0;$i--) {
    $time = date('Y-m-d',strtotime($req['startDate'])+$i*3600*24);
    $times[$i]['time'] = $time;
    $times[$i]['count'] = 0;
}

if (isset($res['Schedules']['Schedule'])) {
    foreach ($res['Schedules']['Schedule'] as $k => $v ) {
        foreach ($times as $index => $time) {

            if ($time['time'] == $v ['ServiceDate']) {

                $times[$index]['count'] = intval($times[$index]['count']) + 1;
                break;
            }
        }
    }
} else {
    $arr[0]['msg'] = '请求出错';
    $arr[0]['time'] = $StartDate;
    echo $callback . '(' . json_encode($arr) .')';exit;
}


foreach ($times as $k => $v) {
    if (intval($v['count']) == 0) {
        $arr[$k][$v['time']]= false;

    } else {
        $arr[$k][$v['time']]= true;
    }
}



echo $callback . '(' . json_encode($arr) .')';exit;

?>