<?php
/**
 * 经纪人积分
 */
require('path.inc.php');

$integral = new Integral($query);
$sql ="select * from fke_member where user_type=1";
$query->open($sql);
while ($rs = $query->next()) {
	//取得昨天的登陆情况
	$dateNow = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$dateNow = $dateNow- 86400;
	//昨天登陆了就加5分
	$haslogin = $query_mysql->getValue("select add_time from fke_member_loginlog where username = '".$rs['username']."' and add_time = ".$dateNow);
	if($haslogin){
		$integral->add($rs['id'],1);
	}
	$last3year = mktime(0,0,0,date('m'),date('d'),date('Y')-3);
	$last2year = mktime(0,0,0,date('m'),date('d'),date('Y')-2);
	$last1year = mktime(0,0,0,date('m'),date('d'),date('Y')-1);
	
	if($rs['add_time']<$last3year-86400){
		//3年以上
		if(!$integral->getLogByRuleId($rs['id'],4)){
			$integral->add($rs['id'],4);
		}
	}elseif($rs['add_time']<$last2year-86400){
		//2年以上
		if(!$integral->getLogByRuleId($rs['id'],3)){
			$integral->add($rs['id'],3);
		}
	}elseif($rs['add_time']<$last1year-86400){
		//1年以上
		if(!$integral->getLogByRuleId($rs['id'],2)){
			$integral->add($rs['id'],2);
		}
	}
}
echo "经纪人登陆积分增加完成";
echo "<br>";
/**
 * 经纪人活跃度
 * 功能：去除第7天的登陆状态
 * 需要在服务器时间过12点后准时执行
 */
 $sql ="select * from fke_member where user_type=1";
//$sql ="select * from fke_member where user_type=1 order by last_login desc limit 50, 10";
$query->open($sql);
while ($rs = $query->next()) {
	//取得前六天的登陆情况
	$dateNow = mktime(0,0,0,date('m'),date('d'),date('Y'));
	
	//$dateNow = MyDate::transform('timestamp','2009-04-30');
	$dateBefore =  $dateNow -518400;
	$loginlog = $query_mysql->select("select add_time from fke_member_loginlog where username = '".$rs['username']."' and add_time>=".$dateBefore." order by add_time asc");
	$active_arr = array_fill(0, 7, 0);
	$activeRate = 0;
	foreach($loginlog as $item){
		for($i=0;$i<=6;$i++){
			if($dateBefore+$i*86400 == $item ){
				//print_rr('$dateBefore='.($dateBefore+$i*86400));
				//print_rr('$item='.$item);
				$activeRate += 1000+pow(2, $i);
				$active_arr[$i]=1;
				break;
			}
		}
	}
	//print_rr($active_arr);
	//print_rr($activeRate);
	if($active_arr){
		$active_str = implode('|',$active_arr);
	}else{
		$active_str = '';
	}
	$total_loginday = $query_mysql->getValue("select count(*) from fke_member_loginlog where username = '".$rs['username']."'");
	$registe_day = (time() - $rs['add_time'])/86400;
	$total_rate = $total_loginday/$registe_day;
	//print_rr("update fke_member set active_str ='".$active_str."' , active_rate='".$activeRate."' , active_total='".$total_rate."' where id = ".$rs['id']);
	$query_mysql->execute("update fke_member set active_str ='".$active_str."' , active_rate='".$activeRate."' , active_total='".$total_rate."' where id = ".$rs['id']);
}
echo "经纪人活跃度更新完成";
echo "<br>";


/**
 * 小区房源数量
 * 可以设定每天计算一次（可根据实时性要求设定时间）
 * 需要在均价评估之前计算一次，避免均价计算错误
 */
 //小区数量
set_time_limit(0); 
$sql ="select * from fke_borough";
$query_mysql->open($sql);
$result = array();
$houseSell= new HouseSell($query);
$houseRent= new HouseRent($query);

while ($rs = $query_mysql->next()) {
	$housesell_num = $houseSell->getCount(0,' and status =1 and borough_id='.$rs['id']);
	$houserent_num = $houseRent->getCount(0,' and status =1 and borough_id='.$rs['id']);
	$query->execute('update fke_borough set sell_num = '.$housesell_num.', rent_num = '.$houserent_num .' where id ='.$rs['id']);
}
print_r("小区房源数量更新完成");
echo "<br>";
 
$sql ="select * from fke_member where user_type=1 ";
$query->open($sql);
while ($rs = $query->next()) {
	$housesell_num = $query_mysql->getValue("select count(*) as num from fke_housesell where status =1 and broker_id = ".$rs['id']);

	$houserent_num = $query_mysql->getValue("select count(*) as num from fke_houserent where status =1 and broker_id = ".$rs['id']);
	
	$query_mysql->execute('update fke_member set sell_num='.$housesell_num.',rent_num='.$houserent_num.' where id='.$rs['id']);
}

