Apipf.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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,is_tb = 0 where num >= 5");
  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,200);
  81. //$list = $this->customer->find_all("id = 873132","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',50,5);
  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'=>trim($item),
  211. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  212. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  213. ];
  214. }else{
  215. $sku_list[] = [
  216. 'sku'=>trim($item),
  217. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:"0",
  218. 'product_name'=>"",
  219. ];
  220. }
  221. }
  222. $goods_list = [];
  223. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  224. foreach($fpdata_arr as $key => $item){
  225. $tmp_data = explode("|",$item);
  226. if(!empty($tmp_data[1])){
  227. $goods_list[] = [
  228. 'goods_name'=>isset($tmp_data[1])?$tmp_data[1]:"",
  229. 'qty'=>isset($tmp_data[2])?$tmp_data[2]:""
  230. ];
  231. }else{
  232. continue;
  233. }
  234. }
  235. //如果产品种类少于2种 且产品数量也小于2 直接排除
  236. // if(($tmp_type <2) && ($tmp_qty < 2)){
  237. // continue;
  238. // }
  239. // var_dump($v['orderinfo']);
  240. // var_dump($tmp_type);
  241. // var_dump($tmp_qty);
  242. // var_dump("------------------");
  243. //var_dump($jisuan_qty);
  244. $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
  245. $order_num++;//下单次数
  246. $order_qty = $order_qty + $tmp_qty;//订单产品总数
  247. if($tmp_qty > $max_order_qty ){
  248. $max_order_qty = $tmp_qty;
  249. }
  250. if($type == 1){
  251. $ret_list[] = [
  252. 'order_qty'=>$jisuan_qty,
  253. 'shop'=>$v['shop'],
  254. 'orderinfo'=>$v['orderinfo'],
  255. 'sku_list'=>$sku_list,
  256. 'goods_list'=>$goods_list,
  257. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  258. 'dtime'=>date('Y-m-d',$v['dtime']),
  259. 'name'=>$v['name'],
  260. 'email'=>$v['email'],
  261. 'shouldmoney'=>$v['shouldmoney'],
  262. 'shipremarks'=>$v['shipremarks']
  263. ];
  264. }else{
  265. $end_time = strtotime(date("Y-m-d"));
  266. $start_time = $end_time - 24* 60 *60;
  267. //这里只按照出库时间算
  268. if(($v['dtime'] >= $start_time)&&($v['dtime'] < $end_time)){
  269. $ret_list[] = [
  270. 'order_qty'=>$jisuan_qty,
  271. 'shop'=>$v['shop'],
  272. 'orderinfo'=>$v['orderinfo'],
  273. 'sku_list'=>$sku_list,
  274. 'goods_list'=>$goods_list,
  275. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  276. 'dtime'=>date('Y-m-d',$v['dtime']),
  277. 'name'=>$v['name'],
  278. 'email'=>$v['email'],
  279. 'shouldmoney'=>$v['shouldmoney'],
  280. 'shipremarks'=>$v['shipremarks']
  281. ];
  282. }
  283. }
  284. }
  285. $final_list = [];
  286. foreach($ret_list as $k=>$v){
  287. // $orderitems = [];
  288. // foreach($v['sku_list'] as $key1 => $val1){
  289. // $orderitems[] = [
  290. // 'product_name'=>$val1['product_name'],
  291. // 'sku'=>$val1['sku'],
  292. // 'qty'=>$val1['qty'],
  293. // ];
  294. // }
  295. $final_list[] = [
  296. 'shop'=>$v['shop'],
  297. 'increment_id'=>$v['orderinfo'],
  298. 'status'=>empty($v['state'])?"Complete":$v['state'],
  299. 'grand_total'=>$v['shouldmoney'],
  300. 'order_created'=>$v['dtime'],
  301. //'product_name'=>array_column($sku_list,'product_name'),
  302. //'sku'=>array_column($sku_list,'sku'),
  303. //'erp_goods_name'=>array_column($goods_list,'goods_name'),
  304. //'qty'=>array_column($sku_list,'qty'),
  305. //'price'=>$v['shouldmoney'],
  306. 'orderitems'=>[
  307. 'sku'=>array_column($v['sku_list'],'sku'),
  308. 'product_name'=>array_column($v['sku_list'],'product_name'),
  309. 'erp_goods_name'=>array_column($v['goods_list'],'goods_name'),
  310. 'qty'=>array_column($v['sku_list'],'qty'),
  311. ],
  312. ];
  313. }
  314. return [
  315. 'order_mount'=>sprintf("%.2f",$order_mount) ,
  316. 'order_num'=>$order_num,
  317. 'order_qty'=>$order_qty,
  318. 'max_order_qty'=>$max_order_qty,
  319. 'orders'=>$final_list,
  320. ];
  321. }
  322. private function sendHttp($url,$header,$list){
  323. $header = [
  324. 'Content-Type: application/json',
  325. ];
  326. //var_dump($list);
  327. $ch = curl_init();
  328. curl_setopt($ch, CURLOPT_URL, $url);
  329. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  330. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  331. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  332. curl_setopt($ch, CURLOPT_POST, 1);
  333. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($list));
  334. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  335. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  336. $res = curl_exec($ch);
  337. $res = json_decode($res,true);
  338. //var_dump($res);
  339. return $res;
  340. }
  341. //每日更新一回
  342. public function dayUpdate(){
  343. $end_time = strtotime(date("Y-m-d"));
  344. $start_time = $end_time - 24* 60 *60;
  345. $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) " );
  346. $customer_list = [];
  347. foreach($list as $k=>$v){
  348. $r = $this->judgeCon($v);
  349. if($r){
  350. $customer = $this->customer->get_email($v['shop'],$v['email']);
  351. if(!empty($customer)){
  352. $customer_list[] = $customer;
  353. }
  354. }
  355. }
  356. if(empty($list)){
  357. die("There is no data that meets the requirements today");
  358. }
  359. //需要执行的list
  360. //查找用户信息 获取用户的订单列表 然后同步
  361. $send_list = [];
  362. foreach($customer_list as $k=>$v){
  363. $orderinfo = $this->getOrderList($v['email'],$v['shop'],2);
  364. $send_list[] = [
  365. 'customer_id'=>$v['id'],
  366. 'name'=>$v['name'],
  367. 'email'=>$v['email'],
  368. 'telephone'=>$v['phone'],
  369. 'ordered_mount'=>$orderinfo['order_mount'],
  370. 'ordered_num'=>$orderinfo['order_num'],
  371. 'ordered_qty'=>$orderinfo['order_qty'],
  372. 'max_ordered_qty'=>$orderinfo['max_order_qty'],
  373. 'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
  374. // 'country'=>$v['country'],
  375. // 'province'=>$v['province'],
  376. // 'city'=>$v['city'],
  377. // 'street'=>$v['street'],
  378. // 'address'=>$v['address'].$v['address2'],
  379. // 'zipcode'=>$v['zipcode']
  380. 'orders'=>$orderinfo['orders'],
  381. ];
  382. }
  383. }
  384. //判断条件
  385. private function judgeCon($info){
  386. $tmp_q = 0;
  387. $extra_price = empty($info['extra_price'])?[]:json_decode($info['extra_price'],true);
  388. $info['issku'] = strtolower($info['issku']);
  389. if(stripos($info['issku'],'payment difference') !== false){
  390. return false;
  391. }
  392. //如果有价格 说明系统已经可以按照单价进行处理了
  393. if(empty($extra_price)){
  394. //$info['issku'] = strtolower($info['issku']);
  395. if(stripos($info['issku'],',') !== false){
  396. $issku = explode(",",$info['issku']);
  397. }else{
  398. $issku = [$info['issku']];
  399. }
  400. if(stripos($info['quantity'],';') !== false){
  401. $quantity = explode(";",$info['quantity']);
  402. }else{
  403. $quantity = [$info['quantity']];
  404. }
  405. // foreach($issku as $index=>$item){
  406. // if(stripos($item,'gift') !== false){
  407. // }else{
  408. // if(isset($quantity[$index])){
  409. // if(is_numeric($quantity[$index])){
  410. // $tmp_q += $quantity[$index];
  411. // }else{
  412. // }
  413. // }else{
  414. // $tmp_q += 1;
  415. // }
  416. // }
  417. // }
  418. foreach($issku as $index=>$item){
  419. $jishu_flag = true;
  420. foreach($this->filter_words as $val){
  421. //判断sku中是否有这种东西
  422. if (strpos($item,$val ) !== false) {
  423. $jishu_flag = false;
  424. }
  425. }
  426. if($jishu_flag){
  427. if(isset($quantity[$index])){
  428. if(is_numeric($quantity[$index])){
  429. $tmp_q += $quantity[$index];
  430. }else{
  431. // var_dump("不是数组的".$info['number']);
  432. // var_dump("不是数组的".$quantity[$index]);
  433. }
  434. }else{
  435. $tmp_q += 1;
  436. }
  437. }
  438. }
  439. }else{
  440. $whlabel_data = explode("|",trim($info['whlabel'],"|"));
  441. $check_totla = 0;
  442. foreach($whlabel_data as $v){
  443. $tmp_d_arr = explode("-",$v);
  444. if(isset($tmp_d_arr[1])){
  445. $check_totla += $tmp_d_arr[1]*1;
  446. }
  447. }
  448. foreach($extra_price as $k=>$v){
  449. if(isset($v['row_total'])){
  450. //这里不考虑礼物类是因为礼物 本身就是0 无需考虑
  451. if($v['row_total'] >= 50){
  452. if(isset($v['qty'])){
  453. $tmp_q += $v['qty'];
  454. }else{
  455. $tmp_q += 1;
  456. }
  457. }
  458. }
  459. }
  460. if($tmp_q > $check_totla){
  461. $tmp_q = 1;
  462. }
  463. }
  464. //大于等于4
  465. if($tmp_q >= 4){
  466. return true;
  467. }
  468. return false;
  469. }
  470. public function _tjOrder(){
  471. echo "<pre>";
  472. // var_dump(date("Y-m-d H:i:s"));
  473. // $save_list = [];
  474. // $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);
  475. // foreach($list as $k=>$v){
  476. // if(empty($v['fpdata'])){
  477. // continue;
  478. // }
  479. // $r = $this->judgeCon($v);
  480. // if($r){
  481. // $save_list[] = [
  482. // 'number'=>$v['number'],
  483. // 'qty'=>$v['quantity'],
  484. // 'sku'=>$v['issku']
  485. // ];
  486. // }
  487. // }
  488. // var_dump(date("Y-m-d H:i:s"));
  489. // var_dump(count($save_list));
  490. // foreach($save_list as $vv){
  491. // $this->zzrecord_logs->insert($vv);
  492. // usleep(50);
  493. // }
  494. die("over");
  495. // $list = $this->fullorder->find_all(" number = 'ALIP-00211-089' ","*",'id asc',0,300000);
  496. // $r = $this->judgeCon($list[0]);
  497. // var_dump($r);
  498. }
  499. public function _ddAllTb(){
  500. echo "<pre>";
  501. $list = $this->zzrecord_logs->find_all(" 1 = 1",'*','id asc',0,50);
  502. $customer_list = [];
  503. $customer_ids = [];
  504. foreach($list as $k=>$v){
  505. $info = $this->fullorder->get_number($v['number']);
  506. if(!empty($info)){
  507. $customer = $this->customer->get_email($info['email'],$info['shop']);
  508. if(!empty($customer)){
  509. $customer_list[] = $customer;
  510. $customer_ids[] = $customer['id'];
  511. }
  512. }
  513. $this->zzrecord_logs->remove($v['id']);
  514. }
  515. var_dump(json_encode($customer_ids));
  516. $send_list = [];
  517. foreach($customer_list as $k=>$v){
  518. $orderinfo = $this->getOrderList($v['email'],$v['shop'],1);
  519. $send_list[] = [
  520. 'customer_id'=>$v['id'],
  521. 'name'=>$v['name'],
  522. 'email'=>$v['email'],
  523. 'telephone'=>$v['phone'],
  524. 'ordered_mount'=>$orderinfo['order_mount'],
  525. 'ordered_num'=>$orderinfo['order_num'],
  526. 'ordered_qty'=>$orderinfo['order_qty'],
  527. 'max_ordered_qty'=>$orderinfo['max_order_qty'],
  528. 'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
  529. // 'country'=>$v['country'],
  530. // 'province'=>$v['province'],
  531. // 'city'=>$v['city'],
  532. // 'street'=>$v['street'],
  533. // 'address'=>$v['address'].$v['address2'],
  534. // 'zipcode'=>$v['zipcode']
  535. 'orders'=>$orderinfo['orders'],
  536. ];
  537. }
  538. //var_dump($send_list);
  539. if(empty($send_list)){
  540. return ;
  541. }
  542. $url = $this->url;
  543. $header = [];
  544. $res = $this->sendHttp($url,$header,['data'=>$send_list]);
  545. var_dump($res);
  546. }
  547. }