| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 | <?php defined('BASEPATH') OR exit('No direct script access allowed');class product extends Start_Controller {	public function __construct(){		parent::__construct();		$this->load->library('session');		$this->load->_model('Model_product','product');		$this->load->_model('Model_typeclass','typeclass');	}	//定义方法的调用规则 获取URI第二段值    public function _remap($arg,$arg_array)    {		if($arg == 'add')//添加        {             $this->_add();        }		else if($arg == 'edit')//修改        {             $this->_edit($arg_array);        }		else if($arg == 'del')//修改        {             $this->_del();        }		else if($arg == 'list')//修改        {             $this->_list();        }		else		{			 $this->_index();		}    }	//管理	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);			$sku = $this->input->post('sku',true);			$category = $this->input->post('category',true);			$productweight = $this->input->post('productweight',true);			$grade = $this->input->post('grade',true);			$size = $this->input->post('size',true);			$lowe = $this->input->post('lowe',true);			$color = $this->input->post('color',true);			$where = "1=1 ";			if($sku)            {                $where  .= " and sku = '$sku'";            }			if($category)            {                $where  .= " and category = '$category'";            }			if($productweight)            {                $where  .= " and productweight = '$productweight'";            }			if($grade)            {                $where  .= " and grade = '$grade'";            }			if($size)            {                $where  .= " and size = '$size'";            }			if($lowe)            {                $where  .= " and lowe = '$lowe'";            }			if($color)            {                $where  .= " and color = '$color'";            }            //数据排序            $order_str = "id asc";            if(empty($page))		    {                $start = 0;		    	$perpage = 1;            }		    else		    {                $start = ($page - 1)*$perpage;            }            //取得信息列表            $info_list = $this->product->find_all($where,'id,sku,title,productweight',$order_str,$start,$perpage);			foreach ($info_list as $key=>$value) 		    {				$info_list[$key]['title'] = $value['title'].' '.$value['productweight'];			}		    $total = $this->product->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('product',$this->data);	}	//添加	public function _add()	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['category']))		{			$category = $this->input->post('category',true);			$productweight = $this->input->post('productweight',true);			$hairtype = explode(',',rtrim($this->input->post('hairtype',true),','));			$grade = explode(',',rtrim($this->input->post('grade',true),','));			$size = explode(',',rtrim($this->input->post('size',true),','));			$color = explode(',',rtrim($this->input->post('color',true),','));			$lowe = explode(',',rtrim($this->input->post('lowe',true),','));			$title = "";			$ps = array('productweight'=>$productweight);			$rows = array(array($category),$hairtype,$grade,$size,$color,$lowe);			$key = array('category','hairtype','grade','size','color','lowe');			if($category == 126 || $category == 131|| $category == 132)			{				$number = $this->cp($ps,$rows,$key);			}			else if($category == 127)			{				$type = $this->input->post('type',true);				$headroad = explode(',',rtrim($this->input->post('headroad',true),','));				$density = explode(',',rtrim($this->input->post('density',true),','));				$lacesize = explode(',',rtrim($this->input->post('lacesize',true),','));				if($type == 195 || $type == 197 || $type == 199)				{					array_push($rows,array($type),$headroad,$density,$lacesize);					array_push($key,'type','headroad','density','lacesize');					$number = $this->cp($ps,$rows,$key);				}			    else				{					array_push($rows,array($type),$headroad,$density);					array_push($key,'type','headroad','density');					$number = $this->cp($ps,$rows,$key);				}							}						else if($category == 128)			{				$lacetype = $this->input->post('lacetype',true);				$haircap = explode(',',rtrim($this->input->post('haircap',true),','));				$density = explode(',',rtrim($this->input->post('density',true),','));			    array_push($rows,array($lacetype),$haircap,$density);				array_push($key,'lacetype','haircap','density');				$number = $this->cp($ps,$rows,$key);							}			else if($category == 129)			{				$wide = explode(',',rtrim($this->input->post('wide',true),','));			    array_push($rows,$wide);				array_push($key,'wide');				$number = $this->cp($ps,$rows,$key);							}			else if($category == 130)			{				$weight = explode(',',rtrim($this->input->post('weight',true),','));			    array_push($rows,$weight);				array_push($key,'weight');				$number = $this->cp($ps,$rows,$key);							}			else if($category == 133)			{				$package = explode(',',rtrim($this->input->post('package',true),','));				$unitweight = explode(',',rtrim($this->input->post('unitweight',true),','));			    array_push($rows,$package,$unitweight);				array_push($key,'package','unitweight');				$number = $this->cp($ps,$rows,$key);							}			else if($category == 134)			{				$pieceweight = explode(',',rtrim($this->input->post('pieceweight',true),','));			    array_push($rows,$pieceweight);				array_push($key,'pieceweight');				$number = $this->cp($ps,$rows,$key);							}			if(isset($number))			{			    echo json_encode(array('msg'=>'提交成功,重复数据:'.$number.' 条。','success'=>true));exit;			}			else			{				echo json_encode(array('msg'=>'提交失败,请重试','success'=>true));exit;			}		}		$this->_Template('product_add',$this->data);	}	//修改	public function _edit($arg_array)	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['id']))		{			$list = array();$num = "";$title = "";			$id = $this->input->post('id',true);			$data = $this->product->read($id);			foreach($data as $k=>$v)			{				$post[$k] = 0;            }			$post['id'] = $id;			$post['sku'] = $this->input->post('sku',true);			$post['productweight'] = $this->input->post('productweight',true);			$category = $this->input->post('category',true);			$list['category'] = $category;			$list['hairtype'] = $this->input->post('hairtype',true);			$list['grade'] = $this->input->post('grade',true);			$list['size'] = $this->input->post('size',true);			$list['color'] = $this->input->post('color',true);			$list['lowe'] = $this->input->post('lowe',true);			if($category == 127)			{				$list['type'] = $this->input->post('type',true);				$list['headroad'] = $this->input->post('headroad',true);				$list['density'] = $this->input->post('density',true);				if($list['type'] == 195 || $list['type'] == 197 || $list['type'] == 199)				{					$list['lacesize'] = $this->input->post('lacesize',true);				}			}			else if($category == 128)			{				$list['lacetype'] = $this->input->post('lacetype',true);				$list['haircap'] = $this->input->post('haircap',true);				$list['density'] = $this->input->post('density',true);			}			else if($category == 129)			{				$list['wide'] = $this->input->post('wide',true);			}			else if($category == 130)			{				$list['weight'] = $this->input->post('weight',true);			}			else if($category == 133)			{				$list['package'] = $this->input->post('package',true);				$list['unitweight'] = $this->input->post('unitweight',true);			}			else if($category == 134)			{				$list['pieceweight'] = $this->input->post('pieceweight',true);			}			foreach($list as $k=>$v)			{				if($v != 0)				{					$num .=$v;					$post[$k] = $v;					$typeclass = $this->typeclass->read($v);			        $title = $title.$typeclass['title']." ";				}            }			$post['title'] = rtrim($title,' ');			$post['number'] = $num;			$rows = $this->product->get_number($num);			if(!$rows || $rows['id'] == $id)        	{				$this->product->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];		$product = $this->product->read($arg_array);		$this->data['product'] = $product;		$this->_Template('product_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) 		    {                $this->product->remove($v);            }            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));		}    }	//笛卡儿积	function cp($ps,$sets,$list)	{        $result = array();$j=0;        for($i=0,$count=count($sets); $i<$count-1; $i++)	    {            if($i==0)			{                $result = $sets[$i];            }        $tmp = array();        foreach($result as $res)		{            foreach($sets[$i+1] as $set)			{                $tmp[] = $res.','.$set;            }        }        $result = $tmp;        }        $this->db->trans_begin();        foreach ($result as $v) 	    {		    $post = array();$title = "";$num="";		    $data = explode(',',$v);		    for($i=0;$i<count($list); $i++)		    {			    $post[$list[$i]] = $data[$i];			    $typeclass = $this->typeclass->read($data[$i]);			    $title = $title.$typeclass['title']." ";				if($data[$i] != 0)				{				    $num .=$data[$i];//所有数据ID拼接				}		    }			 foreach ($ps as $kp=>$vp) 	        {				$post[$kp] = $vp;			}			$post['number'] = $num;			$info_list = $this->product->get_number($num);//查询product是否存在此拼接的数据ID			if($info_list)			{				$j++;			}			else			{				$skunum = $this->product->find_count();				$post['sku'] = 'LY'.substr(strval($skunum+1+10000000),1,7);		        $post['title'] = rtrim($title,' ');		        $this->product->insert($post);			}						    }	    if ($this->db->trans_status() === FALSE)        {            $this->db->trans_commit();			return NULL;        }        else        {            $this->db->trans_commit();			return $j;        }    }			//删除    public function _list()	{		$info_list = $this->product->find_all('1=1','id,category,grade,size,color,lowe,type,	lacetype,headroad,haircap,density,lacesize,wide,weight,package,unitweight,pieceweight');		$this->db->trans_begin();		foreach ($info_list as $key=>$value) 		{			$num = '';			foreach ($value as $k=>$v) 		    {				if($v != 0 && $k != 'id')				{				    $num .= $v;				}			}			$this->product->save(array('number'=>$num),$value['id']);		}		if ($this->db->trans_status() === FALSE)        {            $this->db->trans_commit();			echo 1;        }        else        {            $this->db->trans_commit();			echo 2;        }    }}
 |