print_r("经纪人房源数量数据导入完成");
echo "<br>";


//计算二手房A+房源的单价比
$sql ="select * from fke_borough";
$query->open($sql);
while ($rs = $query->next()) {
	$borough_pic_num = $query_mysql->getValue("select count(*) as num from fke_borough_pic where borough_id = ".$rs['id']);

	$borough_draw_num = $query_mysql->getValue("select count(*) as num from fke_borough_draw where borough_id = ".$rs['id']);
	
	$query_mysql->execute('update fke_borough set layout_picture='.$borough_pic_num.',layout_drawing='.$borough_draw_num.' where id='.$rs['id']);
}

print_r("小区图片数量数据导入完成");
echo "<br>";

$housesell =  new HouseSell($query);
$sql ="select * from fke_borough where isdel = 0";
$query->open($sql);
$threeMonthBefore = mktime(0,0,0,date('m')-3,date('d'),date('Y'));
while ($rs = $query->next()) {
	$percent_change = 0;
	$avg_price = 0;
	$sum_array = $query_mysql->getValue('select sum( t.house_price/t.house_totalarea ) as sum_p,count(*) as sum_c from fke_housesell as t where borough_id='.$rs['id'].' and created >='.$threeMonthBefore.' and house_type in (1,2)');
	if($sum_array['sum_c']<5){
		continue;
	}
	if($sum_array){
		$avg_price = intval($sum_array['sum_p']*10000/$sum_array['sum_c']);
		if($rs['borough_avgprice']){
			$percent_change = round(($avg_price-$rs['borough_avgprice'])/$rs['borough_avgprice']*100,2);
		}
		if($percent_change){
			$query_mysql->execute('update fke_borough set borough_avgprice='.$avg_price.',percent_change ='.$percent_change.' where id = '.$rs['id']);
		}else{
			$query_mysql->execute('update fke_borough set borough_avgprice='.$avg_price.' where id = '.$rs['id']);
		}
	}
	echo $rs['id']."<br/>";
	
	//记录LOG
	//now
	$now_date = mktime(0,0,0,date('m',$cfg['time']),date('d',$cfg['time']),date('Y',$cfg['time']));
	if($query_mysql->getValue("select id from fke_borough_avgprice where borough_id =".$rs['id']." and add_time = ".$now_date)){
		$query_mysql->execute("update fke_borough_avgprice set borough_avgprice=".$avg_price.",percent_change=".$percent_change." where borough_id =".$rs['id']." and add_time = ".$now_date);
	}else{
		$query_mysql->execute("insert into fke_borough_avgprice (borough_id, borough_avgprice,percent_change,add_time) values ('".$rs['id']."','".$avg_price."','".$percent_change."','".$now_date."')");
	}
	
}
echo "小区均价完成";
echo "<br>";

/**
 * 全站数量统计
 * 时间可根据具体情况定
 * 暂定一个小时
 */

$houseSell= new HouseSell($query);
$housesell_num = $houseSell->getCount(0,' and (status =1 or status =2 ) ');
$query->execute("update fke_statistics set stat_value = ".$housesell_num." where stat_index ='sellNum'");

$houseRent= new HouseRent($query);
$houserent_num = $houseRent->getCount(0,' and (status =1 or status =2 ) ');
$query->execute("update fke_statistics set stat_value = ".$houserent_num." where stat_index ='rentNum'");

$borough = new Borough($query);
$borough_num = $borough->getCount(0,' and isdel = 0 ');
$query->execute("update fke_statistics set stat_value = ".$borough_num." where stat_index ='boroughNum'");

$member = new Member($query);

$broker_num = $member->getCount(' status =0 and user_type = 1 ');
$query->execute("update fke_statistics set stat_value = ".$broker_num." where stat_index ='brokerNum'");

$owner_num = $member->getCount(' status =0 and user_type = 2  ');
$query->execute("update fke_statistics set stat_value = ".$owner_num." where stat_index ='ownerNum'");


$sumPrice = $query->getValue("select sum(house_price/house_totalarea) from fke_housesell where status =1 or status =2 ");

$avgPrice = $sumPrice*10000/$housesell_num;
$query->execute("update fke_statistics set stat_value = ".$avgPrice." where stat_index ='avgprice'");

print_r("整站数据统计完成");
echo "<br>";
echo "=======================================================";
echo "<br>";
echo "一键更新完成！";

?>