db = new MdbLib();
        $this->common = new CommonLib();
    }
    //show
    public function getOrders(){
        if(!isset($_GET['now']) || !isset($_GET['startDate']) || !isset($_GET['endDate'])){
            return false;
        }else{
            $now = $this->common->my_filter_input($_GET['now']);
            $startDate = $_GET['startDate'] . ' 00:00:00';
            $endDate = $_GET['endDate'].' 23:59:59';
            $pnsn = isset($_GET['pnsn'])?$this->common->my_filter_input($_GET['pnsn']):50;
        }
        
        $offset = ($now-1)*$pnsn;
        $sql = 'SELECT `id`,`order_barcode`,`goods_no`,`goods_info`,`out_time`,`transport`';
        $sql .= ' FROM `app_wsocheck` '; 
        $sql .= " WHERE `out_time` BETWEEN '{$startDate}' AND '{$endDate}'";
        $sql .= " ORDER BY `out_time` DESC LIMIT {$offset},$pnsn;";
        $query = $this->db->querySql($sql);
        
        $line = "";
        while($item = $this->db->queryRs($query)) { 
            $line .= '
';
            $line .= "{$item['id']}";
            $line .= "{$item['order_barcode']}";
            $line .= "{$item['goods_no']}";
            $line .= "{$item['goods_info']}";
            $line .= "{$item['transport']}";
            $line .= "{$item['out_time']}";
            $line .= '';
            $line .= "";
            $line .= '
';
        }
        return $line;
    }
    
    public function searchOrders(){
    	$sql = 'SELECT `id`,`order_barcode`,`goods_no`,`goods_info`,`out_time`,`transport` FROM `app_wsocheck`';
        if(isset($_GET['code'])){
            $code = $this->common->my_filter_input($_GET['code']);
            $sql .= " WHERE `order_barcode`='{$code}' OR `goods_no`='{$code}';";
        }
        $query = $this->db->querySql($sql);
        $line = "";
        while($item = $this->db->queryRs($query)) {
            $line .= '';
            $line .= "{$item['id']}";
            $line .= "{$item['order_barcode']}";
            $line .= "{$item['goods_no']}";
            $line .= "{$item['goods_info']}";
            $line .= "{$item['transport']}";
            $line .= "{$item['out_time']}";
            $line .= '';
            $line .= "";
            $line .= '
';
        }
        if(!empty($line)){
            return $line;
        }else{
            return -1;
        }
    }
    
    public function getStatistics(){
        if(!isset($_GET['startDate']) || !isset($_GET['endDate'])){
            return false;
        } else {
            $startDate = $_GET['startDate'] . ' 00:00:00';
            $endDate = $_GET['endDate'].' 23:59:59';
        }
        
        $sql_total = "SELECT COUNT(`id`) AS `total_num` FROM `app_wsocheck` ";
        $sql_total .= "WHERE `out_time` BETWEEN '{$startDate}' AND '{$endDate}';";
        $query_total = $this->db->querySql($sql_total);
        $rs_total = $this->db->queryRs($query_total);
        
        $rs = array();
        $rs['total'] = $rs_total['total_num'];
        return json_encode($rs);
    }
    
    public function exportExcel(){
        set_time_limit(120);
        
        if(!isset($_GET['startDate']) || !isset($_GET['endDate'])){
            return false;
        }else{
            $startDate = $_GET['startDate'] . ' 00:00:00';
            $endDate = $_GET['endDate'].' 23:59:59';
        }
        
//         $sql = 'SELECT `id`,`order_barcode`,`goods_no`,`goods_info`,`out_time`,`transport` FROM `app_wsocheck`';
        $sql = 'SELECT `id`,`order_barcode`,`goods_info` FROM `app_wsocheck`';
        $sql .= " WHERE `out_time` BETWEEN '{$startDate}' AND '{$endDate}' ";
        $query = $this->db->querySql($sql);
        
        $PHPExcel = new \PHPExcel();
        $PHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '序号')
                ->setCellValue('B1', '条码编号')   
                ->setCellValue('C1', '货物明细');
// 				->setCellValue('D1', '订单编号')
// 				->setCellValue('E1', '物流方式')
//              ->setCellValue('F1', '出库日期');
        $k = 2;
        while($item = $this->db->queryRs($query)) {
            $PHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$k, $k-1)
            ->setCellValue('B'.$k, $item['order_barcode'])
            ->setCellValue('C'.$k, $item['goods_info']);
