<?php
if (!defined('BASEPATH')) 
	exit('No direct script access allowed');

//帐号密钥
define("ORDERUSER","guilintravel");
define("ORDERKEY","07f811fe29f04008a8fcc86e81c012b9");

//数据返回格式地址
define("JSONRETURN","http://m.ctrip.com/restapi/soa2/12976/json/");
define("XMLRETURN","http://m.ctrip.com/restapi/soa2/12976/xml/");

class ctrip_train extends CI_Controller{
	
	public function __construct(){
        parent::__construct();
		$this->load->helper("train");//加载模型
		$this->load->model("ctrip_train_model");//加载模型
    }
	
	public function addorders(){
		//接收参数
		$cold_sn = $this->input->get("order");
        $bpe_sn = $this->input->get("people");
		$SelectSeat = $this->input->get("selectseat");
		$data = array();
        $rebakc = array();//返回数据
        $rebakc["status"] = 0;
        $rebakc["mes"] = "";
		

		if(!is_numeric($cold_sn)){
            $rebakc["mes"] = "订单号是数字";
            echo json_encode($rebakc);
            return false; 
        }
        if(empty($bpe_sn)){
            $rebakc["mes"] = "请选择乘客";
            echo json_encode($rebakc);
            return false; 
        }
		
        $data['train'] = $this->ctrip_train_model->biz_order_detail($cold_sn);
        $data['people_list'] = $this->ctrip_train_model->in_bpesn_people_info($bpe_sn);
		
		/*print_r($data['train']);
		print_r($data['people_list']);*/

		//生成订单号
		$OrderNumber = ORDERUSER.time();
		
		if (empty($data['train'])) {
            //显示错误,找不到车次
            $rebakc["mes"] = "找不到车次";
            echo json_encode($rebakc);
            return false;

        }
        if (empty($data['people_list'])) {
            //显示错误，找不到用户信息
            $rebakc["mes"] = "找不到乘客信息";
            echo json_encode($rebakc);
            return false;
        }

        if (count($data['people_list']) > 5) {
            //显示错误，用户超过五个
            $rebakc["mes"] = "乘客不能超过五个";
            echo json_encode($rebakc);
            return false;
        }
		
		$db_train_zw = $this->config->item('db_train_zw');
        $train_zw = $this->config->item('train_zw');
        $zwcode = $db_train_zw[$data['train']->Aircraft]; //座位简码
        $zwname = $train_zw[$db_train_zw[$data['train']->Aircraft]]; //座位名称
		$black_list = $this->config->item('black_list');
		
		//拼接发送的报文
		$PostData = array();
		$TimeStamp = time();
		$time = date('Y-m-d H:i:s',$TimeStamp);
		$PostData['Authentication']->TimeStamp = $time;
		$PostData['Authentication']->ServiceName = 'order.PartnerAddOrder';
		$PostData['Authentication']->PartnerName = ORDERUSER;
		$MessageIdentity = md5($time.'order.PartnerAddOrder'.ORDERKEY);
		$PostData['Authentication']->MessageIdentity = $MessageIdentity;
		
		$PostData['TrainOrderService']->PartnerName = ORDERUSER;
		$PostData['TrainOrderService']->Operation = '';
		$PostData['TrainOrderService']->OrderType = '电子';
		$PostData['TrainOrderService']->OrderTicketType = '0';
		$PostData['TrainOrderService']->OrderNumber = $OrderNumber;
		$PostData['TrainOrderService']->ChannelName = ORDERUSER;
		
		$PostData['TrainOrderService']->Order->OrderTime = $time;
		$PostData['TrainOrderService']->Order->OrderMedia = 'pc';
		$PostData['TrainOrderService']->Order->Insurance = 'N';
		$PostData['TrainOrderService']->Order->Invoice = 'N';
		$PostData['TrainOrderService']->Order->PrivateCustomization = '0';
		
		$PostData['TrainOrderService']->Order->TicketItem->FromStationName = $data['train']->DepartAirport_cn;
		$PostData['TrainOrderService']->Order->TicketItem->ToStationName = $data['train']->ArrivalAirport_cn;
		$PostData['TrainOrderService']->Order->TicketItem->TicketTime = date('Y-m-d H:i:s',strtotime($data['train']->DepartureTime));
		$PostData['TrainOrderService']->Order->TicketItem->TrainNumber = $data['train']->FlightsNo;
		$PostData['TrainOrderService']->Order->TicketItem->ArrivalDateTime = date('Y-m-d H:i:s',strtotime($data['train']->ArrivalTime));
		$PostData['TrainOrderService']->Order->TicketItem->TicketPrice = $data['train']->adultcost;
		$PostData['TrainOrderService']->Order->TicketItem->TicketCount = count($data['people_list']);
		
		$AdultNum = 0;
		$ChildNum = 0;
		$Passport = '';
		foreach ($data['people_list'] as $PassagerInfo){
			//乘客类型
			switch ($PassagerInfo->BPE_GuestType) {
                case 1:
                    $PiaoType = 1;
                    $PiaoTypeName = "成人票";
					$AdultNum++;
                    break;
                case 2:
                    $PiaoType = 2;
                    $PiaoTypeName = "儿童票";
					$ChildNum++;
                    break;
                default://外国人应该就两种票吧
                    $PiaoType = 1;
                    $PiaoTypeName = "成人票";
                    break;
            }	

			//证件类型
			switch ($PassagerInfo->BPE_PassportType){
				case 'Chinese ID':
					$PassportTypeseId = "1";
					$PassportTypeseidName = "二代身份证";
					break;
				case 'Travel Permit from Hong Kong / Macau':
					$PassportTypeseidName = "港澳通行证";
					break;
				case 'Travel Permit from Taiwan':
					$PassportTypeseId = "G";
					$PassportTypeseidName = "台湾通行证";
					break;
				default :
					$PassportTypeseId = "B";
					$PassportTypeseidName = "护照";
					break;
			}
			//$Passport .= chk_sp_name($PassagerInfo->BPE_FirstName.$PassagerInfo->BPE_MiddleName.$PassagerInfo->BPE_LastName).','.$PassportTypeseidName.','.$PassagerInfo->BPE_Passport.','.$PiaoTypeName.','.''.',0|';
			
			if($PiaoType == 1){
				$RelatioNme = chk_sp_name($PassagerInfo->BPE_FirstName.$PassagerInfo->BPE_MiddleName.$PassagerInfo->BPE_LastName);
				$Passport .= chk_sp_name($PassagerInfo->BPE_FirstName.$PassagerInfo->BPE_MiddleName.$PassagerInfo->BPE_LastName).','.$PassportTypeseidName.','.$PassagerInfo->BPE_Passport.','.$PiaoTypeName.','.''.',0|';
			}elseif($PiaoType == 2){
				$Passport .= $RelatioNme.','.$PassportTypeseidName.','.$PassagerInfo->BPE_Passport.','.$PiaoTypeName.','.''.',0,'.chk_sp_name($PassagerInfo->BPE_FirstName.$PassagerInfo->BPE_MiddleName.$PassagerInfo->BPE_LastName).'|';
			}
			
		}
		
		$PostData['TrainOrderService']->Order->TicketItem->AuditTicketCount = $AdultNum;
		$PostData['TrainOrderService']->Order->TicketItem->ChildTicketCount = $ChildNum;
		$PostData['TrainOrderService']->Order->TicketItem->SeatName = $train_zw[$db_train_zw[$data['train']->Aircraft]];
		$PostData['TrainOrderService']->Order->TicketItem->SelectedSeat = $SelectSeat;
		$PostData['TrainOrderService']->Order->TicketItem->AcceptSeat = '';
		$PostData['TrainOrderService']->Order->TicketItem->passport = substr($Passport,0,strlen($Passport)-1);
		$PostData['TrainOrderService']->Order->TicketItem->OrderPrice = $data['train']->adultcost * $AdultNum + $data['train']->childcost * $ChildNum;
		
		$PostData['TrainOrderService']->Order->FrontSeatFlag = '0';
		
		$PostData['TrainOrderService']->Order->User->UserID = '';
		$PostData['TrainOrderService']->Order->User->UserName = 'guilintravel';
		$PostData['TrainOrderService']->Order->User->userLoginName = 'guilintravel';
		$PostData['TrainOrderService']->Order->User->UserMobile = '18877381547';
		//print_r($PostData);die();
		//本地添加记录
		$add_data = new stdClass();
		$add_data->cold_sn = $cold_sn;
		$add_data->ordernumber = $OrderNumber;
		$add_data->returncode = '';
		$add_data->status = '2';
		$add_data->errormsg = '预定中';
		$add_data->checi = $data['train']->FlightsNo;
		$add_data->fromstationame = $data['train']->DepartAirport_cn;
		$add_data->fromstationcode = $data['train']->DepartAirport;
		$add_data->tostationame = $data['train']->ArrivalAirport_cn;
		$add_data->tostationcode = $data['train']->ArrivalAirport;
		$add_data->startdate = date('Y-m-d',strtotime($data['train']->DepartureDate));
		$add_data->startime = date('H:i',strtotime($data['train']->DepartureTime));
		$add_data->endtime = date('H:i',strtotime($data['train']->ArrivalTime));
		$add_data->runtime = (strtotime($data['train']->ArrivalTime) - strtotime($data['train']->DepartureTime)) / 60; 
		$add_data->channel = 'ctrip';
		$add_data->isauto = 0;
		
		
		//存储到数据库
		$this->ctrip_train_model->add_orders($add_data);
		
		$Url = 'http://m.ctrip.com/restapi/soa2/11009/json/PartnerAddOrder';
		$ResponseJson = GetPost_http($Url,json_encode($PostData),'POST');
		$ResponseData = json_decode($ResponseJson);
		
		//echo '预定';
		//print_r($ResponseData);
		
		//预定请求成功后执行支付
		if($ResponseData->Status == 'SUCCESS'){
			//计算订单总价，进行支付
			$total_price = $AdultNum * $data['train']->adultcost + $ChildNum * $data['train']->childcost;
			$this->payorders($OrderNumber,$total_price);
			$rebakc["status"] = 1;
            $rebakc["order"] = $OrderNumber;
            $rebakc["mes"] = "订单提交成功，等待回调";
			echo json_encode($rebakc);
		}
	}
	
