<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
checkAuth('31');	//判断登录和权限

$start = intval($page);
$blocknum = $dbback->GetOne("select count(id) from block where esta = 2 and newhouseid > 0");
$blockarr = $dbback->GetAll("select id,blockname,newhouseid from block where esta = 2 and newhouseid > 0 order by id limit ".$start.",1");
$dbback->close();

$tjlen = round((($start+1)/$blocknum)*100, 1);
$dvlen = floor($tjlen) * 2;
$tjsta = "<div style='width:300px;height:100px;margin:200px auto;'><div style='width:200;height:15;border:1px solid #898989;text-align:left'><div style='width:$dvlen;height:15;background-color:#829D83'></div></div>";
$tjsta .= "<br/>完成当前任务的：$tjlen%";
if($start + 1 >= $blocknum)
{
    $tjsta .= "<br/><br/>数据采集完成，请关闭该页面。";
}
else
{
     
    if($conf_cityflag == 'nj')
    {
        $url = 'http://newhouse.house365.com/api/365tf/get_fun.php?pic_channel=1&ts=0&page=1&type=hx&prj_listid='.$blockarr[0]['newhouseid'].'&city='.$conf_cityflag.'&conf_cityflag='.$conf_cityflag;

        $url2 = 'http://newhouse.house365.com/api/365tf/get_fun.php?pic_channel=1&xctype=0&page=1&type=xc&prj_listid='.$blockarr[0]['newhouseid'].'&city='.$conf_cityflag.'&conf_cityflag='.$conf_cityflag;
    }
    else
    {
        $url = 'http://newhouse.'.$conf_cityflag.'.house365.com/api/365tf/get_fun.php?pic_channel=1&ts=0&page=1&type=hx&prj_listid='.$blockarr[0]['newhouseid'];

        $url2 = 'http://newhouse.'.$conf_cityflag.'.house365.com/api/365tf/get_fun.php?pic_channel=1&ts=0&page=1&type=xc&prj_listid='.$blockarr[0]['newhouseid'];
    }
    
    get_pic($blockarr, $url, 0);

    //get_pic($blockarr, $url2, 1);

    $page++;
    $tjsta .= "<script>window.location.href='get_block_pic.php?page=".$page."'</script>";
}
echo $tjsta."</div>";

function islog($blockid, $picmd5, $type)
{
    global $db;

    $islog = $db->GetOne("select id from admin_newhouse_pic_entry_log where blockid = '".$blockid."' and pic_md5 = '".$picmd5."' and type = '".$type."'");
    
    return $islog;
}
function deallog($blockid, $picmd5, $type)
{
    global $db;

    $sql = "replace into admin_newhouse_pic_entry_log (blockid,pic_md5,type) values('".$blockid."','".$picmd5."','".$type."')";
    //echo $sql;
    $db->Execute($sql);
}
function entry_pic($blockid, $blockname, $sort, $room, $hall, $toilet, $pic, $time)
{
    global $db;

    $sql = "insert into admin_pic_entry (blockid,blockname,sort,room,hall,toilet,pic_url,dateline,houseid) values('".$blockid."','".$blockname."','".$sort."','".$room."','".$hall."','".$toilet."','".$pic."','".$time."',0)";
    //echo $sql;
    $db->Execute($sql);
}
function get_pic($blockarr, $url, $type)
{
    $data = curl_get_contents($url);
    $arr = unserialize($data);
    
    if(count($arr['piclist']) > 0)
    {print_r($arr);exit;
        foreach($arr['piclist'] as $key=>$val)
        {
            $pic = $val['pic_thumb'];
            
            if(preg_match("/thumb/i", $pic))
            {
                $room = intval($val['this_type_info']['type_title']);
                $hall = intval($val['pic_ting']);
                $toilet = intval($val['pic_wei']);

                $picmd5 = md5($pic);

                $time = time();
                
                $islog = islog($blockarr[0]['id'], $picmd5, $type);
                $islog = intval($islog);
                if($islog == 0)
                {
                    deallog($blockarr[0]['id'], $picmd5, $type);
                    entry_pic($blockarr[0]['id'], $blockarr[0]['blockname'], $type, $room, $hall, $toilet, $pic, $time);
                    sleep(1);
                }
            }
        }
    }
}
?>