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'); } 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','giftpack','gift']; 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 == 'customer_depart_data') { $this->_customerDepartTbData(); } 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"); if($hour != 20){ die("Execution conditions hour do not allow"); } if(($minute >= 10)&&($minute <=40)){ $this->db->query("update crowd_customer set more_three = 1 where num >= 3 and more_three = 0"); 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","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 = 71015","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,100);
       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);
            $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'],
                '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 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;
           }
           $data[] = $item; 
           $customer_ids[] = $item['customer_id'];
       }
       var_dump(json_encode($customer_ids));
       //var_dump($data);
       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);
            }
            
        }
    }
    //同步已经同步过的订单客户近期订单
    public function  _customerDepartTbData(){
        $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","id,shop,source,name,email,phone,money,num,country,province,city,street,address,address2,zipcode",'id asc',0,3);
        //$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);
        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);
             $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'],
                 '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 customer data to be synchronized");  
        }
        $header = [];
        $url = $this->url;
        
        var_dump($send_list);
        die;
        foreach($send_list as $item){
            if(empty($item['orders'])){
                 $this->customer->save([
                     'is_tb'=>1,
                 ],$item['customer_id']);
                continue;
            }
             $res = $this->sendHttp($url,$header,[
                    $item
                 ]);
             if($res){
                 $this->customer->save([
                     'is_tb'=>1,
                 ],$item['customer_id']);
             }
        }
    }
    /**
     * $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','id desc');
        //var_dump($fdata);
        $ret_list = [];
        $order_mount = 0;//订单总金额
        $order_num = 0;//下单次数
        $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])){
                        $tmp_qty = $quantity_arr[$key] + $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'=>$item,
                        'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
                        'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
                    ];
                }
                
            }
            $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'=>$tmp_data[1],
                        'qty'=>$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("------------------");
            $order_mount = $order_mount +$v['shouldmoney']*1;//订单总金额
            $order_num++;//下单次数
            $order_qty =  $order_qty + $jisuan_qty;//订单产品总数
           
            if($type == 1){
                $ret_list[] = [
                    '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[] = [
                        '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($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'=>$orderitems,
            ];
        }
        return [
            'order_mount'=>$order_mount,
            'order_num'=>$order_num,
            'order_qty'=>$order_qty,
            'orders'=>$final_list,
        ];
    }
    public function _orderTbAllData(){
        $typeclass = $this->typeclass->find_all("classid = 29","id,classtitle,title,spare");
        $typeclass = array_column($typeclass,null,'id');
       
        $list = $this->customer->find_all("more_three = 2","id,shop,name,email",'id asc',0,100);
        
        foreach($list as $k=>$v){
            $order_list = $this->getOrder($v,$typeclass);
            if(empty($order_list)){
                continue;
            }
        }
    }
    private function getOrder($v,$typeclass){
        $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');
        $ret_list = [];
        foreach ($fdata as $k=>$v) 
        {
            $sku_list = [];
            $issku_arr = explode(",",trim($v['issku'],','));
            $quantity_arr = explode(";",trim($v['quantity'],";"));
            $product_arr = explode(",",trim($v['product'],";"));
            foreach($issku_arr as $key => $item){
                $sku_list[] = [
                    'sku'=>$item,
                    'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
                    'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
                ];
            }
            $goods_list = [];
            $fpdata_arr = explode(";",trim($v['fpdata'],";"));
            if(!empty($tmp_data[1])){
                foreach($fpdata_arr as $key => $item){
                    $tmp_data = explode("|",$item);
                    $goods_list[] = [
                        'goods_name'=>$tmp_data[1],
                        'qty'=>$tmp_data[2]
                    ];
                }
            }else{
                continue;
            }
           
            
            $ret_list[] = [
                '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']
            ];
        }
        return $fdata;
    }
    public function _orderTbData(){
        $fdata =  $this->fullorder->find_all('id = 1228433','shop,orderinfo,issku,quantity,fpdata,state,product,name,email,dtime,shouldmoney,shipremarks','id desc');
        $ret_list = [];
        echo "
";
        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'],";"));
            foreach($issku_arr as $key => $item){
                $sku_list[] = [
                    'sku'=>$item,
                    'qty'=>isset($quantity_arr[$key])?$quantity_arr[$key]:1,
                    'product_name'=>isset($product_arr[$key])?$product_arr[$key]:"",
                ];
            }
            $goods_list = [];
            $fpdata_arr = explode(";",trim($v['fpdata'],";"));
            if(!empty($tmp_data[1])){
                foreach($fpdata_arr as $key => $item){
                    $tmp_data = explode("|",$item);
                    $goods_list[] = [
                        'goods_name'=>$tmp_data[1],
                        'qty'=>$tmp_data[2]
                    ];
                }
            }else{
                continue;
            }
           
            
            $ret_list[] = [
                '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']
            ];
        }
        echo "
";
        var_dump($ret_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;
    }
    
  

}