<?php
/**
 * 批量赠送房豆
 */
class PayRewardUpload {

    private $dbback;
    
    private $objReader;
    
    private $time;
    
    private $db;
    
    private $mc;


    /**
     *  构造函数
     * 
     */
    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->tbl_upload = "pay_housebean_reward";
        $this->tbl_upload_false = "pay_housebean_reward_false";
    
    }
    
    /**
     * 处理上传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,$num,$filepath,$brokernumber,
            $operator) {
        $arrField = array();
        $arrField['event'] = $activity; 
        $arrField['type'] = $type;
        $arrField['num'] = $num;
        $arrField['file'] = $filepath;
        $arrField['number'] = $brokernumber;
        $arrField['status'] = 0;
        $arrField['operator'] = $operator;
        $arrField['ctime'] = date('Y-m-d H:i:s');
        $sql = SqlCombinHelper::insert_sql($this->tbl_upload, $arrField);  
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }

     /**
     * 获取赠送记录条数     
     * @return int
     */
    public function getRewardLogNum($where) {       
        $sqlCredit = SqlCombinHelper::count_sql($this->tbl_upload,$where);        
        return $this->dbback->getOne($sqlCredit);
    }
    
    /**
     * 获取赠送房豆记录
     * @param int $where
     * @param int $offset 
     * @param int $length
     * @return array
     */
    public function getRewardLog($where,$offset,$length) {        
        $sqlCredit = SqlCombinHelper::select_sql($this->tbl_upload, "*",$where)." order by id desc limit $offset,$length";
        return $this->dbback->getAll($sqlCredit);
        
    }
    
    /**
     * 获取赠送房豆记录
     * @param int $where
     * @return array
     */
    public function getOneReward($where) {        
        $sqlCredit = SqlCombinHelper::select_sql($this->tbl_upload, "*",$where);      
        return $this->dbback->getRow($sqlCredit);
        
    }

    /**
     * 房豆添加失败插入     
     * @return boolean
     */
    public function insertFalseLog($uid,$activityid) {
        $arrField['uid'] = $uid;
        $arrField['activityid'] = $activityid;
        $sqlCredit = SqlCombinHelper::insert_sql($this->tbl_upload_false, $arrField);       
        if($this->db->execute($sqlCredit)) {
            return true;
        } else {
            return false;
        }      
    }

    /**
     * 房豆添加修改状态     
     * @return boolean
     */
    public function updateStatus($activityid,$operator) {
        $arrField['operator'] =$operator;
        $arrField['status'] =1;
        $sqlCredit = SqlCombinHelper::update_sql($this->tbl_upload, $arrField, 
                "id = $activityid");      
        if($this->db->execute($sqlCredit)) {
            return true;
        } else {
            return false;
        }      
    }
    
    
    /**
     * 返回房豆添加失败人数
     * @param int $id
     * @return int
     */
    public function updateFalseNum($id){          
        $arrField['falsenumber'] =  '1';
        $sql = SqlCombinHelper::updateSelf_sql($this->tbl_upload,$arrField,"id = $id");     
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }            
    }
    
    public function delete($id)
    {
        $sql = SqlCombinHelper::delete_sql($this->tbl_upload, "id = $id and status = 0");
        $sql_false = SqlCombinHelper::delete_sql($this->tbl_upload_false, "activityid = $id");
        if ($this->db->execute($sql)) {
            return $this->db->execute($sql_false);
        } else {
            return false;
        }
    }
}

?>
