load->library('session');
		$this->load->_model('Model_systemwigs','systemwigs');
		$this->load->_model('Model_excel','excel');
		$this->load->_model('Model_shop','shop');
		$this->load->_model('Model_warehouse','warehouse');
		$this->load->_model('Model_fullorder','fullorder');
		$this->load->_model('Model_fullordertt','fullordertt');
		$this->load->_model('Model_fullordersmt','fullordersmt');
		$this->load->_model('Model_typeclass','typeclass');
	}
	//定义方法的调用规则 获取URI第二段值
    public function _remap($arg,$arg_array)
    {
		if($arg == 'data')
        {
             $this->_data();
        }
		else if($arg == 'out')
        {
             $this->_out();
        }
		else if($arg == 'del')
        {
             $this->_del();
        }
		else if($arg == 'add')
        {
             $this->_add();
        }
		else if($arg == 'excel')
        {
             $this->_excel();
        }
		else if($arg == 'barcode')
        {
             $this->_barcode($arg_array);
        }
		else if($arg == 'hb')
        {
             $this->_hb();
        }
		else
		{
			 $this->_index();
		}
    }
	
	public function _index()
	{
		$this->_Template('systemwigs',$this->data);
	}
	
	public function _data()
	{
		$post = $this->input->post(NULL, TRUE);
	    if(isset($post['page']))  
		{
		    $page = $this->input->post('page',true);
		    $perpage = $this->input->post('perpage',true);
			$number = $this->input->post('number',true);
			$type = $this->input->post('type',true);
			$timetk = $this->input->post('timetk',true);
			$timetk = strtotime($timetk);
			$where = "type != 0";
			if($timetk)
            {
                $where  .= " and gtime like '%".date('Ymd',$timetk)."%'";
            }
			if($type)
            {
                $where  .= " and type = '$type'";
            }
            //数据排序
            $order_str = "id desc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
            //取得信息列表
            $info_list = $this->systemwigs->find_all($where,'id,number,shipremarks,outtime,num',$order_str,$start,$perpage);
			 //格式化数据
            foreach ($info_list as $key=>$value)
		    {
				$outtime = explode('|',trim($value['outtime'],'|'));
				$num = explode('|',trim($value['num'],'|'));
				$info_list[$key]['outtime'] = '';
				$info_list[$key]['num'] = '';
				foreach ($outtime as $v) 
			    {
				    $info_list[$key]['outtime'] .= date('Y-m-d H:i:s',$v).'
';
			    }
				foreach ($num as $v) 
			    {
					$info_list[$key]['num'] .= $v.'
';
			    }
				$info_list[$key]['outtime'] = trim($info_list[$key]['outtime'],'
');
				$info_list[$key]['num'] = trim($info_list[$key]['num'],'
');
            }
		    $total = $this->systemwigs->find_count($where);
		    $pagenum = ceil($total/$perpage);
		    $over = $total-($start+$perpage);
			$exdata = 0;
			$count = $this->systemwigs->find_all($where." and gtime like '%".date('Ymd',time())."%' ");
			foreach ($count as $v)
			{
				$gtime = explode('|',trim($v['gtime'],'|'));
				$num = explode('|',trim($v['num'],'|'));
				$gtime = array_flip($gtime);
				$exdata += $num[$gtime[date('Ymd',time())]];
			}
		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list),'datacount'=>$exdata);
		    echo json_encode($rows);exit;
		}
	}
	
	public function _out()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['number']))
		{
			$number = $this->input->post('number',true);
			$type = $this->input->post('type',true);
			$time = time();
			if(!$number)
			{
				echo json_encode(array('msg'=>'未扫入数据!','success'=>false));exit;
			}
			if(!$type)
			{
				echo json_encode(array('msg'=>'请选择需要录入数据的仓库!','success'=>false));exit;
			}
			$nu = $this->fullorder->get_number($number);
			if(!$nu)
			{
				$nu = $this->fullordersmt->get_number($number);
			}
			if(!$nu)
			{
				echo json_encode(array('msg'=>'错误!未找到此编号的订单','success'=>false));exit;
			}
			$systemwigs = $this->systemwigs->get_num($number);
			if($systemwigs)
			{
				$gtime = explode('|',trim($systemwigs['gtime'],'|'));
				$gtime = array_flip($gtime);
				if(isset($gtime[date('Ymd',$time)]))
				{
					$num = explode('|',trim($systemwigs['num'],'|'));
					$num[$gtime[date('Ymd',$time)]] += 1;
					$num = '|'.implode('|',$num);
				    if($this->systemwigs->save(array('num'=>$num),$systemwigs['id']))
				   	{
				    	echo json_encode(array('music'=>'1','success'=>true));exit;
				    }
				    else
				    {
				    	echo json_encode(array('msg'=>'数据写入失败,请重试!','success'=>false));exit;
				    }
				}
				else
				{
					if($this->systemwigs->save(array('num'=>'|1'.$systemwigs['num'],'gtime'=>'|'.date('Ymd',$time).$systemwigs['gtime'],'outtime'=>'|'.$time.$systemwigs['outtime']),$systemwigs['id']))
				   	{
				    	echo json_encode(array('music'=>'1','success'=>true));exit;
				    }
				    else
				    {
				    	echo json_encode(array('msg'=>'数据写入失败,请重试!','success'=>false));exit;
				    }
				}
			}
			else
			{
				if($this->systemwigs->insert(array('number'=>$number,'type'=>$type,'shipremarks'=>$nu['shipremarks'],'fpdata'=>$nu['fpdata'],'outtime'=>'|'.$time,'gtime'=>'|'.date('Ymd',$time),'num'=>'|1')))
				{
				    echo json_encode(array('music'=>'1','success'=>true));exit;
				}
				else
				{
				    echo json_encode(array('msg'=>'数据写入失败,请重试!','success'=>false));exit;
				}
			}
		}
	}
	
	public function _del()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['delarr']))  
		{
			$number = $this->input->post('number',true);
			$type = $this->input->post('type',true);
			$timetk = $this->input->post('timetk',true);
			$timetj = $this->input->post('timetj',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "type != 0";
			if(isset($_SESSION['api']))
			{
				if($type)
            	{
                	$where  .= " and type = '$type'";
           		}
				else
				{
					$uw = '';
				    $u = $this->user->get_api($_SESSION['api']);
				    $u['warehouse'] =  explode('|',trim($u['warehouse'],'|'));
				    foreach ($u['warehouse'] as $v) 
		            {
					    $uw  .= " type = '$v' or";
                    }
				    $uw = rtrim($uw,'or');
					$uw = " and (".ltrim($uw,' ').")";
					$where  .= $uw;
				}
			}
			if($timetk && $timetj)
            {
                $where  .= " and time > '$timetk' and time < '$timetj'";
            }
			if($number)
            {
                $where  .= " and number = '$number'";
            }
            $id_arr = $this->input->post('delarr');
            $id_arr =  explode(',',rtrim($id_arr,','));
            if(!$id_arr)
            {
                echo json_encode(array('msg'=>'未选择需要删除的内容!','success'=>false));exit;
            }
            //循环删除记录
			$a=0;
            foreach ($id_arr as $v) 
		    {
				if($this->systemwigs->remove($v))
				{
					$a++;
				}
				
            }
			if($a == count($id_arr))
			{
				$data = 0;
				$count = $this->systemwigs->find_all($where);
				foreach ($count as $v) 
				{
					$data += $v['quantity']; 
				}
                echo json_encode(array('del'=>$id_arr,'data'=>$data-$a,'msg'=>'删除成功!','success'=>true));
			}
			else
			{
				echo json_encode(array('del'=>$id_arr,'msg'=>'部分记录删除失败,请查询确认!','success'=>true));
			}
		}
	}
	
	public function _excel()
	{
		/*  匹配ID加入  */
		$tc = array();$ztc = array();$tclass = array();
		$typeclass = $this->typeclass->find_all();
		foreach ($typeclass as $v) 
		{
			$ztc[$v['id']] = $v['zh'];
			$tc[$v['id']] = $v['title'];
			$tclass[$v['id']] = $v['classid'];
		}
		
		$timetk = $this->input->get('timetk',true);
		$timetk = strtotime($timetk);
		$dcdata = array();
		$list = $this->systemwigs->find_all("gtime like '%".date('Ymd',$timetk)."%'");
        foreach ($list as $key=>$value)
		{
			$thisnumber = $value;$thisshipremarks = array();
			if(stripos($thisnumber['fpdata'],';') !== false)
			{
		   		$fpdata = explode(';',rtrim($thisnumber['fpdata'],';'));
		        foreach ($fpdata as $ke=>$va)
		        {
					$title = '';
			        $fg = explode('|',$va);
					$fg[0] = str_replace(array('163-','164-','165-','166-','-0-'),array('','','','','-'),$fg[0]);
					$cp = explode('-',rtrim($fg[0],'-'));
					if(isset($cp[5]))
					{
						$tt = $ztc[$cp[5]];
					}
					else
					{
						$tt = '';
					}
					$md = '';
					foreach ($cp as $v)
		            {
						if(!isset($tclass[$v]))
						{
							continue;
						}
						if($tclass[$v] == '10')
						{
							$md = $tc[$v];
						}
					}
					if(isset($cp[4]))
					{
						$dchx = $tc[$cp[4]];
					}
					else
					{
						$dchx = ' ';
					}
					$thisshipremarks[] = array($tt,$tc[$cp[3]],$md,$dchx,$fg[5],$fg[2]);//头套种类,颜色,密度,花型,尺寸,数量
               }
			}
			$thisnumber['shipremarks'] = str_replace(array('<','>'),array('<','>'),$thisnumber['shipremarks']);
			$dcdata[] = array('number'=>$thisnumber['number'],'shipremarks'=>$thisnumber['shipremarks'],'fpdata'=>$thisshipremarks);
		}
		$title = date('Y-m-d H:i:s',time())." 产品详细信息"; 
        $titlename = "
"; 
        $filename = $title.".xls"; 
		$tail = "\n"; 
        $downexcel = $this->excel->get_fz($dcdata,$titlename,$filename,$tail);
		$time = date('Ymd',time());
		$dir = '/data/excel/'.$time.'/';
		$file_name = $title;
		if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
		$myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
		fwrite($myfile,$downexcel);
		fclose($myfile);
		$goexcel = $dir.$file_name.'.xls';
		echo json_encode(array('msg'=>'操作成功 ','goexcel'=>$goexcel,'success'=>true));exit;
    }
	
	public function get_fz($info_list,$titlename,$filename,$tail)
	{
		$str = "
        
        
        ";
		$str .= $titlename; 
		$str .= ""; 
		foreach ($info_list as $key=>$value) 
		{ 
		    if($value['fpdata'] == '')
				{
					continue;
				}
		    $str .= "";
		    foreach ($value as $ke=>$va) 
			{
				if($ke == 'orderinfo' || $ke == 'waybill')
				{
					$str .= "| ".$va."";
				}
				else if($ke != 'fpdata' && $ke != 'hl' && $ke != 'currencytitle')
				{
		            $str .= " | ".$va.""; 
				}
		    }
			$str .= " | "; 
			$str .= "";
			$a = 0;
			foreach ($value['fpdata'] as $k=>$v) 
		    {
				$vd = '';
				foreach ($v as $vv) 
		        {
					$vd .= "| ".$vv."";
				}
				$str .= " | ".$vd."";
				$endv = is_numeric(end($v))?end($v):0;
				$a = $a + $endv;
			}
			$str .= " | ".$a."";
		    $str .= " | 
\n"; 
		}
		$str .= $tail;
		$str .= "
"; 
		return $str; 
		//return $str;
	}
}