Apipf.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. $send_list = [];
  73. foreach($list as $k=>$v){
  74. $orderinfo = $this->getOrderList($v['email'],$v['shop'],1);
  75. $send_list[] = [
  76. 'customer_id'=>$v['id'],
  77. 'name'=>$v['name'],
  78. 'email'=>$v['email'],
  79. 'telephone'=>$v['phone'],
  80. 'ordered_mount'=>$orderinfo['order_mount'],
  81. 'ordered_num'=>$orderinfo['order_num'],
  82. 'ordered_qty'=>$orderinfo['order_qty'],
  83. // 'country'=>$v['country'],
  84. // 'province'=>$v['province'],
  85. // 'city'=>$v['city'],
  86. // 'street'=>$v['street'],
  87. // 'address'=>$v['address'].$v['address2'],
  88. // 'zipcode'=>$v['zipcode']
  89. 'orders'=>$orderinfo['orders'],
  90. ];
  91. }
  92. if(empty($send_list)){
  93. die("There is no customer data to be synchronized");
  94. }
  95. $header = [];
  96. $url = "";
  97. var_dump($send_list);
  98. die;
  99. foreach($send_list as $item){
  100. if(empty($item['orders'])){
  101. //直接同步更新了吧 都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
  102. $this->customer->save([
  103. 'more_three'=>3,
  104. 'is_tb'=>1,
  105. ],$item['customer_id']);
  106. continue;
  107. }
  108. // $res = $this->sendHttp($url,$header,$item);
  109. // if($res){
  110. // $this->customer->save([
  111. // 'more_three'=>3,
  112. // 'is_tb'=>1,
  113. // ],$item['customer_id']);
  114. // }
  115. }
  116. // die("update crowd_customer set more_three = 2 where id in (".implode(",",$list_ids).")");
  117. // $this->db->query("update crowd_customer set more_three = 3,is_tb = 1 where id in (".implode(",",$list_ids).")");
  118. // $res = $this->sendHttp($url,$header,$send_list);
  119. // if($res){
  120. // $this->db->query("update crowd_customer set more_three = 3,is_tb = 1 where id in (".implode(",",$list_ids).")");
  121. // }
  122. }
  123. /**
  124. * $email 邮箱
  125. * $shop 店铺id
  126. * $type 1 获取全部订单列表 2 只获取昨天新增的订单列表 一般在凌晨1点执行
  127. */
  128. //要求订单必须为线下单,并且为processing或complete
  129. public function getOrderList($email,$shop,$type = 2){
  130. var_dump('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ');
  131. $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');
  132. var_dump($fdata);
  133. $ret_list = [];
  134. $order_mount = 0;//订单总金额
  135. $order_num = 0;//下单次数
  136. $order_qty = 0;//订单产品总数
  137. foreach ($fdata as $k=>$v)
  138. {
  139. //var_dump($v);
  140. $sku_list = [];
  141. $issku_arr = explode(",",trim($v['issku'],','));
  142. $quantity_arr = explode(";",trim($v['quantity'],";"));
  143. $product_arr = explode(",",trim($v['product'],";"));
  144. $tmp_type = 0;
  145. $tmp_qty = 0;
  146. $jisuan_qty = 0;//需要更新的产品的总数量上
  147. //货物的种类按照订单的原始sku种类 数量也是
  148. foreach($issku_arr as $key => $item){
  149. $flag_jishu = false;
  150. foreach($this->filter_words as $val){
  151. //判断sku中是否有这种东西
  152. if (strpos(strtolower($item),$val ) !== false) {
  153. $flag_jishu = true;
  154. }
  155. }
  156. //var_dump($flag_jishu);
  157. if(!$flag_jishu){
  158. $tmp_type++;
  159. if(isset($quantity_arr[$key])){
  160. $tmp_qty = $quantity_arr[$key] + $tmp_qty;
  161. }else{
  162. $tmp_qty = $tmp_qty + 1;
  163. }
  164. }
  165. if(isset($quantity_arr[$key])){
  166. $jisuan_qty = $quantity_arr[$key]*1 + $jisuan_qty;
  167. }else{
  168. $jisuan_qty = $jisuan_qty + 1;
  169. }
  170. $sku_list[] = [
  171. 'sku'=>$item,
  172. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  173. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  174. ];
  175. }
  176. $goods_list = [];
  177. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  178. foreach($fpdata_arr as $key => $item){
  179. $tmp_data = explode("|",$item);
  180. if(!empty($tmp_data[1])){
  181. $goods_list[] = [
  182. 'goods_name'=>$tmp_data[1],
  183. 'qty'=>$tmp_data[2]
  184. ];
  185. }else{
  186. continue;
  187. }
  188. }
  189. //如果产品种类少于2种 且产品数量也小于2 直接排除
  190. if(($tmp_type <2) && ($tmp_qty < 2)){
  191. continue;
  192. }
  193. var_dump($v['orderinfo']);
  194. var_dump($tmp_type);
  195. var_dump($tmp_qty);
  196. var_dump("------------------");
  197. $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
  198. $order_num++;//下单次数
  199. $order_qty = $order_qty + $jisuan_qty;//订单产品总数
  200. if($type == 1){
  201. $ret_list[] = [
  202. 'shop'=>$v['shop'],
  203. 'orderinfo'=>$v['orderinfo'],
  204. 'sku_list'=>$sku_list,
  205. 'goods_list'=>$goods_list,
  206. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  207. 'dtime'=>date('Y-m-d',$v['dtime']),
  208. 'name'=>$v['name'],
  209. 'email'=>$v['email'],
  210. 'shouldmoney'=>$v['shouldmoney'],
  211. 'shipremarks'=>$v['shipremarks']
  212. ];
  213. }else{
  214. $end_time = strtotime(date("Y-m-d"));
  215. $start_time = $end_time - 24* 60 *60;
  216. //这里只按照出库时间算
  217. if(($v['librarytime'] >= $start_time)&&($v['librarytime'] < $end_time)){
  218. $ret_list[] = [
  219. 'shop'=>$v['shop'],
  220. 'orderinfo'=>$v['orderinfo'],
  221. 'sku_list'=>$sku_list,
  222. 'goods_list'=>$goods_list,
  223. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  224. 'dtime'=>date('Y-m-d',$v['dtime']),
  225. 'name'=>$v['name'],
  226. 'email'=>$v['email'],
  227. 'shouldmoney'=>$v['shouldmoney'],
  228. 'shipremarks'=>$v['shipremarks']
  229. ];
  230. }
  231. }
  232. }
  233. $final_list = [];
  234. foreach($ret_list as $k=>$v){
  235. $final_list[] = [
  236. 'shop'=>$v['shop'],
  237. 'orderinfo'=>$v['orderinfo'],
  238. 'status'=>$v['state'],
  239. 'grand_total'=>$v['shouldmoney'],
  240. 'order_created'=>$v['dtime'],
  241. 'product_name'=>array_column($sku_list,'product_name'),
  242. 'sku'=>array_column($sku_list,'sku'),
  243. 'erp_goods_name'=>array_column($goods_list,'goods_name'),
  244. 'qty'=>array_column($sku_list,'qty'),
  245. //'price'=>$v['shouldmoney'],
  246. ];
  247. }
  248. return [
  249. 'ordered_mount'=>$order_mount,
  250. 'order_num'=>$order_num,
  251. 'order_qty'=>$order_qty,
  252. 'orders'=>$final_list,
  253. ];
  254. }
  255. public function _orderTbAllData(){
  256. $typeclass = $this->typeclass->find_all("classid = 29","id,classtitle,title,spare");
  257. $typeclass = array_column($typeclass,null,'id');
  258. $list = $this->customer->find_all("more_three = 2","id,shop,name,email",'id asc',0,100);
  259. foreach($list as $k=>$v){
  260. $order_list = $this->getOrder($v,$typeclass);
  261. if(empty($order_list)){
  262. continue;
  263. }
  264. }
  265. }
  266. private function getOrder($v,$typeclass){
  267. $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');
  268. $ret_list = [];
  269. foreach ($fdata as $k=>$v)
  270. {
  271. $sku_list = [];
  272. $issku_arr = explode(",",trim($v['issku'],','));
  273. $quantity_arr = explode(";",trim($v['quantity'],";"));
  274. $product_arr = explode(",",trim($v['product'],";"));
  275. foreach($issku_arr as $key => $item){
  276. $sku_list[] = [
  277. 'sku'=>$item,
  278. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  279. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  280. ];
  281. }
  282. $goods_list = [];
  283. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  284. if(!empty($tmp_data[1])){
  285. foreach($fpdata_arr as $key => $item){
  286. $tmp_data = explode("|",$item);
  287. $goods_list[] = [
  288. 'goods_name'=>$tmp_data[1],
  289. 'qty'=>$tmp_data[2]
  290. ];
  291. }
  292. }else{
  293. continue;
  294. }
  295. $ret_list[] = [
  296. 'shop'=>$v['shop'],
  297. 'orderinfo'=>$v['orderinfo'],
  298. 'sku_list'=>$sku_list,
  299. 'goods_list'=>$goods_list,
  300. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  301. 'dtime'=>date('Y-m-d',$v['dtime']),
  302. 'name'=>$v['name'],
  303. 'email'=>$v['email'],
  304. 'shouldmoney'=>$v['shouldmoney'],
  305. 'shipremarks'=>$v['shipremarks']
  306. ];
  307. }
  308. return $fdata;
  309. }
  310. public function _orderTbData(){
  311. $fdata = $this->fullorder->find_all('id = 1228433','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
  312. $ret_list = [];
  313. echo "<pre>";
  314. foreach ($fdata as $k=>$v)
  315. {
  316. var_dump($v);
  317. $sku_list = [];
  318. $issku_arr = explode(",",trim($v['issku'],','));
  319. $quantity_arr = explode(";",trim($v['quantity'],";"));
  320. $product_arr = explode(",",trim($v['product'],";"));
  321. foreach($issku_arr as $key => $item){
  322. $sku_list[] = [
  323. 'sku'=>$item,
  324. 'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
  325. 'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
  326. ];
  327. }
  328. $goods_list = [];
  329. $fpdata_arr = explode(";",trim($v['fpdata'],";"));
  330. if(!empty($tmp_data[1])){
  331. foreach($fpdata_arr as $key => $item){
  332. $tmp_data = explode("|",$item);
  333. $goods_list[] = [
  334. 'goods_name'=>$tmp_data[1],
  335. 'qty'=>$tmp_data[2]
  336. ];
  337. }
  338. }else{
  339. continue;
  340. }
  341. $ret_list[] = [
  342. 'shop'=>$v['shop'],
  343. 'orderinfo'=>$v['orderinfo'],
  344. 'sku_list'=>$sku_list,
  345. 'goods_list'=>$goods_list,
  346. 'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
  347. 'dtime'=>date('Y-m-d',$v['dtime']),
  348. 'name'=>$v['name'],
  349. 'email'=>$v['email'],
  350. 'shouldmoney'=>$v['shouldmoney'],
  351. 'shipremarks'=>$v['shipremarks']
  352. ];
  353. }
  354. echo "<pre>";
  355. var_dump($ret_list);
  356. }
  357. private function sendHttp($url,$header,$list){
  358. $ch = curl_init();
  359. curl_setopt($ch, CURLOPT_URL, $url);
  360. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  361. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  362. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
  363. curl_setopt($ch, CURLOPT_POST, 1);
  364. curl_setopt($ch, CURLOPT_POSTFIELDS, $list);
  365. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  366. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  367. $res = curl_exec($ch);
  368. $res = json_decode($res,true);
  369. return $res;
  370. }
  371. }