	//取消订单
	public function cancelorders(){
		$CtripOrder = $this->input->post('CtripOrder');
		
		$CtripOrder = '488110485_1543999756';
		
		//生成报文
		$PostData = array();
		$TimeStamp = time();
		$time = date('Y-m-d H:i:s',$TimeStamp);
		$PostData['Authentication']->TimeStamp = $time;
		$PostData['Authentication']->ServiceName = 'order.PartnerPayOrder';
		$PostData['Authentication']->PartnerName = ORDERUSER;
		$MessageIdentity = md5($time.'order.PartnerPayOrder'.ORDERKEY);
		$PostData['Authentication']->MessageIdentity = $MessageIdentity;
		
		$PostData['TrainOrderService']->PartnerName = ORDERUSER;
		$PostData['TrainOrderService']->OrderNumber = $CtripOrder;
		$PostData['TrainOrderService']->CancelTime = date('Y-m-d H:s:i',time());
		
		$Url = 'http://ws-ordercenter-train.fat.ctripqa.com/orderCore/api/json/PartnerCancelOrder';
		
		$ResponseData = GetPost_http($Url,json_encode($PostData),'POST');
		
		print_r($ResponseData);
	}
	
	//请求支付
	public function payorders($CtripOrder,$Price){
		if(empty($CtripOrder) && !is_numeric($Price)){
			exit('传参错误！');
		}
		
		//生成报文
		$PostData = array();
		$TimeStamp = time();
		$time = date('Y-m-d H:i:s',$TimeStamp);
		$PostData['Authentication']->TimeStamp = $time;
		$PostData['Authentication']->ServiceName = 'order.PartnerPayOrder';
		$PostData['Authentication']->PartnerName = ORDERUSER;
		$MessageIdentity = md5($time.'order.PartnerPayOrder'.ORDERKEY);
		$PostData['Authentication']->MessageIdentity = $MessageIdentity;
		
		$PostData['TrainOrderService']->PartnerName = ORDERUSER;
		$PostData['TrainOrderService']->OrderNumber = $CtripOrder;
		$PostData['TrainOrderService']->PayedPrice = $Price;
		//$PostData['TrainOrderService']->PayType = $time;
		//$PostData['TrainOrderService']->TradeNumber = $time;
		
		$Url = 'http://m.ctrip.com/restapi/soa2/11009/json/PartnerPayOrder';
		
		$ResponseJson = GetPost_http($Url,json_encode($PostData),'POST');
		$ResponseData = json_decode($ResponseJson);
		
		//echo '支付';
		//print_r($ResponseData);
		//支付同步回调信息 {"Status":"SUCCESS","PartnerName":"guilintravel","OrderNumber":"guilintravel1546071576","OperationDateTime":"2018-12-29 16:19:37","RetCode":0,"ResponseStatus":{"Timestamp":"\/Date(1546071577236+0800)\/","Ack":"Success","Errors":[],"Extension":[]}}
	}
	
