query($sql); $row = $result->fetchAll(PDO::FETCH_ASSOC); if (!isset($row[0]['id'])) { $sql = "INSERT into run_bus_station_view(run_id,bus_no,prod_id,order_id,station,lat_long,station_type,station_time,update_time) VALUES(" . $run_id . ",'" . $bus_no . "','" . $prod_id . "','" . $order_id . "','" . $station . "','" . $lat_long . "','" . $station_type . "','" . $station_time . "',NOW());"; writeLog("更新run_bus_station_view的sql语句" . json_encode($sql)); $result = $pdo->query($sql); if (!$result) { return false; } } else { $sql = "UPDATE run_bus_station_view set cancel_flag =0,order_id='" . $order_id . "',station='" . $station . "',lat_long='" . $lat_long . "',station_type='" . $station_type . "',station_time='" . $station_time . "',update_time=NOW() where run_id =" . $run_id . " and bus_no =" . $bus_no; $result = $pdo->query($sql); writeLog("更新run_bus_station_view的sql语句" . json_encode($sql)); if (!$result) { return FALSE; } } return true; } /** * 更新订单的开始时间和结束时间 * @param $order_id * @param $start_time * @param $end_time */ public function update_order_time($order_id, $start_time, $end_time) { $pdo = conn(); $sql = "update order_main set PROD_START_STATION_TIME = '" . $start_time . "',PROD_END_STATION_TIME = '" . $end_time . "' where order_id =" . $order_id . " or parent_order_id =" . $order_id; $pdo->exec($sql); } /** * @param $runid 班次ID * @return int 返回库存量 */ public function stock($runid) { $pdo = conn(); $sql = "select seat_count,saled_count,BUS_ORDER_ID from run_bus where cancel_flag = 0 AND run_id = " . $runid . " ORDER BY BUS_ORDER_ID ASC "; $result = $pdo->query($sql); $stock = $result->fetchAll(PDO::FETCH_ASSOC); writeLog($sql); $run_stock = 0; $bus_num = 0; $blank_car_order = false; foreach ($stock as $key => $value) { if ($value['saled_count'] <= 0) { $bus_num++; } $run_stock += ($value['seat_count'] - $value['saled_count']); if ($value['saled_count'] <= 0 && $blank_car_order == false) { $blank_car_order = $value['BUS_ORDER_ID']; } } $stock = array("bus_num" => $bus_num, "run_stock" => $run_stock, "next_blank_car" => $blank_car_order); //返回的是车辆剩余信息和座位剩余信息 return $stock; } /* public function add_stock($runid, $bus_order_id, $num) { $pdo = conn(); $sql = "update run_bus set SALED_COUNT = SALED_COUNT+{$num} where RUN_ID = " . $runid . " AND BUS_ORDER_ID = {$bus_order_id} AND CANCEL_FLAG = 0 "; $result = $pdo->exec($sql); }*/ /** *生成时间矩阵 * @param $orderList 未锁定的订单池 * @param $beginX_Y 上车点纬经度 * @param $endX_Y 下车点纬经度 * @return array|bool 返回时间矩阵 */ public function timeMatrix($orderList, $X_Y, $tag) { //读取订单池的订单 $count = count($orderList); //此订单进入之前订单池已经有的订单个数 $str_beginX_Y = ""; //$str_endX_Y = ""; foreach ($orderList as $k => $v) { if ($tag == 1) { $str_beginX_Y .= $v['startx_y'] . '|'; } else { $str_beginX_Y .= $v['endx_y'] . '|'; } } //通过新的经纬度和订单池里面的订单经纬度计算出新的时间矩阵 $str_beginX_Y .= $X_Y; //var_dump($str_beginX_Y); $data = array('origins' => $str_beginX_Y, 'destinations' => $str_beginX_Y, 'output' => 'json', 'tactics' => 11, 'ak' => commonUtils::$AK); $res = commonUtils::send_get(commonUtils::$URL, $data); $shortTimeList = json_decode($res, true); $time = array(); if (isset($shortTimeList['status']) && $shortTimeList['status'] == 0) { $i = 1; $j = 1; foreach ($shortTimeList['result'] as $v) { $time[$j][$i] = ceil(($v['duration']['value']) / 60); if ($i % ($count + 1) == 0) { //生成2*(count+1)乘以2*(count+1)的时间矩阵 $j++; $i = 0; } if ($i == $j) //将对角线元素置为零(由于上面的if,对角线的最后一个元素不会被置为0) { $time[$j][$i] = 0; } $max = ($count + 1); if (isset($time[$max][$max])) { //将对角线最后一个元素也置为0 $time[$max][$max] = 0; } /* if ($j > ($count + 1) && $j <= $max && $i <= ($count + 1)) { //将矩阵的左下角不符合情况的值置为0 $time[$j][$i] = -1; if (isset($time[$j][0])) { unset($time[$j][0]); //该方法会在$j>订单数的数组中多加入一个键值为0的元素,所以在此处进行处理 } }*/ $i++; } return $time; //产生时间矩阵 } } /** * 根据订单池查询订单池内的人数 * @param $orderPool */ public function poolNum($orderPool) { $poolNum = 0; foreach ($orderPool as $item) { $poolNum += $item['num']; } return $poolNum; } /** *用于输入上车点和下车点后返回上车时间和下车时间 * @param $orderID * @param $beginX_Y * @param $endX_Y * @param $run_date * @return mixed */ public function firstOrderNeedTime($beginX_Y, $endX_Y, $run_date) { if (($beginX_Y == "32.116682,118.784105" && $endX_Y == "32.011714,120.894448") || ($beginX_Y == "32.011714,120.894448" && $endX_Y == "32.116682,118.784105")) { $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y); $start1 = strtotime($run_date) + 5 * 60; $start2 = strtotime($run_date) - 10 * 60; $end1 = (strtotime($run_date) + $time * 60) + 5 * 60; $end2 = (strtotime($run_date) + $time * 60) - 10 * 60; $result['code'] = 0; $result['result'] = array($run_date, date("Y-m-d H:i", strtotime($run_date) + $time * 60)); $result['str_time'] = array(date("H:i", $start2) . '--' . date("H:i", $start1), date("H:i", $end2) . '--' . date("H:i", $end1)); return $result; } else { $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y); $temp_time_on_the_way = strtotime($run_date) + 210 * 60; $arrive = date("Y-m-d H:i", $temp_time_on_the_way); $receive_time = date("Y-m-d H:i", $temp_time_on_the_way - $time * 60); $result['code'] = 0; $result['result'] = array($receive_time, $arrive); $start1 = $temp_time_on_the_way + 5 * 60; $start2 = $temp_time_on_the_way - 10 * 60; $end1 = ($temp_time_on_the_way - $time * 60) + 5 * 60; $end2 = ($temp_time_on_the_way - $time * 60) - 10 * 60; $result['str_time'] = array(date("H:i", $end2) . '--' . date("H:i", $end1), date("H:i", $start2) . '--' . date("H:i", $start1)); return $result; } } /** *用于每个车的第一个订单生效后的调配顺序 * @param $orderID * @param $beginX_Y * @param $endX_Y * @param $run_date * @return mixed */ public function firstOrderTime($orderID, $beginX_Y, $endX_Y, $run_date) { //说明是携程下进来的订单 if (($beginX_Y == "32.116682,118.784105" && $endX_Y == "32.011714,120.894448") || ($beginX_Y == "32.011714,120.894448" && $endX_Y == "32.116682,118.784105")) { $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y); $temp_time_on_the_way = strtotime($run_date); $arrive = date("Y-m-d H:i", $temp_time_on_the_way + $time * 60); $result = array(0 => array("order_id" => $orderID, "time" => $run_date), 1 => array("order_id" => $orderID, "time" => $arrive)); } else { //c端的订单 $time = commonUtils::baiduShortTime($beginX_Y, $endX_Y); $temp_time_on_the_way = strtotime($run_date) + 210 * 60; $arrive = date("Y-m-d H:i", $temp_time_on_the_way); $receive_time = date("Y-m-d H:i", $temp_time_on_the_way - $time * 60); $result = array(0 => array("order_id" => $orderID, "time" => $receive_time), 1 => array("order_id" => $orderID, "time" => $arrive)); } return $result; } /** * 向run_bus_station_view表中插入数据 * @param $mem * @param $orderService * @param $runID * @param $res * @param $bus_order_num * @param $prodID */ function makeOrder($mem, $runID, $res, $bus_order_num, $prodID) { if ($res == false) { $pdo = conn(); $sql = "UPDATE run_bus_station_view set cancel_flag =1,update_time=NOW() where run_id =" . $runID . " and bus_no =" . $bus_order_num; $result = $pdo->query($sql); return true; } $station = ""; $lat_long = ""; $station_type = ""; $station_time = ""; $order_id_list = ""; $order_arr = $mem->get("orderList[$runID]"); $order_index_array = array(); foreach ($order_arr as $bus_id => $orderlist) { $orderNum = count($orderlist); foreach ($orderlist as $num => $orderInfo) { $orderInfo["bus_id"] = $bus_id; $orderInfo["start_num"] = $num; $orderInfo["bus_order_count"] = $orderNum; $order_index_array[$orderInfo["order_id"]] = $orderInfo; } } $start_flag = false; foreach ($res as $id => $arr) { if (!isset($order_index_array[$arr['order_id']])) { continue; } $order_info = $order_index_array[$arr['order_id']]; if (!isset($order_info["meet_flag"]) || $order_info["meet_flag"] != 1) { $order_info["meet_flag"] = 1; $order_index_array[$arr['order_id']] = $order_info; $order_id_list .= $arr['order_id'] . ";"; $station .= $order_info['begin_name'] . ";"; $lat_long .= $order_info['startx_y'] . ";"; $station_type .= "0" . ";"; $station_time .= $arr['time'] . ";"; } else { $order_id_list .= $arr['order_id'] . ";"; $station .= $order_info['end_name'] . ";"; $lat_long .= $order_info['endx_y'] . ";"; $station_type .= "1" . ";"; $station_time .= $arr['time'] . ";"; } } $order_id_list = rtrim($order_id_list, ';'); $station = rtrim($station, ';'); $lat_long = rtrim($lat_long, ';'); $station_type = rtrim($station_type, ';'); $station_time = rtrim($station_time, ';'); $res = $this->set_run_station($runID, $bus_order_num, $prodID, $order_id_list, $station, $lat_long, $station_type, $station_time); return $res; } }