Commodity.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Commodity extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_commodity','commodity');
  7. $this->load->_model('Model_commodityread','commodityread');
  8. $this->load->_model('Model_shop','shop');
  9. $this->load->_model('Model_apismt','apismt');
  10. $this->load->_model('Model_typeclass','typeclass');
  11. $this->load->_model('Model_commodityread_del','commodityreaddel');
  12. }
  13. //定义方法的调用规则 获取URI第二段值
  14. public function _remap($arg,$arg_array)
  15. {
  16. if($arg == 'aeorder')//同步
  17. {
  18. $this->_aeorder();
  19. }
  20. else if($arg == 'edit')//修改
  21. {
  22. $this->_edit($arg_array);
  23. }
  24. else if($arg == 'batchedit')//批量修改
  25. {
  26. $this->_batchedit();
  27. }
  28. else if($arg == 'skuedit')//修改
  29. {
  30. $this->_skuedit($arg_array);
  31. }
  32. else
  33. {
  34. $this->_index();
  35. }
  36. }
  37. //管理
  38. public function _index()
  39. {
  40. $dt = 0;
  41. if(isset($_SESSION['api']))
  42. {
  43. $user = $this->user->get_api($_SESSION['api']);
  44. $usp = $user;
  45. $fgshop = "";$sid = "";
  46. $usersp = explode('|',trim($user['shop'],'|'));
  47. foreach ($usersp as $value)
  48. {
  49. $fgshop .= " shop = ".$value." or";
  50. $sid .= " id = ".$value." or";
  51. }
  52. }
  53. $post = $this->input->post(NULL, TRUE);
  54. if(isset($post['page']))
  55. {
  56. $page = $this->input->post('page',true);
  57. $perpage = $this->input->post('perpage',true);
  58. $shop = $this->input->post('shop',true);
  59. $productid = $this->input->post('productid',true);
  60. $title = $this->input->post('title',true);
  61. $type = $this->input->post('type',true);
  62. $code = $this->input->post('code',true);
  63. $skuid = $this->input->post('skuid',true);
  64. $category = $this->input->post('category',true);
  65. $categorytypeclass = $this->typeclass->get_titleclassid($category,16);
  66. $where = "1=1 and (".rtrim($fgshop,'or').")";
  67. if($shop)
  68. {
  69. $where .= " and shop = '$shop'";
  70. }
  71. if($productid)
  72. {
  73. $where .= " and productid = '$productid'";
  74. }
  75. if($title)
  76. {
  77. $where .= " and title like '%$title%'";
  78. }
  79. if($type)
  80. {
  81. $where .= " and type = '$type'";
  82. }
  83. if($category)
  84. {
  85. $where .= " and category like '%".$categorytypeclass['id']."%'";
  86. }
  87. if($code)
  88. {
  89. $u = $this->commodityread->get_sku($code);
  90. $u = $u['productid'];
  91. $where .= " and productid = '$u'";
  92. }
  93. if($skuid)
  94. {
  95. $u = $this->commodityread->get_skuid($skuid);
  96. $u = $u['productid'];
  97. $where .= " and productid = '$u'";
  98. }
  99. //数据排序
  100. $order_str = "time desc";
  101. if(empty($page))
  102. {
  103. $start = 0;
  104. $perpage = 1;
  105. }
  106. else
  107. {
  108. $start = ($page - 1)*$perpage;
  109. }
  110. //取得信息列表
  111. $info_list = $this->commodity->find_all($where,'id,shop,productid,img,title,category,time',$order_str,$start,$perpage);
  112. //格式化数据
  113. foreach ($info_list as $key=>$v)
  114. {
  115. $shop = $this->shop->read($v['shop']);
  116. $info_list[$key]['shop'] = $shop['shopname'];
  117. $info_list[$key]['img'] = "<img src='".$v['img']."' style='height:55px' />";
  118. $category = '';
  119. if($v['category'])
  120. {
  121. $v['category'] = explode(',',trim($v['category'],','));
  122. foreach ($v['category'] as $vv)
  123. {
  124. $typeclass = $this->typeclass->read($vv);
  125. $category .= "<p>".$typeclass['title']."</p>";
  126. }
  127. $info_list[$key]['category'] = $category;
  128. }
  129. else
  130. {
  131. $info_list[$key]['category'] = '';
  132. }
  133. $info_list[$key]['time'] = "<p><a href='http://posting.aliexpress.com/wsproduct/edit_wholesale_product.htm?product_id=".$v['productid']."' target='_blank'>编辑</a></p><p><a href='http://www.aliexpress.com/item/".$v['productid'].".html' target='_blank'>浏览</a></p>";
  134. }
  135. $total = $this->commodity->find_count($where);
  136. $pagenum = ceil($total/$perpage);
  137. $over = $total-($start+$perpage);
  138. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  139. echo json_encode($rows);exit;
  140. }
  141. $wlshop = $this->shop->find_all('1=1 and '.rtrim($sid,'or'));
  142. $this->data['wlshop'] = $wlshop;
  143. $this->_Template('commodity',$this->data);
  144. }
  145. //修改
  146. public function _edit($arg_array)
  147. {
  148. $post = $this->input->post(NULL, TRUE);
  149. if(isset($post['id']))
  150. {
  151. $id = $this->input->post('id',true);
  152. $category = $this->input->post('category',true);
  153. $data = $this->input->post('data');
  154. if($data)
  155. {
  156. $data = explode('|',rtrim($data,'|'));
  157. foreach ($data as $v)
  158. {
  159. $d = explode('@',$v);
  160. $this->commodityread->save(array('sku'=>$d[1]),$d[0]);
  161. }
  162. }
  163. if($this->commodity->save(array('category'=>','.$category),$id))
  164. {
  165. echo json_encode(array('msg'=>'操作成功','success'=>true));exit;
  166. }
  167. else
  168. {
  169. echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit;
  170. }
  171. }
  172. $arg_array = $arg_array[0];
  173. $commodity = $this->commodity->read($arg_array);
  174. $this->data['commodity'] = $commodity;
  175. $sku = $this->commodityread->find_all('productid = '.$commodity['productid']);
  176. $this->data['sku'] = $sku;
  177. $this->_Template('commodity_edit',$this->data);
  178. }
  179. public function _batchedit_b()//老系统形式,保留,不用
  180. {
  181. $dictionaries = $this->typeclass->find_all('ae!= "" and (classid=13 or classid=22 or classid=8 or classid=15 or classid=27 or classid=25 or classid=26 or classid=18 or classid=14 or classid=9 or classid=12 or classid=10 or classid=6)'); //产品人发类型颜色
  182. $dtc = array();$ctd = array();
  183. foreach ($dictionaries as $v)
  184. {
  185. if(stripos($v['spare'],'|') !== false)//如果有多个值
  186. {
  187. $v['spare'] = explode('|',$v['spare']);
  188. foreach ($v['spare'] as $vs)
  189. {
  190. $dtc[strtolower($vs)] = array('classid'=>$v['classid'],'title'=>$v['title'],'id'=>$v['id']);
  191. }
  192. }
  193. else
  194. {
  195. $dtc[strtolower($v['spare'])] = array('classid'=>$v['classid'],'title'=>$v['title'],'id'=>$v['id']);
  196. }
  197. $ctd[$v['id']] = $v['title'];
  198. }
  199. $ctd[126] = 'Hair Weaving';
  200. $ctd[127] = 'Closure';
  201. $ctd[128] = 'Wigs';
  202. $ctd[130] = 'Clip-in Full Head';
  203. $post = $this->input->post(NULL, TRUE);
  204. if(isset($post['id']))
  205. {
  206. $id = $this->input->post('id',true);
  207. $ndata = $this->input->post('data',true);
  208. $id = explode(',',rtrim($id,','));
  209. $data = explode('-',rtrim($ndata,'-'));
  210. $rows = array();
  211. foreach ($id as $val)
  212. {
  213. //a:颜色 b:密度 c:头路设计
  214. $u = $this->commodityread->read($val);
  215. $u['codeid'] = strtolower($u['codeid']);
  216. $u['codeid'] = str_replace(array('stretched length','inches','& '),array('length','',''),$u['codeid']);
  217. $sku = explode(';',rtrim($u['codeid'],';'));
  218. $codeid = array();$whlabel = ''; $fpdata = '';
  219. foreach ($sku as $v)
  220. {
  221. $va = explode(':',$v);
  222. $codeid[$va[0]] = $va[1];
  223. }
  224. $num = '';$ti = '';$tit = '';$error = '';$i = 1;
  225. foreach ($data as $k=>$v)
  226. {
  227. if($v == 'a')
  228. {
  229. if(isset($codeid['color']) && isset($dtc[$codeid['color']]))
  230. {
  231. $num .= $dtc[$codeid['color']]['id'].'-';
  232. $ti .= $dtc[$codeid['color']]['title'].' ';
  233. }
  234. else
  235. {
  236. $num .= '57-';
  237. $ti .= 'Natural Black ';
  238. }
  239. }
  240. else if($v == 'b')
  241. {
  242. if(isset($codeid['density']) && isset($dtc[$codeid['density']]))
  243. {
  244. $num .= $dtc[$codeid['density']]['id'].'-';
  245. $ti .= $dtc[$codeid['density']]['title'].' ';
  246. }
  247. else
  248. {
  249. $num .= '71-';
  250. $ti .= '150% ';
  251. }
  252. }
  253. else if($v == 'c')
  254. {
  255. if(isset($codeid['part design']) && isset($dtc[$codeid['part design']]))
  256. {
  257. $num .= $dtc[$codeid['part design']]['id'].'-';
  258. $ti .= $dtc[$codeid['part design']]['title'].' ';
  259. }
  260. else
  261. {
  262. $num .= '75-';
  263. $ti .= 'Free Part ';
  264. }
  265. }
  266. else
  267. {
  268. if($i == 2)
  269. {
  270. $num .= $v.'-id-';
  271. }
  272. else
  273. {
  274. $num .= $v.'-';
  275. }
  276. $ti .= $ctd[$v].' ';
  277. }
  278. $i++;
  279. }
  280. if(stripos($codeid['length'],'closure') !== false && stripos($ndata,'127') !== false)
  281. {
  282. $v = preg_replace(array('/([\s\S]*)closure/','/([\s\S]*)closure\s/'),array('',''),$codeid['length']);
  283. $v = preg_replace('/([\s]*)/','',$v);
  284. $fpdata = $dtc[$v]['id'].',-'.str_replace('id-','',$num).'|'.$ti.$v.'inch |1|0.00|0.00|'.$v.'|0.00|0.00;';
  285. $tit = '<p>'.$ti.$v.'inch</p>';
  286. $whlabel = '|'.str_replace(array('-','id'),array('',$dtc[$v]['id']),$num).'-1';
  287. }
  288. else
  289. {
  290. $codeid['length'] = explode(' ',$codeid['length']);
  291. foreach ($codeid['length'] as $v)
  292. {
  293. if(is_numeric($v))
  294. {
  295. $fpdata .= $dtc[$v]['id'].',-'.str_replace('id-','',$num).'|'.$ti.$v.'inch |1|0.00|0.00|'.$v.'|0.00|0.00;';
  296. $tit .= '<p>'.$ti.$v.'inch</p>';
  297. $whlabel .= '|'.str_replace(array('-','id'),array('',$dtc[$v]['id']),$num).'-1';
  298. }
  299. else
  300. {
  301. break;
  302. }
  303. }
  304. }
  305. $fa = '';$wl = '';
  306. $u['fpdata'] = explode(';',rtrim($u['fpdata'],';'));
  307. $u['whlabel'] = explode('|',trim($u['whlabel'],'|'));
  308. for($i=0;$i<count($u['fpdata']);$i++)
  309. {
  310. if(stripos($u['fpdata'][$i],$data[0]) === false)
  311. {
  312. if($u['fpdata'][$i] != '')
  313. {
  314. $fa .= $u['fpdata'][$i].';';
  315. $te = explode('|',$u['fpdata'][$i]);
  316. $tit .= '<p>'.$te[1].'</p>';
  317. }
  318. }
  319. if(stripos($u['whlabel'][$i],$data[0]) === false)
  320. {
  321. if($u['whlabel'][$i] != '')
  322. {
  323. $wl .= '|'.$u['whlabel'][$i];
  324. }
  325. }
  326. }
  327. $this->commodityread->save(array('fpdata'=>$fa.$fpdata,'whlabel'=>$wl.$whlabel.'|'),$val);
  328. $rows[] = array('id'=>$val,'title'=>$tit);
  329. }
  330. echo json_encode(array('rows'=>$rows,'success'=>true));exit;
  331. }
  332. }
  333. public function _skuedit($arg_array)
  334. {
  335. $post = $this->input->post(NULL, TRUE);
  336. if(isset($post['id']))
  337. {
  338. $id = $this->input->post('id',true);
  339. $post['fpdata'] = $this->input->post('fpdata',true);
  340. $post['whlabel'] = $this->input->post('whlabel',true);
  341. if($this->commodityread->save($post,$id))
  342. {
  343. echo json_encode(array('msg'=>'操作成功','success'=>true));exit;
  344. }
  345. else
  346. {
  347. echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit;
  348. }
  349. }
  350. $arg_array = $arg_array[0];$fpdata = array();
  351. $commodityread = $this->commodityread->read($arg_array);
  352. if(stripos($commodityread['fpdata'],';') !== false)
  353. {
  354. $fpdata = explode(';',rtrim($commodityread['fpdata'],';'));
  355. foreach ($fpdata as $k=>$v)
  356. {
  357. $fpdata[$k] = explode('|',$v);
  358. }
  359. }
  360. $this->data['fpdata'] = $fpdata;
  361. $this->data['commodityread'] = $commodityread;
  362. $this->_Template('commodityread_edit',$this->data);
  363. }
  364. //获取
  365. public function _aeorder()
  366. {
  367. $sid = '';
  368. if(isset($_SESSION['api']))
  369. {
  370. $user = $this->user->get_api($_SESSION['api']);
  371. $user = explode('|',trim($user['shop'],'|'));
  372. foreach ($user as $value)
  373. {
  374. $sid .= " id = '$value' or";
  375. }
  376. $sid = " and (".rtrim($sid,'or').")";
  377. }
  378. $post = $this->input->post(NULL, TRUE);
  379. if($sid)
  380. {
  381. $shop = $this->shop->find_all("code != '' and type = '270'".$sid);//获取店铺信息
  382. foreach ($shop as $value)
  383. {
  384. $res = array();
  385. for($x=1;$x<5;$x++)
  386. {
  387. $resi = $this->apismt->get_commoditylist($x,100,$value['code']);//获取商品列表
  388. if($resi)
  389. {
  390. $res += $resi;
  391. }
  392. }
  393. if($res)
  394. {
  395. $yzpid = $this->commodity->find_all('shop = '.$value['id']);
  396. foreach ($yzpid as $v)
  397. {
  398. if($v['productid'] == '' || $v['productid'] == '0')
  399. {
  400. $this->commodity->remove($v['id']);
  401. continue;
  402. }
  403. if(!isset($res[$v['productid']]))
  404. {
  405. $this->commodity->save(array('type'=>'offline'),$v['id']);
  406. }
  407. }
  408. foreach ($res as $v)
  409. {
  410. $productid = $this->commodity->get_productid($v);
  411. //if(!$productid)
  412. //{
  413. @$read = $this->apismt->get_commodityread($v,$value['code']);
  414. if(is_array($read))
  415. {
  416. @$cid = $this->apismt->get_commoditysku($read['cid'],$value['code']);
  417. }
  418. else
  419. {
  420. continue;
  421. }
  422. //处理结束
  423. if(isset($cid[0]))
  424. {
  425. // 处理 $cid
  426. $cc = array();
  427. foreach ($cid as $c)
  428. {
  429. $names = json_decode($c['names'],true);
  430. $cc[$c['id']] = $names['en'];
  431. if(!isset($c['values']))
  432. {
  433. continue;
  434. }
  435. if(isset($c['values']['aeop_attr_value_dto'][0]))
  436. {
  437. foreach ($c['values']['aeop_attr_value_dto'] as $cv)
  438. {
  439. if(isset($read['gg'][$cv['id']]))//如果有自定义
  440. {
  441. $cc[$cv['id']] = $read['gg'][$cv['id']];
  442. }
  443. else
  444. {
  445. $cnames = json_decode($cv['names'],true);
  446. $cc[$cv['id']] = $cnames['en'];
  447. }
  448. }
  449. }
  450. else
  451. {
  452. $cnames = json_decode($c['values']['aeop_attr_value_dto']['names'],true);
  453. $cc[$c['values']['aeop_attr_value_dto']['id']] = $cnames['en'];
  454. }
  455. }
  456. $read['title'] = ($read['title'])?$read['title']:'';
  457. if(!$productid)
  458. {
  459. //处理结束
  460. $this->commodity->insert(array('shop'=>$value['id'],'productid'=>$v,'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type'],'time'=>time()));
  461. }
  462. else
  463. {
  464. $this->commodity->save(array('shop'=>$value['id'],'productid'=>$read['productid'],'title'=>$read['title'],'img'=>$read['img'],'type'=>$read['type']),$productid['id']);
  465. }
  466. $this->_aeordersku($read['sku'],$value['id'],$v,$cc);
  467. }
  468. //}
  469. }
  470. }
  471. else
  472. {
  473. continue;
  474. }
  475. }
  476. echo json_encode(array('msg'=>'同步完成!','success'=>true));exit;
  477. }
  478. }
  479. public function _aeordersku($sku,$shop,$productid,$cc)
  480. {
  481. $yzsku = $this->commodityread->find_all("productid = '$productid'");
  482. foreach ($yzsku as $v)
  483. {
  484. $s = 0;
  485. if(!isset($sku[$v['skuid']]))
  486. {
  487. $skuid = explode(';',$v['skuid']);
  488. if(count($skuid) < 2)
  489. {
  490. $this->commodityreaddel->insert($v);
  491. $this->commodityread->remove($v['id']);//asd
  492. continue;
  493. }
  494. else
  495. {
  496. $skuidarray = $this->_qpl($skuid);
  497. foreach ($skuidarray as $skuv)
  498. {
  499. if(isset($sku[$skuv]))
  500. {
  501. $this->commodityread->save(array('skuid'=>$skuv),$v['id']);
  502. $s++;
  503. break 1;
  504. }
  505. }
  506. }
  507. if($s < 1)
  508. {
  509. $this->commodityreaddel->insert($v);
  510. $this->commodityread->remove($v['id']);//asd
  511. }
  512. }
  513. }
  514. foreach ($sku as $v)
  515. {
  516. $read = $this->commodityread->get_productid($productid,$v['skuid']);
  517. if(isset($read['productid']))
  518. {
  519. $cd = explode(';',$v['skuid']);$cod = '';
  520. foreach ($cd as $vv)
  521. {
  522. $cv = explode(':',$vv);
  523. $cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
  524. }
  525. $this->commodityread->save(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()),$read['id']);
  526. }
  527. else
  528. {
  529. $cd = explode(';',$v['skuid']);$cod = '';
  530. foreach ($cd as $vv)
  531. {
  532. $cv = explode(':',$vv);
  533. $cod .=$cc[$cv[0]].":".$cc[$cv[1]].";";
  534. }
  535. $this->commodityread->insert(array('shop'=>$shop,'productid'=>$productid,'codeid'=>$cod,'skuid'=>$v['skuid'],'code'=>$v['code'],'time'=>time()));
  536. }
  537. }
  538. }
  539. public function _qpl($source)//全排列
  540. {
  541. $qpl = array();
  542. sort($source); //保证初始数组是有序的
  543. $last = count($source) - 1; //$source尾部元素下标
  544. $x = $last;
  545. $count = 1; //组合个数统计
  546. $qpl[implode(';', $source)] = implode(';', $source); //输出第一种组合
  547. while (true)
  548. {
  549. $y = $x--; //相邻的两个元素
  550. if ($source[$x] < $source[$y])//如果前一个元素的值小于后一个元素的值
  551. {
  552. $z = $last;
  553. while ($source[$x] > $source[$z]) //从尾部开始,找到第一个大于 $x 元素的值
  554. {
  555. $z--;
  556. }
  557. /* 交换 $x 和 $z 元素的值 */
  558. list($source[$x], $source[$z]) = array($source[$z], $source[$x]);
  559. /* 将 $y 之后的元素全部逆向排列 */
  560. for ($i = $last; $i > $y; $i--, $y++)
  561. {
  562. list($source[$i], $source[$y]) = array($source[$y], $source[$i]);
  563. }
  564. $qpl[implode(';', $source)] = implode(';', $source); //输出组合
  565. $x = $last;
  566. $count++;
  567. }
  568. if ($x == 0)//全部组合完毕
  569. {
  570. break;
  571. }
  572. }
  573. return $qpl;
  574. }
  575. public function _skudelhf($a)//commodityread 同步删除的恢复
  576. {
  577. $this->db->trans_begin();
  578. $qbpid = array();
  579. $a = $this->commodityread->find_all("time > '1653613200'");
  580. foreach ($a as $v)
  581. {
  582. $qbpid[$v['productid']] = $v['productid'];
  583. }
  584. foreach ($qbpid as $valer)
  585. {
  586. $del = $this->commodityreaddel->find_all("productid = '".$valer."' and sku != ''",'*','id asc');
  587. if($del)
  588. {
  589. foreach ($del as $val)
  590. {
  591. $skuid = explode(';',$val['skuid']);
  592. if(count($skuid) < 2)
  593. {
  594. continue;
  595. }
  596. $skuidarray = $this->_qpl($skuid);
  597. $red = $this->commodityread->find_all("productid = '".$val['productid']."' and sku = ''");
  598. foreach ($red as $v)
  599. {
  600. if(isset($skuidarray[$v['skuid']]))
  601. {
  602. $this->commodityread->save(array('sku'=>$val['sku']),$v['id']);
  603. }
  604. }
  605. }
  606. }
  607. }
  608. if ($this->db->trans_status() === TRUE)
  609. {
  610. $this->db->trans_commit();
  611. echo 1;
  612. }
  613. else
  614. {
  615. $this->db->trans_rollback();
  616. echo 2;
  617. }
  618. }
  619. }