<?php 
vendor('PHPExcel.PHPExcel');
class statAction extends FirstendAction
{
private $accessKey = '';
	
public function _initialize()
    {
        parent::_initialize();
        $this->accessKey = trim(C('yh_gongju'));
    }


	
protected function sotime($st){
switch($st){
	case "最近1天":
	$start =date('Y-m-d',NOW_TIME);
	break;
	case "最近3天":
    $start =date('Y-m-d',strtotime("-3 day"));
	break;
	case "最近7天":
    $start =date('Y-m-d',strtotime("-7 day"));
	break;
	case "最近15天":
    $start =date('Y-m-d',strtotime("-15 day"));
	break;
	case "最近20天":
    $start =date('Y-m-d',strtotime("-20 day"));
	break;
	case "最近30天":
    $start =date('Y-m-d',strtotime("-30 day"));
	break;
	default:
	$start =date('Y-m-d',NOW_TIME);
	break;
}
	
return $start;	

}
	
	
public function update_pay_stat(){
$this->check_key();
$cookie=I('cookie','','trim');
$alixls=I('alixls','','trim');
$local = TQK_DATA_PATH ."runtime/Data/2.xls";
if(!empty($alixls)){
if(is_file($alixls)){
 $file_name=$alixls;
                $objReader = PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn(); 
				$n=0;
                for($i=2;$i<=$highestRow;$i++)
                {
                	$data['orderid']= $objPHPExcel->getActiveSheet()->getCell("Z".$i)->getValue();
				$data['status']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['up_time']= strtotime($objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue());  
                	$data['price']= number_format($objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue(), 2, '.', '');  
				$data['goods_iid']= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
			    if($this->_ajax_yh_publish_update($data)){
			    $n++;
			    }
			
				}  	
			
	 	    $json = array(
                'data'=>array(),
                'total'=>$n,
                'state'=>'yes',
                'msg'=>'同步成功！'
            );
			
		  @unlink($alixls);
$this->ajaxReturn(1, '同步成功');
		   exit(json_encode($json));
	
}

}

if(!empty($cookie)){
if(function_exists('opcache_invalidate')){
$dir=TQK_DATA_PATH.'runtime/Data/coupon/stat_update.php';
$ret=opcache_invalidate($dir,TRUE);
}
$start = F('coupon/stat_update');
if(!$start){
$start =date('Y-m-d',strtotime("-2 day"));
}
F('coupon/stat_update', date('Y-m-d',time()));
$end=date('Y-m-d',time());
$url = 'https://pub.alimama.com/report/getTbkOrderDetails.json?downloadId=DOWNLOAD_REPORT_ORDER_DETAILS&startTime='.$start.'&endTime='.$end.'&queryType=3&tkStatus=3&memberType=';
set_time_limit(0);
$cp = curl_init($url);
$fp = fopen($local,"w");
curl_setopt($cp, CURLOPT_FILE, $fp);
curl_setopt($cp, CURLOPT_HEADER, 0);
curl_setopt($cp, CURLOPT_COOKIE, $cookie);
curl_exec($cp);
curl_close($cp);
fclose($fp);

if(is_file($local)){
                $file_name=$local;
                $objReader = PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn(); 
				$n=0;
                for($i=2;$i<=$highestRow;$i++)
                {
     	        $data['orderid']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				$data['status']= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$data['up_time']= strtotime($objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue());  
                	$data['price']= number_format($objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue(), 2, '.', '');  
				$data['goods_iid']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
			    if($this->_ajax_yh_publish_update($data)){
			    $n++;
			    }
			
				}  	
			
	 	    $json = array(
                'data'=>array(),
                'total'=>$n,
                'state'=>'yes',
                'msg'=>'同步成功！'
            );
			
		@unlink($local);
		exit(json_encode($json));
	
}

 $json = array(
                'data'=>array(),
                'total'=>'0',
                'state'=>'no',
                'msg'=>'暂时没有新数据，或者抓取数据异常'
        );

	exit(json_encode($json));

	
}

}

	
public function putin_pay_stat(){
$this->check_key();
$cookie=I('cookie','','trim');
$alixls=I('alixls','','trim');
$local = TQK_DATA_PATH ."runtime/Data/1.xls";

if(!empty($alixls)){
if(is_file($alixls)){
                $file_name=$alixls;
                $objReader = PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn(); 
				$n=0;
                for($i=2;$i<=$highestRow;$i++)
                {
               	$data['orderid']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();  
				$data['add_time']= strtotime($objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue());  
				$data['status']= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
                	$data['price']= number_format($objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue(), 2, '.', '');  
				$data['goods_iid']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['goods_title']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['goods_num']= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['ad_id']= $objPHPExcel->getActiveSheet()->getCell("AJ".$i)->getValue();
//				$data['site_id']= $objPHPExcel->getActiveSheet()->getCell("AH".$i)->getValue();
				$data['income']= $objPHPExcel->getActiveSheet()->getCell("AD".$i)->getValue();
				$data['ad_name']= $objPHPExcel->getActiveSheet()->getCell("AK".$i)->getValue();
				$data['goods_rate']= $objPHPExcel->getActiveSheet()->getCell("T".$i)->getValue();
			    if($this->_ajax_yh_publish_insert($data)){
			    $n++;
			    }
				
				}  	
			
	 	    $json = array(
                'data'=>array(),
                'total'=>$n,
                'state'=>'yes',
                'msg'=>'成功入库'.$n.'个订单'
       );
			
	    @unlink($alixls);
		$this->ajaxReturn(1, '成功入库'.$n.'个订单');
		exit(json_encode($json));	
	
}
	
}

if(!empty($cookie)){
if(function_exists('opcache_invalidate')){
$dir=TQK_DATA_PATH.'runtime/Data/coupon/stat_start.php';
$ret=opcache_invalidate($dir,TRUE);
}
$start = F('coupon/stat_start');
if(!$start){
$start =date('Y-m-d',strtotime("-2 day"));
}
F('coupon/stat_start', date('Y-m-d',time()));
$end=date('Y-m-d',time());
$url = 'https://pub.alimama.com/report/getTbkOrderDetails.json?downloadId=DOWNLOAD_REPORT_ORDER_DETAILS&startTime='.$start.'&endTime='.$end.'&queryType=2&tkStatus=12&memberType=';
set_time_limit(0);
$cp = curl_init($url);
$fp = fopen($local,"w");
curl_setopt($cp, CURLOPT_FILE, $fp);
curl_setopt($cp, CURLOPT_HEADER, 0);
curl_setopt($cp, CURLOPT_COOKIE, $cookie);
curl_exec($cp);
curl_close($cp);
fclose($fp);

if(is_file($local)){
	
                $file_name=$local;
                $objReader = PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn(); 
				$n=0;
                for($i=2;$i<=$highestRow;$i++)
                {
             	$data['orderid']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();  
				$data['add_time']= strtotime($objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue());  
				$data['status']= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
                	$data['price']= number_format($objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue(), 2, '.', '');  
				$data['goods_iid']= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['goods_title']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['goods_num']= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['ad_id']= $objPHPExcel->getActiveSheet()->getCell("AJ".$i)->getValue();
//				$data['site_id']= $objPHPExcel->getActiveSheet()->getCell("AH".$i)->getValue();
				$data['income']= $objPHPExcel->getActiveSheet()->getCell("AD".$i)->getValue();
				$data['ad_name']= $objPHPExcel->getActiveSheet()->getCell("AK".$i)->getValue();
				$data['goods_rate']= $objPHPExcel->getActiveSheet()->getCell("T".$i)->getValue();
			    if($this->_ajax_yh_publish_insert($data)){
			    $n++;
			    }
				
				}  	
			
	 	    $json = array(
                'data'=>array(),
                'total'=>$n,
                'state'=>'yes',
                'msg'=>'成功入库'.$n.'个订单'
            );
			
		 @unlink($local);
		exit(json_encode($json));
	
}

 $json = array(
                'data'=>array(),
                'total'=>'0',
                'state'=>'no',
                'msg'=>'暂时没有新数据，或者抓取数据异常'
        );

	exit(json_encode($json));

	
}	
}	
	
	

