<?php
set_time_limit(0); 
$conf_basedir = "../../"; //相对主目录的位置
$conf_where = "admin"; //选择调用相应模块
include_once($conf_basedir . "/base.inc.php");

//有关本页面的信息
checkAuth('34');
//基本配置
include_once($conf_basedir."model/univercityChannel/univercityChannelFunction.php");
$uc = new UnivercityChannel('test');
$tbl = $uc->getSchoolTable();
$tblSchoolImg = $uc->getSchoolImgTable();
$tblSchoolBlock = $uc->getSchoolBlockTable();
$schoolEnableCity = array('nj','hz','sz','hf','wx','cz','cq','xa','ks','wh');
$blockId = intval($block_id);//echo 1;echo $blockId;exit;
//基本配置结束
$school  = new School($db,$dbback,$uc);


    $q = "select * from $tbl where id = $id";
    if ($q_ext != "")
        $q.=" and " . $q_ext;
    $data_info = $db->GetRow($q);
    if($data_info) {
            fresh_schoo_distance($data_info);
    }
    $nextId = $id+1;

    
function fresh_schoo_distance($data_info) {
    global $db,$dbback,$uc,$conf_city,$blockId;
    $citys = array('wx'=>'无锡','hz'=>'杭州','hf'=>'合肥','sz'=>'苏州','cz'=>'常州','wh'=>'芜湖','cq'=>'重庆','sy'=>'沈阳','ks'=>'昆山','nj'=>'南京',);//,'hz'=>'杭州'
	$sb = new SchoolBlock($db,$dbback,$uc);
	$sbOrder = new order('a');
	$sbOrder->setOrder('id','asc');
	$sbConf = new Conf();
    $id = $data_info['id'];
	$sbConf->add('school_id',$id);
	$schoolBlock = $sb->getAllBy($sbConf,$sbOrder);
    $sName = trim($data_info['schoolname']);
    $sLat = $data_info['b_map_y'];;$sLng = $data_info['b_map_x'];
    $city = $citys[$conf_city];//exit;
    if($city&&$sName&&$sLat&&$sLng) {
        //print_r($schoolBlock);exit;
        $sql = '';
        if(count($schoolBlock)>10) {
            //echo '关联的小区太多';exit;
        }
        
        foreach($schoolBlock as $k=>$b) {
            if($blockId!=$b['block_id']) {
                continue;
            }
            //echo $blockId;exit;
            $bName = trim($b['blockname']);
            $bLat = $b['b_map_y'];
            $bLng = $b['b_map_x'];
            $dis = distance_t($city,$sName,$sLat,$sLng,$bName,$bLat,$bLng);
            echo $dis = number_format($dis, 2, '.', '');
            $id = $b['id'];
            $sql = "update school_block set block_distance = $dis where id=$id";
            //echo '<p><span class="s_name">'.$sName.'</span>距离<span  class="b_name">'.$bName.'</span><span class="dis">'.$dis.'</span>千米</p>';
            $db->Execute($sql);
            break;
        }
        if($sql) {
            
        }
        
    }
}
function distance_t($city,$sName,$sLat,$sLng,$bName,$bLat,$bLng) {
        // 创建一个cURL资源
        $ch = curl_init();
        $url = "http://api.map.baidu.com/direction/v1";
        $p['origin'] = 'origin='.$sName."|".$sLat.",".$sLng;
        $p['destination'] = 'destination='.$bName."|".$bLat.",".$bLng;
        $p['origin_region'] = 'origin_region='.$city;
        $p['destination_region'] = 'destination_region='.$city;
        $p['output'] = 'output=json';
        $p['ak'] = 'ak=L03h2Teoucd5OlIgmaD3llM8';
        //?mode=driving&origin=上地五街&destination=北京大学&origin_region=北京&destination_region=北京&output=json&ak=E4805d16520de693a3fe707cdc962045
        $url = $url.'?'.implode("&",$p);
        // 设置URL和相应的选项
        if($city=='苏州'){
            //echo $url;exit;
        }
        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        
        // 抓取URL并把它传递给浏览器
        $nano = time_nanosleep(0, 20000000);
        if($nano === true) {
            $res = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($res);
        
        if($res->status==0) {
            return $res->result->taxi->distance/1000;
        } else {
            return 0;
        }
        } else {
            echo '失败';
        }
        // 关闭cURL资源，并且释放系统资源

}
?>