	//退票接口
	public function returnticket(){
		//接收数据
		$CtripOrder = $this->input->get_post('CtripOrder');
		$PassagerId = $this->input->get_post('PassagerId');
		
		//根据获取到的订单号获取信息
		if(empty($CtripOrder)){
			exit('订单号为空');
		}
		
		$ReturnObj = $this->ctrip_train_model->get_passager_info($CtripOrder,$PassagerId);
		if(empty($ReturnObj)){
			exit('订单详情为空');
		}
		
		$PostData = array();
		$TimeStamp = time();
		$time = date('Y-m-d H:i:s',$TimeStamp);
		$PostData['Authentication']->TimeStamp = $time;
		$PostData['Authentication']->ServiceName = 'order.ticketReturn';
		$PostData['Authentication']->PartnerName = ORDERUSER;
		$MessageIdentity = md5($time.'order.ticketReturn'.ORDERKEY);
		$PostData['Authentication']->MessageIdentity = $MessageIdentity;
		
		$PostData['TrainOrderService']->contactName = '陈宇超';
		$PostData['TrainOrderService']->contactMobile = '18877381547';
		$PostData['TrainOrderService']->OrderNumber = $CtripOrder;
		$PostData['TrainOrderService']->OperatorType = '0';
		$PostData['TrainOrderService']->TicketInfo = '';
		$PostData['TrainOrderService']->TicketInfo = array();
		
		$i = 0;
		foreach($ReturnObj as $items){
			$PostData['TrainOrderService']->TicketInfo[$i]['eOrderNumber'] = $items->ts_elecnumber;
			if($items->tst_ticketype == '儿童票'){
				$PostData['TrainOrderService']->TicketInfo[$i]['eOrderType'] = '2';
			}else{
				$PostData['TrainOrderService']->TicketInfo[$i]['eOrderType'] = '1';
			}
			$PostData['TrainOrderService']->TicketInfo[$i]['seatNumber'] = $items->tst_seatdetail;
			$PostData['TrainOrderService']->TicketInfo[$i]['passportName'] = $items->tst_realname;
			$PostData['TrainOrderService']->TicketInfo[$i]['passport'] = $items->tst_numberid;
			$PostData['TrainOrderService']->TicketInfo[$i]['realName'] = $items->tst_realname;
			$i++;
		}
		
		
		//发起退票请求
		$Url = 'http://m.ctrip.com/restapi/soa2/11009/json/PartnerReturnTicket';
		$ResponseJson = GetPost_http($Url,json_encode($PostData),'POST');
		$ResponseData = json_decode($ResponseJson);
		
		//请求结束后，将乘客状态更改为出票状态
		/*$ResponseData = new stdClass();
		$ResponseData->Status = '';
		$ResponseData->Status = 'SUCCESS';*/
		
		if($ResponseData->Status == 'SUCCESS'){
			echo "<script>alert('请求成功，正在处理退票...');location.href='".site_url("/apps/trainsystem/pages/refund?order=$CtripOrder")."';</script>";
		}
		//print_r($ResponseJson);
	}
	
