|
- <?php
- /*
- Author:XM
- Compeny:Spiders Travel
- */
- require_once '../Common/Mysql.php';
- if (ALLOW_ORIGIN)
- header("Access-Control-Allow-Origin:*");
- $userid=getUserId();
- if ($userid ===false){
- echo json_encode(array("code"=>1,"info"=>"用户未登录"));
- exit;
- }
- $lineid=$_GET['lineid'];
- $startime=$_GET['startime'];
- $endtime=$_GET['endtime'];
- $week=$_GET['week'];
- $allstation=$_GET['allstation'];
- $allcar=$_GET['allcar'];
- $allticket=$_GET['allticket'];
- $sellday=$_GET['sellday'];
- date_default_timezone_set('PRC'); //设置中国时区
- $current_time=date("Y-m-d H:i:s");//获取当前时间
-
- $yii_url = 'http://'.CS1_DOMAIN.'/api/dynamic-bus/run-bus-new-save';
- $send_data = array(
- 'line_id'=>$lineid,
- 'start_time'=>$startime,
- 'end_time'=>$endtime,
- 'week'=>$week,
- 'all_station'=>$allstation,
- 'all_car'=>$allcar,
- 'all_ticket'=>$allticket,
- 'sell_day'=>$sellday,
- 'current_time'=>$current_time,
- 'user_id'=>$userid
- );
-
- $res = httpRequest($yii_url,$send_data);
- echo $res;
- exit;
- //$testsql=$userid.",".$lineid.",'".$startime."','".$endtime."','".$week."','".$allstation."','".$allcar."','".$allticket."',".$sellday;
-
- /*$json['code'] = '154';
- $json['info'] =$allticket;
- echo json_encode($json);
- exit;*/
-
- //判断lineid有效性
- $line_id = isset($lineid)?trim($lineid):false;
- if(!$line_id )
- {
- $json['code'] = '154';
- $json['info'] = '线路产品参数错误';
- echo json_encode($json);
- exit;
- }
- //获得run_main表数据数量并判断星期日期有效性
- for($i=0;$i<strlen($week);$i++){
- $arr[$i] = $week[$i];
- }
- $start_time = strtotime($startime);
- $end_time = strtotime($endtime);
- $record_num = 0;
- for($i = $start_time; $i <= $end_time; $i += 86400) {
- if(in_array(date('w',$i),$arr)){
- $record_num++;
- }
- }
- if($record_num == 0){
- $json['code'] = '2';
- $json['info'] = '日期星期选择有误';
- echo json_encode($json) ;
- }/*else{
- $json['code'] = '2';
- $json['info'] = $record_num;
- echo json_encode($json) ;
- }*/
- //在计数器里生成run_id
- $pdo=conn();
-
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//mysql错误时抛出异常
- $pdo->beginTransaction(); //开始事务
-
- try{
-
- $get_run_id_update_sql = "UPDATE BASE_UNIQUE_ID2 SET UNIQUE_ID = UNIQUE_ID + ${record_num} WHERE ID = 3;";
- $get_run_id_result = $pdo->exec($get_run_id_update_sql);
-
- $get_run_id_select_sql = "SELECT UNIQUE_ID As total_num FROM BASE_UNIQUE_ID2 WHERE ID = 3";
- $result1 = $pdo->query($get_run_id_select_sql);
- $result1_rowset = $result1->fetchAll(PDO::FETCH_ASSOC);
- $start_num = $result1_rowset[0]["total_num"]-$record_num+1;
- $end_num = $result1_rowset[0]["total_num"];
- /*$result1->closeCursor();*/
-
- if (empty($result1_rowset)) {
- throw new PDOException('生成run_id失败');
- }
- //处理get参数allstation
- //获取station数量
- $station_count = substr_count($allstation,'{');
- if($station_count == 0){
- $json['code'] = '2';
- $json['info'] = '数据有误';
- echo json_encode($json) ;
- }
-
- $station_arr = (explode('}',$allstation));
- array_pop($station_arr);
- $station_str1 = $station_arr[0];
- $station_arr1 = explode(',',$station_str1);
- $run_minutes = $station_arr1[1];//得到all_station中的第一个run_minutes
- $station_str2 = floor($run_minutes/60);
- $station_str3 = ($run_minutes%60)<10?($run_minutes%60) . '0' :($run_minutes%60);
- $run_time = $station_str2 . ':' . $station_str3; //拼出对应的run_time
- //处理run_station数据
- $station_res_ids = array();
- $station_run_minutes = array();
- $station_inout_types = array();
- $station_checkport_ids = array();
- $station_run_times = array();
-
- foreach($station_arr as $val ) {
- $val = substr($val, 1);
- $station_arr2 = explode(',', $val);
- $station_str4 = floor($station_arr2[1] / 60);
- $station_str5 = ($station_arr2[1] % 60) < 10 ? ($station_arr2[1] % 60) . '0' : ($station_arr2[1] % 60);
- $station_run_time = $station_str4 . ':' . $station_str5; //拼出$run_time
-
- $station_res_ids[] = $station_arr2[0];
- $station_run_minutes[] = $station_arr2[1];
- $station_inout_types[] = $station_arr2[2];
- $station_checkport_ids[] = $station_arr2[3];
- $station_run_times[] = $station_run_time;
- }
- //station_order_id 和 area_id 从opera_station表里面取
- $opera_station_sql = "SELECT order_id,
- res_id,
- area_id
- FROM opera_station WHERE LINE_ID = {$lineid}";
- $opera_station_result = $pdo->query($opera_station_sql);
- $opera_station_rowset = $opera_station_result->fetchAll(PDO::FETCH_ASSOC);
- if (empty($opera_station_rowset) ) {
- throw new PDOException('获取站点信息失败');
- }
- $opera_station_array = array();
- foreach( $opera_station_rowset as $opera_station_info ) {
- $opera_station_array[$opera_station_info["res_id"]] = $opera_station_info;
- }
- /*$opera_station_result->closeCursor();*/
-
- //处理参数$allcar
- $car_count = substr_count($allcar,'{');//获得数据组数
- if($car_count == 0){
- $json['code'] = '2';
- $json['info'] = '数据有误';
- echo json_encode($json) ;
- }
- $car_arr = (explode('}',$allcar));
- array_pop($car_arr);
- //处理run_bus数据
- $bus_type_res_ids = array();
- $allow_select_seats = array();
- $brand_res_ids = array();
- $cost_motorcade_ids = array();
- $cost_types = array();
- $cost_prices = array();
-
- foreach($car_arr as $val ) {
- $val = substr($val, 1);
- $car_arr2 = explode(',', $val);
-
- $bus_type_res_ids[] = $car_arr2[0];
- $allow_select_seats[] = $car_arr2[2];
- $brand_res_ids[] = $car_arr2[3];
- $cost_motorcade_ids[] = $car_arr2[4];
- $cost_types[] = $car_arr2[5];
- $cost_prices[]=$car_arr2[6];
- }
-
- //处理参数allticket
- $ticket_count = substr_count($allticket,'{');//获得数据组数
- if($ticket_count == 0){
- $json['code'] = '2';
- $json['info'] = '数据有误';
- echo json_encode($json) ;
- }
- $ticket_arr = (explode('}',$allticket));
- array_pop($ticket_arr);
- //处理run_prod数据
- $start_station_area_ids = array();
- $end_station_area_ids = array();
- $seat_types = array();
- $human_types = array();
- $max_counts = array();
- $opera_tickets_results = array();
-
- foreach($ticket_arr as $val ) {
- $val = substr($val, 1);
- $ticket_arr2 = explode(',', $val);
-
- $start_station_area_ids[] = $ticket_arr2[0];
- $end_station_area_ids[]=$ticket_arr2[1];
- $seat_types[] = $ticket_arr2[2];
- $human_types[] = $ticket_arr2[3];
- $max_counts[] = $ticket_arr2[4];
-
- $opera_tickets_sql = "SELECT
- TICKET_ID,
- START_STATION_RES_ID,
- END_STATION_RES_ID
- FROM
- opera_tickets
- WHERE
- START_STATION_AREA_ID = $ticket_arr2[0]
- AND END_STATION_AREA_ID = $ticket_arr2[1]
- AND SEAT_TYPE = $ticket_arr2[2]
- AND HUMAN_TYPE = $ticket_arr2[3]
- AND LINE_ID = $lineid
- AND CANCEL_FLAG = 0";
-
- $opera_tickets_result = $pdo->query($opera_tickets_sql);
- $opera_tickets_results[] = $opera_tickets_result->fetchAll(PDO::FETCH_ASSOC);
- $tes = array(
- 'code'=>0,
- 'info'=>$opera_tickets_results
- );
- echo json_encode($tes);
- exit;
- if (empty($opera_tickets_result) ) {
- throw new PDOException('获取车票信息失败');
- }
-
- }
-
- //生成run_x表
- $monarr=array();
- $start_month = date('Y-m',strtotime($startime));
- /*$aaa = strtotime("$start_month+1 month");
- $bbb = strtotime('2018-01');
- $aab = strtotime($endtime);*/
- for($i=0; (strtotime("$start_month+$i month")) <= strtotime($endtime); $i++){
- $timex = strtotime("$start_month+$i month");
- $monarr[] = 'run_' . date('Ym',$timex); // 取得递增月;
- }
- foreach ($monarr as $val ){
- $create_run_x_sql .= "create table if not EXISTS {$val}
- (ID int(11) not null auto_increment,PRIMARY KEY (ID),
- RUN_ID int(11) NOT NULL DEFAULT 0,
- RUN_LOCK_TYPE int(11) NOT NULL DEFAULT 0,
- RUN_DATE varchar(20) NOT NULL DEFAULT '',
- LOCK_COUNT int(11) NOT NULL DEFAULT 1,
- ORDER_ID int(11) NOT NULL DEFAULT 0,
- STATION_RES_ID int(11) NOT NULL DEFAULT 0,
- STATION_INOUT_TYPE int(11) NOT NULL DEFAULT 0,
- BUS_ORDER_ID int(11) NOT NULL DEFAULT 0,
- BUS_RES_ID int(11) NOT NULL DEFAULT 0,
- SEAT_X int(11) NOT NULL DEFAULT 0,
- SEAT_Y int(11) NOT NULL DEFAULT 0,
- SEAT_TYPE int(11) NOT NULL DEFAULT 0,
- SEAT_SEQ_ID int(11) NOT NULL DEFAULT 0,
- SEAT_NAME varchar(100) NOT NULL DEFAULT '',
- SEAT_STATUS int(11) NOT NULL DEFAULT 0,
- CREATE_USER_ID int(11) NOT NULL DEFAULT 0,
- CREATE_TIME varchar(20) NOT NULL DEFAULT '',
- ORDER_MAIN_ID int(11) NOT NULL DEFAULT 0,
- ORDER_MAIN_PROD_ID int(11) NOT NULL DEFAULT 0,
- ORDER_PROD_ID int(11) NOT NULL DEFAULT 0,
- ORDER_SEQ_ID int(11) NOT NULL DEFAULT 0,
- ORDER_LOCK_USER_ID int(11) NOT NULL DEFAULT 0,
- ORDER_LOCK_TIME datetime NOT NULL,
- ORDER_LOCK_END_TIME datetime NOT NULL,
- HUMAN_TYPE int(11) NOT NULL DEFAULT 0,
- AREA_ID int(11) NOT NULL DEFAULT 0,
- ALLOW_SELECT_SEAT int(11) NOT NULL DEFAULT 0,
- CHECK_USER_ID INT(11) NOT NULL DEFAULT 0,
- CHECK_TIME VARCHAR(20) DEFAULT '' NOT NULL,
- CANCEL_FLAG TINYINT NOT NULL DEFAULT 1,
- INDEX IX_{$val}(RUN_ID, BUS_ORDER_ID) USING BTREE );";
- }
-
- $create_run_x_result = $pdo->exec($create_run_x_sql);
- //TODO
- //数据写入数据库
- //插入run_main表
- $tmp_run_date = $startime;
- for($insert_run_id = $start_num;$insert_run_id<=$end_num;$insert_run_id++){
- $tmp_run_x = 'run_' . date('Ym',strtotime($tmp_run_date));
- $run_main_sql = "INSERT INTO run_main (run_id,run_lock_type,run_date,run_time,run_minutes,prod_id,
- create_user_id,create_time,update_user_id,update_time,run_status)
- VALUES ( {$insert_run_id},
- 121,
- '{$tmp_run_date}',
- '{$run_time}',
- {$run_minutes},
- {$lineid},
- {$userid},
- '{$current_time}',
- {$userid},
- '{$current_time}',
- 137)";
- $run_main_result = $pdo->exec($run_main_sql);
- /*if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }*/
- //$tmp_run_date = date('Y-m-d',strtotime('+1 days',strtotime($tmp_run_date)));
- //插入run_station表
- $run_station_sql='';
- for($i=0;$i<$station_count;$i++){
- $opera_station_info = $opera_station_array[$station_res_ids[$i]];
- $station_order_id = $opera_station_info["order_id"];
- $area_id = $opera_station_info["area_id"];
- $run_station_sql .= "INSERT INTO run_station (run_id,prod_id,start_time,start_minutes,station_res_id,
- station_inout_type,checkport_res_id,create_user_id,create_time,station_order_id,area_id)
- VALUES({$insert_run_id},
- {$lineid},
- '{$station_run_times[$i]}',
- {$station_run_minutes[$i]},
- {$station_res_ids[$i]},
- {$station_inout_types[$i]},
- {$station_checkport_ids[$i]},
- {$userid},
- '{$current_time}',
- {$station_order_id},
- {$area_id}
- );";
-
- }
- $run_station_result = $pdo->exec($run_station_sql);
- /*if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }*/
- //插入run_bus表
- $run_bus_sql='';
- $run_x_sql='';
- for($j=0;$j<$car_count;$j++){
- $run_bus_sql .= "INSERT INTO run_bus (create_user_id,create_time,update_user_id,run_id,bus_order_id,
- bus_type_res_id,allow_select_seat,brand_res_id,cost_motorcade_id,cost_type,cost_price,seat_count)
- VALUES ({$userid},
- '{$current_time}',
- {$userid},
- {$insert_run_id},
- {$j}+1,
- {$bus_type_res_ids[$j]},
- {$allow_select_seats[$j]},
- {$brand_res_ids[$j]},
- {$cost_motorcade_ids[$j]},
- {$cost_types[$j]},
- {$cost_prices[$j]},
- (select count(*) from base_resource_matrix
- where RES_ID = {$bus_type_res_ids[$j]}
- and pos_type not in (0,104,105)
- and cancel_flag = 0)
- );";
-
- //同时插入run_x表
- $run_x_sql .= "INSERT INTO {$tmp_run_x}
- ( RUN_ID,
- RUN_LOCK_TYPE,
- RUN_DATE,
- ORDER_ID,
- STATION_RES_ID,
- STATION_INOUT_TYPE,
- BUS_ORDER_ID,
- BUS_RES_ID,
- SEAT_X,
- SEAT_Y,
- SEAT_TYPE,
- SEAT_SEQ_ID,
- SEAT_NAME,
- SEAT_STATUS,
- CREATE_USER_ID,
- CREATE_TIME,
- ORDER_LOCK_TIME,
- ORDER_LOCK_END_TIME,
- AREA_ID,
- ALLOW_SELECT_SEAT)
- SELECT
- {$insert_run_id},
- 121,
- '{$tmp_run_date}',
- b.station_order_id,
- b.station_res_id,
- b.station_inout_type,
- {$j}+1,
- {$bus_type_res_ids[$j]},
- a.POS_X,
- a.POS_Y,
- a.POS_TYPE,
- a.POS_SEQ_ID,
- a.POS_NAME,
- 0,
- {$userid},
- '{$current_time}',
- '{$current_time}',
- '{$current_time}',
- b.area_id,
- {$allow_select_seats[$j]}
- FROM
- base_resource_matrix AS a,
- run_station AS b
- WHERE
- a.RES_ID = {$bus_type_res_ids[$j]}
- AND a.cancel_flag = 0
- AND b.run_id = {$insert_run_id}
- /*AND b.STATION_ORDER_ID <> (
- SELECT
- max(STATION_ORDER_ID)
- FROM
- run_station
- WHERE
- run_id = {$insert_run_id}
- )*/;";
- }
- $run_bus_result = $pdo->exec($run_bus_sql);
- /*if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }*/
-
- $run_x_result = $pdo->exec($run_x_sql);
- $tmp_run_date = date('Y-m-d',strtotime('+1 days',strtotime($tmp_run_date)));
-
- /*if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }*/
-
- //插入run_prod表
- $run_prod_sql='';
- for($k = 0;$k < $ticket_count;$k++){
- foreach($opera_tickets_results[$k] as $val ){
- $run_prod_sql .= "INSERT INTO run_prod
- ( RUN_ID,
- PARENT_PROD_ID,
- PROD_ID,
- LIST_SEQ_ID,
- MAX_COUNT,
- SALED_COUNT,
- CREATE_USER_ID,
- CREATE_TIME,
- UPDATE_USER_ID,
- START_STATION_RES_ID,
- START_STATION_AREA_ID,
- END_STATION_RES_ID,
- END_STATION_AREA_ID,
- HUMAN_TYPE,
- SEAT_TYPE)
- VALUES (
- {$insert_run_id},
- {$lineid},
- {$val['TICKET_ID']},
- 0,
- {$max_counts[$k]},
- 0,
- {$userid},
- '{$current_time}',
- {$userid},
- {$val['START_STATION_RES_ID']},
- {$start_station_area_ids[$k]},
- {$val['END_STATION_RES_ID']},
- {$end_station_area_ids[$k]},
- {$human_types[$k]},
- {$seat_types[$k]}
- );";
- }
-
- $run_prod_result = $pdo->exec($run_prod_sql);
- /*if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }*/
- }
-
- //插入run_stock表
- $run_stock_sql='';
- for($l = 0;$l < $station_count;$l++){
- $run_stock_sql .= "INSERT INTO run_stock(
- run_id,
- seq_id,
- res_id,
- prod_id,
- create_user_id,
- create_time,
- update_time,
- update_user_id,
- total_count,
- seat_type)
- SELECT
- {$insert_run_id},
- {$l}+1,
- {$station_res_ids[$l]},
- {$lineid},
- {$userid},
- '{$current_time}',
- '{$current_time}',
- {$userid},
- count(ID),
- seat_type
- FROM
- {$tmp_run_x}
- WHERE
- RUN_ID = {$insert_run_id}
- AND STATION_RES_ID = {$station_res_ids[$l]}
- GROUP BY SEAT_TYPE;";
- }
-
- $run_stock_result = $pdo->exec($run_stock_sql);
- /*if ($pdo->errorCode() != '00000') {
- $errorArr = $pdo->errorInfo();
- throw new PDOException($errorArr[2]);
- }*/
-
- }
-
- $pdo->commit();
- $data = array();
- $data['code'] = '0';
- $data['info'] = '添加班次成功';
- echo json_encode($data);
- }catch(PDOException $e){
- $pdo->rollBack();
- $data = array();
- $data['code'] = '2';
- $data['info'] = $e->getMessage();
- $data['error_info'] = $e->getMessage();
- echo json_encode($data);
- exit;
- }
-
-
- /*$sql="call sp_bus_run_new_save(".$userid.",".$lineid.",'".$startime."','".$endtime."','".$week."','".$allstation."','".$allcar."','".$allticket."',".$sellday.")";
- writeLog("run_bus_new_save: ".$sql);
- if (is_object($pdo)) {
- $result = $pdo->query($sql);
- $rowset = $result->fetchAll(PDO::FETCH_ASSOC);
- $data['code'] = "0";
- $data['info'] = "";
- $data['save'] = $rowset[0];
- echo json_encode($data);
- }*/
|