<?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 PaymentRewardUpload();
$operator = $_SESSION[$web_auth]["username"];
if($type == 'reward'){
    $where = "id =".$id;
    $creditArr = $pay->getOneReward($where);    
    $upload_dir = getcwd();
    $upload_dir = str_replace("module/payment", "", $upload_dir)."../uploads/payment/";    
    $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 PaymentBrokerHousebean($arrBroker); 
                if ($creditArr['num'] == 0) {
                    $num = $uid_arr[$i][1];
                } else {
                    $num = $creditArr['num'];
                }
                if ($num == 0) {continue;}
                if ($creditArr['type'] == 2) {
                    $title = "房豆扣除成功";
                    $isExecute = $payBrokerHousebean->reduce('systemReduce', $creditArr['event'] . '扣除', $num);
                    $msg = '亲爱的' . $brokerInfo['truename'] . '，因转账号原因扣除您' . $brokerInfo['username'] 
                         . '账户' .$num. '房豆，详情请与您所在的门店沟通，房豆账户情况请至支付中心查看。';         
                } else {
                    $title = "房豆赠送成功";
                    $isExecute = $payBrokerHousebean->increase('systemReward', $creditArr['event'] . '赠送', $num);
                    $msg = '亲爱的' . $brokerInfo['truename'] . '，' . $creditArr['event'] 
                        . '活动赠送给您' . $brokerInfo['username']. '账户的' . $num . '房豆已经到账，请查收。';
                }
                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;
    }
}
?>