Apipf.php 17 KB

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