<?php

/*
  统计函数集合
  文件名：func_statistics.php
  创建日期：2011.3.7
  更改日期：2011.3.7
 */

//获得用户今日登陆次数
function get_login() {
    global $web_auth, $db, $dbback;
    if ($_SESSION[$web_auth]["uid"] == '') {
        return 0;
    } else {
        $now = strtotime(date("Y-m-d"));
        $sql = "select count(*) from log_login  where uid=" . $_SESSION[$web_auth]["uid"] . " and logintime>'" . $now . "' and logintime<'" . ($now + 24 * 3600) . "'";
        return $dbback->GetOne($sql);
    }
}

//有效房源量，默认为激活+冻结
function stock_esta($type, $flg = '') {
    global $web_auth, $db, $dbback;
    if ($_SESSION[$web_auth]["uid"] == '') {
        return 0;
    } else {
        if ($flg != "") {

            $sellstock = $dbback->GetOne("select count(*) from sell where uid='" . $_SESSION[$web_auth]["uid"] . "' and infofrom = '5' and esta = $flg");
            $rentstock = $dbback->GetOne("select count(*) from rent where uid='" . $_SESSION[$web_auth]["uid"] . "' and infofrom = '5' and esta = $flg");
        } else {

            $sellstock = $dbback->GetOne("select count(*) from sell where uid='" . $_SESSION[$web_auth]["uid"] . "' and infofrom = '5' and (esta = 1 OR esta =5)");
            $rentstock = $dbback->GetOne("select count(*) from rent where uid='" . $_SESSION[$web_auth]["uid"] . "' and infofrom = '5' and (esta = 1 OR esta =5)");
        }

        if ($type == "read") {

            $stocked = $sellstock + $rentstock;

            return $stocked ? $stocked : 0;
        }

        if ($type == "sell")
            return $sellstock;
        if ($type == "rent")
            return $rentstock;
    }
}

//当前房源总量
function stock_manage($type) {
    global $web_auth, $db, $dbback;
    if ($_SESSION[$web_auth]["uid"] == '') {
        return 0;
    } else {
        $sql1 = "select count(*) from sell where uid='" . $_SESSION[$web_auth]["uid"] . "' and esta in (1,2,3,5) and agentcode='B000'";

        $sql2 = "select count(*) from rent where uid='" . $_SESSION[$web_auth]["uid"] . "' and esta in (1,2,3,5) and agentcode='B000'";

        if ($type == "read") {
            return $dbback->GetOne($sql1) + $dbback->GetOne($sql2);
        }

        if ($type == "sell")
            return $dbback->GetOne($sql1);
        if ($type == "rent")
            return $dbback->GetOne($sql2);
    }
}

//库存上限
function post_stock($uid) {
    global $db;
    $sql = "select inventory from broker where uid=" . $uid;
    //echo $sql;
    return (int) $db->GetOne($sql);
}

//多图房源量
function stock_isstar($type, $flg = '') {
    global $web_auth, $db, $dbback;
    if ($_SESSION[$web_auth]["uid"] == '') {
        return 0;
    } else {
        if ($flg == '') {
            $sql1 = "select count(*) from sell where uid='" . $_SESSION[$web_auth]["uid"] . "' and isstar=3 and esta=1 and agentcode='B000'";

            $sql2 = "select count(*) from rent where uid='" . $_SESSION[$web_auth]["uid"] . "' and isstar=3 and esta=1 and agentcode='B000'";
        } else {
            $sql1 = "select count(*) from sell where uid='" . $_SESSION[$web_auth]["uid"] . "' and isstar=3 and esta='$flg' and agentcode='B000'";

            $sql2 = "select count(*) from rent where uid='" . $_SESSION[$web_auth]["uid"] . "' and isstar=3 and esta='$flg' and agentcode='B000'";
        }

        if ($type == "read") {

            return $dbback->GetOne($sql1) + $dbback->GetOne($sql2);
        }

        if ($type == "sell")
            return $dbback->GetOne($sql1);
        if ($type == "rent")
            return $dbback->GetOne($sql2);
    }
}

/**
 * 多图房源点击
 */
