<?php
$conf_basedir	= "../../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
include_once WEB_MODULE . '/growth/base.inc.php';
$growthExp = new GrowthExp();
/**
 * 付费套餐使用天数*100
 * 付费套餐房源条数*100  有效房源
 * 标签数*100        
 * 头像（50） p_2_2
 * 身份认证（50） p_2
 * 名片认证（50） p_3
 * 经纪人证书认证（50） p_4_2
 * 当前刷新率*1000           昨天的
 * 问答积分/10    
 */
function percent($p,$t){
    if ($t == 0) {
            $s = 0;
    } else {
            $s = $p/$t;
    }
    return round($s, 2);
}
$dbback = Container::getInstance()->get('dbback');
$sql = SqlCombinHelper::select_sql('broker', 'uid, valid_flag,groupid,is_pay,regdate,refresh');
$arrBrokerUid = $dbback->getAll($sql);
$growthExp->emptyTable();
$arrFieldsValue = array();
foreach($arrBrokerUid as $key => $value) {
    $intBrokerExp = 0;
    if ($value['valid_flag'] == 1 && $value['is_pay'] == 0) {
        //付费天数
        if ($value['regdate'] != '0000-00-00') {
            $day = round((time() - strtotime($value['regdate']))/3600/24);
            $intBrokerExp += $day * 100;
        }
        $sql = SqlCombinHelper::select_sql('broker_group', 'post,tag_num', "id = $value[groupid]");
        $arrBrokerGroup = $dbback->getRow($sql);
        $intBrokerExp += $arrBrokerGroup['post'] * 100;
        $intBrokerExp += $arrBrokerGroup['tag_num'] * 100;
    }
    $sql = SqlCombinHelper::select_sql("broker_certificate", 'kind', "uid = $value[uid] and valid_flag = 1 AND (kind = 'p_2_2' or kind = 'p_2' or kind='p_3' or kind ='p_4_2')");
    $arrCert = $dbback->getAll($sql);
    if ($arrCert) {
        foreach($arrCert as $k => $v) {
            switch ($v['kind']) {
                case 'p_2_2':
                    $intBrokerExp += 50;
                    break;
                case 'p_2':
                    $intBrokerExp += 50;
                    break;
                case 'p_3':
                    $intBrokerExp += 50;
                    break;
                case 'p_4_2':
                    $intBrokerExp += 50;
                    break;
                default :
            }
        }
    }
    $stattime = date('Y-m-d', strtotime('-1 day'));
    $sql = SqlCombinHelper::select_sql('broker_stat', 'refresh', "uid = $value[uid] AND stattime = '{$stattime}'");
    $arrBrokerStat = $dbback->getRow($sql);
    $intBrokerExp += percent($arrBrokerStat['refresh'], $value['refresh']) * 1000;
	$arrFieldsValue[] = array('broker_id' => $value['uid'], 'exp' => $intBrokerExp, 'status' => 1);
}
$number = $growthExp->insertMutl($arrFieldsValue);
echo "初始化".$number."个经纪人";
