You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

381 regels
15 KiB

  1. <?php
  2. /*
  3. Author:fuhc
  4. Compeny:修改车号
  5. */
  6. require_once '../Common/Mysql.php';
  7. if (ALLOW_ORIGIN)
  8. header("Access-Control-Allow-Origin:*");
  9. $user_id=getUserId();
  10. if($user_id === false){
  11. $data['code']="-3";
  12. $data['info']="未登录";
  13. echo json_encode($data);exit();
  14. }
  15. //$user_id=1;
  16. $method = isset($_GET['method']) ? $_GET['method'] : "";
  17. if($method==""){
  18. $data['code']="-2";
  19. $data['info']="缺少method";
  20. echo json_encode($data);
  21. exit();
  22. }
  23. $pdo=conn();
  24. switch($method){
  25. case "getStations":
  26. $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
  27. if($run_id==""){
  28. $data['code']="-1";
  29. $data['info']="缺少参数";
  30. echo json_encode($data);exit();
  31. }
  32. //这里加一个对该班次时间的判断,只有在发车前一天的18:30之前才可以修改订单车次
  33. $run_id=$run_id+0;
  34. $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;";
  35. $check=$pdo->query($sql_c);
  36. $res1=$check->fetch(PDO::FETCH_ASSOC);
  37. $check->closeCursor();
  38. $run_date=$res1['run_date'];
  39. $now=time();
  40. $yesStamp=strtotime('-1 day',strtotime("$run_date 18:30:00"));//得到run_date前一天18:30的时间戳
  41. $diffTime=$now-$yesStamp;
  42. /*
  43. if($diffTime>=0){
  44. $data['code']="666";
  45. $data['info']="只能在班次发车日期前一天18:30之前重置车次订单!";
  46. echo json_encode($data);exit();
  47. }
  48. */
  49. // $sql="CALL XM_GET_STATION_BY_RUNID('".$run_id."');";
  50. // writeLog("getStations sql=".$sql);
  51. // $result=$pdo->query($sql);
  52. // $data=array();
  53. // $data['code']="0";
  54. // $data['info']="";
  55. // $re_data=array();
  56. // do{
  57. // $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
  58. // $re_data[]=$rowset;
  59. // }while($result->nextRowset());
  60. // $data['start_station']=isset($re_data[0])?$re_data[0]:array();
  61. // $data['end_station']=isset($re_data[1])?$re_data[1]:array();
  62. // $result->closeCursor();
  63. $sql_start = "select t2.RES_NAME as start_station,t1.STATION_RES_ID as start_station_id
  64. from run_station t1
  65. left join base_resource t2 on t1.station_res_id = t2.res_id
  66. where t1.run_id = {$run_id} and t1.STATION_INOUT_TYPE in (108,109)";
  67. $sql_end = "select t2.RES_NAME as end_station,t1.STATION_RES_ID as end_station_id
  68. from run_station t1
  69. left join base_resource t2 on t1.station_res_id = t2.res_id
  70. where t1.run_id = {$run_id} and t1.STATION_INOUT_TYPE in (110)";
  71. $data=array();
  72. $data['code']="0";
  73. $data['info']="";
  74. $data['start_station']=array();
  75. $data['end_station']=array();
  76. $start_result = $pdo->query($sql_start);
  77. if( false != $start_result ) {
  78. $res_start = $start_result->fetchAll(PDO::FETCH_ASSOC);
  79. $start_result->closeCursor();
  80. $data['start_station']= $res_start == false ? array() : $res_start;
  81. }
  82. $end_result = $pdo->query($sql_end);
  83. if( false != $end_result ) {
  84. $res_end = $end_result->fetchAll(PDO::FETCH_ASSOC);
  85. $end_result->closeCursor();
  86. $data['end_station']= $res_end == false ? array() : $res_end;
  87. }
  88. echo json_encode($data);
  89. exit();
  90. break;
  91. case "getOrderList":
  92. $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
  93. $start_station_id=isset($_GET['start_station_id'])?$_GET['start_station_id']:"";
  94. $end_station_id=isset($_GET['end_station_id'])?$_GET['end_station_id']:"";
  95. if($run_id==""){
  96. $data['code']="-1";
  97. $data['info']="缺少参数";
  98. echo json_encode($data);exit();
  99. }
  100. // $sql="CALL GET_ORDERLIST_FOR_SETBUS('".$run_id."','".$start_station_id."','".$end_station_id."');";
  101. // writeLog('getOrderList sql='.$sql);
  102. // $result=$pdo->query($sql);
  103. // $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
  104. $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,
  105. 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,
  106. customer_name,customer_mobile
  107. FROM order_main
  108. 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 ";
  109. $result=$pdo->query($sql);
  110. $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
  111. if( false != $rowset && count($rowset) > 0 ) {
  112. $all_supplier_array = get_memcache("ARY_ALL_SUPPLIER");
  113. $rowset_tmp = array();
  114. foreach( $rowset as $order_info ) {
  115. if( isset($all_supplier_array[$order_info["org_id"]]) ) {
  116. $order_info["org_name"] = $all_supplier_array[$order_info["outside_sale_org_id"]]["supplier_name"];
  117. } else {
  118. $order_info["org_name"] = "";
  119. }
  120. $order_info["travel_time"] = $order_info["run_date"]." ".$order_info["run_time"];
  121. $rowset_tmp[] = $order_info;
  122. }
  123. $rowset = $rowset_tmp;
  124. }
  125. $data=array();
  126. $data['code']="0";
  127. $data['info']="";
  128. $data['orderList']=isset($rowset)?$rowset:array();
  129. $result->closeCursor();
  130. echo json_encode($data);
  131. exit();
  132. break;
  133. case "getCarList":
  134. $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
  135. if($run_id==""){
  136. $data['code']="-1";
  137. $data['info']="缺少参数";
  138. echo json_encode($data);exit();
  139. }
  140. $sql="CALL SP_GET_RUN_BUS(".$run_id.");";
  141. writeLog('carList sql='.$sql);
  142. $result=$pdo->query($sql);
  143. $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
  144. $result->closeCursor();
  145. $data=array();
  146. $data['code']="0";
  147. $data['info']="";
  148. $data['carList']=isset($rowset)?$rowset:array();
  149. echo json_encode($data);exit();
  150. break;
  151. case "reset":
  152. $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
  153. $order_ids=isset($_GET['order_ids'])?$_GET['order_ids']:"";
  154. if($run_id=="" || $order_ids==""){
  155. $data['code']="-1";
  156. $data['info']="缺少参数";
  157. echo json_encode($data);exit();
  158. }
  159. //对重置做一个判断,班次只有在发车前一天的18:30之前才可以重置订单车次
  160. $run_id=$run_id+0;
  161. $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;";
  162. $check=$pdo->query($sql_c);
  163. $res1=$check->fetch(PDO::FETCH_ASSOC);
  164. $check->closeCursor();
  165. $run_date=$res1['run_date'];
  166. $now=time();
  167. $yesStamp=strtotime('-1 day',strtotime("$run_date 18:30:00"));//得到run_date前一天18:30的时间戳
  168. $diffTime=$now-$yesStamp;
  169. /*
  170. if($diffTime>=0){
  171. $data['code']="-2";
  172. $data['info']="只能在班次发车日期前一天18:30之前重置车次订单!";
  173. echo json_encode($data);exit();
  174. }
  175. */
  176. $sql="CALL SP_RESET_BUS_FOR_ORDER(".$run_id.",'".$order_ids."');";
  177. writeLog('reset sql='.$sql);
  178. writelog_update_car(' reset::::user_id='.$user_id."|run_id=".$run_id."|order_ids=".$order_ids);
  179. $result=$pdo->query($sql);
  180. $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
  181. $result->closeCursor();
  182. $data=array();
  183. writeLog(json_encode($rowset));
  184. writelog_update_car(json_encode($rowset));
  185. $data['code']=isset($rowset[0]['errorcode'])?$rowset[0]['errorcode']:"500";
  186. $data['info']=isset($rowset[0]['errorinfo'])?$rowset[0]['errorinfo']:"未从数据库获得返回值";
  187. echo json_encode($data);exit();
  188. break;
  189. case "updateCar":
  190. $run_id=isset($_GET['run_id'])?$_GET['run_id']:"";
  191. $order_ids=isset($_GET['order_ids'])?$_GET['order_ids']:"";
  192. $bus_order_id=isset($_GET['bus_order_id'])?$_GET['bus_order_id']:"";
  193. if($run_id=="" || $order_ids=="" || $bus_order_id==""){
  194. $data['code']="-1";
  195. $data['info']="缺少参数";
  196. echo json_encode($data);exit();
  197. }
  198. $sql="CALL SP_REASSIGN_BUS_FOR_ORDER(".$run_id.",'".$order_ids."',".$bus_order_id.");";
  199. $sql_set="CALL SP_RESET_BUS_FOR_ORDER(".$run_id.",'".$order_ids."');";
  200. //这里增加两个校验:1,班次只有在发车前一天的18:30之前才可以修改订单车次;2,只有在该车次的run_x数据全被重置的情况下才允许修改订单的车次。
  201. #1,班次只有在发车前一天的18:30之前才可以修改订单车次
  202. $run_id=$run_id+0;
  203. $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;";
  204. $check=$pdo->query($sql_c);
  205. $res1=$check->fetch(PDO::FETCH_ASSOC);
  206. $check->closeCursor();
  207. $run_date=$res1['run_date'];
  208. $now=time();
  209. $yesStamp=strtotime('-1 day',strtotime("$run_date 18:30:00"));//得到run_date前一天18:30的时间戳
  210. $diffTime=$now-$yesStamp;
  211. /*
  212. if($diffTime>=0){
  213. $data['code']="-2";
  214. $data['info']="只能在班次发车日期前一天18:30之前修改车次订单!";
  215. echo json_encode($data);exit();
  216. }
  217. */
  218. #2,只有在该车次的run_x数据全被重置的情况下才允许修改订单的车次。
  219. $x_name='run_'.$res1['x_name'];
  220. $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";
  221. $check2=$pdo->query($sql_c2);
  222. $res2=$check2->fetch(PDO::FETCH_ASSOC);
  223. $check2->closeCursor();
  224. if($res2){
  225. $data['code']="-3";
  226. $data['info']="目标车次全部订单重置后才可修改订单车次";
  227. echo json_encode($data);exit();
  228. }
  229. writeLog('reset sql='.$sql);
  230. writelog_update_car(' reset::::user_id='.$user_id."|run_id=".$run_id."|order_ids=".$order_ids);
  231. $result2=$pdo->query($sql_set);
  232. $rowset2=$result2->fetchAll(PDO::FETCH_ASSOC);
  233. $result2->closeCursor();
  234. writeLog(json_encode($rowset2));
  235. writelog_update_car(json_encode($rowset2));
  236. if($rowset2[0]['errorcode']=="0"){
  237. writeLog('updateCar sql='.$sql);
  238. writelog_update_car('update::::user_id='.$user_id."|run_id=".$run_id."|order_ids=".$order_ids."|bus_order_id=".$bus_order_id);
  239. $result=$pdo->query($sql);
  240. $rowset=$result->fetchAll(PDO::FETCH_ASSOC);
  241. $result->closeCursor();
  242. writeLog(json_encode($rowset));
  243. writelog_update_car(json_encode($rowset));
  244. $data=array();
  245. $data['code']=isset($rowset[0]['errorcode'])?$rowset[0]['errorcode']:"500";
  246. $data['info']=isset($rowset[0]['errorinfo'])?$rowset[0]['errorinfo']:"未从数据库获得返回值";
  247. //update connect orders' info
  248. $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 ";
  249. writelog_update_car("update_car_no extra_sql:".$sql_connect_order);
  250. $pdo_connect = conn();
  251. $result2_connect = $pdo_connect->query($sql_connect_order);
  252. if( false != $result2_connect ) {
  253. $order_array=$result2_connect->fetchAll(PDO::FETCH_ASSOC);
  254. $result2_connect->closeCursor();
  255. if( false != $order_array && count($order_array) > 0 ) {
  256. $order_detail_array = array();
  257. $connect_order_id = array();
  258. $order_reflect_array = array();
  259. //收集新车号,座位号,座位名
  260. foreach( $order_array as $order_detail_info ) {
  261. $order_id = $order_detail_info["order_id"];
  262. $parent_order_id = $order_detail_info["parent_order_id"];
  263. if( $parent_order_id == 0 ) {
  264. if( !isset($order_detail_array[$order_id]) ) {
  265. $order_detail_array[$order_id] = array();
  266. // $order_detail_array[$order_id]["bus_order_id"] = $order_detail_info["run_bus_order_id"];
  267. $order_detail_array[$order_id]["seat_id"] = array();
  268. $order_detail_array[$order_id]["seat_name"] = array();
  269. }
  270. $sale_path_id = explode( ",", $order_detail_info["sale_path"] );
  271. foreach( $sale_path_id as $order_id_tmp ) {
  272. $order_reflect_array[$order_id_tmp] = $order_id;
  273. }
  274. $connect_order_id = array_merge( $connect_order_id, $sale_path_id);
  275. } else {
  276. if( !isset($order_detail_array[$parent_order_id]) ) {
  277. $order_detail_array[$parent_order_id] = array();
  278. $order_detail_array[$parent_order_id]["bus_order_id"] = $order_detail_info["run_bus_order_id"];
  279. $order_detail_array[$parent_order_id]["seat_no"] = array();
  280. }
  281. $order_detail_array[$parent_order_id]["bus_order_id"] = $order_detail_info["run_bus_order_id"];
  282. $order_detail_array[$parent_order_id]["seat_id"][] = $order_detail_info["run_bus_seat_seq_id"];
  283. $order_detail_array[$parent_order_id]["seat_name"][] = $order_detail_info["run_bus_seat_name"];
  284. }
  285. }
  286. //查找所有需要
  287. $search_order_ids = implode( ",", $connect_order_id );
  288. $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 ";
  289. writelog_update_car("update_car_no extra_sql2:".$sql_connect_order);
  290. $result2_connect = $pdo_connect->query($sql_connect_order);
  291. if( $result2_connect == false ) {
  292. break;
  293. }
  294. $order_connect_array=$result2_connect->fetchAll(PDO::FETCH_ASSOC);
  295. $result2_connect->closeCursor();
  296. if( $order_connect_array == false ) {
  297. break;
  298. }
  299. //遍历:目的1 查找相关子记录并修改座位信息 2修改车号
  300. $excute_sql_array = array();
  301. $sub_order_count_array = array();
  302. foreach( $order_connect_array as $order_info_temp ) {
  303. $order_id = $order_info_temp["order_id"];
  304. $parent_order_id = $order_info_temp["parent_order_id"];
  305. $reflect_order_id = $parent_order_id == 0 ? $order_reflect_array[$order_id] : $order_reflect_array[$parent_order_id];
  306. $order_detail_info2 = $order_detail_array[$reflect_order_id];
  307. if( $parent_order_id == 0 ) {
  308. // $sql = " UPDATE order_main SET run_bus_order_id={$order_detail_info2["bus_order_id"]} WHERE parent_order_id = {$order_id} ";
  309. // $excute_sql_array[] = $sql;
  310. } else {
  311. if( !isset($sub_order_count_array[$parent_order_id]) ) {
  312. $sub_order_count_array[$parent_order_id] = 0;
  313. }
  314. $sub_order_index = $sub_order_count_array[$parent_order_id];
  315. $sub_order_count_array[$parent_order_id]++;
  316. $seat_id = $order_detail_info2["seat_id"][$sub_order_index];
  317. $seat_name = $order_detail_info2["seat_name"][$sub_order_index];
  318. $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} ";
  319. $excute_sql_array[] = $sql;
  320. }
  321. }
  322. //执行SQL语句
  323. foreach( $excute_sql_array as $excute_sql_tmp ) {
  324. writelog_update_car("excute sql:".$excute_sql_tmp);
  325. $pdo_exe = conn();
  326. $pdo_exe->exec( $excute_sql_tmp );
  327. }
  328. }
  329. }
  330. //end of update connect orders' info
  331. echo json_encode($data);exit();
  332. }
  333. break;
  334. default :
  335. break;
  336. }
  337. function writelog_update_car($log){
  338. $dir=__DIR__."/../Log/";
  339. if (!is_dir($dir)){
  340. mkdir($dir);
  341. }
  342. $filename=$dir.date("Y-m-d").".logtemp";
  343. file_put_contents($filename, date("Y-m-d H:i:s")."\t".$log.PHP_EOL,FILE_APPEND);
  344. }
  345. function writelog_connect_log($log) {
  346. $dir=__DIR__."/../Log/";
  347. if (!is_dir($dir)){
  348. mkdir($dir);
  349. }
  350. $filename=$dir.'connent_'.date("Y-m-d").".log";
  351. file_put_contents($filename, date("Y-m-d H:i:s")."\t".$log.PHP_EOL,FILE_APPEND);
  352. }
  353. exit();