//             ->setCellValue('D'.$k, $item['goods_no'])
//             ->setCellValue('E'.$k, $item['transport'])
//             ->setCellValue('F'.$k, $item['out_time']);
            $k++;
        }
        
        // set wdith
        $PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
        $PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(70);
//         $PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40);
//         $PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
//         $PHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
        
        // set align
        $PHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setWrapText(TRUE);
        
        $filename = 'wsocheck.'.date('His',time()).'.xls';
        $PHPExcel->getActiveSheet()->setTitle('wsocheck');
        $PHPExcel->setActiveSheetIndex(0);
        
        header("Content-Type: application/ms-excel; charset=utf-8");
        header("Content-Disposition: attachment; filename={$filename}");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        $PHPWriter = new PHPExcel_Writer_Excel5($PHPExcel, 'Excel5');
        $PHPWriter->save('php://output');
    }
    
    public function pagination(){
        if(!isset($_GET['startDate']) || !isset($_GET['endDate']) || !isset($_GET['wh'])){
            return false;
        }else{
            $startDate = $_GET['startDate'] . ' 00:00:00';
            $endDate = $_GET['endDate'].' 23:59:59';
            $wh = $this->common->my_filter_input($_GET['wh']);
            $pnsn = isset($_GET['pnsn'])?$this->common->my_filter_input($_GET['pnsn']):50;
        }
        
        $sql = "SELECT `id` FROM `app_wsocheck`";
        $sql .= " WHERE `out_time` BETWEEN '{$startDate}' AND '{$endDate}';";
        $query = $this->db->querySql($sql);
        $total = $this->db->queryNum($query);
        
        if($total == 0){
            $num = '0';
        }else{
            $num = ceil($total/$pnsn);
            $wh_max = ceil($num/10);
        }
        
        $direction = substr($wh, 0, 1);
        $wh = substr($wh, 1);
        if($direction == 'h'){
            $whh = ($wh==0)?'0':$wh-1;
            $wht = ($wh==0)?'1':$wh;
        }elseif($direction == 't'){
            $whh = ($wh==$wh_max)?($wh-1):$wh;
            $wht = ($wh==$wh_max)?$wh:($wh+1);
        }else{
            $whh = '0';
            $wht = '1';
        }
                
        $pagination = '共 '.$num.' 页';
        if($num > 1){
            $pagination .= ($num>10)?"<<":'';
            $clazz = '';
            for($i=1+$whh*10; $i<=10+$whh*10; $i++){
                if($i > $num){
                    break;
                }else{
                    if($i != 1+$whh*10){
                        $clazz = 'page';
                    }else{
                        $clazz = 'page pn';
                    }
                    $start = substr($startDate, 0, 10);
                    $end = substr($endDate, 0, 10);
                    $param = "&now={$i}&startDate={$start}&endDate={$end}&pnsn=50";
                    $pagination .= "".$i."";
                }
            }
            $pagination .= ($num>10)?">>":'';
        }
        $pagination .= "展示  记录";
        return $pagination;
    }
    
    public function deleteOrder(){
        if(isset($_GET['id']) && !empty($_GET['id'])){
        	$id = $this->common->my_filter_input($_GET['id']);
        }else{
            return false;
        }
        
        $sql = "DELETE FROM  `app_wsocheck` WHERE `id`='{$id}';";
        if($this->db->querySql($sql)){
            return 1;
        }else{
            return -1;
        }
    }
    
    public function addNewOrder(){
    	if(!isset($_POST['barcode']) || empty($_POST['barcode'])){
    		return false;
    	}else{
    		$barcode = $_POST['barcode'];
    	}
    	
    	$sql = "SELECT `goods_no`,`goods_info`,`transport` FROM `app_wigsout` WHERE `order_barcode`='{$barcode}';";
    	$query = $this->db->querySql($sql);
    	$rs = $this->db->queryRs($query);
    	
    	if(empty($rs)){
    		return -1;
    	}
    	$sql = "INSERT INTO `app_wsocheck`(`order_barcode`,`goods_no`,`goods_info`,`transport`) VALUES ('{$barcode}','{$rs['goods_no']}','{$rs['goods_info']}','{$rs['transport']}');";
    	if(!($this->db->querySql($sql))){
    		return -2;
    	}else{
    		return 1;
    	}
    }
    
    public function __destruct(){
        //TODO
    }
}