load->_model('Model_logic_order','logic_order'); $this->load->_model('Model_logic_tools','logic_tools'); $this->load->_model("Model_logic_ding",'logic_ding'); $this->load->_model('Model_customer','customer'); $this->load->_model('Model_typeclass','typeclass'); $this->load->_model("Model_zzrecord_logs","zzrecord_logs"); } private $url = "http://wholesale.hnwmzp.cn/syncerp/customer"; private $key = "bpng!pjgirv6amnfr"; //加密所需要到的key private $iv = "k4k!94m66oojtm2w";//加密所需要到的iv private $api = "202503121009@ly"; private $filter_words = ['bonus','flashsale','clearance','gift','payment difference','pack','freewig','zerocostitem','qqsfcw','22inchwig','asfreerobe']; private $shop_list = [ 1=>'h', 2=>'as', 3=>'ap', 4=>"wk", 5=>"y", 6=>"wg", ]; //定义方法的调用规则 获取URI第二段值 public function _remap($arg,$arg_array) { if($arg == 'checkMoreThree')//添加 { $this->_checkMoreThree(); } elseif($arg == 'customer_all_data') { $this->_customerAllTbData(); }elseif($arg == 'day_update_c'){ $this->_dayUpdateCustomer(); }elseif($arg == 'day_u'){ $this->_dayUpdate(); } // elseif($arg == 'tjorder') // { // $this->_tjOrder(); // } // elseif($arg == 'dd_all_tb'){ // $this->_ddAllTb(); // } else { exit('No direct script access allowed'); } } //检测客户是否超过3次且为传输 public function _checkMoreThree(){ $api = $this->input->get('api',true); if($api != $this->api){ die("No data to be executed"); } $hour = date("H"); $minute = date("i"); //$this->db->query("update crowd_customer set more_three = 1,is_tb = 0 where num >= 5"); if($hour !=19){ die("Execution conditions hour do not allow"); } if(($minute >= 0)&&($minute <=20)){ $this->db->query("update crowd_customer set more_three = 1 where num >= 5 and more_three = 0 and shop in (1,2,3,4,5,6)"); die("No executable data available"); }else{ die("Execution conditions minute do not allow !"); } } /*** * 上传没有给批发站没有传递的客户信息 */ public function _customerAllTbData(){ echo "
";
       
       $api = $this->input->get('api',true);
       if($api != $this->api){
            die("No data to be executed");
       }
       $list = $this->customer->find_all("more_three = 1 and shop in (1,2,3,4,5,6) ","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,200);
       //$list = $this->customer->find_all("id = 873132","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',50,5);
       if(empty($list)){
           die("This is a request without data");  
       }
       
       $send_list = [];
       foreach($list as $k=>$v){
            $orderinfo = $this->getOrderList($v['email'],$v['shop'],1);
            if(count($orderinfo['orders']) > 4){
                $send_list[] = [
                    'customer_id'=>$v['id'],
                    'name'=>$v['name'],
                    'email'=>$v['email'],
                    'telephone'=>$v['phone'],
                    'ordered_mount'=>$orderinfo['order_mount'],
                    'ordered_num'=>$orderinfo['order_num'],
                    'ordered_qty'=>$orderinfo['order_qty'],
                    'max_ordered_qty'=>$orderinfo['max_order_qty'],
                    'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
                    // 'country'=>$v['country'],
                    // 'province'=>$v['province'],
                    // 'city'=>$v['city'],
                    // 'street'=>$v['street'],
                    // 'address'=>$v['address'].$v['address2'],
                    // 'zipcode'=>$v['zipcode']
                    'orders'=>$orderinfo['orders'],
                ];
            }else{
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$v['id']);
            }
            

       }
      
       if(empty($send_list)){
          die("There is no customer data to be synchronized");  
       }
       $header = [];
       $url = $this->url;
       
       //var_dump($send_list);
       $data = [];
       $customer_ids = [];
       foreach($send_list as $item){
           if(empty($item['orders']) || empty($item['email'])){
               //直接同步更新了吧  都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$item['customer_id']);
               continue;
           }
           if($item['brand'] == 'other'){
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$item['customer_id']);
               continue;
           }
           $data[] = $item; 
           $customer_ids[] = $item['customer_id'];
       }
       
       var_dump(json_encode($customer_ids));
    //   var_dump($data);
    //   die;
       if(empty($data)){
            return ;
       }
       $res = $this->sendHttp($url,$header,['data'=>$data]);
        var_dump($res);
        if($res['status']){
            foreach($customer_ids as $k=>$v){
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$v);
            }
            
        }
    }

    /**
     * 更新满足5的且已经同步过后的,新的订单
     */
    public function _dayUpdateCustomer(){
        echo "
";
       
       $api = $this->input->get('api',true);
       if($api != $this->api){
            die("No data to be executed");
       }
       $list = $this->customer->find_all("more_three = 3 and is_tb = 0 and shop in (1,2,3,4,5,6)","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,200);
       //$list = $this->customer->find_all("id = 873132","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',50,5);
       if(empty($list)){
           die("This is a request without data");  
       }
       
       $send_list = [];
       foreach($list as $k=>$v){
            $orderinfo = $this->getOrderList($v['email'],$v['shop'],2);
            if(count($orderinfo['orders']) > 4){
                $send_list[] = [
                    'customer_id'=>$v['id'],
                    'name'=>$v['name'],
                    'email'=>$v['email'],
                    'telephone'=>$v['phone'],
                    'ordered_mount'=>$orderinfo['order_mount'],
                    'ordered_num'=>$orderinfo['order_num'],
                    'ordered_qty'=>$orderinfo['order_qty'],
                    'max_ordered_qty'=>$orderinfo['max_order_qty'],
                    'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
                    // 'country'=>$v['country'],
                    // 'province'=>$v['province'],
                    // 'city'=>$v['city'],
                    // 'street'=>$v['street'],
                    // 'address'=>$v['address'].$v['address2'],
                    // 'zipcode'=>$v['zipcode']
                    'orders'=>$orderinfo['orders'],
                ];
            }else{
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$v['id']);
            }
            

       }
      
       if(empty($send_list)){
          die("There is no customer data to be synchronized");  
       }
    
       //var_dump($send_list);
       $data = [];
       $customer_ids = [];
       foreach($send_list as $item){
           if(empty($item['orders']) || empty($item['email'])){
               //直接同步更新了吧  都满足条件了 结果还没订单 就先不查询了 等有新的订单在判断一遍
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$item['customer_id']);
               continue;
           }
           if($item['brand'] == 'other'){
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$item['customer_id']);
               continue;
           }
           $data[] = $item; 
           $customer_ids[] = $item['customer_id'];
       }
       
       var_dump(json_encode($customer_ids));
    //   var_dump($data);
    //   die;
       if(empty($data)){
            return ;
       }
       
        var_dump(json_encode($data,JSON_UNESCAPED_UNICODE));
        $url = $this->url;
        $header = [];
        $res = $this->sendHttp($url,$header,['data'=>$data]);
        var_dump($res);

        if($res['status']){
            foreach($customer_ids as $k=>$v){
                $this->customer->save([
                    'more_three'=>3,
                    'is_tb'=>1,
                ],$v);
            }
            
        }
    
    }
   
    /**
     * $email  邮箱
     * $shop   店铺id
     * $type   1 获取全部订单列表  2 只获取昨天新增的订单列表  一般在凌晨1点执行
     */
    //要求订单必须为线下单,并且为processing或complete
    private function getOrderList($email,$shop,$type = 2){
        //var_dump('email = "'.$email.'" and shop = "'.$shop.'" and source != 1 and state in (207,216) ');
        $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,shopify','id desc');
        //var_dump($fdata);
        $ret_list = [];
        $order_mount = 0;//订单总金额
        $order_num = 0;//下单次数
        $order_qty = 0;//订单产品总数
        $max_order_qty = 0;
        foreach ($fdata as $k=>$v) 
        {
            //var_dump($v);
            $sku_list = [];
            $issku_arr = explode(",",trim($v['issku'],','));
            $quantity_arr = explode(";",trim($v['quantity'],";"));
            $product_arr = explode(",",trim($v['product'],";"));
            $tmp_type = 0;
            $tmp_qty = 0;
            $jisuan_qty = 0;//需要更新的产品的总数量上
            //货物的种类按照订单的原始sku种类 数量也是
            foreach($issku_arr as $key => $item){
                $flag_jishu = false;
                foreach($this->filter_words as $val){
                    //判断sku中是否有这种东西
                    if (strpos(strtolower($item),$val ) !== false) {
                        $flag_jishu = true;
                    } 
                    
                }
                //var_dump($flag_jishu);
                if(!$flag_jishu){
                    $tmp_type++;
                    if(isset($quantity_arr[$key])){
                        if(is_numeric($quantity_arr[$key])){
                            $tmp_qty = $quantity_arr[$key]*1 + $tmp_qty;
                        }
                    }else{
                        $tmp_qty = $tmp_qty + 1;
                    }
                }
                if(isset($quantity_arr[$key])){
                    $jisuan_qty = $quantity_arr[$key]*1 +  $jisuan_qty;
                }else{
                    $jisuan_qty =  $jisuan_qty + 1;
                }
               
                
                if(!empty($product_arr[$key])){
                    $sku_list[] = [
                        'sku'=>trim($item),
                        'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
                        'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
                    ];
                }else{
                    $sku_list[] = [
                        'sku'=>trim($item),
                        'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:"0",
                        'product_name'=>"",
                    ];
                }
                
            }
            $goods_list = [];
            $fpdata_arr = explode(";",trim($v['fpdata'],";"));
            foreach($fpdata_arr as $key => $item){
                $tmp_data = explode("|",$item);
                if(!empty($tmp_data[1])){
                    $goods_list[] = [
                        'goods_name'=>isset($tmp_data[1])?$tmp_data[1]:"",
                        'qty'=>isset($tmp_data[2])?$tmp_data[2]:""
                    ];
                }else{
                    continue;
                }
            }
            
            //如果产品种类少于2种 且产品数量也小于2 直接排除
            // if(($tmp_type <2) && ($tmp_qty < 2)){
            //     continue;
            // }
            // var_dump($v['orderinfo']);
            // var_dump($tmp_type);
            // var_dump($tmp_qty);
            // var_dump("------------------");
            //var_dump($jisuan_qty);
           
           
            $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
            $order_num++;//下单次数
            $order_qty =  $order_qty + $tmp_qty;//订单产品总数
            if($tmp_qty >  $max_order_qty ){
                $max_order_qty = $tmp_qty;
            }
            if($v['shop'] == 5){
                $v['orderinfo'] = isset($v['shopify'])?$v['shopify']:$v['orderinfo'];
            }
            if($type == 1){
                $ret_list[] = [
                    'order_qty'=>$jisuan_qty,
                    'shop'=>$v['shop'],
                    'orderinfo'=>$v['orderinfo'],
                    'sku_list'=>$sku_list,
                    'goods_list'=>$goods_list,
                    'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
                    'dtime'=>date('Y-m-d',$v['dtime']),
                    'name'=>$v['name'],
                    'email'=>$v['email'],
                    'shouldmoney'=>$v['shouldmoney'],
                    'shipremarks'=>$v['shipremarks']
                ];
            }else{
                $end_time = strtotime(date("Y-m-d"));
                $start_time = $end_time - 24* 60 *60;
                //这里只按照出库时间算 
                if(($v['dtime'] >= $start_time)&&($v['dtime'] < $end_time)){
                    $ret_list[] = [
                        'order_qty'=>$jisuan_qty,
                        'shop'=>$v['shop'],
                        'orderinfo'=>$v['orderinfo'],
                        'sku_list'=>$sku_list,
                        'goods_list'=>$goods_list,
                        'state'=>isset($typeclass[$v['state']])?$typeclass[$v['state']]['spare']:"",
                        'dtime'=>date('Y-m-d',$v['dtime']),
                        'name'=>$v['name'],
                        'email'=>$v['email'],
                        'shouldmoney'=>$v['shouldmoney'],
                        'shipremarks'=>$v['shipremarks']
                    ];
                }
               
            }
           
        }
    
        $final_list = [];
        foreach($ret_list as $k=>$v){
            // $orderitems = [];
            // foreach($v['sku_list'] as $key1 => $val1){
            //     $orderitems[] = [
            //         'product_name'=>$val1['product_name'],
            //         'sku'=>$val1['sku'],
            //         'qty'=>$val1['qty'],
            //     ];
            // }
            $final_list[] = [
                'shop'=>$v['shop'],
                'increment_id'=>$v['orderinfo'],
                'status'=>empty($v['state'])?"Complete":$v['state'],
                'grand_total'=>$v['shouldmoney'],
                'order_created'=>$v['dtime'],
                //'product_name'=>array_column($sku_list,'product_name'),
                //'sku'=>array_column($sku_list,'sku'),
                //'erp_goods_name'=>array_column($goods_list,'goods_name'),
                //'qty'=>array_column($sku_list,'qty'),
                //'price'=>$v['shouldmoney'],
                'orderitems'=>[
                    'sku'=>array_column($v['sku_list'],'sku'),
                    'product_name'=>array_column($v['sku_list'],'product_name'),
                    'erp_goods_name'=>array_column($v['goods_list'],'goods_name'),
                    'qty'=>array_column($v['sku_list'],'qty'),
                ],
            ];
        }
       

        return [
            'order_mount'=>sprintf("%.2f",$order_mount) ,
            'order_num'=>$order_num,
            'order_qty'=>$order_qty,
            'max_order_qty'=>$max_order_qty,
            'orders'=>$final_list,
        ];
    }
    
   
    

    private function sendHttp($url,$header,$list){
        $header = [
             'Content-Type: application/json',
        ];
        //var_dump($list);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
		curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($list));
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $res = curl_exec($ch);
		$res = json_decode($res,true);
		//var_dump($res);
	    
		return $res;
    }
   //每日更新一回 
   public function _dayUpdate(){
    $api = $this->input->get('api',true);
      if($api != $this->api){
            die("No data to be executed");
     }
     $end_time = strtotime(date("Y-m-d"));
     $start_time = $end_time - 24* 60 *60;
     $list = $this->fullorder->find_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) " );
     $customer_list = [];
     $customer_ids = [];
     foreach($list as $k=>$v){
        $r = $this->judgeCon($v);
        if($r){
            $customer = $this->customer->get_email($v['shop'],$v['email']);
            if(!empty($customer)){
                $customer_list[] = $customer;
                $customer_ids[] = $v['id'];

            }
        }
     }
    var_dump(json_encode($customer_ids));
    if(empty($customer_list)){
        die("There is no data that meets the requirements today");  
    }
     //需要执行的list
     //查找用户信息  获取用户的订单列表  然后同步
    $send_list = [];
    foreach($customer_list as $k=>$v){
        $orderinfo = $this->getOrderList($v['email'],$v['shop'],2);
        $send_list[] = [
            'customer_id'=>$v['id'],
            'name'=>$v['name'],
            'email'=>$v['email'],
            'telephone'=>$v['phone'],
            'ordered_mount'=>$orderinfo['order_mount'],
            'ordered_num'=>$orderinfo['order_num'],
            'ordered_qty'=>$orderinfo['order_qty'],
            'max_ordered_qty'=>$orderinfo['max_order_qty'],
            'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
            // 'country'=>$v['country'],
            // 'province'=>$v['province'],
            // 'city'=>$v['city'],
            // 'street'=>$v['street'],
            // 'address'=>$v['address'].$v['address2'],
            // 'zipcode'=>$v['zipcode']
            'orders'=>$orderinfo['orders'],
        ];
    }
    if(empty($send_list)){
        die("There is no queue to execute");  
    }
    var_dump(json_encode($send_list,JSON_UNESCAPED_UNICODE));
    $url = $this->url;
    $header = [];
    $res = $this->sendHttp($url,$header,['data'=>$send_list]);
    var_dump($res);

   } 

   //判断条件
   private function judgeCon($info){
        $tmp_q = 0; 
        $extra_price = empty($info['extra_price'])?[]:json_decode($info['extra_price'],true);
        
         $info['issku'] = strtolower($info['issku']);
         if(stripos($info['issku'],'payment difference') !== false){
             return false;
         }
        //如果有价格 说明系统已经可以按照单价进行处理了
        if(empty($extra_price)){
            //$info['issku'] = strtolower($info['issku']);
            if(stripos($info['issku'],',') !== false){
                $issku = explode(",",$info['issku']);
            }else{
                $issku = [$info['issku']];
            }
            if(stripos($info['quantity'],';') !== false){
                $quantity = explode(";",$info['quantity']);
            }else{
                $quantity = [$info['quantity']];
            }
            // foreach($issku as $index=>$item){
            //     if(stripos($item,'gift') !== false){
                    
            //     }else{
                   
            //         if(isset($quantity[$index])){
            //             if(is_numeric($quantity[$index])){
            //                 $tmp_q += $quantity[$index];
            //             }else{
                          
            //             }
                        
            //         }else{
            //             $tmp_q += 1;
            //         }
            //     }

            // }
            
            foreach($issku as $index=>$item){
                $jishu_flag = true;
                foreach($this->filter_words as $val){
                    //判断sku中是否有这种东西
                    if (strpos($item,$val ) !== false) {
                       $jishu_flag = false;
                    } 
                    
                }
                if($jishu_flag){
                    if(isset($quantity[$index])){
                        if(is_numeric($quantity[$index])){
                            $tmp_q += $quantity[$index];
                        }else{
                            // var_dump("不是数组的".$info['number']);
                            // var_dump("不是数组的".$quantity[$index]);
                        }
                        
                    }else{
                        $tmp_q += 1;
                    }
                        
                }
               
            }
            
        }else{
            $whlabel_data = explode("|",trim($info['whlabel'],"|"));
            $check_totla = 0;
            foreach($whlabel_data as $v){
                $tmp_d_arr = explode("-",$v);
                if(isset($tmp_d_arr[1])){
                    $check_totla += $tmp_d_arr[1]*1;
                }
            }
            foreach($extra_price as $k=>$v){
                if(isset($v['row_total'])){
                    //这里不考虑礼物类是因为礼物 本身就是0 无需考虑
                    if($v['row_total'] >= 50){
                        if(isset($v['qty'])){
                            $tmp_q += $v['qty'];
                        }else{
                            $tmp_q += 1;
                        }
                    }
                }

            }
            if($tmp_q > $check_totla){
                $tmp_q = 1;
            }
        }
        //大于等于4
        if($tmp_q >= 4){
            return true;
        }
        return false;
   }
    
   

   public function _tjOrder(){
    die;
        echo "
";
        // var_dump(date("Y-m-d H:i:s"));
        // $save_list = [];
        // $list = $this->fullorder->find_all(" shop in (1,2,3,4,5,6) and source != 1 and state in (207,216) ","*",'id asc',900000,300000);
        // foreach($list as $k=>$v){
        //     if(empty($v['fpdata'])){
        //         continue;
        //     }
        //     $r = $this->judgeCon($v);
        //     if($r){
        //         $save_list[] = [
        //                 'number'=>$v['number'],
        //                 'qty'=>$v['quantity'],
        //                 'sku'=>$v['issku']
        //             ];
        //     }
        // }
        // var_dump(date("Y-m-d H:i:s"));
        // var_dump(count($save_list));
        // foreach($save_list as $vv){
        //         $this->zzrecord_logs->insert($vv);
        //         usleep(50);
        // }
        die("over");
        
        // $list = $this->fullorder->find_all(" number = 'ALIP-00211-089' ","*",'id asc',0,300000);
        // $r = $this->judgeCon($list[0]);
        // var_dump($r);
   }
   
   public function _ddAllTb(){
    die;
       echo "
";
       $list = $this->zzrecord_logs->find_all(" 1 = 1",'*','id asc',0,50);
      
       $customer_list = [];
       $customer_ids = [];
       foreach($list as $k=>$v){
           $info = $this->fullorder->get_number($v['number']);
           if(!empty($info)){
               $customer = $this->customer->get_email($info['email'],$info['shop']);
                if(!empty($customer)){
                    $customer_list[] = $customer;
                    $customer_ids[] = $customer['id'];
                }
           }
          $this->zzrecord_logs->remove($v['id']);
       }
       var_dump(json_encode($customer_ids));
       
       $send_list = [];
       foreach($customer_list as $k=>$v){
            $orderinfo = $this->getOrderList($v['email'],$v['shop'],1);
            $send_list[] = [
                'customer_id'=>$v['id'],
                'name'=>$v['name'],
                'email'=>$v['email'],
                'telephone'=>$v['phone'],
                'ordered_mount'=>$orderinfo['order_mount'],
                'ordered_num'=>$orderinfo['order_num'],
                'ordered_qty'=>$orderinfo['order_qty'],
                'max_ordered_qty'=>$orderinfo['max_order_qty'],
                'brand'=>isset($this->shop_list[$v['shop']])?$this->shop_list[$v['shop']]:"other",
                // 'country'=>$v['country'],
                // 'province'=>$v['province'],
                // 'city'=>$v['city'],
                // 'street'=>$v['street'],
                // 'address'=>$v['address'].$v['address2'],
                // 'zipcode'=>$v['zipcode']
                'orders'=>$orderinfo['orders'],
            ];
        }
        //var_dump($send_list);
       if(empty($send_list)){
            return ;
       }
       $url = $this->url;
       $header = [];
       $res = $this->sendHttp($url,$header,['data'=>$send_list]);
       var_dump($res);
      
       
       
   }

}