<?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>$ExtUserID</ExtUserID><ClientType>$ClientType</ClientType><TradeCode>$TradeCode</TradeCode><DeptType>$DeptType</DeptType><DoctorCode>$DoctorCode</DoctorCode><SessType>$SessType</SessType><StartDate>$StartDate</StartDate><EndDate>$EndDate</EndDate><RBASSessionCode>$RBASSessionCode</RBASSessionCode><ServiceCode>$ServiceCode</ServiceCode><StopScheduleFlag>$StopScheduleFlag</StopScheduleFlag><DepartmentCode>$DepartmentCode</DepartmentCode><SearchCode>$SearchCode</SearchCode></Request>";
$postData = str_replace(' ','%20',$postData);
$wsdl = "http://yygh1.dept.nfyy.com/csp/oep/DHC.OEP.BS.OEPSTANWebService.cls?soap_method=QuerySchedule&Input=";
$result = file_get_contents($wsdl.$postData);

$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;

?>
