骑手自动派单平均分配实现

//自动派单脚本
public function autoRider()
{
    //查询骑手设置
    $rider = app('db')->table('st_config_rider')->first();
    if(empty($rider)){
        return response()->json(['code' => 400, 'message' => '骑手配置不存在']);
    }
    if($rider->is_auto !=1){
        return response()->json(['code' => 400, 'message' => '自动派单未开启']);
    }


    //查询所有待分配、配送中、已分配的订单
    $rider_data = app('db')->table('st_order_delivery')
        ->select('id','order_id','mall_id',"delivery_phone","status")
        ->where("channel_index","riderSend")
        ->where("created_at","<=",date("Y-m-d H:i:s",strtotime("-".$rider->send_time." minute")))
        ->whereIn('status',[0,1,7,8,2,3,10])->orderBy('id','desc')->get()->toArray();
    $order_arr=[];  //待分配的订单
    $send_mobile = [];  //配送中的手机号
    $mobile_number = []; //手机号已被分配的订单数

    foreach($rider_data as $item){
        if(in_array($item->status,[0,1,7,8,2])){

            $order_arr[$item->mall_id][]=$item->order_id;
        }else if($item->status==3){
            $send_mobile [] =$item->delivery_phone;
            $mobile_number[$item->delivery_phone] = isset($mobile_number[$item->delivery_phone])?$mobile_number[$item->delivery_phone]+1:1;
        } else{
            $mobile_number[$item->delivery_phone] = isset($mobile_number[$item->delivery_phone])?$mobile_number[$item->delivery_phone]+1:1;
        }
    }

    $rider_send = app("db")->table("st_rider")->select("mobile","mall_id","name","device")
        ->where(['status'=>1,"receive_status"=>1])->orderBy('updated_at','ASC')->get()->toArray();

    $rider_arr = []; //能分配的配送员数组
    $rider_name_arr = []; //手机号 配送名关联数组
    $device_arr = [];
    foreach ($rider_send as $value){
        $device_arr[$value->mobile]=$value->device;
        //不支持离店配送,过滤正在配送的手机号
        if($rider->is_out ==0){
            if(!in_array($value->mobile,$send_mobile)){
                $rider_arr[$value->mall_id][]= $value->mobile;
            }
        }else{
            $rider_arr[$value->mall_id][] = $value->mobile;
        }
        $rider_name_arr[$value->mobile]=$value->name;
    }

    //查询所有待配送和配送中的手机号
    $error_date = [];
    foreach ($order_arr as $k=>$v){ //$k门店id
        //不是该门店的骑手跳出
        if(!isset($rider_arr[$k])){
            continue;
        }
        //计算已分配订单数
        $n=0;
        foreach ($rider_arr[$k] as $val) {
            if(isset($mobile_number[$val])){
                $n+=$mobile_number[$val];
            }
        }
        //ceil((count($v)+$n)/count($rider_arr[$k]))配送员平均接单数向上取整
        //组装配送员配送顺序数组
        //$mobile_number手机号已经分配的订单数
        $arr_rider = [];
        for ($x=0; $x$v1){
            try{
                if(!isset($arr_rider[$k1])){
                    continue;
                }
                if(isset($device_arr[$arr_rider[$k1]])){
                    if (!empty($device_arr[$arr_rider[$k1]])) {
                        if(!in_array($device_arr[$arr_rider[$k1]], $push_device)){
                            $push_device[] = $device_arr[$arr_rider[$k1]];
                        }
                    }
                }
                app("db")->table("st_order_delivery")->where(["order_id"=>$v1])->update([
                    "delivery_phone"=>$arr_rider[$k1],
                    "delivery_name"=>$rider_name_arr[$arr_rider[$k1]],
                    "status"=>10,
                    'updated_at'=>date("Y-m-d H:i:s")
                ]);
            }catch (\Exception $e){
                $error_date[] = "订单号为:".$v1.";错误信息:".$e->getMessage();
            }
        }
        $message = '您有新的配送单,请查看。';
        $ios_source = 'rider_order.caf';
        if (!empty($push_device)) {
            $JPush = new JPush();
            $JPush->send($message, $push_device, ['android_source' => 8,
                'ios_source' => $ios_source]);
        }
    }
    if(empty($error_date)){
        return response()->json(['code' => 200, 'message' => '操作成功!']);
    }else{
        return response()->json(['code' => 200, 'message' => '操作成功!', "data"=>$error_date]);
    }

}

你可能感兴趣的:(骑手自动派单平均分配实现)