|
- <?php
- require_once __DIR__ . '/../st-xm/Common/Mysql.php';
- header("Access-Control-Allow-Origin:*");
- //车次run_x表重排
-
- date_default_timezone_set('Asia/Shanghai');
- $pdo = conn();//pdo连接数据库
-
- $run_id = isset($_POST['run_id']) ? $_POST['run_id'] : 0;
- $bus_order_id = isset($_POST['bus_order_id']) ? $_POST['bus_order_id'] : 0;
- #0,根据父订单order_id判断是单一线路还是组合线路,如果是单一线路,则重排run_x;如果是组合线路直接返回,不做重排
- $order_id = isset($_POST['order_id']) ? $_POST['order_id'] : 0;
- $order_id = $order_id + 0;
- $run_id = $run_id + 0;
- $bus_order_id = $bus_order_id + 0;
-
- if (isset($_POST['order_id'])) { //如果没传order_id,则直接使用前面的run_id,bus_order_id;否则根据order_id得到run_id,bus_order_id
- if (!$order_id) {
- $data = array();
- $data['code'] = '1';
- $data['info'] = '参数不正确';
- echo json_encode($data);
- exit;
- }
- //判断所属线路类型,为255,256的才做重排
- $sql_c1 = "
- SELECT
- b.line_type
- FROM
- order_main a
- LEFT JOIN opera_line b ON a.PARENT_PROD_ID = b.line_id
- WHERE
- a.order_id = $order_id
- AND a.PARENT_ORDER_ID = 0
- LIMIT 1;";
- $res0 = $pdo->query($sql_c1);
- $res0->setFetchMode(PDO::FETCH_ASSOC);
- $line_type = $res0->fetchColumn();
-
- if ($line_type == 256 || $line_type == 255 || $line_type = 368) {
- $sql_c2 = "SELECT
- run_id,
- run_bus_order_id as bus_order_id
- FROM
- order_main
- WHERE
- parent_order_id = $order_id
- AND cancel_flag = 0
- limit 1;";
-
- $res = $pdo->query($sql_c2);
- $res->setFetchMode(PDO::FETCH_ASSOC);
- $run_info = $res->fetch();
- $run_id = $run_info['run_id'];
- $bus_order_id = $run_info['bus_order_id'];
- }
-
- } else {
- if (!$run_id || !$bus_order_id) {
- $data = array();
- $data['code'] = '1';
- $data['info'] = '参数不全';
- echo json_encode($data);
- exit;
- }
- }
-
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//mysql错误时抛出异常
- $pdo->beginTransaction(); //开始事务
-
-
- try {
- #1,获取所在run_x表名称
- $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";
-
- $res1 = $pdo->query($sql1);
- $res1->setFetchMode(PDO::FETCH_ASSOC);
- $x_name = $res1->fetchColumn();
-
- if (!$x_name) {
- throw new PDOException('获取相应run_x表失败');
- }
- #2获取车次的run_x数据
- $sql2 = "SELECT
- id,
- run_id,
- bus_order_id,
- order_id,
- seat_seq_id,
- seat_name,
- seat_status,
- order_main_id,
- 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,
- order_main_prod_id,
- order_prod_id,
- order_lock_user_id
- FROM
- run_$x_name
- WHERE
- run_id = $run_id
- AND bus_order_id = $bus_order_id;";
- $res2 = $pdo->query($sql2);
- $res2->setFetchMode(PDO::FETCH_ASSOC);
- $x_arr = $res2->fetchAll();
- $res2->closeCursor();
- #3获取相关子订单 --注意排序会对最后run_x重排是否紧密有影响
- $sql3 = "SELECT
- id,
- run_id,
- run_bus_order_id as bus_order_id,
- order_id as order_main_id,
- parent_order_id,
- parent_prod_id as order_main_prod_id,
- prod_id as order_prod_id,
- prod_start_station_seq_id as start_seq_id,
- prod_start_station_res_id as start_res_id,
- prod_end_station_seq_id as end_seq_id,
- prod_end_station_res_id as end_res_id
- FROM
- order_main
- WHERE
- run_id = $run_id
- AND run_bus_order_id= $bus_order_id
- AND order_valid_status = 1
- AND order_prod_type=82
- ORDER BY start_seq_id,end_seq_id,parent_order_id;";
-
- $res3 = $pdo->query($sql3);
- $order_ori = $res3->fetchAll(PDO::FETCH_ASSOC);
-
-
- #4特别处理订单数组,得到需要更新到run_x表的数据
- $order_x = array();
- foreach ($order_ori as $k1 => $v1) {
- $order_ori[$k1]['s_id'] = $k1 + 1;
- }
-
- $len = count($order_x);
- foreach ($order_ori as $k => $v) {
- $num = $v['end_seq_id'] - $v['start_seq_id'];
- if ($num >= 1) {
- for ($i = 1; $i <= $num; $i++) {
- $order_x[] = $v;
- $v['start_seq_id'] = $v['start_seq_id'] + 1;
- }
- }
- }
-
- if (empty($order_ori) || empty($order_x)) {
- throw new PDOException('获取相应订单数据失败');
- }
- #5取消原run_x(对应run_id,bus_order_id)的数据
- $sql_reset = "
- UPDATE run_$x_name
- SET seat_status = 0,
- order_lock_user_id = 0,
- order_main_id = 0,
- order_main_prod_id = 0,
- order_prod_id = 0
- WHERE
- run_id = $run_id
- AND bus_order_id= $bus_order_id;";
-
- $res4 = $pdo->exec($sql_reset);
- if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }
-
- #6重排相应run_x数据
- //6.1创建临时表,供update使用
- $sql_create = "drop temporary table if exists temp_runx;
- create temporary table temp_runx(
- id int,
- run_id int,
- bus_order_id int,
- order_main_id int,
- parent_order_id int,
- order_main_prod_id int,
- order_prod_id int,
- start_seq_id int,
- start_res_id int,
- end_seq_id int,
- end_res_id int
- );";
- $res5 = $pdo->exec($sql_create);
- if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }
- //6.2需要插入临时表的数据
- $values = '';
- foreach ($order_x as $k => $v) {
- $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'] . '),';
- };
- $values = trim($values, ',');
- $sql_insert = "INSERT INTO temp_runx (
- id,
- run_id,
- bus_order_id,
- order_main_id,
- parent_order_id,
- order_main_prod_id,
- order_prod_id,
- start_seq_id,
- start_res_id,
- end_seq_id,
- end_res_id
- )
- VALUES
- $values;";
- //$info=$pdo->errorInfo();
- $res6 = $pdo->exec($sql_insert);
- if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }
- //6.3连接temp_runx更新run_x相关数据
- $sql_reorder = "
- UPDATE run_$x_name a
- INNER JOIN temp_runx b ON a.run_id = b.run_id
- AND a.bus_order_id = b.bus_order_id
- AND a.order_id = b.start_seq_id
- AND b.id = a.seat_seq_id
- SET a.create_time = now(),
- a.order_lock_user_id = 2,
- a.seat_status = 2,
- a.order_main_id = b.order_main_id,
- a.order_main_prod_id = b.order_main_prod_id,
- a.order_prod_id = b.order_prod_id ;";
-
- $res7 = $pdo->exec($sql_reorder);
-
- if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }
- #7,更新order_main中相关订单的座位号
- $sql_main = "UPDATE order_main a
- INNER JOIN run_$x_name b ON a.run_id = b.run_id
- AND a.order_id = b.order_main_id
- AND b.order_main_id > 0
- SET a.run_bus_seat_seq_id = b.seat_seq_id,
- a.run_bus_seat_name = b.seat_name
- WHERE
- a.run_id = $run_id
- AND a.run_bus_order_id = $bus_order_id
- AND order_valid_status = 1";
-
- $res8 = $pdo->exec($sql_main);
-
- if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }
-
- $pdo->commit();
- $data = array();
- $data['code'] = '0';
- $data['info'] = 'run_x重排成功';
- echo json_encode($data);
- exit;
- } catch (PDOException $e) {
- $pdo->rollBack();
- $data = array();
- $data['code'] = '2';
- $data['info'] = 'run_x重排失败';
- $data['error_info'] = $e->getMessage();
- echo json_encode($data);
- exit;
- }
-
|