load->_model('Model_print_code','printcode');
		$this->load->_model('Model_excel','excel');
	}
	//定义方法的调用规则 获取URI第二段值
    public function _remap($arg,$arg_array)
    {
		if($arg == 'addexcel')
        {
             $this->_addexcel();
        }
		else if($arg == 'print')
        {
             $this->_print();
        }
		else if($arg == 'see')
        {
             $this->_see();
        }
		else if($arg == 'yz')
        {
             $this->_yz();
        }
		else if($arg == 'add')
        {
             $this->_add();
        }
		else if($arg == 'edit')
        {
             $this->_edit($arg_array);
        }
		else if($arg == 'del')
        {
             $this->_del();
        }
		else
		{
			 $this->_index();
		}
    }
	
	public function _see()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['page']))  
		{
		    $page = $this->input->post('page',true);
		    $perpage = $this->input->post('perpage',true);
			$timetk = $this->input->post('timetk',true);
			$timetj = $this->input->post('timetj',true);
			$number = $this->input->post('number',true);
			$label = $this->input->post('label',true);
			$print = $this->input->post('print',true);
			$xztime = $this->input->post('xztime',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "1=1";
			if($number)
            {
                $where  .= " and number = '$number'";
            }
			if($label)
            {
				$where  .= " and label = '$label'";
            }
			if($print != '')
            {
                $where  .= " and print = '$print'";
            }
			if($timetk && $timetj)
            {
                $where  .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
            }
            //数据排序
            $order_str = $xztime." desc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
			$info_list = $this->printcode->find_all($where,'id,title,number,label,label2,num,time,addtime',$order_str,$start,$perpage);
            foreach ($info_list as $key=>$value) 
		    {
				if($value['time'] != 0)
				{
				    $info_list[$key]['time'] = date('Y-m-d H:i',$value['time']);
				}
				else
				{
					$info_list[$key]['time'] = '';
				}
				if($value['addtime'] != 0)
				{
				    $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
				}
				else
				{
					$info_list[$key]['addtime'] = '';
				}
				$info_list[$key]['cz'] = "";
            }
			$total = $this->printcode->find_count($where);
		    $pagenum = ceil($total/$perpage);
		    $over = $total-($start+$perpage);
		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
		    echo json_encode($rows);exit;
		}
		$this->_Template('printcode_see',$this->data);
	}
	
	public function _index()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['page']))  
		{
		    $page = $this->input->post('page',true);
		    $perpage = $this->input->post('perpage',true);
			$timetk = $this->input->post('timetk',true);
			$timetj = $this->input->post('timetj',true);
			$number = $this->input->post('number',true);
			$label = $this->input->post('label',true);
			$print = $this->input->post('print',true);
			$xztime = $this->input->post('xztime',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "1=1";
			if($number)
            {
                $where  .= " and number = '$number'";
            }
			if($label)
            {
				$where  .= " and label = '$label'";
            }
			if($print != '')
            {
                $where  .= " and print = '$print'";
            }
			if($timetk && $timetj)
            {
                $where  .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
            }
            //数据排序
            $order_str = $xztime." desc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
			$info_list = $this->printcode->find_all($where,'id,title,number,label,label2,num,time,addtime',$order_str,$start,$perpage);
            foreach ($info_list as $key=>$value) 
		    {
				if($value['time'] != 0)
				{
				    $info_list[$key]['time'] = date('Y-m-d H:i',$value['time']);
				}
				else
				{
					$info_list[$key]['time'] = '';
				}
				if($value['addtime'] != 0)
				{
				    $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
				}
				else
				{
					$info_list[$key]['addtime'] = '';
				}
            }
			$total = $this->printcode->find_count($where);
		    $pagenum = ceil($total/$perpage);
		    $over = $total-($start+$perpage);
		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
		    echo json_encode($rows);exit;
		}
		$this->_Template('printcode',$this->data);
	}
	
	
	public function _yz()
	{
		$this->_Template('printcode_yz',$this->data);
	}
	
	
	public function _add()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['num']))
		{
		    $post['num'] = $this->input->post('num');
			$post['addtime'] = time();
			if(!is_numeric($post['num']))
			{
				echo json_encode(array('msg'=>'数量必须是数字!','success'=>false));exit;
			}
			if($this->printcode->insert($post))
        	{
         		echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
        	}
		}
		$this->_Template('printcode_add',$this->data);
	}
	
	
	public function _edit($arg_array)
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['id']))
		{
			$id = $this->input->post('id',true);
			$data = $this->printcode->read($id);
			$post['num'] = $this->input->post('num');
			if(!is_numeric($post['num']))
			{
				echo json_encode(array('msg'=>'数量必须是数字!','success'=>false));exit;
			}
			if($data['time'] != 0)
			{
				echo json_encode(array('msg'=>'此单已打印,无法修改!','success'=>false));exit;
			}
			if($this->printcode->save($post,$id))
        	{
         		echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
        	}
		}
		$arg_array = $arg_array[0];
		$printcode = $this->printcode->read($arg_array);
		$this->data['printcode'] = $printcode;
		$this->_Template('printcode_edit',$this->data);
	}
	
	//删除
	public function _del()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['s']))  
		{
            $id_arr = $this->input->post('s');
            $id_arr =  explode(',',$id_arr);
            if(!$id_arr)
            {
                echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
            }
            //循环删除记录
            foreach ($id_arr as $v) 
		    {
				$data = $this->printcode->read($v);
				if($data['time'] != 0)
				{
					echo json_encode(array('msg'=>$data['title'].'
已打印,无法删除!','success'=>false));exit;
				}
                $this->printcode->remove($v);
            }
            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
		}
    }
	
	public function _print()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['s']))  
		{
			$v = $this->input->post('s');
			$va = explode(',',rtrim($v,','));
			$n = $this->input->post('n');
			$sl = $this->input->post('sl');
			$text = array();$text['data'] = array();$time = time();
			$data = $this->printcode->read($va[$n]);
			$rows = array('number'=>$data['number'],'label'=>$data['label'],'label2'=>$data['label2'],'num'=>$sl,'data'=>$v,'n'=>$n-1);
			if($this->printcode->save(array('print'=>1,'time'=>$time),$data['id']))
        	{
         		echo json_encode(array('rows'=>($rows),'success'=>true));exit;
       	 	}
       		else
        	{	
           		echo json_encode(array('msg'=>'数据写入异常,请重新打印!','success'=>false));exit;
        	}
		}
	}
	
	public function _addexcel()
	{
		$dir = '/data/excel/'.date('Ymd',time()).'/';
		$config['upload_path'] = '.'.$dir ;
		$config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
        $config['allowed_types'] = 'xls|xlsx|csv';
        $config['max_size'] = 10240;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
        if ($this->upload->do_upload('userfile'))
        {
			$full_path = $dir.$this->upload->data('file_name');
			$fileName = '.' . $full_path;
            if (!file_exists($fileName)) 
			{
				echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
            }
			else
			{
				libxml_use_internal_errors(true);
				require_once "./data/excel/PHPExcel/IOFactory.php";
				$phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
				$phpExcel->setActiveSheetIndex(0);// 设置为默认表
				$sheetCount = $phpExcel->getSheetCount();// 获取表格数量
				$row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
				$column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
				++$column;//如果列数大于26行
				$list = array();
				for ($i = 3; $i <= $row; $i++) // 行数循环
				{
					$data = array();
                    for ($c = 'A'; $c != $column; $c++)  // 列数循环
	                {
						$data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
                    }
					$list[] = $data;
                }
			}
			$i = 0;$j = 0;$ed = array();$time = time();
		    foreach ($list as $key=>$value)
		    {
				$time = time();
				if($value['0'] == "")
				{
					continue;
				}
				$post['title'] =  $value['1'];
				$post['number'] =  $value['3'];
				$post['label'] =  $value['0'];
				$post['label2'] =  $value['2'];
				$post['num'] =  $value['4'];
				$post['addtime'] =  $time;
				if(!is_numeric($post['num']))
				{
					$ed[] = array($key.' 行,非数字!');
					$j++;
					continue;
				}
				if($post['num'] < 1)
				{
					$ed[] = array($key.' 行,数量错误!');
					$j++;
					continue;
				}
				$this->printcode->insert($post);
			}
			if($j > 0)
			{
				$tt = date('Ymd',time());
				$title = '库存导入错误信息-'.$tt;
        		$titlename = "
| 错误详情 |