Apipf.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. }
  15. private $key = "bpng!pjgirv6amnfr"; //加密所需要到的key
  16. private $iv = "k4k!94m66oojtm2w";//加密所需要到的iv
  17. private $api = "202503121009@ly";
  18. private $filter_words = ['bonus','flashsale','clearance','giftpack','gift'];
  19. //定义方法的调用规则 获取URI第二段值
  20. public function _remap($arg,$arg_array)
  21. {
  22. if($arg == 'checkMoreThree')//添加
  23. {
  24. $this->_checkMoreThree();
  25. }
  26. elseif($arg == 'customer_all_data')
  27. {
  28. $this->_customerAllTbData();
  29. }
  30. elseif($arg == 'orderTbAllData')
  31. {
  32. $this->_orderTbAllData();
  33. }
  34. elseif($arg == 'orderTbData')
  35. {
  36. $this->_orderTbData();
  37. }
  38. else
  39. {
  40. exit('No direct script access allowed');
  41. }
  42. }
  43. //检测客户是否超过3次且为传输
  44. public function _checkMoreThree(){
  45. $api = $this->input->get('api',true);
  46. if($api != $this->api){
  47. die("No data to be executed");
  48. }
  49. $hour = date("H");
  50. $minute = date("i");
  51. if($hour != 20){
  52. die("Execution conditions hour do not allow");
  53. }
  54. if(($minute >= 10)&&($minute <=20)){
  55. $this->db->query("update crowd_customer set more_three = 1 where num >= 3 and more_three = 0");
  56. die("No executable data available");
  57. }else{
  58. die("Execution conditions minute do not allow !");
  59. }
  60. }
  61. /***
  62. * 上传没有给批发站没有传递的客户信息
  63. */
  64. public function _customerAllTbData(){
  65. echo "<pre>";
  66. $api = $this->input->get('api',true);
  67. if($api != $this->api){
  68. die("No data to be executed");
  69. }
  70. $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);
  71. //$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);
  72. if(empty($list)){
  73. die("This is a request without data");
  74. }
  75. $send_list = [];
  76. foreach($list as $k=>$v){
  77. $orderinfo = $this->getOrderList($v['email'],$v['shop'],1);
  78. $send_list[] = [
  79. 'customer_id'=>$v['id'],
  80. 'name'=>$v['name'],
  81. 'email'=>$v['email'],
  82. 'telephone'=>$v['phone'],
  83. 'ordered_mount'=>$orderinfo['order_mount'],
  84. 'ordered_num'=>$orderinfo['order_num'],
  85. 'ordered_qty'=>$orderinfo['order_qty'],
  86. // 'country'=>$v['country'],
  87. // 'province'=>$v['province'],
  88. // 'city'=>$v['city'],
  89. // 'street'=>$v['street'],
  90. // 'address'=>$v['address'].$v['address2'],
  91. // 'zipcode'=>$v['zipcode']
  92. 'orders'=>$orderinfo['orders'],
  93. ];
  94. }
  95. if(empty($send_list)){
  96. die("There is no customer data to be synchronized");
  97. }
  98. $header = [];
  99. $url = "";
  100. var_dump($send_list);
  101. die;
  102. foreach($send_list as $item){
  103. if(empty($item['orders'])){
  104. //直接同步更新了吧 都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
  105. $this->customer->save([
  106. 'more_three'=>3,
  107. 'is_tb'=>1,
  108. ],$item['customer_id']);
  109. continue;
  110. }
  111. // $res = $this->sendHttp($url,$header,$item);
  112. // if($res){
  113. // $this->customer->save([
  114. // 'more_three'=>3,
  115. // 'is_tb'=>1,
  116. // ],$item['customer_id']);
  117. // }
  118. }
  119. // die("update crowd_customer set more_three = 2 where id in (".implode(",",$list_ids).")");
  120. // $this->db->query("update crowd_customer set more_three = 3,is_tb = 1 where id in (".implode(",",$list_ids).")");
  121. // $res = $this->sendHttp($url,$header,$send_list);
  122. // if($res){
  123. // $this->db->query("update crowd_customer set more_three = 3,is_tb = 1 where id in (".implode(",",$list_ids).")");
  124. // }
  125. }
  126. //同步已经同步过的订单客户近期订单
  127. public function _customerDepartTbData(){
  128. $api = $this->input->get('api',true);
  129. if($api != $this->api){
  130. die("No data to be executed");
  131. }
  132. $list = $this->customer->find_all("more_three = 3 and is_tb = 0","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,100);
  133. //$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);
  134. if(empty($list)){
  135. die("This is a request without data");
  136. }
  137. $send_list = [];
  138. foreach($list as $k=>$v){
  139. $orderinfo = $this->getOrderList($v['email'],$v['shop'],2);
  140. $send_list[] = [
  141. 'customer_id'=>$v['id'],
  142. 'name'=>$v['name'],
  143. 'email'=>$v['email'],
  144. 'telephone'=>$v['phone'],
  145. 'ordered_mount'=>$orderinfo['order_mount'],
  146. 'ordered_num'=>$orderinfo['order_num'],
  147. 'ordered_qty'=>$orderinfo['order_qty'],
  148. // 'country'=>$v['country'],
  149. // 'province'=>$v['province'],
  150. // 'city'=>$v['city'],
  151. // 'street'=>$v['street'],
  152. // 'address'=>$v['address'].$v['address2'],
  153. // 'zipcode'=>$v['zipcode']
  154. 'orders'=>$orderinfo['orders'],
  155. ];
  156. }
  157. if(empty($send_list)){
  158. die("There is no customer data to be synchronized");
  159. }
  160. $header = [];
  161. $url = "";
  162. var_dump($send_list);
  163. die;
  164. foreach($send_list as $item){
  165. if(empty($item['orders'])){
  166. continue;
  167. }
  168. // $res = $this->sendHttp($url,$header,$item);
  169. // if($res){
  170. // $this->customer->save([
  171. // 'more_three'=>3,
  172. // 'is_tb'=>1,
  173. // ],$item['customer_id']);
  174. // }
  175. }
  176. }
  177. /**
  178. * $email 邮箱
  179. * $shop 店铺id
  180. * $type 1 获取全部订单列表 2 只获取昨天新增的订单列表 一般在凌晨1点执行
  181. */
  182. //要求订单必须为线下单,并且为processing或complete
  183. private function getOrderList($email,$shop,$type = 2){
  184. //var_dump('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ');
  185. $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');
  186. //var_dump($fdata);
  187. $ret_list = [];
  188. $order_mount = 0;//订单总金额
  189. $order_num = 0;//下单次数
  190. $order_qty = 0;//订单产品总数
  191. foreach ($fdata as $k=>$v)
  192. {
  193. //var_dump($v);
  194. $sku_list = [];
  195. $issku_arr = explode(",",trim($v['issku'],','));
  196. $quantity_arr = explode(";",trim($v['quantity'],";"));
  197. $product_arr = explode(",",trim($v['product'],";"));
  198. $tmp_type = 0;
  199. $tmp_qty = 0;
  200. $jisuan_qty = 0;//需要更新的产品的总数量上
  201. //货物的种类按照订单的原始sku种类 数量也是
  202. foreach($issku_arr as $key => $item){
  203. $flag_jishu = false;
  204. foreach($this->filter_words as $val){
  205. //判断sku中是否有这种东西
  206. if (strpos(strtolower($item),$val ) !== false) {
  207. $flag_jishu = true;
  208. }
  209. }
  210. //var_dump($flag_jishu);
  211. if(!$flag_jishu){
  212. $tmp_type++;
  213. if(isset($quantity_arr[$key])){
  214. $tmp_qty = $quantity_arr[$key] + $tmp_qty;
  215. }else{
  216. $tmp_qty = $tmp_qty + 1;
  217. }
  218. }
  219. if(isset($quantity_arr[$key])){
  220. $jisuan_qty = $quantity_arr[$key]*1 + $jisuan_qty;
  221. }else{
  222. $jisuan_qty = $jisuan_qty + 1;
  223. }
  224. $sku_list[] = [
  225. 'sku'=>$item,
  226. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  227. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  228. ];
  229. }
  230. $goods_list = [];
  231. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  232. foreach($fpdata_arr as $key => $item){
  233. $tmp_data = explode("|",$item);
  234. if(!empty($tmp_data[1])){
  235. $goods_list[] = [
  236. 'goods_name'=>$tmp_data[1],
  237. 'qty'=>$tmp_data[2]
  238. ];
  239. }else{
  240. continue;
  241. }
  242. }
  243. //如果产品种类少于2种 且产品数量也小于2 直接排除
  244. if(($tmp_type <2) && ($tmp_qty < 2)){
  245. continue;
  246. }
  247. // var_dump($v['orderinfo']);
  248. // var_dump($tmp_type);
  249. // var_dump($tmp_qty);
  250. // var_dump("------------------");
  251. $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
  252. $order_num++;//下单次数
  253. $order_qty = $order_qty + $jisuan_qty;//订单产品总数
  254. if($type == 1){
  255. $ret_list[] = [
  256. 'shop'=>$v['shop'],
  257. 'orderinfo'=>$v['orderinfo'],
  258. 'sku_list'=>$sku_list,
  259. 'goods_list'=>$goods_list,
  260. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  261. 'dtime'=>date('Y-m-d',$v['dtime']),
  262. 'name'=>$v['name'],
  263. 'email'=>$v['email'],
  264. 'shouldmoney'=>$v['shouldmoney'],
  265. 'shipremarks'=>$v['shipremarks']
  266. ];
  267. }else{
  268. $end_time = strtotime(date("Y-m-d"));
  269. $start_time = $end_time - 24* 60 *60;
  270. //这里只按照出库时间算
  271. if(($v['dtime'] >= $start_time)&&($v['dtime'] < $end_time)){
  272. $ret_list[] = [
  273. 'shop'=>$v['shop'],
  274. 'orderinfo'=>$v['orderinfo'],
  275. 'sku_list'=>$sku_list,
  276. 'goods_list'=>$goods_list,
  277. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  278. 'dtime'=>date('Y-m-d',$v['dtime']),
  279. 'name'=>$v['name'],
  280. 'email'=>$v['email'],
  281. 'shouldmoney'=>$v['shouldmoney'],
  282. 'shipremarks'=>$v['shipremarks']
  283. ];
  284. }
  285. }
  286. }
  287. $final_list = [];
  288. foreach($ret_list as $k=>$v){
  289. $final_list[] = [
  290. 'shop'=>$v['shop'],
  291. 'orderinfo'=>$v['orderinfo'],
  292. 'status'=>$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. ];
  301. }
  302. return [
  303. 'ordered_mount'=>$order_mount,
  304. 'order_num'=>$order_num,
  305. 'order_qty'=>$order_qty,
  306. 'orders'=>$final_list,
  307. ];
  308. }
  309. public function _orderTbAllData(){
  310. $typeclass = $this->typeclass->find_all("classid = 29","id,classtitle,title,spare");
  311. $typeclass = array_column($typeclass,null,'id');
  312. $list = $this->customer->find_all("more_three = 2","id,shop,name,email",'id asc',0,100);
  313. foreach($list as $k=>$v){
  314. $order_list = $this->getOrder($v,$typeclass);
  315. if(empty($order_list)){
  316. continue;
  317. }
  318. }
  319. }
  320. private function getOrder($v,$typeclass){
  321. $fdata = $this->fullorder->find_all('email = "'.$customer['email'].'" and shop = "'.$customer['shop'].'" and mergeid = 0 and reviewtime <= ','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  322. $ret_list = [];
  323. foreach ($fdata as $k=>$v)
  324. {
  325. $sku_list = [];
  326. $issku_arr = explode(",",trim($v['issku'],','));
  327. $quantity_arr = explode(";",trim($v['quantity'],";"));
  328. $product_arr = explode(",",trim($v['product'],";"));
  329. foreach($issku_arr as $key => $item){
  330. $sku_list[] = [
  331. 'sku'=>$item,
  332. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  333. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  334. ];
  335. }
  336. $goods_list = [];
  337. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  338. if(!empty($tmp_data[1])){
  339. foreach($fpdata_arr as $key => $item){
  340. $tmp_data = explode("|",$item);
  341. $goods_list[] = [
  342. 'goods_name'=>$tmp_data[1],
  343. 'qty'=>$tmp_data[2]
  344. ];
  345. }
  346. }else{
  347. continue;
  348. }
  349. $ret_list[] = [
  350. 'shop'=>$v['shop'],
  351. 'orderinfo'=>$v['orderinfo'],
  352. 'sku_list'=>$sku_list,
  353. 'goods_list'=>$goods_list,
  354. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  355. 'dtime'=>date('Y-m-d',$v['dtime']),
  356. 'name'=>$v['name'],
  357. 'email'=>$v['email'],
  358. 'shouldmoney'=>$v['shouldmoney'],
  359. 'shipremarks'=>$v['shipremarks']
  360. ];
  361. }
  362. return $fdata;
  363. }
  364. public function _orderTbData(){
  365. $fdata = $this->fullorder->find_all('id = 1228433','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  366. $ret_list = [];
  367. echo "<pre>";
  368. foreach ($fdata as $k=>$v)
  369. {
  370. var_dump($v);
  371. $sku_list = [];
  372. $issku_arr = explode(",",trim($v['issku'],','));
  373. $quantity_arr = explode(";",trim($v['quantity'],";"));
  374. $product_arr = explode(",",trim($v['product'],";"));
  375. foreach($issku_arr as $key => $item){
  376. $sku_list[] = [
  377. 'sku'=>$item,
  378. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  379. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  380. ];
  381. }
  382. $goods_list = [];
  383. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  384. if(!empty($tmp_data[1])){
  385. foreach($fpdata_arr as $key => $item){
  386. $tmp_data = explode("|",$item);
  387. $goods_list[] = [
  388. 'goods_name'=>$tmp_data[1],
  389. 'qty'=>$tmp_data[2]
  390. ];
  391. }
  392. }else{
  393. continue;
  394. }
  395. $ret_list[] = [
  396. 'shop'=>$v['shop'],
  397. 'orderinfo'=>$v['orderinfo'],
  398. 'sku_list'=>$sku_list,
  399. 'goods_list'=>$goods_list,
  400. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  401. 'dtime'=>date('Y-m-d',$v['dtime']),
  402. 'name'=>$v['name'],
  403. 'email'=>$v['email'],
  404. 'shouldmoney'=>$v['shouldmoney'],
  405. 'shipremarks'=>$v['shipremarks']
  406. ];
  407. }
  408. echo "<pre>";
  409. var_dump($ret_list);
  410. }
  411. private function sendHttp($url,$header,$list){
  412. $ch = curl_init();
  413. curl_setopt($ch, CURLOPT_URL, $url);
  414. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  415. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  416. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  417. curl_setopt($ch, CURLOPT_POST, 1);
  418. curl_setopt($ch, CURLOPT_POSTFIELDS, $list);
  419. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  420. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  421. $res = curl_exec($ch);
  422. $res = json_decode($res,true);
  423. return $res;
  424. }
  425. }