|
-
- <?php
- /*
- Author:fuhc
- Compeny:修改车号
- */
- require_once '../Common/Mysql.php';
- if (ALLOW_ORIGIN)
- header("Access-Control-Allow-Origin:*");
-
- $user_id=getUserId();
- if($user_id === false){
- $data['code']="-3";
- $data['info']="未登录";
- echo json_encode($data);exit();
- }
- //$user_id=1;
-
-
- $method = isset($_GET['method']) ? $_GET['method'] : "";
- if($method==""){
- $data['code']="-2";
- $data['info']="缺少method";
- echo json_encode($data);
- exit();
- }
-
-
- $pdo=conn();
- switch($method){
- case "getStations":
- $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
- if($run_id==""){
- $data['code']="-1";
- $data['info']="缺少参数";
- echo json_encode($data);exit();
- }
- //这里加一个对该班次时间的判断,只有在发车前一天的18:30之前才可以修改订单车次
- $run_id=$run_id+0;
- $sql_c="select concat(substring(run_date,1,4),substring(run_date,6,2)) as x_name,run_id,run_date from run_main where run_id=$run_id limit 1;";
- $check=$pdo->query($sql_c);
- $res1=$check->fetch(PDO::FETCH_ASSOC);
- $check->closeCursor();
- $run_date=$res1['run_date'];
- $now=time();
- $yesStamp=strtotime('-1 day',strtotime("$run_date 18:30:00"));//得到run_date前一天18:30的时间戳
- $diffTime=$now-$yesStamp;
-
- /*
- if($diffTime>=0){
- $data['code']="666";
- $data['info']="只能在班次发车日期前一天18:30之前重置车次订单!";
- echo json_encode($data);exit();
- }
- */
-
- // $sql="CALL XM_GET_STATION_BY_RUNID('".$run_id."');";
- // writeLog("getStations sql=".$sql);
- // $result=$pdo->query($sql);
- // $data=array();
- // $data['code']="0";
- // $data['info']="";
- // $re_data=array();
- // do{
- // $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
- // $re_data[]=$rowset;
- // }while($result->nextRowset());
- // $data['start_station']=isset($re_data[0])?$re_data[0]:array();
- // $data['end_station']=isset($re_data[1])?$re_data[1]:array();
- // $result->closeCursor();
-
- $sql_start = "select t2.RES_NAME as start_station,t1.STATION_RES_ID as start_station_id
- from run_station t1
- left join base_resource t2 on t1.station_res_id = t2.res_id
- where t1.run_id = {$run_id} and t1.STATION_INOUT_TYPE in (108,109)";
-
- $sql_end = "select t2.RES_NAME as end_station,t1.STATION_RES_ID as end_station_id
- from run_station t1
- left join base_resource t2 on t1.station_res_id = t2.res_id
- where t1.run_id = {$run_id} and t1.STATION_INOUT_TYPE in (110)";
-
- $data=array();
- $data['code']="0";
- $data['info']="";
- $data['start_station']=array();
- $data['end_station']=array();
- $start_result = $pdo->query($sql_start);
- if( false != $start_result ) {
- $res_start = $start_result->fetchAll(PDO::FETCH_ASSOC);
- $start_result->closeCursor();
- $data['start_station']= $res_start == false ? array() : $res_start;
- }
- $end_result = $pdo->query($sql_end);
- if( false != $end_result ) {
- $res_end = $end_result->fetchAll(PDO::FETCH_ASSOC);
- $end_result->closeCursor();
- $data['end_station']= $res_end == false ? array() : $res_end;
- }
-
-
- echo json_encode($data);
- exit();
- break;
- case "getOrderList":
- $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
- $start_station_id=isset($_GET['start_station_id'])?$_GET['start_station_id']:"";
- $end_station_id=isset($_GET['end_station_id'])?$_GET['end_station_id']:"";
- if($run_id==""){
- $data['code']="-1";
- $data['info']="缺少参数";
- echo json_encode($data);exit();
- }
- // $sql="CALL GET_ORDERLIST_FOR_SETBUS('".$run_id."','".$start_station_id."','".$end_station_id."');";
- // writeLog('getOrderList sql='.$sql);
- // $result=$pdo->query($sql);
- // $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
-
- $sql = " SELECT parent_order_id,create_time,order_description,outside_sale_org_id,outside_order_no,prod_name, parent_prod_name,count(order_id) as passenger_num,
- run_date,run_time,run_bus_order_id,order_status,prod_start_station_res_name as start_station,prod_end_station_res_name as end_station,
- customer_name,customer_mobile
- FROM order_main
- WHERE run_id={$run_id} AND cancel_flag=0 AND order_status <> 148 AND order_prod_type IN (81,82) AND parent_order_id > 0 and agent_level=1 GROUP BY parent_order_id ORDER BY run_bus_order_id ";
- $result=$pdo->query($sql);
- $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
- if( false != $rowset && count($rowset) > 0 ) {
- $all_supplier_array = get_memcache("ARY_ALL_SUPPLIER");
- $rowset_tmp = array();
- foreach( $rowset as $order_info ) {
- if( isset($all_supplier_array[$order_info["org_id"]]) ) {
- $order_info["org_name"] = $all_supplier_array[$order_info["outside_sale_org_id"]]["supplier_name"];
- } else {
- $order_info["org_name"] = "";
- }
- $order_info["travel_time"] = $order_info["run_date"]." ".$order_info["run_time"];
- $rowset_tmp[] = $order_info;
- }
- $rowset = $rowset_tmp;
- }
-
- $data=array();
- $data['code']="0";
- $data['info']="";
- $data['orderList']=isset($rowset)?$rowset:array();
- $result->closeCursor();
- echo json_encode($data);
- exit();
- break;
- case "getCarList":
- $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
- if($run_id==""){
- $data['code']="-1";
- $data['info']="缺少参数";
- echo json_encode($data);exit();
- }
- $sql="CALL SP_GET_RUN_BUS(".$run_id.");";
- writeLog('carList sql='.$sql);
- $result=$pdo->query($sql);
- $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
- $result->closeCursor();
- $data=array();
- $data['code']="0";
- $data['info']="";
- $data['carList']=isset($rowset)?$rowset:array();
- echo json_encode($data);exit();
- break;
- case "reset":
- $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
- $order_ids=isset($_GET['order_ids'])?$_GET['order_ids']:"";
-
- if($run_id=="" || $order_ids==""){
- $data['code']="-1";
- $data['info']="缺少参数";
- echo json_encode($data);exit();
- }
- //对重置做一个判断,班次只有在发车前一天的18:30之前才可以重置订单车次
- $run_id=$run_id+0;
- $sql_c="select concat(substring(run_date,1,4),substring(run_date,6,2)) as x_name,run_id,run_date from run_main where run_id=$run_id limit 1;";
- $check=$pdo->query($sql_c);
- $res1=$check->fetch(PDO::FETCH_ASSOC);
- $check->closeCursor();
- $run_date=$res1['run_date'];
- $now=time();
- $yesStamp=strtotime('-1 day',strtotime("$run_date 18:30:00"));//得到run_date前一天18:30的时间戳
- $diffTime=$now-$yesStamp;
- /*
- if($diffTime>=0){
- $data['code']="-2";
- $data['info']="只能在班次发车日期前一天18:30之前重置车次订单!";
- echo json_encode($data);exit();
- }
- */
-
- $sql="CALL SP_RESET_BUS_FOR_ORDER(".$run_id.",'".$order_ids."');";
- writeLog('reset sql='.$sql);
- writelog_update_car(' reset::::user_id='.$user_id."|run_id=".$run_id."|order_ids=".$order_ids);
- $result=$pdo->query($sql);
- $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
- $result->closeCursor();
- $data=array();
- writeLog(json_encode($rowset));
- writelog_update_car(json_encode($rowset));
- $data['code']=isset($rowset[0]['errorcode'])?$rowset[0]['errorcode']:"500";
- $data['info']=isset($rowset[0]['errorinfo'])?$rowset[0]['errorinfo']:"未从数据库获得返回值";
- echo json_encode($data);exit();
- break;
- case "updateCar":
- $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
- $order_ids=isset($_GET['order_ids'])?$_GET['order_ids']:"";
- $bus_order_id=isset($_GET['bus_order_id'])?$_GET['bus_order_id']:"";
- if($run_id=="" || $order_ids=="" || $bus_order_id==""){
- $data['code']="-1";
- $data['info']="缺少参数";
- echo json_encode($data);exit();
- }
- $sql="CALL SP_REASSIGN_BUS_FOR_ORDER(".$run_id.",'".$order_ids."',".$bus_order_id.");";
- $sql_set="CALL SP_RESET_BUS_FOR_ORDER(".$run_id.",'".$order_ids."');";
-
- //这里增加两个校验:1,班次只有在发车前一天的18:30之前才可以修改订单车次;2,只有在该车次的run_x数据全被重置的情况下才允许修改订单的车次。
- #1,班次只有在发车前一天的18:30之前才可以修改订单车次
- $run_id=$run_id+0;
- $sql_c="select concat(substring(run_date,1,4),substring(run_date,6,2)) as x_name,run_id,run_date from run_main where run_id=$run_id limit 1;";
- $check=$pdo->query($sql_c);
- $res1=$check->fetch(PDO::FETCH_ASSOC);
- $check->closeCursor();
- $run_date=$res1['run_date'];
- $now=time();
- $yesStamp=strtotime('-1 day',strtotime("$run_date 18:30:00"));//得到run_date前一天18:30的时间戳
- $diffTime=$now-$yesStamp;
- /*
- if($diffTime>=0){
- $data['code']="-2";
- $data['info']="只能在班次发车日期前一天18:30之前修改车次订单!";
- echo json_encode($data);exit();
- }
- */
-
- #2,只有在该车次的run_x数据全被重置的情况下才允许修改订单的车次。
- $x_name='run_'.$res1['x_name'];
- $sql_c2="select * from $x_name where run_id=$run_id and bus_order_id=$bus_order_id and cancel_flag=0 and seat_type=72 and seat_status=2 limit 1";
- $check2=$pdo->query($sql_c2);
- $res2=$check2->fetch(PDO::FETCH_ASSOC);
- $check2->closeCursor();
-
- if($res2){
- $data['code']="-3";
- $data['info']="目标车次全部订单重置后才可修改订单车次";
- echo json_encode($data);exit();
- }
-
-
- writeLog('reset sql='.$sql);
- writelog_update_car(' reset::::user_id='.$user_id."|run_id=".$run_id."|order_ids=".$order_ids);
- $result2=$pdo->query($sql_set);
- $rowset2=$result2->fetchAll(PDO::FETCH_ASSOC);
- $result2->closeCursor();
- writeLog(json_encode($rowset2));
- writelog_update_car(json_encode($rowset2));
- if($rowset2[0]['errorcode']=="0"){
- writeLog('updateCar sql='.$sql);
- writelog_update_car('update::::user_id='.$user_id."|run_id=".$run_id."|order_ids=".$order_ids."|bus_order_id=".$bus_order_id);
- $result=$pdo->query($sql);
- $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
- $result->closeCursor();
- writeLog(json_encode($rowset));
- writelog_update_car(json_encode($rowset));
- $data=array();
- $data['code']=isset($rowset[0]['errorcode'])?$rowset[0]['errorcode']:"500";
- $data['info']=isset($rowset[0]['errorinfo'])?$rowset[0]['errorinfo']:"未从数据库获得返回值";
-
- //update connect orders' info
- $sql_connect_order = " SELECT order_id,parent_order_id,sale_path,run_bus_order_id,run_bus_seat_seq_id,run_bus_seat_name FROM order_main WHERE ( order_id IN ({$order_ids}) OR parent_order_id IN ({$order_ids})) and sale_path <> '' and cancel_flag=0 and order_status <> 148 ORDER BY order_id ";
- writelog_update_car("update_car_no extra_sql:".$sql_connect_order);
- $pdo_connect = conn();
- $result2_connect = $pdo_connect->query($sql_connect_order);
- if( false != $result2_connect ) {
- $order_array=$result2_connect->fetchAll(PDO::FETCH_ASSOC);
- $result2_connect->closeCursor();
- if( false != $order_array && count($order_array) > 0 ) {
- $order_detail_array = array();
- $connect_order_id = array();
- $order_reflect_array = array();
- //收集新车号,座位号,座位名
- foreach( $order_array as $order_detail_info ) {
- $order_id = $order_detail_info["order_id"];
- $parent_order_id = $order_detail_info["parent_order_id"];
- if( $parent_order_id == 0 ) {
- if( !isset($order_detail_array[$order_id]) ) {
- $order_detail_array[$order_id] = array();
- // $order_detail_array[$order_id]["bus_order_id"] = $order_detail_info["run_bus_order_id"];
- $order_detail_array[$order_id]["seat_id"] = array();
- $order_detail_array[$order_id]["seat_name"] = array();
- }
- $sale_path_id = explode( ",", $order_detail_info["sale_path"] );
- foreach( $sale_path_id as $order_id_tmp ) {
- $order_reflect_array[$order_id_tmp] = $order_id;
- }
- $connect_order_id = array_merge( $connect_order_id, $sale_path_id);
- } else {
- if( !isset($order_detail_array[$parent_order_id]) ) {
- $order_detail_array[$parent_order_id] = array();
- $order_detail_array[$parent_order_id]["bus_order_id"] = $order_detail_info["run_bus_order_id"];
- $order_detail_array[$parent_order_id]["seat_no"] = array();
- }
- $order_detail_array[$parent_order_id]["bus_order_id"] = $order_detail_info["run_bus_order_id"];
- $order_detail_array[$parent_order_id]["seat_id"][] = $order_detail_info["run_bus_seat_seq_id"];
- $order_detail_array[$parent_order_id]["seat_name"][] = $order_detail_info["run_bus_seat_name"];
- }
- }
- //查找所有需要
- $search_order_ids = implode( ",", $connect_order_id );
- $sql_connect_order = " SELECT order_id,parent_order_id,sale_path,run_bus_order_id,run_bus_seat_seq_id,run_bus_seat_name FROM order_main WHERE ( order_id IN ({$search_order_ids}) OR parent_order_id IN ({$search_order_ids}) ) and sale_path <> '' and agent_level > 1 and cancel_flag=0 and order_status <> 148 ORDER BY id ASC ";
- writelog_update_car("update_car_no extra_sql2:".$sql_connect_order);
- $result2_connect = $pdo_connect->query($sql_connect_order);
- if( $result2_connect == false ) {
- break;
- }
- $order_connect_array=$result2_connect->fetchAll(PDO::FETCH_ASSOC);
- $result2_connect->closeCursor();
- if( $order_connect_array == false ) {
- break;
- }
- //遍历:目的1 查找相关子记录并修改座位信息 2修改车号
- $excute_sql_array = array();
- $sub_order_count_array = array();
- foreach( $order_connect_array as $order_info_temp ) {
- $order_id = $order_info_temp["order_id"];
- $parent_order_id = $order_info_temp["parent_order_id"];
- $reflect_order_id = $parent_order_id == 0 ? $order_reflect_array[$order_id] : $order_reflect_array[$parent_order_id];
- $order_detail_info2 = $order_detail_array[$reflect_order_id];
- if( $parent_order_id == 0 ) {
- // $sql = " UPDATE order_main SET run_bus_order_id={$order_detail_info2["bus_order_id"]} WHERE parent_order_id = {$order_id} ";
- // $excute_sql_array[] = $sql;
- } else {
- if( !isset($sub_order_count_array[$parent_order_id]) ) {
- $sub_order_count_array[$parent_order_id] = 0;
- }
- $sub_order_index = $sub_order_count_array[$parent_order_id];
- $sub_order_count_array[$parent_order_id]++;
- $seat_id = $order_detail_info2["seat_id"][$sub_order_index];
- $seat_name = $order_detail_info2["seat_name"][$sub_order_index];
- $sql = " UPDATE order_main SET run_bus_order_id={$order_detail_info2["bus_order_id"]}, run_bus_seat_seq_id={$seat_id},run_bus_seat_name='{$seat_name}' WHERE order_id = {$order_id} ";
- $excute_sql_array[] = $sql;
- }
- }
- //执行SQL语句
- foreach( $excute_sql_array as $excute_sql_tmp ) {
- writelog_update_car("excute sql:".$excute_sql_tmp);
- $pdo_exe = conn();
- $pdo_exe->exec( $excute_sql_tmp );
- }
- }
- }
- //end of update connect orders' info
- echo json_encode($data);exit();
- }
-
- break;
- default :
- break;
-
- }
- function writelog_update_car($log){
- $dir=__DIR__."/../Log/";
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $filename=$dir.date("Y-m-d").".logtemp";
- file_put_contents($filename, date("Y-m-d H:i:s")."\t".$log.PHP_EOL,FILE_APPEND);
- }
-
- function writelog_connect_log($log) {
- $dir=__DIR__."/../Log/";
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $filename=$dir.'connent_'.date("Y-m-d").".log";
- file_put_contents($filename, date("Y-m-d H:i:s")."\t".$log.PHP_EOL,FILE_APPEND);
- }
-
- exit();
|