	//火车票改签
	public function rescheduleticket(){
		//订单号
		$CtripOrder = $this->input->get_post('CtripOrder');
		//改签车次
		$RescheduleTrainNumber = $this->input->get_post('RescheduleTrainNumber');
		//改签出发站
		$DepartStationName = $this->input->get_post('DepartStationName');
		//改签到达站
		$ArriveStationName = $this->input->get_post('ArriveStationName');
		//改签车次票价
		$RescheduleTicketPrice = $this->input->get_post('RescheduleTicketPrice');
		//改签坐席
		$RescheduleSeatName = $this->input->get_post('RescheduleSeatName');
		//改签出发时间
		$RescheduleDepartTime = $this->input->get_post('RescheduleDepartTime');
		//改签到达时间
		$RescheduleArriveTime = $this->input->get_post('RescheduleArriveTime');
		
		//赋值测试
		$CtripOrder = '488111988_1544754322';
		$RescheduleTrainNumber = 'D8205';
		$DepartStationName = '桂林';
		$ArriveStationName = '南宁东';
		$RescheduleTicketPrice = '128.5';
		$RescheduleSeatName = '二等座';
		$RescheduleDepartTime = '2019/01/01 10:38';
		$RescheduleArriveTime = '2019/01/01 13:03';
		
		$PostData = array();
		$TimeStamp = time();
		$time = date('Y-m-d H:i:s',$TimeStamp);
		$PostData['Authentication']->TimeStamp = $time;
		$PostData['Authentication']->ServiceName = 'order.partnerreschedule';
		$PostData['Authentication']->PartnerName = ORDERUSER;
		$MessageIdentity = md5($time.'order.partnerreschedule'.ORDERKEY);
		$PostData['Authentication']->MessageIdentity = $MessageIdentity;
		
		$PostData['TrainOrderService']->OrderNumber = $CtripOrder;
		$PostData['TrainOrderService']->Operator = '陈宇超';
		$PostData['TrainOrderService']->RescheduleTrainNumber = $RescheduleTrainNumber;
		$PostData['TrainOrderService']->DepartStationName = $DepartStationName;
		$PostData['TrainOrderService']->ArriveStationName = $ArriveStationName;
		$PostData['TrainOrderService']->RescheduleDepartTime = $ArriveStationName;
		$PostData['TrainOrderService']->RescheduleArriveTime = $ArriveStationName;
		$PostData['TrainOrderService']->RescheduleTicketPrice = $RescheduleTicketPrice;
		$PostData['TrainOrderService']->RescheduleSeatName = $RescheduleSeatName;
		
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos = array();
		
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['0']['eOrderNumber'] = 'E1317265149';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['0']['eOrderType'] = '1';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['0']['realName'] = 'LISI';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['0']['CarriageNo'] = '12';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['0']['seatNumber'] = '877号';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['0']['passportName'] = 'LISI';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['0']['passport'] = '123456789';
		
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['1']['eOrderNumber'] = 'E1317265149';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['1']['eOrderType'] = '1';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['1']['realName'] = 'WANGWU';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['1']['CarriageNo'] = '13';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['1']['seatNumber'] = '878号';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['1']['passportName'] = 'WANGWU';
		$PostData['TrainOrderService']->RescheduleTicketPassengerInfos['1']['passport'] = '123456789';
		
		//print_r(json_encode($PostData));die();
		$Url = 'http://ws-ordercenter-train.fat.ctripqa.com/orderCore/api/json/PartnerReschedule';
		$ResponseData = GetPost_http($Url,json_encode($PostData),'POST');
		print_r($ResponseData);
	}
	
