<?php

$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
include_once(CORE_LIB.'/CommonStatisticDownloader.php'); 
checkAuth('85');	//判断登录和权限
$js .= "<script src='".$conf_basedir."script/js/layer/layer.js'></script>";
$title = "数据报表";
$growCredit = new GrowthCredit();
$growLevel = new GrowthLevel();
$arrLevel = $growLevel->findAllLevelData();
if(CommonBase::isFullArray($arrLevel)){
    foreach($arrLevel as $key => $value) {
       $brokerNum = $growLevel->findBrokerNumByLevelId($value['level_id']);  
       $arrLevel[$key]['brokernum'] = $brokerNum;    
   }
}

function getBrokerDetail($mc, $dbback, $uid) {
	$sql = SqlCombinHelper::select_sql('broker', 'agentcode, master', "uid =" . $uid);
	$brokerInfo = $mc->get($sql);
	if ($brokerInfo === false) {
		$brokerInfo = $dbback->getRow($sql);
		$mc->add($sql, $brokerInfo, 3600);
	}
	
	$sql = SqlCombinHelper::select_sql('agency', 'agentshortname, parentcode', "agentcode = '$brokerInfo[agentcode]'");
	$branchAgent = $mc->get($sql);
	if ($branchAgent === false) {
		$branchAgent = $dbback->getRow($sql);
		$mc->add($sql, $branchAgent, 3600);
	}
	$sql = SqlCombinHelper::select_sql('agency', 'agentshortname', "agentcode = '$branchAgent[parentcode]'");
	$companyName = $mc->get($sql);
	if ($companyName === false) {
		$companyName = $dbback->getOne($sql);
		$mc->add($sql, $companyName, 3600);
	}
	return array('companyname' => $companyName, 'agentname' => $branchAgent['agentshortname'], 'master' => $brokerInfo['master']);
}

if($todo=='all'){
 
   $allColumns = array(
	   '1'=>array('key'=>'公司名称','name'=>'公司名称','field'=>'companyname', 
       'value'=>array()
       ),
	   '2'=>array('key'=>'门店名称','name'=>'门店名称','field'=>'agentname', 
       'value'=>array()
       ),
	   '3'=>array('key'=>'客户经理','name'=>'客户经理','field'=>'master', 
       'value'=>array()
       ),
       '4'=>array('key'=>'经纪人账号','name'=>'经纪人账号','field'=>'username', 
       'value'=>array()
       ),
       '5'=>array('key'=>'经纪人姓名','name'=>'经纪人姓名','field'=>'truename', 
       'value'=>array()
       ),  
        '6'=>array('key'=>'等级','name'=>'等级','field'=>'level', 
       'value'=>array()
       ),  
        '7'=>array('key'=>'成长值','name'=>'成长值','field'=>'exp', 
       'value'=>array()
       ),  
        '8'=>array('key'=>'积分','name'=>'积分','field'=>'credit', 
       'value'=>array()
       ),
    );
    
    $growExp = new GrowthExp();
    $arrBroker = $growExp->findAllBrokerAndExpInfo();
    if(is_array($arrBroker)&&!empty($arrBroker)){
        foreach($arrBroker as $key => $value) {
			$brokerDetail = getBrokerDetail($mc, $dbback, $value['broker_id']);
            $arrRowCredit = $growCredit->findByBrokerId($value['broker_id']);
            $level_name = $growLevel->findLevelNameByExp($value['exp']);
            $arrBroker[$key]['credit'] = $arrRowCredit['credit'];        
            $arrBroker[$key]['level'] = $level_name;
			$arrBroker[$key]['companyname'] = $brokerDetail['companyname'];
			$arrBroker[$key]['agentname'] = $brokerDetail['agentname'];
			$arrBroker[$key]['master'] = $brokerDetail['master'];
        }
        
    }

    
    $data = $arrBroker;
    $dataDownloadColumn = new DataDownloadColumn();
    $dataDownloadColumn->setAllColumns($allColumns);

    $downloader = new AgencyStatisticDownloader();

    $downloader->setDataDownloadColumn($dataDownloadColumn);

    $downloader->setData($data);

    $downloader->createTitle();
    $downloader->writeData();

    //开始下载

    //下载操作日志
    $logMessage = "下载经纪人积分和成长值";
    $operateRecord->insertData('85', 'insert or update', 
                $logMessage, $_SESSION['esfadmin']['username']);
        
    //$downloader->setFileName($fileName);
    $downloader->output();
    

}elseif($todo=='level') {
   
    $arrBroker = $growLevel->findAllBrokerByLevelId($level_id);
    if(CommonBase::isFullArray($arrBroker)){
        foreach($arrBroker as $key => $value) { 
			$brokerDetail = getBrokerDetail($mc, $dbback, $value['broker_id']);
			$arrRowCredit = $growCredit->findByBrokerId($value['broker_id']);
			$arrBroker[$key]['credit'] = $arrRowCredit['credit'];        
			$arrBroker[$key]['level'] = 'LV'.$level_id;
			$arrBroker[$key]['companyname'] = $brokerDetail['companyname'];
			$arrBroker[$key]['agentname'] = $brokerDetail['agentname'];
			$arrBroker[$key]['master'] = $brokerDetail['master'];
        }
    }    
   $allColumns = array(
	   '1'=>array('key'=>'公司名称','name'=>'公司名称','field'=>'companyname', 
       'value'=>array()
       ),
	   '2'=>array('key'=>'门店名称','name'=>'门店名称','field'=>'agentname', 
       'value'=>array()
       ),
	   '3'=>array('key'=>'客户经理','name'=>'客户经理','field'=>'master', 
       'value'=>array()
       ),
       '4'=>array('key'=>'经纪人账号','name'=>'经纪人账号','field'=>'username', 
       'value'=>array()
       ),
       '5'=>array('key'=>'经纪人姓名','name'=>'经纪人姓名','field'=>'truename', 
       'value'=>array()
       ),  
        '6'=>array('key'=>'等级','name'=>'等级','field'=>'level', 
       'value'=>array()
       ),  
        '7'=>array('key'=>'成长值','name'=>'成长值','field'=>'exp', 
       'value'=>array()
       ),  
        '8'=>array('key'=>'积分','name'=>'积分','field'=>'credit', 
       'value'=>array()
       ),
    );


    $data = $arrBroker;
    $dataDownloadColumn = new DataDownloadColumn();
    $dataDownloadColumn->setAllColumns($allColumns);

    $downloader = new AgencyStatisticDownloader();

    $downloader->setDataDownloadColumn($dataDownloadColumn);

    $downloader->setData($data);

    $downloader->createTitle();
    $downloader->writeData();

    //开始下载

    //下载操作日志
    $logMessage = "下载经纪人积分和成长值";
    $operateRecord->insertData('85', 'insert or update', 
                $logMessage, $_SESSION['esfadmin']['username']);
    //$downloader->setFileName($fileName);
    $downloader->output();
 
}



include_once(Template("header"));
include_once(Template("growth/download"));
include_once(Template("footer"));