<?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"); 

$pay = new PayRewardUpload();
$operator = $_SESSION[$web_auth]["username"];
if($type == 'reward'){
    $where = "id =".$id;
    $creditArr = $pay->getOneReward($where);    
    $upload_dir = getcwd();
    $upload_dir = str_replace("module/pay", "", $upload_dir)."../uploads/pay/";    
    $filename = $upload_dir.$creditArr['file'];
    if (file_exists($filename)) {        
        $uid_arr = $pay->uploadexcel($filename);   
        $brokerArr = array();//去重数组
        for($i=0;$i<count($uid_arr);$i++){                         
            $brokerid = iconv( "UTF-8", "gbk" , $uid_arr[$i][0]);         
            $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)){
                //积分
                $payBrokerHousebean = new PayBrokerHousebean($arrBroker); 
                if ($creditArr['num'] == 0) {
                    $num = $uid_arr[$i][1];
                } else {
                    $num = $creditArr['num'];
                }
                if ($num == 0) {continue;}
                if ($creditArr['type'] == 2) {
                    $isExecute = $payBrokerHousebean->increase('systemReward', $creditArr['event'] . '赠送', $num);
                } else {
                    $isExecute = $payBrokerHousebean->reduce('systemReward', $creditArr['event'] . '扣除', $num);
                }
                $title = "房豆赠送成功";
                $msg = "恭喜您通过". $creditArr['event']."活动赠送".$creditArr['score']."房豆，已到账，请查收。";
                apply::send_systemmsg($brokerInfo['uid'], $title, $msg);
                if($isExecute['status']==0){
                    $pay->insertFalseLog($brokerid, $id);//添加赠送失败人员名单                   
                    $pay->updateFalseNum($id);//添加赠送失败人数
                }else {
                     $brokerArr[] = $brokerInfo['uid'];  
                }            
            }else{  
                $pay->insertFalseLog($brokerid, $id);//添加赠送失败人员名单  
                $pay->updateFalseNum($id);//添加赠送失败人数
               
            }
       
        } 
   
    $pay->updateStatus($id,$operator);//修改赠送状态 
    echo 1;
        
    }else {
      echo 0;
    }
   
 
}elseif ($type == 'delete') {    
    if ($pay->delete($id)) {
        echo 1;
    } else {
        echo 0;
    }
}
?>