Apipf.php 20 KB

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