function get_clickcountbygrade($day, $uid = '', $app = 'sell') {
    $strSql = $this->tourl($array);
    if ($uid) {
        $url = "http://" . $this->appName . "/getclickcount?type=getcountbygrade&day=" . $day . "&uid=" . $uid . "&app=" . $app;
    }
    //return $url;
    $strData = @file_get_contents($url);
    return $this->strIconv($strData, "gbk", $this->sourceCharSet);
}

function stock_show_window($type) {
    global $web_auth, $db, $dbback;
    if ($_SESSION[$web_auth]["uid"] == '') {
        return 0;
    } else {
        $sellstock = $dbback->GetOne("select count(*) from sell where uid='" . $_SESSION[$web_auth]["uid"] . "' and agentcode='B000' and esta!=4 and istop=1");
        $rentstock = $dbback->GetOne("select count(*) from rent where uid='" . $_SESSION[$web_auth]["uid"] . "' and agentcode='B000' and esta!=4 and istop=1");

        if ($type == "read") {

            $stocked = $sellstock + $rentstock;

            return $stocked ? $stocked : 0;
        }

        if ($type == "sell")
            return $sellstock;
        if ($type == "rent")
            return $rentstock;
    }
}

//店铺推荐房源量
function stock_istop($type) {
    global $web_auth, $db, $dbback;
    if ($_SESSION[$web_auth]["uid"] == '') {
        return 0;
    } else {
        $sql1 = "select count(*) from sell where uid='" . $_SESSION[$web_auth]["uid"] . "' and agentcode='B000' and esta!=4 and istop=1";
        $sql2 = "select count(*) from rent where uid='" . $_SESSION[$web_auth]["uid"] . "' and agentcode='B000' and esta!=4 and istop=1";

        if ($type == "read") {

            return $dbback->GetOne($sql1) + $dbback->GetOne($sql2);
        }

        if ($type == "sell")
            return $dbback->GetOne($sql1);
        if ($type == "rent")
            return $dbback->GetOne($sql2);
    }
}

//获得云标签
function get_blocktag($sql) {
    global $db, $dbback, $dbback;
    $rs = $dbback->GetAll($sql);
    $blocktag = array();
    if ($rs) {
        foreach ($rs as $k => $v) {
            $blocktag[$k][blockid] = $v[blockid];
            $blocktag[$k][blockname] = $rs = $dbback->GetOne("select blockname from block where id=$v[blockid]");
        }
    }
    return $blocktag;
}

function get_ownidtag($sql) {
    global $db, $dbback;
    $rs = $dbback->GetAll($sql);
    $ownidtag = array();
    if ($rs) {
        foreach ($rs as $k => $v) {
            $ownidtag[] = $v[ownid];
        }
    }
    return $ownidtag;
}

//$type:sys;guest;total
function get_message($type = "total") {
    global $web_auth, $db, $dbback;

    $sys = "select count(*) as count_num from agent_announceitem left join agent_announce on announceid=agent_announce.id where agentcode='B000' and uid='" . $_SESSION[$web_auth]["uid"] . "' and new='1'";

    $guest = "select count(*) as count_num from broker_guestbook where  uid='" . $_SESSION[$web_auth]["uid"] . "' and new='1'";

    if ($type == "sys") {
        return $db->GetOne($sys);
    }
    if ($type == "guest") {
        return $db->GetOne($guest);
    }
    if ($type == "total") {
        return $db->GetOne($sys) + $db->GetOne($guest);
    }
}

//获取房源是否在橱窗展示中
function get_house_show($id, $uid, $tbl2, $m_8_credit) {
    global $db, $dbback;
    $q = "select count(*) as number from w_broker_house as wbs left join window as w on w.window_id = wbs.window_id where wbs.rowid  ='" . $id . "' and wbs.uid='" . $uid . "' and w.visble=1 and wbs.esta=1 and wbs.tbl='" . $tbl2 . "'";
    $number = $dbback->GetRow($q);
    $q = "select wbs.keys_integral,wbs.display from w_broker_house as wbs left join window as w on w.window_id = wbs.window_id where wbs.rowid  ='" . $id . "' and wbs.uid='" . $uid . "' and w.visble=1 and wbs.esta=1 and wbs.tbl='" . $tbl2 . "'";
    $number2 = $dbback->GetRow($q);
    $visbale_integral = $number2['keys_integral'] + $number2['display'] * $m_8_credit; //读剩余积分
    if ($number['number'] > 0 && $visbale_integral != 0) {
        return true;
    } else {
        return false;
    }
}

?>