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

$growth = new GrowthRewardUpload();
$operator = $_SESSION[$web_auth]["username"];
if($type == 'credit'){
    $where = "id =".$id;
    $creditArr = $growth->getRewardCredit($where);    
    $upload_dir = getcwd();
    $upload_dir = str_replace("module/growth", "", $upload_dir)."../uploads/growth/";    
    $filename = $upload_dir.$creditArr['file'];
    if (file_exists($filename)) {        
        $uid_arr = $growth->uploadexcel($filename);   
        $brokerArr = array();//去重数组
        
        for($i=0;$i<count($uid_arr);$i++){            
            $brokerid = $uid_arr[$i][0];                 
            $brokerid = iconv( "UTF-8", "gbk" , $brokerid);         
            $brokerGroup = new BrokerGroup();
            $brokerInfo = $brokerGroup->getBrokerInfo($brokerid);        
            $arrBroker = array(
                'id' => $brokerInfo['uid'],
                'groupid' => $brokerInfo['groupid'],
                'username'=>$brokerid
            );  
        
          
            
            if($brokerInfo['uid']!=''&&$brokerInfo['groupid']!=''&& !in_array($brokerInfo['uid'], $brokerArr)){
                //积分
                $gbc = new GrowthBrokerCredit($arrBroker);       
                $isExecute = $gbc->increase('systemReward', $creditArr['event'], $creditArr['score']);
                $title = "积分赠送成功";
                $msg = "恭喜您通过". $creditArr['event']."活动，获得赠送".$creditArr['score']."积分，已到账，请查收。";
                apply::send_systemmsg($brokerInfo['uid'], $title, $msg);
                if($isExecute['status']==0){
                    $growth->insertCreditFalseLog($brokerid, $id);//添加赠送失败人员名单                   
                    $growth->updateCreditFalseNum($id);//添加赠送失败人数
                }else {
                     $brokerArr[] = $brokerInfo['uid'];  
                }            
            }else{  
                $growth->insertCreditFalseLog($brokerid, $id);//添加赠送失败人员名单  
                $growth->updateCreditFalseNum($id);//添加赠送失败人数
               
            }
       
        } 
   
    $growth->updateCreditStatus($id,$operator);//修改赠送状态 
    echo 1;
        
    }else {
      echo 0;
    }
   
 
}elseif ($type == 'exp') {    
    
    $where = "id =".$id;
    $expArr = $growth->getRewardExp($where);
    $upload_dir = getcwd();
    $upload_dir = str_replace("module/growth", "", $upload_dir)."../uploads/growth/";    
    $filename = $upload_dir.$expArr['file'];
    if(file_exists($filename)){
        $uid_arr = $growth->uploadexcel($filename);  
        $brokerArr = array();
        for($i=0;$i<count($uid_arr);$i++){
            $brokerid = $uid_arr[$i][0];     
            $brokerGroup = new BrokerGroup();
            $brokerInfo = $brokerGroup->getBrokerInfo($brokerid);
            $arrBroker = array(
                'id' => $brokerInfo['uid'],
                'groupid' => $brokerInfo['groupid'],
            );          
 
           if($brokerInfo['uid']!=''&&$brokerInfo['groupid']!=''&&!in_array($brokerInfo['uid'], $brokerArr)){
               $gbe = new GrowthBrokerExp($arrBroker);   
           
               $isExecute = $gbe->increase('systemReward',$expArr['event'], $expArr['score']);//增长代码
                $title = "成长值赠送成功";
                $msg = "恭喜您通过".$expArr['event']."活动，获得赠送".$expArr['score']."成长值，已到账，请查收。";
                apply::send_systemmsg($brokerInfo['uid'], $title, $msg);
               if($isExecute['status']==0){
                   $growth->insertExpFalseLog($brokerid, $id);//添加赠送失败人员名单
                   $growth->updateExpFalseNum($id);//添加未赠送人数
               }else{
                    $brokerArr[] = $brokerInfo['uid'];  
               }    
               
           }else{
                $growth->updateExpFalseNum($id);//添加未赠送人数
                $growth->insertExpFalseLog($brokerid, $id);//添加赠送失败人员名单
           }
            
    
    }
      $growth->updateExpStatus($id,$operator);
      echo 1;
    }else{
        echo 0;   
    }
    
}







//获取Excel上传的文档


// $upload_dir = "./../../../upload/growth/";//上传文件夹    


    
    







?>