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.
 
 
 
 

274 lines
8.2 KiB

  1. <?php
  2. require_once __DIR__ . '/../st-xm/Common/Mysql.php';
  3. header("Access-Control-Allow-Origin:*");
  4. //车次run_x表重排
  5. date_default_timezone_set('Asia/Shanghai');
  6. $pdo = conn();//pdo连接数据库
  7. $run_id = isset($_POST['run_id']) ? $_POST['run_id'] : 0;
  8. $bus_order_id = isset($_POST['bus_order_id']) ? $_POST['bus_order_id'] : 0;
  9. #0,根据父订单order_id判断是单一线路还是组合线路,如果是单一线路,则重排run_x;如果是组合线路直接返回,不做重排
  10. $order_id = isset($_POST['order_id']) ? $_POST['order_id'] : 0;
  11. $order_id = $order_id + 0;
  12. $run_id = $run_id + 0;
  13. $bus_order_id = $bus_order_id + 0;
  14. if (isset($_POST['order_id'])) { //如果没传order_id,则直接使用前面的run_id,bus_order_id;否则根据order_id得到run_id,bus_order_id
  15. if (!$order_id) {
  16. $data = array();
  17. $data['code'] = '1';
  18. $data['info'] = '参数不正确';
  19. echo json_encode($data);
  20. exit;
  21. }
  22. //判断所属线路类型,为255,256的才做重排
  23. $sql_c1 = "
  24. SELECT
  25. b.line_type
  26. FROM
  27. order_main a
  28. LEFT JOIN opera_line b ON a.PARENT_PROD_ID = b.line_id
  29. WHERE
  30. a.order_id = $order_id
  31. AND a.PARENT_ORDER_ID = 0
  32. LIMIT 1;";
  33. $res0 = $pdo->query($sql_c1);
  34. $res0->setFetchMode(PDO::FETCH_ASSOC);
  35. $line_type = $res0->fetchColumn();
  36. if ($line_type == 256 || $line_type == 255 || $line_type = 368) {
  37. $sql_c2 = "SELECT
  38. run_id,
  39. run_bus_order_id as bus_order_id
  40. FROM
  41. order_main
  42. WHERE
  43. parent_order_id = $order_id
  44. AND cancel_flag = 0
  45. limit 1;";
  46. $res = $pdo->query($sql_c2);
  47. $res->setFetchMode(PDO::FETCH_ASSOC);
  48. $run_info = $res->fetch();
  49. $run_id = $run_info['run_id'];
  50. $bus_order_id = $run_info['bus_order_id'];
  51. }
  52. } else {
  53. if (!$run_id || !$bus_order_id) {
  54. $data = array();
  55. $data['code'] = '1';
  56. $data['info'] = '参数不全';
  57. echo json_encode($data);
  58. exit;
  59. }
  60. }
  61. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//mysql错误时抛出异常
  62. $pdo->beginTransaction(); //开始事务
  63. try {
  64. #1,获取所在run_x表名称
  65. $sql1 = "select concat(substring(run_date,1,4),substring(run_date,6,2)) as x_name from run_main where run_main.run_id = $run_id";
  66. $res1 = $pdo->query($sql1);
  67. $res1->setFetchMode(PDO::FETCH_ASSOC);
  68. $x_name = $res1->fetchColumn();
  69. if (!$x_name) {
  70. throw new PDOException('获取相应run_x表失败');
  71. }
  72. #2获取车次的run_x数据
  73. $sql2 = "SELECT
  74. id,
  75. run_id,
  76. bus_order_id,
  77. order_id,
  78. seat_seq_id,
  79. seat_name,
  80. seat_status,
  81. order_main_id,
  82. ifnull((select parent_order_id from order_main where order_id=order_main_id and order_prod_type=82 limit 1),0) as parent_order_id,
  83. order_main_prod_id,
  84. order_prod_id,
  85. order_lock_user_id
  86. FROM
  87. run_$x_name
  88. WHERE
  89. run_id = $run_id
  90. AND bus_order_id = $bus_order_id;";
  91. $res2 = $pdo->query($sql2);
  92. $res2->setFetchMode(PDO::FETCH_ASSOC);
  93. $x_arr = $res2->fetchAll();
  94. $res2->closeCursor();
  95. #3获取相关子订单 --注意排序会对最后run_x重排是否紧密有影响
  96. $sql3 = "SELECT
  97. id,
  98. run_id,
  99. run_bus_order_id as bus_order_id,
  100. order_id as order_main_id,
  101. parent_order_id,
  102. parent_prod_id as order_main_prod_id,
  103. prod_id as order_prod_id,
  104. prod_start_station_seq_id as start_seq_id,
  105. prod_start_station_res_id as start_res_id,
  106. prod_end_station_seq_id as end_seq_id,
  107. prod_end_station_res_id as end_res_id
  108. FROM
  109. order_main
  110. WHERE
  111. run_id = $run_id
  112. AND run_bus_order_id= $bus_order_id
  113. AND order_valid_status = 1
  114. AND order_prod_type=82
  115. ORDER BY start_seq_id,end_seq_id,parent_order_id;";
  116. $res3 = $pdo->query($sql3);
  117. $order_ori = $res3->fetchAll(PDO::FETCH_ASSOC);
  118. #4特别处理订单数组,得到需要更新到run_x表的数据
  119. $order_x = array();
  120. foreach ($order_ori as $k1 => $v1) {
  121. $order_ori[$k1]['s_id'] = $k1 + 1;
  122. }
  123. $len = count($order_x);
  124. foreach ($order_ori as $k => $v) {
  125. $num = $v['end_seq_id'] - $v['start_seq_id'];
  126. if ($num >= 1) {
  127. for ($i = 1; $i <= $num; $i++) {
  128. $order_x[] = $v;
  129. $v['start_seq_id'] = $v['start_seq_id'] + 1;
  130. }
  131. }
  132. }
  133. if (empty($order_ori) || empty($order_x)) {
  134. throw new PDOException('获取相应订单数据失败');
  135. }
  136. #5取消原run_x(对应run_id,bus_order_id)的数据
  137. $sql_reset = "
  138. UPDATE run_$x_name
  139. SET seat_status = 0,
  140. order_lock_user_id = 0,
  141. order_main_id = 0,
  142. order_main_prod_id = 0,
  143. order_prod_id = 0
  144. WHERE
  145. run_id = $run_id
  146. AND bus_order_id= $bus_order_id;";
  147. $res4 = $pdo->exec($sql_reset);
  148. if ($pdo->errorCode() != '00000') {
  149. $errorArr = $pdo->errorInfo();
  150. throw new PDOException($errorArr[2]);
  151. }
  152. #6重排相应run_x数据
  153. //6.1创建临时表,供update使用
  154. $sql_create = "drop temporary table if exists temp_runx;
  155. create temporary table temp_runx(
  156. id int,
  157. run_id int,
  158. bus_order_id int,
  159. order_main_id int,
  160. parent_order_id int,
  161. order_main_prod_id int,
  162. order_prod_id int,
  163. start_seq_id int,
  164. start_res_id int,
  165. end_seq_id int,
  166. end_res_id int
  167. );";
  168. $res5 = $pdo->exec($sql_create);
  169. if ($pdo->errorCode() != '00000') {
  170. $errorArr = $pdo->errorInfo();
  171. throw new PDOException($errorArr[2]);
  172. }
  173. //6.2需要插入临时表的数据
  174. $values = '';
  175. foreach ($order_x as $k => $v) {
  176. $values .= '(' . $v['s_id'] . ',' . $v['run_id'] . ',' . $v['bus_order_id'] . ',' . $v['order_main_id'] . ',' . $v['parent_order_id'] . ',' . $v['order_main_prod_id'] . ',' . $v['order_prod_id'] . ',' . $v['start_seq_id'] . ',' . $v['start_res_id'] . ',' . $v['end_seq_id'] . ',' . $v['end_res_id'] . '),';
  177. };
  178. $values = trim($values, ',');
  179. $sql_insert = "INSERT INTO temp_runx (
  180. id,
  181. run_id,
  182. bus_order_id,
  183. order_main_id,
  184. parent_order_id,
  185. order_main_prod_id,
  186. order_prod_id,
  187. start_seq_id,
  188. start_res_id,
  189. end_seq_id,
  190. end_res_id
  191. )
  192. VALUES
  193. $values;";
  194. //$info=$pdo->errorInfo();
  195. $res6 = $pdo->exec($sql_insert);
  196. if ($pdo->errorCode() != '00000') {
  197. $errorArr = $pdo->errorInfo();
  198. throw new PDOException($errorArr[2]);
  199. }
  200. //6.3连接temp_runx更新run_x相关数据
  201. $sql_reorder = "
  202. UPDATE run_$x_name a
  203. INNER JOIN temp_runx b ON a.run_id = b.run_id
  204. AND a.bus_order_id = b.bus_order_id
  205. AND a.order_id = b.start_seq_id
  206. AND b.id = a.seat_seq_id
  207. SET a.create_time = now(),
  208. a.order_lock_user_id = 2,
  209. a.seat_status = 2,
  210. a.order_main_id = b.order_main_id,
  211. a.order_main_prod_id = b.order_main_prod_id,
  212. a.order_prod_id = b.order_prod_id ;";
  213. $res7 = $pdo->exec($sql_reorder);
  214. if ($pdo->errorCode() != '00000') {
  215. $errorArr = $pdo->errorInfo();
  216. throw new PDOException($errorArr[2]);
  217. }
  218. #7,更新order_main中相关订单的座位号
  219. $sql_main = "UPDATE order_main a
  220. INNER JOIN run_$x_name b ON a.run_id = b.run_id
  221. AND a.order_id = b.order_main_id
  222. AND b.order_main_id > 0
  223. SET a.run_bus_seat_seq_id = b.seat_seq_id,
  224. a.run_bus_seat_name = b.seat_name
  225. WHERE
  226. a.run_id = $run_id
  227. AND a.run_bus_order_id = $bus_order_id
  228. AND order_valid_status = 1";
  229. $res8 = $pdo->exec($sql_main);
  230. if ($pdo->errorCode() != '00000') {
  231. $errorArr = $pdo->errorInfo();
  232. throw new PDOException($errorArr[2]);
  233. }
  234. $pdo->commit();
  235. $data = array();
  236. $data['code'] = '0';
  237. $data['info'] = 'run_x重排成功';
  238. echo json_encode($data);
  239. exit;
  240. } catch (PDOException $e) {
  241. $pdo->rollBack();
  242. $data = array();
  243. $data['code'] = '2';
  244. $data['info'] = 'run_x重排失败';
  245. $data['error_info'] = $e->getMessage();
  246. echo json_encode($data);
  247. exit;
  248. }