Apipf.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. * 本类是为了处理 erp协同其他系统,通过其他系统传递的条件 返回所需信息
  5. */
  6. class Apipf extends Start_Controller{
  7. public function __construct(){
  8. parent::__construct();
  9. $this->load->_model('Model_logic_order','logic_order');
  10. $this->load->_model('Model_logic_tools','logic_tools');
  11. $this->load->_model("Model_logic_ding",'logic_ding');
  12. $this->load->_model('Model_customer','customer');
  13. $this->load->_model('Model_typeclass','typeclass');
  14. }
  15. private $url = "http://wholesale.hnwmzp.cn/syncerp/customer";
  16. private $key = "bpng!pjgirv6amnfr"; //加密所需要到的key
  17. private $iv = "k4k!94m66oojtm2w";//加密所需要到的iv
  18. private $api = "202503121009@ly";
  19. private $filter_words = ['bonus','flashsale','clearance','giftpack','gift'];
  20. private $shop_list = [
  21. 1=>'h',
  22. 2=>'as',
  23. 3=>'ap',
  24. 4=>"wk",
  25. 5=>"y",
  26. 6=>"wg",
  27. ];
  28. //定义方法的调用规则 获取URI第二段值
  29. public function _remap($arg,$arg_array)
  30. {
  31. if($arg == 'checkMoreThree')//添加
  32. {
  33. $this->_checkMoreThree();
  34. }
  35. elseif($arg == 'customer_all_data')
  36. {
  37. $this->_customerAllTbData();
  38. }
  39. elseif($arg == 'customer_depart_data')
  40. {
  41. $this->_customerDepartTbData();
  42. }
  43. else
  44. {
  45. exit('No direct script access allowed');
  46. }
  47. }
  48. //检测客户是否超过3次且为传输
  49. public function _checkMoreThree(){
  50. $api = $this->input->get('api',true);
  51. if($api != $this->api){
  52. die("No data to be executed");
  53. }
  54. $hour = date("H");
  55. $minute = date("i");
  56. if($hour != 20){
  57. die("Execution conditions hour do not allow");
  58. }
  59. if(($minute >= 10)&&($minute <=40)){
  60. $this->db->query("update crowd_customer set more_three = 1 where num >= 3 and more_three = 0");
  61. die("No executable data available");
  62. }else{
  63. die("Execution conditions minute do not allow !");
  64. }
  65. }
  66. /***
  67. * 上传没有给批发站没有传递的客户信息
  68. */
  69. public function _customerAllTbData(){
  70. echo "<pre>";
  71. $api = $this->input->get('api',true);
  72. if($api != $this->api){
  73. die("No data to be executed");
  74. }
  75. $list = $this->customer->find_all("more_three = 1","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,200);
  76. //$list = $this->customer->find_all("id = 71015","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,100);
  77. if(empty($list)){
  78. die("This is a request without data");
  79. }
  80. $send_list = [];
  81. foreach($list as $k=>$v){
  82. $orderinfo = $this->getOrderList($v['email'],$v['shop'],1);
  83. $send_list[] = [
  84. 'customer_id'=>$v['id'],
  85. 'name'=>$v['name'],
  86. 'email'=>$v['email'],
  87. 'telephone'=>$v['phone'],
  88. 'ordered_mount'=>$orderinfo['order_mount'],
  89. 'ordered_num'=>$orderinfo['order_num'],
  90. 'ordered_qty'=>$orderinfo['order_qty'],
  91. 'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
  92. // 'country'=>$v['country'],
  93. // 'province'=>$v['province'],
  94. // 'city'=>$v['city'],
  95. // 'street'=>$v['street'],
  96. // 'address'=>$v['address'].$v['address2'],
  97. // 'zipcode'=>$v['zipcode']
  98. 'orders'=>$orderinfo['orders'],
  99. ];
  100. }
  101. if(empty($send_list)){
  102. die("There is no customer data to be synchronized");
  103. }
  104. $header = [];
  105. $url = $this->url;
  106. //var_dump($send_list);
  107. $data = [];
  108. $customer_ids = [];
  109. foreach($send_list as $item){
  110. if(empty($item['orders']) || empty($item['email'])){
  111. //直接同步更新了吧 都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
  112. $this->customer->save([
  113. 'more_three'=>3,
  114. 'is_tb'=>1,
  115. ],$item['customer_id']);
  116. continue;
  117. }
  118. $data[] = $item;
  119. $customer_ids[] = $item['customer_id'];
  120. }
  121. var_dump(json_encode($customer_ids));
  122. //var_dump($data);
  123. if(empty($data)){
  124. return ;
  125. }
  126. $res = $this->sendHttp($url,$header,['data'=>$data]);
  127. var_dump($res);
  128. if($res['status']){
  129. foreach($customer_ids as $k=>$v){
  130. $this->customer->save([
  131. 'more_three'=>3,
  132. 'is_tb'=>1,
  133. ],$v);
  134. }
  135. }
  136. }
  137. //同步已经同步过的订单客户近期订单
  138. public function _customerDepartTbData(){
  139. $api = $this->input->get('api',true);
  140. if($api != $this->api){
  141. die("No data to be executed");
  142. }
  143. $list = $this->customer->find_all("more_three = 3 and is_tb = 0","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,3);
  144. //$list = $this->customer->find_all("id = 71015","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,100);
  145. if(empty($list)){
  146. die("This is a request without data");
  147. }
  148. $send_list = [];
  149. foreach($list as $k=>$v){
  150. $orderinfo = $this->getOrderList($v['email'],$v['shop'],2);
  151. $send_list[] = [
  152. 'customer_id'=>$v['id'],
  153. 'name'=>$v['name'],
  154. 'email'=>$v['email'],
  155. 'telephone'=>$v['phone'],
  156. 'ordered_mount'=>$orderinfo['order_mount'],
  157. 'ordered_num'=>$orderinfo['order_num'],
  158. 'ordered_qty'=>$orderinfo['order_qty'],
  159. 'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
  160. // 'country'=>$v['country'],
  161. // 'province'=>$v['province'],
  162. // 'city'=>$v['city'],
  163. // 'street'=>$v['street'],
  164. // 'address'=>$v['address'].$v['address2'],
  165. // 'zipcode'=>$v['zipcode']
  166. 'orders'=>$orderinfo['orders'],
  167. ];
  168. }
  169. if(empty($send_list)){
  170. die("There is no customer data to be synchronized");
  171. }
  172. $header = [];
  173. $url = $this->url;
  174. //var_dump($send_list);
  175. $data = [];
  176. $customer_ids = [];
  177. foreach($send_list as $item){
  178. if(empty($item['orders']) || empty($item['email'])){
  179. //直接同步更新了吧 都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
  180. $this->customer->save([
  181. 'is_tb'=>1,
  182. ],$item['customer_id']);
  183. continue;
  184. }
  185. $data[] = $item;
  186. $customer_ids[] = $item['customer_id'];
  187. }
  188. var_dump(json_encode($customer_ids));
  189. var_dump($data);
  190. die;
  191. if(empty($data)){
  192. return ;
  193. }
  194. $res = $this->sendHttp($url,$header,['data'=>$data]);
  195. var_dump($res);
  196. if($res['status']){
  197. foreach($customer_ids as $k=>$v){
  198. $this->customer->save([
  199. 'is_tb'=>1,
  200. ],$v);
  201. }
  202. }
  203. }
  204. /**
  205. * $email 邮箱
  206. * $shop 店铺id
  207. * $type 1 获取全部订单列表 2 只获取昨天新增的订单列表 一般在凌晨1点执行
  208. */
  209. //要求订单必须为线下单,并且为processing或complete
  210. private function getOrderList($email,$shop,$type = 2){
  211. //var_dump('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ');
  212. $fdata = $this->fullorder->find_all('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks,librarytime,source','id desc');
  213. //var_dump($fdata);
  214. $ret_list = [];
  215. $order_mount = 0;//订单总金额
  216. $order_num = 0;//下单次数
  217. $order_qty = 0;//订单产品总数
  218. foreach ($fdata as $k=>$v)
  219. {
  220. //var_dump($v);
  221. $sku_list = [];
  222. $issku_arr = explode(",",trim($v['issku'],','));
  223. $quantity_arr = explode(";",trim($v['quantity'],";"));
  224. $product_arr = explode(",",trim($v['product'],";"));
  225. $tmp_type = 0;
  226. $tmp_qty = 0;
  227. $jisuan_qty = 0;//需要更新的产品的总数量上
  228. //货物的种类按照订单的原始sku种类 数量也是
  229. foreach($issku_arr as $key => $item){
  230. $flag_jishu = false;
  231. foreach($this->filter_words as $val){
  232. //判断sku中是否有这种东西
  233. if (strpos(strtolower($item),$val ) !== false) {
  234. $flag_jishu = true;
  235. }
  236. }
  237. //var_dump($flag_jishu);
  238. if(!$flag_jishu){
  239. $tmp_type++;
  240. if(isset($quantity_arr[$key])){
  241. $tmp_qty = $quantity_arr[$key] + $tmp_qty;
  242. }else{
  243. $tmp_qty = $tmp_qty + 1;
  244. }
  245. }
  246. if(isset($quantity_arr[$key])){
  247. $jisuan_qty = $quantity_arr[$key]*1 + $jisuan_qty;
  248. }else{
  249. $jisuan_qty = $jisuan_qty + 1;
  250. }
  251. if(!empty($product_arr[$key])){
  252. $sku_list[] = [
  253. 'sku'=>$item,
  254. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  255. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  256. ];
  257. }
  258. }
  259. $goods_list = [];
  260. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  261. foreach($fpdata_arr as $key => $item){
  262. $tmp_data = explode("|",$item);
  263. if(!empty($tmp_data[1])){
  264. $goods_list[] = [
  265. 'goods_name'=>$tmp_data[1],
  266. 'qty'=>$tmp_data[2]
  267. ];
  268. }else{
  269. continue;
  270. }
  271. }
  272. //如果产品种类少于2种 且产品数量也小于2 直接排除
  273. if(($tmp_type <2) && ($tmp_qty < 2)){
  274. continue;
  275. }
  276. // var_dump($v['orderinfo']);
  277. // var_dump($tmp_type);
  278. // var_dump($tmp_qty);
  279. // var_dump("------------------");
  280. $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
  281. $order_num++;//下单次数
  282. $order_qty = $order_qty + $jisuan_qty;//订单产品总数
  283. if($type == 1){
  284. $ret_list[] = [
  285. 'shop'=>$v['shop'],
  286. 'orderinfo'=>$v['orderinfo'],
  287. 'sku_list'=>$sku_list,
  288. 'goods_list'=>$goods_list,
  289. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  290. 'dtime'=>date('Y-m-d',$v['dtime']),
  291. 'name'=>$v['name'],
  292. 'email'=>$v['email'],
  293. 'shouldmoney'=>$v['shouldmoney'],
  294. 'shipremarks'=>$v['shipremarks']
  295. ];
  296. }else{
  297. $end_time = strtotime(date("Y-m-d"));
  298. $start_time = $end_time - 24* 60 *60;
  299. //这里只按照出库时间算
  300. if(($v['dtime'] >= $start_time)&&($v['dtime'] < $end_time)){
  301. $ret_list[] = [
  302. 'shop'=>$v['shop'],
  303. 'orderinfo'=>$v['orderinfo'],
  304. 'sku_list'=>$sku_list,
  305. 'goods_list'=>$goods_list,
  306. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  307. 'dtime'=>date('Y-m-d',$v['dtime']),
  308. 'name'=>$v['name'],
  309. 'email'=>$v['email'],
  310. 'shouldmoney'=>$v['shouldmoney'],
  311. 'shipremarks'=>$v['shipremarks']
  312. ];
  313. }
  314. }
  315. }
  316. $final_list = [];
  317. foreach($ret_list as $k=>$v){
  318. $orderitems = [];
  319. foreach($sku_list as $key1 => $val1){
  320. $orderitems[] = [
  321. 'product_name'=>$val1['product_name'],
  322. 'sku'=>$val1['sku'],
  323. 'qty'=>$val1['qty'],
  324. ];
  325. }
  326. $final_list[] = [
  327. 'shop'=>$v['shop'],
  328. 'increment_id'=>$v['orderinfo'],
  329. 'status'=>empty($v['state'])?"Complete":$v['state'],
  330. 'grand_total'=>$v['shouldmoney'],
  331. 'order_created'=>$v['dtime'],
  332. //'product_name'=>array_column($sku_list,'product_name'),
  333. //'sku'=>array_column($sku_list,'sku'),
  334. //'erp_goods_name'=>array_column($goods_list,'goods_name'),
  335. //'qty'=>array_column($sku_list,'qty'),
  336. //'price'=>$v['shouldmoney'],
  337. 'orderitems'=>$orderitems,
  338. ];
  339. }
  340. return [
  341. 'order_mount'=>$order_mount,
  342. 'order_num'=>$order_num,
  343. 'order_qty'=>$order_qty,
  344. 'orders'=>$final_list,
  345. ];
  346. }
  347. public function _orderTbAllData(){
  348. $typeclass = $this->typeclass->find_all("classid = 29","id,classtitle,title,spare");
  349. $typeclass = array_column($typeclass,null,'id');
  350. $list = $this->customer->find_all("more_three = 2","id,shop,name,email",'id asc',0,100);
  351. foreach($list as $k=>$v){
  352. $order_list = $this->getOrder($v,$typeclass);
  353. if(empty($order_list)){
  354. continue;
  355. }
  356. }
  357. }
  358. private function getOrder($v,$typeclass){
  359. $fdata = $this->fullorder->find_all('email = "'.$customer['email'].'" and shop = "'.$customer['shop'].'" and mergeid = 0 and reviewtime <= ','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  360. $ret_list = [];
  361. foreach ($fdata as $k=>$v)
  362. {
  363. $sku_list = [];
  364. $issku_arr = explode(",",trim($v['issku'],','));
  365. $quantity_arr = explode(";",trim($v['quantity'],";"));
  366. $product_arr = explode(",",trim($v['product'],";"));
  367. foreach($issku_arr as $key => $item){
  368. $sku_list[] = [
  369. 'sku'=>$item,
  370. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  371. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  372. ];
  373. }
  374. $goods_list = [];
  375. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  376. if(!empty($tmp_data[1])){
  377. foreach($fpdata_arr as $key => $item){
  378. $tmp_data = explode("|",$item);
  379. $goods_list[] = [
  380. 'goods_name'=>$tmp_data[1],
  381. 'qty'=>$tmp_data[2]
  382. ];
  383. }
  384. }else{
  385. continue;
  386. }
  387. $ret_list[] = [
  388. 'shop'=>$v['shop'],
  389. 'orderinfo'=>$v['orderinfo'],
  390. 'sku_list'=>$sku_list,
  391. 'goods_list'=>$goods_list,
  392. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  393. 'dtime'=>date('Y-m-d',$v['dtime']),
  394. 'name'=>$v['name'],
  395. 'email'=>$v['email'],
  396. 'shouldmoney'=>$v['shouldmoney'],
  397. 'shipremarks'=>$v['shipremarks']
  398. ];
  399. }
  400. return $fdata;
  401. }
  402. public function _orderTbData(){
  403. $fdata = $this->fullorder->find_all('id = 1228433','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  404. $ret_list = [];
  405. echo "<pre>";
  406. foreach ($fdata as $k=>$v)
  407. {
  408. var_dump($v);
  409. $sku_list = [];
  410. $issku_arr = explode(",",trim($v['issku'],','));
  411. $quantity_arr = explode(";",trim($v['quantity'],";"));
  412. $product_arr = explode(",",trim($v['product'],";"));
  413. foreach($issku_arr as $key => $item){
  414. $sku_list[] = [
  415. 'sku'=>$item,
  416. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  417. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  418. ];
  419. }
  420. $goods_list = [];
  421. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  422. if(!empty($tmp_data[1])){
  423. foreach($fpdata_arr as $key => $item){
  424. $tmp_data = explode("|",$item);
  425. $goods_list[] = [
  426. 'goods_name'=>$tmp_data[1],
  427. 'qty'=>$tmp_data[2]
  428. ];
  429. }
  430. }else{
  431. continue;
  432. }
  433. $ret_list[] = [
  434. 'shop'=>$v['shop'],
  435. 'orderinfo'=>$v['orderinfo'],
  436. 'sku_list'=>$sku_list,
  437. 'goods_list'=>$goods_list,
  438. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  439. 'dtime'=>date('Y-m-d',$v['dtime']),
  440. 'name'=>$v['name'],
  441. 'email'=>$v['email'],
  442. 'shouldmoney'=>$v['shouldmoney'],
  443. 'shipremarks'=>$v['shipremarks']
  444. ];
  445. }
  446. echo "<pre>";
  447. var_dump($ret_list);
  448. }
  449. private function sendHttp($url,$header,$list){
  450. $header = [
  451. 'Content-Type: application/json',
  452. ];
  453. //var_dump($list);
  454. $ch = curl_init();
  455. curl_setopt($ch, CURLOPT_URL, $url);
  456. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  457. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  458. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  459. curl_setopt($ch, CURLOPT_POST, 1);
  460. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($list));
  461. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  462. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  463. $res = curl_exec($ch);
  464. $res = json_decode($res,true);
  465. //var_dump($res);
  466. return $res;
  467. }
  468. }