Apipf.php 17 KB

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