	//回调函数
	public function ctrip_callback(){
		$back_json = file_get_contents('php://input');
		log_message('error','携程回调信息：'.$back_json);
		/*$back_json = '{"Authentication":{"ServiceName":"web.order.returnTicketNotice","PartnerName":"tieyou","TimeStamp":"2019-1-18 11:35:22","MessageIdentity":"93F2BA3253829E8FAD29B5DEB7646A59"},"TrainOrderService":{"contactName":{},"contactMobile":{},"OrderNumber":"guilintravel1547778269","refundTicket":{"childBillId":{},"orderId":"8360041214","eOrderNumber":"EB59937931","eOrderType":"1","seatNumber":"01D\u53f7","passport":"544712454","passportName":"YANGFRANCISCHENG","realName":"YANGFRANCISCHENG","status":"1","reason":"\u9000\u7968\u6210\u529f\uff0c\u9000\u6b3e\u91d1\u989d:218.50\u5143"}}}';*/
		$ctrip_backdata = json_decode($back_json);
		//print_r($ctrip_backdata);
		if(!empty($ctrip_backdata)){
			$update_data = new stdClass();
			$update_data->ServiceName = $ctrip_backdata->Authentication->ServiceName;
			$update_data->ordernumber = '';
			$update_data->seatsinfo = '';
			$update_data->TicketCheck = '';
			$update_data->bookcallback = '';
			$update_data->confirmcallback = '';
			$update_data->returncallback = '';
			$update_data->OrderTotleFee = 0;
			$update_data->ElectronicOrderNumber = '';
			$update_data->reschedulecallback = '';
			
			if($update_data->ServiceName == 'web.order.notifyTicket'){
				$update_data->OrderStatus = '4';
				$update_data->ErrorMsg = '出票成功';
				$update_data->ordernumber = $ctrip_backdata->TrainOrderService->OrderInfo->OrderNumber;
				$update_data->OrderTotleFee = $ctrip_backdata->TrainOrderService->OrderInfo->OrderTotleFee;
				$update_data->ElectronicOrderNumber = $ctrip_backdata->TrainOrderService->OrderInfo->ElectronicOrderNumber;
				
				//新添加检票口信息
				if(isset($ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->TicketCheck)){
					if(!is_object($ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->TicketCheck)){
						$update_data->TicketCheck = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->TicketCheck;
					}
				}
				
				//获取总票数，由于携程接口单人和多人返回的数据结构不一致
				$person_num = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->TicketCount;
				
				//存储座位信息 转换为英文
				$coach_arr = array();
				$seats_arr = array();
				$data_passager = new stdClass();
				$string = '';
				$i = 0;
				if($person_num > 1){
					foreach ($ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->DetailInfos->DetailInfo as $items){
						if(strpos($items->SeatNo,'车厢')){
							$coach = mb_substr($items->SeatNo,0,strpos($items->SeatNo,'车厢'));
							array_push($coach_arr,$coach);
							$seat = mb_substr($items->SeatNo,strpos($items->SeatNo,'车厢')+2,mb_strlen($items->SeatNo,'UTF8'));
							$find = array('号');
							$replace = array('');
							$seat = str_replace($find,$replace,$seat);
							array_push($seats_arr,$seat);
						}
						
						//对订票乘客进行存储
						$data_passager->ordernumber = $ctrip_backdata->TrainOrderService->OrderInfo->OrderNumber;
						$data_passager->realname = $items->PassengerName;
						$data_passager->identitytype = $items->IdentityType;
						$data_passager->numberid = $items->NumberID;
						$data_passager->ticketype = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->TicketType;
						$data_passager->ticketprice = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->OrderTicketPrice;
						$data_passager->seatype = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->OrderTicketSeat;
						$data_passager->seatdetail = $items->SeatNo;
						$this->ctrip_train_model->add_passagers($data_passager);
						$i++;
					}
					
				}else{
					$seatinfo_html = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->DetailInfos->DetailInfo->SeatNo;
					if(strpos($seatinfo_html,'车厢')){
						$coach = mb_substr($seatinfo_html,0,strpos($seatinfo_html,'车厢'));
						array_push($coach_arr,$coach);
						$seat = mb_substr($seatinfo_html,strpos($seatinfo_html,'车厢')+2,mb_strlen($seatinfo_html,'UTF8'));
						$find = array('号');
						$replace = array('');
						$seat = str_replace($find,$replace,$seat);
						array_push($seats_arr,$seat);
					}
					
					//对订票乘客进行存储
					$data_passager->ordernumber = $ctrip_backdata->TrainOrderService->OrderInfo->OrderNumber;
					$data_passager->realname = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->DetailInfos->DetailInfo->PassengerName;
					$data_passager->identitytype = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->DetailInfos->DetailInfo->IdentityType;
					$data_passager->numberid = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->DetailInfos->DetailInfo->NumberID;
					$data_passager->ticketype = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->TicketType;
					$data_passager->ticketprice = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->OrderTicketPrice;
					$data_passager->seatype = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->OrderTicketSeat;
					$data_passager->seatdetail = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfoFinal->Tickets->Ticket->DetailInfos->DetailInfo->SeatNo;
					$this->ctrip_train_model->add_passagers($data_passager);
				}
				
				if(count(array_unique($coach_arr)) == 1){
					$onlycoach = array_unique($coach_arr);
					$update_data->seatsinfo .= 'Coach '.$onlycoach[0].',';
				}else{
					foreach (array_unique($coach_arr) as $item_coach){
						$update_data->seatsinfo .= 'Coach '.$item_coach.',';
					}
				}
				
				$update_data->seatsinfo .= 'Seat ';
				foreach($seats_arr as $item_seat){
					$update_data->seatsinfo .= $item_seat.',';
				}
				
				$update_data->seatsinfo = substr($update_data->seatsinfo,0,strlen($update_data->seatsinfo)-1);
				
				$update_data->bookcallback = $back_json;
				
				//添加支付记录
				$add_train_payment_data->TOC_Memo = $update_data->ordernumber;
				//根据订单号获取cold_sn
				$order_info = $this->ctrip_train_model->get_order_info($update_data->ordernumber);
				$cold_sn = $order_info->ts_cold_sn;
				$add_train_payment_data->TOC_COLD_SN = $cold_sn;
				$add_train_payment_data->TOC_TrainNumber = $ctrip_backdata->TrainOrderService->OrderInfo->TicketInfo->OrderTicketCheci;
				$add_train_payment_data->TOC_DepartureDate = date('Y-m-d',strtotime($ctrip_backdata->TrainOrderService->OrderInfo->TicketInfo->OrderTicketYMD));
				$add_train_payment_data->TOC_TicketCost = $update_data->OrderTotleFee;
				$add_train_payment_data->poundage = ($person_num*5)."";//手续费，每人五块，转换成字符串
				$add_train_payment_data->FOI_TrainNetOrderNo = $update_data->ElectronicOrderNumber;
				//print_r($add_train_order_data);die();
				$this->ctrip_train_model->add_train_payment($add_train_payment_data);
				//记录供应商(瀚特)
				$this->ctrip_train_model->update_cold_planvei_sn($cold_sn);
			}else if($update_data->ServiceName == 'web.order.notifyNoTicket'){
				$update_data->ordernumber = $ctrip_backdata->TrainOrderService->OrderInfo->OrderNumber;
				$update_data->OrderStatus = '1';
				$update_data->ErrorMsg = $ctrip_backdata->TrainOrderService->OrderInfo->NoTicketReasons;
				$update_data->confirmcallback = $back_json;
			}else if($update_data->ServiceName == 'web.order.returnTicketNotice'){
				$update_data->ordernumber = $ctrip_backdata->TrainOrderService->OrderNumber;
				$update_data->OrderStatus = '7';
				$update_data->ErrorMsg = $ctrip_backdata->TrainOrderService->refundTicket->reason;
				$update_data->returncallback = $back_json;

				//退票时还需要单独对对每个乘客存储回调信息
				$passpager_info = new stdClass();
				$passpager_info->returncallback = $back_json;
				$passpager_info->status = '7';
				$passpager_info->ordernumber = $ctrip_backdata->TrainOrderService->OrderNumber;
				$passpager_info->realname = $ctrip_backdata->TrainOrderService->refundTicket->realName;
				$passpager_info->numberid = $ctrip_backdata->TrainOrderService->refundTicket->passport;
				$this->ctrip_train_model->update_passpager_info($passpager_info);
			}else if($update_data->ServiceName == 'web.order.requestRefund'){
				$return_order = $ctrip_backdata->TrainOrderService->OrderInfo->OrderNumber;
				$return_money = $ctrip_backdata->TrainOrderService->TotalRefundAmount;
					
				//根据订单号获取cold_sn
				$order_info = $this->ctrip_train_model->get_order_info($return_order);
				$cold_sn = $order_info->ts_cold_sn;
				//print_r($order_info);
				
				$add_train_payment_data->TOC_Memo = $return_order.'_'.$ctrip_backdata->TrainOrderService->OrderInfo->OrderTid;
				$add_train_payment_data->TOC_COLD_SN = $cold_sn;
				$add_train_payment_data->TOC_TrainNumber = $order_info->ts_checi;
				$add_train_payment_data->TOC_DepartureDate = $order_info->ts_startdate;
				$add_train_payment_data->TOC_TicketCost = -$ctrip_backdata->TrainOrderService->TotalRefundAmount;
				$add_train_payment_data->FOI_TrainNetOrderNo=null;
				//print_r($add_train_payment_data);die();
				$this->ctrip_train_model->add_train_payment($add_train_payment_data);
				return false;
			}
			
			//更新订单信息(出票系统)
			$this->ctrip_train_model->update_orders($update_data);
		}
		//print_r($update_data);
		//print_r(json_decode($back_xml));	
		
	}
	
}