connect('127.0.0.1', 11211); $PoolStatus = $mem->get("orderPoolStatus[$runID]"); //添加runid标记是为了区别是哪个时间的班次 $unlock_orderlist = $mem->get("orderList[$runID]"); //拿到当前的未锁定的订单池进行进一步操作 $orderService = new createOrderService(); if ($PoolStatus) //存在订单状态池 { foreach ($PoolStatus as $k => $v) { if (isset($v['status']) && $v['status'] == 1) { //还有未锁定的订单池 $orderService->stock($runID); $value = $unlock_orderlist[$k]; $pool_passager_num = $orderService->poolNum($value); //计算该订单池中总共的乘客数 if (!$value || $pool_passager_num + $num > 6) { //说明该订单池不能加入该订单 continue; } $begin_x_y = $orderService->timeMatrix($value, $beginX_Y, 1); //获取所有起点时间矩阵 $end_x_y = $orderService->timeMatrix($value, $endX_Y, 2); //获取所有终点时间矩阵 $new_order = array("order_id" => $orderID, "num" => $num, "startx_y" => $beginX_Y, "endx_y" => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name); $da = new dispatch_algorithm(); $res = $da->dispatch($prodID, $value, $new_order, $begin_x_y, $end_x_y, $run_date, $should_be_count,$bool_total_time);//调用调配订单算法 writeLog("调度算法返回结果:".json_encode($res)); if (!$res) { continue; } $order = array('order_id' => $orderID, 'num' => $num, 'startx_y' => $beginX_Y, 'endx_y' => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name); array_push($unlock_orderlist[$k], $order); $mem->set("orderList[$runID]", $unlock_orderlist); //加入订单池 //如果该订单池满6个人之后,就要将这个订单池的状态置为锁定 if ($pool_passager_num + $num == 6) { $PoolStatus[$k]['status'] = 0; $mem->set("orderPoolStatus[$runID]", $PoolStatus); //加入状态 //更改该订单池在状态池中的状态 } //TODO:将订单信息存入数据库中 $orderService->makeOrder($mem, $runID, $res, $k, $prodID); //更新order_main中该车的里面的其他订单的上车时间和下车时间 $start_end_time = array(); writeLog("开始结束时间--:".json_encode($res)); foreach ($res as $id => $arr) { if (!isset($start_end_time[$arr['order_id']])) { $start_end_time[$arr['order_id']] = array(); $start_end_time[$arr['order_id']]["start_time"] = $arr['time']; } else { $start_end_time[$arr['order_id']]["end_time"] = $arr['time']; $orderService->update_order_time($arr['order_id'], $start_end_time[$arr['order_id']]["start_time"], $arr['time']); } } $arr_result['order_id'] = $orderID; $arr_result['bus_no'] = $k; $order_time = array(); foreach ($res as $num => $order) { if ($order["order_id"] == $orderID) { $order_time[] = $order['time']; } } $arr_result['start_time'] = $order_time[0]; $arr_result['end_time'] = $order_time[1]; $data['code'] = '0'; $data['info'] = "配车成功,该订单已加入{$k}号车"; $data['result'] = $arr_result; return $data; } } //如果该订单均不能符合当前存在的任意订单池,继续派车 $stock = $orderService->stock($runID); if ($stock['bus_num'] < 1) { $data['code'] = '101'; $data['info'] = '当前班次无可派车辆'; return $data; } //$stock['next_blank_car'] 车ID $unlock_orderlist[$stock['next_blank_car']][] = array('order_id' => $orderID, 'num' => $num, 'startx_y' => $beginX_Y, 'endx_y' => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name); //通过orderID标记不同的订单池 $mem->set("orderList[$runID]", $unlock_orderlist); //加入订单池 //初始化该订单池,状态值:0:已锁定;1:未锁定 $PoolStatus[$stock['next_blank_car']] = array('status' => 1); $mem->set("orderPoolStatus[{$runID}]", $PoolStatus); //如果该订单池人数满6,状态池置为锁定 if ($num >= 6) { $PoolStatus[$stock['next_blank_car']]['status'] = 0; $mem->set("orderPoolStatus[$runID]", $PoolStatus); //加入状态 //更改该订单池在状态池中的状态 } //TODO:将订单信息存入数据库中 $order_res = $orderService->firstOrderTime($orderID, $beginX_Y, $endX_Y, $run_date); $orderService->makeOrder($mem, $runID, $order_res, $stock['next_blank_car'], $prodID); $result['code'] = 0; $result['info'] = "该订单不能与有订单的车辆匹配,加入{$stock['next_blank_car']}成功"; $res_arr['order_id'] = $orderID; $res_arr['bus_no'] = $stock['next_blank_car']; $res_arr['start_time'] = $order_res[0]['time']; $res_arr['end_time'] = $order_res[1]['time']; $result['result'] = $res_arr; return $result; } else { //不存在当前班次的订单池,直接创建新的订单池以及订单状态池 //这里还需要进行判断有没有可以派的车(即有没有超出规定车辆数量的限制) 需要从数据库读取当前 $stock = $orderService->stock($runID); if ($stock['bus_num'] < 1) { $data['code'] = '101'; $data['info'] = '当前班次无可派车辆(当前班次的第一个订单)'; return $data; } //初始化该订单状态池,状态值:0:已锁定;1:未锁定 $PoolStatus[$stock['next_blank_car']] = array('status' => 1); $mem->set("orderPoolStatus[$runID]", $PoolStatus); $orderList[$stock['next_blank_car']][] = array('order_id' => $orderID, 'num' => $num, 'startx_y' => $beginX_Y, 'endx_y' => $endX_Y, "begin_name" => $begin_name, "end_name" => $end_name); //通过orderID标记不同的订单池 $mem->set("orderList[$runID]", $orderList); //加入订单池 //如果该订单池人数满6,状态池置为锁定 if ($num >= 6) { $PoolStatus[$stock['next_blank_car']]['status'] = 0; $mem->set("orderPoolStatus[$runID]", $PoolStatus); //加入状态 //更改该订单池在状态池中的状态 } //TODO:将订单信息存入数据库中 $order_res = $orderService->firstOrderTime($orderID, $beginX_Y, $endX_Y, $run_date); $orderService->makeOrder($mem, $runID, $order_res, $stock['next_blank_car'], $prodID); $result['code'] = 0; $result['info'] = "当前班次的第一个订单"; $res_arr['order_id'] = $orderID; $res_arr['bus_no'] = $stock['next_blank_car']; $res_arr['start_time'] = $order_res[0]['time']; $res_arr['end_time'] = $order_res[1]['time']; $result['result'] = $res_arr; return $result; } } }