 private function _ajax_yh_publish_update($item)
    {
        $result = $this->ajax_yh_update_order($item);
		
        return $result;
    }


 private function _ajax_yh_publish_insert($item)
    {
        $result =$this->ajax_yh_publish_stat($item);
		
        return $result;
    }
	
	
private function ajax_yh_update_order($item) {
	$prefix = C(DB_PREFIX);
	$table=$prefix.'order';
	$sql='select id,uid,integral from '.$table.' where (status=1 or status=2) and orderid ="'.$item['orderid'].'"';
	$res=M()->query($sql);

 if ($item['status']=='已结算' && $res){
	$map=array(
	'id'=>$res[0]['id']
	);
    	
	$data=array(
	'up_time'=>$item['up_time'],
	'status' =>3
	);
	$result=M('order')->where($map)->save($data);
	if($result){
if($res[0]['integral']>0){
M('user')->where('id='.$res[0]['uid'])->save(array(
 'score'=>array('exp','score+'.$res[0]['integral'])
));
}
	return 1;
	}else{
	return 0;
	}
		
		
	}else{
			
		return 0;

        }
     
    }

    
	
private function ajax_yh_publish_stat($item){

	$prefix = C(DB_PREFIX);
	 $table=$prefix.'order';
	$sql='select id from '.$table.' where orderid ="'.$item['orderid'].'"';
	$num=M()->execute($sql);
	$mod=M('order');
if($num<=0 && ($item['status']=='已付款' || $item['status']=='已结算')){
	
	 $item['status']=1;
		$mod->create($item);
        $item_id = $mod->add();
        if ($item_id) {
            return 1;
        } else {
            return 0;
        }
	
}else{
	
	return 0;
	
}

     
    }


protected  function check_key(){
 $json = array(
         'state'=>'no',
          'msg'=>'通行密钥不正确'
         );

$key = I('key', '', 'trim');
if(!$key || $key!=$this->accessKey){
exit(json_encode($json));
}	
 }



	
}