<?php
/**
 * 批量赠送积分成长值类
 *
 * 
 */
class GrowthRewardUpload {
    /**
     * 
     * @var 
     */
    private $dbback;
    
    private $referer;
    
    private $objReader;
    
    private $time;
    
    private $db;
    
    private $mc;

    private $tbl1;
    
    private $tbl2;


    /**
     *  构造函数
     * 
     */
    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');    
        $this->objReader = PHPExcel_IOFactory::createReader('Excel5');
        $this->time = time();//去当前上传的时间 
        $this->tbl1 = "growth_exp_reward";
        $this->tbl2 = "growth_credit_reward";
    
    }
    
    /**
     * 处理上传excel方法
     * @return array
     */
    public function uploadexcel($filetempname) {  
      
        //$objReader = PHPExcel_IOFactory::createReader('Excel5');
        $objPHPExcel	= $this->objReader->load($filetempname); 
        $sheet		= $objPHPExcel->getSheet(0); 
        $highestRow	        = $sheet->getHighestRow();           //取得总行数 
        $highestColumn	= $sheet->getHighestColumn();	//取得总列数

        $objWorksheet	= $objPHPExcel->getActiveSheet();
        $highestRow	        = $objWorksheet->getHighestRow();

        $highestColumn	= $objWorksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
        $headtitle	        = array();
        $strs	        = array();

        for ($i=0,$row = 2;$row <= $highestRow;$row++,$i++) {            
            //注意highestColumnIndex的列数索引从0开始
            for ($col = 0;$col < $highestColumnIndex;$col++){
                    $temp           = $objWorksheet->getCellByColumnAndRow($col,$row)->getValue();     
                    if($temp!='')
                    $strs[$i][$col] = strip_tags($temp);                   
            }          
        }        
        return $strs;
    }
    
  
    
        
    /**
     * 获取文件扩展名
     * @return string
     */
    public function getFileExt($file){
        return strrchr ($file,'.');
    }
    
    /**
     * 插入成长值、积分记录
     * @param String $activity 活动名
     * @param int $type 插入类型
     * @param int $score 赠送分值
     * @param String $filepath 实际名
     * @param int $brokernumber 赠送人数
     * @return boolean
     */
    public function insertReward($activity,$type,$score,$filepath,$brokernumber,$operator) {
        $arrField = array();
        $arrField['event'] = $activity;       
        $arrField['score'] = $score;
        $arrField['file'] = $filepath;
        $arrField['number'] = $brokernumber;
        $arrField['status'] = 0;
        $arrField['operator'] = $operator;
        $tbl = $type ==0 ? $this->tbl2 : $this->tbl1;
        $sql = SqlCombinHelper::insert_sql($tbl, $arrField);   
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }
    
    
    /**
     * 获取赠送成长值记录
     * @param int $where 
     * @param int $offset 
     * @param int $length
     * @return array
     */
    public function getRewardExpLog($where,$offset,$length) {        
        $sqlExp = SqlCombinHelper::select_sql($this->tbl1, "*",$where)." order by id desc limit $offset,$length";
        return $this->dbback->getAll($sqlExp);
    }
    
    /**
     * 获取赠送积分记录
     * @param int $where
     * @param int $offset 
     * @param int $length
     * @return array
     */
    public function getRewardCreditLog($where,$offset,$length) {        
        $sqlCredit = SqlCombinHelper::select_sql($this->tbl2, "*",$where)." order by id desc limit $offset,$length";
        return $this->dbback->getAll($sqlCredit);
        
    }
    
    /**
     * 获取某个赠送成长值记录
     * @param string $where 
     * @return array
     */
    public function getRewardExp($where) {        
        $sqlExp = SqlCombinHelper::select_sql($this->tbl1, "*",$where);        
        return $this->dbback->getRow($sqlExp);
    }
    
    /**
     * 获取赠送积分记录
     * @param int $where
     * @return array
     */
    public function getRewardCredit($where) {        
        $sqlCredit = SqlCombinHelper::select_sql($this->tbl2, "*",$where);      
        return $this->dbback->getRow($sqlCredit);
        
    }
    
    /**
     * 获取赠送记录条数     
     * @return int
     */
    public function getRewardExpLogNum($where) {        
        $sqlExp = SqlCombinHelper::count_sql($this->tbl1,$where);     
        $rewardExp = $this->dbback->getOne($sqlExp);       
        return $rewardExp;
    }
    
     /**
     * 获取赠送积分记录条数     
     * @return int
     */
    public function getRewardCreditLogNum($where) {       
        $sqlCredit = SqlCombinHelper::count_sql($this->tbl2,$where);        
        $rewardCredit = $this->dbback->getOne($sqlCredit);
        return $rewardCredit;
    }
    
    /**
     * 积分添加失败插入     
     * @return boolean
     */
    public function insertCreditFalseLog($uid,$activityid) {
        $table = "growth_credit_reward_false";
        $arrField['uid'] = $uid;
        $arrField['activityid'] = $activityid;
        $sqlCredit = SqlCombinHelper::insert_sql($table, $arrField);       
        if($this->db->execute($sqlCredit)) {
            return true;
        } else {
            return false;
        }      
    }
    
    /**
     * 成长值添加失败插入     
     * @return boolean
     */
    public function insertExpFalseLog($uid,$activityid) {
        $table = "growth_exp_reward_false";
        $arrField['uid'] = $uid;
        $arrField['activityid'] = $activityid;
        $sqlExp = SqlCombinHelper::insert_sql($table, $arrField);             
        if($this->db->execute($sqlExp)) {
            return true;
        } else {
            return false;
        }      
    }
    
    /**
     * 积分添加修改状态     
     * @return boolean
     */
    public function updateCreditStatus($activityid,$operator) {
        $arrField['operator'] =$operator;
        $arrField['status'] =1;
        $sqlCredit = SqlCombinHelper::update_sql($this->tbl2, $arrField, "id = $activityid");      
        if($this->db->execute($sqlCredit)) {
            return true;
        } else {
            return false;
        }      
    }
    
    /**
     * 成长值添加修改状态     
     * @return boolean
     */
    public function updateExpStatus($activityid,$operator) {
        $arrField['operator'] =$operator;
        $arrField['status'] =1;
        $sqlExp = SqlCombinHelper::update_sql($this->tbl1, $arrField, "id = $activityid");        
        if($this->db->execute($sqlExp)) {
            return true;
        } else {
            return false;
        }      
    }
    
    /**
     * 返回积分添加失败人数
     * @param int $id
     * @return int
     */
    public function updateCreditFalseNum($id){          
        $arrField['falsenumber'] =  '1';
        $sql = SqlCombinHelper::updateSelf_sql('growth_credit_reward',$arrField,"id = $id");     
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }            
    }
    
    /**
     * 返回成长值添加失败人数
     * @param int $id
     * @return int
     */
    public function updateExpFalseNum($id){     
        $arrField['falsenumber'] =  '1';
        $sql = SqlCombinHelper::updateSelf_sql('growth_credit_exp',$arrField,"id = $id");        
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }                 
    }
    
   
    

}

?>
