Product.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class product extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_product','product');
  7. $this->load->_model('Model_typeclass','typeclass');
  8. }
  9. //定义方法的调用规则 获取URI第二段值
  10. public function _remap($arg,$arg_array)
  11. {
  12. if($arg == 'add')//添加
  13. {
  14. $this->_add();
  15. }
  16. else if($arg == 'edit')//修改
  17. {
  18. $this->_edit($arg_array);
  19. }
  20. else if($arg == 'del')//修改
  21. {
  22. $this->_del();
  23. }
  24. else if($arg == 'list')//修改
  25. {
  26. $this->_list();
  27. }
  28. else
  29. {
  30. $this->_index();
  31. }
  32. }
  33. //管理
  34. public function _index()
  35. {
  36. $post = $this->input->post(NULL, TRUE);
  37. if(isset($post['page']))
  38. {
  39. $page = $this->input->post('page',true);
  40. $perpage = $this->input->post('perpage',true);
  41. $sku = $this->input->post('sku',true);
  42. $category = $this->input->post('category',true);
  43. $productweight = $this->input->post('productweight',true);
  44. $grade = $this->input->post('grade',true);
  45. $size = $this->input->post('size',true);
  46. $lowe = $this->input->post('lowe',true);
  47. $color = $this->input->post('color',true);
  48. $where = "1=1 ";
  49. if($sku)
  50. {
  51. $where .= " and sku = '$sku'";
  52. }
  53. if($category)
  54. {
  55. $where .= " and category = '$category'";
  56. }
  57. if($productweight)
  58. {
  59. $where .= " and productweight = '$productweight'";
  60. }
  61. if($grade)
  62. {
  63. $where .= " and grade = '$grade'";
  64. }
  65. if($size)
  66. {
  67. $where .= " and size = '$size'";
  68. }
  69. if($lowe)
  70. {
  71. $where .= " and lowe = '$lowe'";
  72. }
  73. if($color)
  74. {
  75. $where .= " and color = '$color'";
  76. }
  77. //数据排序
  78. $order_str = "id asc";
  79. if(empty($page))
  80. {
  81. $start = 0;
  82. $perpage = 1;
  83. }
  84. else
  85. {
  86. $start = ($page - 1)*$perpage;
  87. }
  88. //取得信息列表
  89. $info_list = $this->product->find_all($where,'id,sku,title,productweight',$order_str,$start,$perpage);
  90. foreach ($info_list as $key=>$value)
  91. {
  92. $info_list[$key]['title'] = $value['title'].' '.$value['productweight'];
  93. }
  94. $total = $this->product->find_count($where);
  95. $pagenum = ceil($total/$perpage);
  96. $over = $total-($start+$perpage);
  97. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  98. echo json_encode($rows);exit;
  99. }
  100. $this->_Template('product',$this->data);
  101. }
  102. //添加
  103. public function _add()
  104. {
  105. $post = $this->input->post(NULL, TRUE);
  106. if(isset($post['category']))
  107. {
  108. $category = $this->input->post('category',true);
  109. $productweight = $this->input->post('productweight',true);
  110. $hairtype = explode(',',rtrim($this->input->post('hairtype',true),','));
  111. $grade = explode(',',rtrim($this->input->post('grade',true),','));
  112. $size = explode(',',rtrim($this->input->post('size',true),','));
  113. $color = explode(',',rtrim($this->input->post('color',true),','));
  114. $lowe = explode(',',rtrim($this->input->post('lowe',true),','));
  115. $title = "";
  116. $ps = array('productweight'=>$productweight);
  117. $rows = array(array($category),$hairtype,$grade,$size,$color,$lowe);
  118. $key = array('category','hairtype','grade','size','color','lowe');
  119. if($category == 126 || $category == 131|| $category == 132)
  120. {
  121. $number = $this->cp($ps,$rows,$key);
  122. }
  123. else if($category == 127)
  124. {
  125. $type = $this->input->post('type',true);
  126. $headroad = explode(',',rtrim($this->input->post('headroad',true),','));
  127. $density = explode(',',rtrim($this->input->post('density',true),','));
  128. $lacesize = explode(',',rtrim($this->input->post('lacesize',true),','));
  129. if($type == 195 || $type == 197 || $type == 199)
  130. {
  131. array_push($rows,array($type),$headroad,$density,$lacesize);
  132. array_push($key,'type','headroad','density','lacesize');
  133. $number = $this->cp($ps,$rows,$key);
  134. }
  135. else
  136. {
  137. array_push($rows,array($type),$headroad,$density);
  138. array_push($key,'type','headroad','density');
  139. $number = $this->cp($ps,$rows,$key);
  140. }
  141. }
  142. else if($category == 128)
  143. {
  144. $lacetype = $this->input->post('lacetype',true);
  145. $haircap = explode(',',rtrim($this->input->post('haircap',true),','));
  146. $density = explode(',',rtrim($this->input->post('density',true),','));
  147. array_push($rows,array($lacetype),$haircap,$density);
  148. array_push($key,'lacetype','haircap','density');
  149. $number = $this->cp($ps,$rows,$key);
  150. }
  151. else if($category == 129)
  152. {
  153. $wide = explode(',',rtrim($this->input->post('wide',true),','));
  154. array_push($rows,$wide);
  155. array_push($key,'wide');
  156. $number = $this->cp($ps,$rows,$key);
  157. }
  158. else if($category == 130)
  159. {
  160. $weight = explode(',',rtrim($this->input->post('weight',true),','));
  161. array_push($rows,$weight);
  162. array_push($key,'weight');
  163. $number = $this->cp($ps,$rows,$key);
  164. }
  165. else if($category == 133)
  166. {
  167. $package = explode(',',rtrim($this->input->post('package',true),','));
  168. $unitweight = explode(',',rtrim($this->input->post('unitweight',true),','));
  169. array_push($rows,$package,$unitweight);
  170. array_push($key,'package','unitweight');
  171. $number = $this->cp($ps,$rows,$key);
  172. }
  173. else if($category == 134)
  174. {
  175. $pieceweight = explode(',',rtrim($this->input->post('pieceweight',true),','));
  176. array_push($rows,$pieceweight);
  177. array_push($key,'pieceweight');
  178. $number = $this->cp($ps,$rows,$key);
  179. }
  180. if(isset($number))
  181. {
  182. echo json_encode(array('msg'=>'提交成功,重复数据:'.$number.' 条。','success'=>true));exit;
  183. }
  184. else
  185. {
  186. echo json_encode(array('msg'=>'提交失败,请重试','success'=>true));exit;
  187. }
  188. }
  189. $this->_Template('product_add',$this->data);
  190. }
  191. //修改
  192. public function _edit($arg_array)
  193. {
  194. $post = $this->input->post(NULL, TRUE);
  195. if(isset($post['id']))
  196. {
  197. $list = array();$num = "";$title = "";
  198. $id = $this->input->post('id',true);
  199. $data = $this->product->read($id);
  200. foreach($data as $k=>$v)
  201. {
  202. $post[$k] = 0;
  203. }
  204. $post['id'] = $id;
  205. $post['sku'] = $this->input->post('sku',true);
  206. $post['productweight'] = $this->input->post('productweight',true);
  207. $category = $this->input->post('category',true);
  208. $list['category'] = $category;
  209. $list['hairtype'] = $this->input->post('hairtype',true);
  210. $list['grade'] = $this->input->post('grade',true);
  211. $list['size'] = $this->input->post('size',true);
  212. $list['color'] = $this->input->post('color',true);
  213. $list['lowe'] = $this->input->post('lowe',true);
  214. if($category == 127)
  215. {
  216. $list['type'] = $this->input->post('type',true);
  217. $list['headroad'] = $this->input->post('headroad',true);
  218. $list['density'] = $this->input->post('density',true);
  219. if($list['type'] == 195 || $list['type'] == 197 || $list['type'] == 199)
  220. {
  221. $list['lacesize'] = $this->input->post('lacesize',true);
  222. }
  223. }
  224. else if($category == 128)
  225. {
  226. $list['lacetype'] = $this->input->post('lacetype',true);
  227. $list['haircap'] = $this->input->post('haircap',true);
  228. $list['density'] = $this->input->post('density',true);
  229. }
  230. else if($category == 129)
  231. {
  232. $list['wide'] = $this->input->post('wide',true);
  233. }
  234. else if($category == 130)
  235. {
  236. $list['weight'] = $this->input->post('weight',true);
  237. }
  238. else if($category == 133)
  239. {
  240. $list['package'] = $this->input->post('package',true);
  241. $list['unitweight'] = $this->input->post('unitweight',true);
  242. }
  243. else if($category == 134)
  244. {
  245. $list['pieceweight'] = $this->input->post('pieceweight',true);
  246. }
  247. foreach($list as $k=>$v)
  248. {
  249. if($v != 0)
  250. {
  251. $num .=$v;
  252. $post[$k] = $v;
  253. $typeclass = $this->typeclass->read($v);
  254. $title = $title.$typeclass['title']." ";
  255. }
  256. }
  257. $post['title'] = rtrim($title,' ');
  258. $post['number'] = $num;
  259. $rows = $this->product->get_number($num);
  260. if(!$rows || $rows['id'] == $id)
  261. {
  262. $this->product->save($post,$id);
  263. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  264. }
  265. else
  266. {
  267. echo json_encode(array('msg'=>'修改失败,有重复数据','success'=>false));exit;
  268. }
  269. }
  270. $arg_array = $arg_array[0];
  271. $product = $this->product->read($arg_array);
  272. $this->data['product'] = $product;
  273. $this->_Template('product_edit',$this->data);
  274. }
  275. //删除
  276. public function _del()
  277. {
  278. $post = $this->input->post(NULL, TRUE);
  279. if(isset($post['s']))
  280. {
  281. $id_arr = $this->input->post('s');
  282. $id_arr = explode(',',$id_arr);
  283. if(!$id_arr)
  284. {
  285. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  286. }
  287. //循环删除记录
  288. foreach ($id_arr as $v)
  289. {
  290. $this->product->remove($v);
  291. }
  292. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  293. }
  294. }
  295. //笛卡儿积
  296. function cp($ps,$sets,$list)
  297. {
  298. $result = array();$j=0;
  299. for($i=0,$count=count($sets); $i<$count-1; $i++)
  300. {
  301. if($i==0)
  302. {
  303. $result = $sets[$i];
  304. }
  305. $tmp = array();
  306. foreach($result as $res)
  307. {
  308. foreach($sets[$i+1] as $set)
  309. {
  310. $tmp[] = $res.','.$set;
  311. }
  312. }
  313. $result = $tmp;
  314. }
  315. $this->db->trans_begin();
  316. foreach ($result as $v)
  317. {
  318. $post = array();$title = "";$num="";
  319. $data = explode(',',$v);
  320. for($i=0;$i<count($list); $i++)
  321. {
  322. $post[$list[$i]] = $data[$i];
  323. $typeclass = $this->typeclass->read($data[$i]);
  324. $title = $title.$typeclass['title']." ";
  325. if($data[$i] != 0)
  326. {
  327. $num .=$data[$i];//所有数据ID拼接
  328. }
  329. }
  330. foreach ($ps as $kp=>$vp)
  331. {
  332. $post[$kp] = $vp;
  333. }
  334. $post['number'] = $num;
  335. $info_list = $this->product->get_number($num);//查询product是否存在此拼接的数据ID
  336. if($info_list)
  337. {
  338. $j++;
  339. }
  340. else
  341. {
  342. $skunum = $this->product->find_count();
  343. $post['sku'] = 'LY'.substr(strval($skunum+1+10000000),1,7);
  344. $post['title'] = rtrim($title,' ');
  345. $this->product->insert($post);
  346. }
  347. }
  348. if ($this->db->trans_status() === FALSE)
  349. {
  350. $this->db->trans_commit();
  351. return NULL;
  352. }
  353. else
  354. {
  355. $this->db->trans_commit();
  356. return $j;
  357. }
  358. }
  359. //删除
  360. public function _list()
  361. {
  362. $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');
  363. $this->db->trans_begin();
  364. foreach ($info_list as $key=>$value)
  365. {
  366. $num = '';
  367. foreach ($value as $k=>$v)
  368. {
  369. if($v != 0 && $k != 'id')
  370. {
  371. $num .= $v;
  372. }
  373. }
  374. $this->product->save(array('number'=>$num),$value['id']);
  375. }
  376. if ($this->db->trans_status() === FALSE)
  377. {
  378. $this->db->trans_commit();
  379. echo 1;
  380. }
  381. else
  382. {
  383. $this->db->trans_commit();
  384. echo 2;
  385. }
  386. }
  387. }