Apipf.php 21 KB

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