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

class innerTrainSearch extends CI_Controller{

	public function __construct(){
		parent::__construct();
		header('Access-Control-Allow-Origin:*');
	    header('Access-Control-Allow-Methods:POST, GET');
	    header('Access-Control-Max-Age:0');
	    header('Access-Control-Allow-Headers:x-requested-with, Content-Type');
	    header('Access-Control-Allow-Credentials:true');
		$this->load->helper('train');
		$this->start_time = microtime(true);
		//$this->load->model("BIZ_intel_train_model");//国际火车模型
	}

	public function index(){
		exit('你走错了！');
	}

	//查询选择
	public function search(){
		//接收参数
		$date = $this->input->get_post('date');
		$from = $this->input->get_post('from');
		$to = $this->input->get_post('to');

		if(!$date || !$from || !$to){
			header("HTTP/1.1 404 Not Found");
			exit('{"httpstatus":404,"data":{"seven":false,"cache":false,"result":[],"map":{}}}');
		}else{
			//根据选择的接口供应商调用对应的接口,默认使用携程的接口
			$supplier = $this->input->get_post('supplier');

			switch ($supplier){
				case 'juhe':
					$this->juheApi($date,$from,$to);
					break;
				case 'ctrip':
					$this->NewCtripApi($date,$from,$to);
					break;
				case 'lanhai':
					$this->LanHaiApi($date,$from,$to);
					break;
				default :
					$this->NewCtripApi($date,$from,$to);
					break;
			}
		}
	}

	//聚合数据接口
	public function juheApi($train_date=null,$fromStation=null,$toStation=null,$return=false){
		//定义一些变量
		$flag = true;
		$juheTrainInfo = '';

		set_time_limit(0);

		//判断数据是否合法
		if(!$train_date || !$fromStation || !$toStation){
			exit('传参不能为空！');
		}else{
			if(strlen($fromStation) != 3){
				exit('fromStation:参数错误！');
			}
			if(strlen($toStation) != 3){
				exit('toStation:参数错误！');
			}

			$api_start_time = microtime(true);
			$train_date = date('Y-m-d', strtotime($train_date)); // 调整格式:YYYY-mm-dd, 携程的参数是YYYY-m-dd
			//调用查询模块
			$search_return = $this->Searchtrain($train_date,$fromStation,$toStation);
			$api_end_time = microtime(true);

			//调用拼接处理模块
			$trainjson = $this->createTrainJson($search_return);
			$end_time = microtime(true);

			//聚合接口相应时间
			$api_responsive_time = $api_end_time - $api_start_time;

			//我们的接口相应时间
			$responsive_time = $end_time - $this->start_time;
			log_message('error','TRAINSPEED|请求url:'.$this->url.'|聚合相应时间:'.$api_responsive_time.'s|接口运行时间:'.$responsive_time.'s|+7天:'.$this->seveth);

			if($return){
				return ($trainjson);
			}else{
				echo ($trainjson);
			}

		}
	}

	//搜索模块
	function Searchtrain($train_date,$fromStation,$toStation){
		$this->train_date = $train_date;
		$now_time = time();
		$this->differ_time = (strtotime($train_date) - $now_time) / 86400;
		$this->seveth = false;
		//屏蔽掉超过预售期的搜索，提高速度,2021-7-5 zp修改预售期为15天，不再是30天
		if($this->differ_time > 14){
			$train_date = date('Y-m-d',strtotime('+7day'));
			$this->seveth = true;
		}
		$this->url = JUHE_TRAIN_CX_API.'?key='.JUHE_TRAIN_API_KEY.'&train_date='.$train_date.'&from_station='.$fromStation.'&to_station='.$toStation;
		$train_info = GetPost_http($this->url);
		return $train_info;
	}

	//字符串拼接模块
	function createTrainJson($returnjson){
		$return_data = new stdClass();
		$return_data->data = new stdClass();
		$return_data->data->httpstatus = 200;
		$return_data->data->seveth = $this->seveth;
		$return_data->data->supplier = 'juhe';
		$return_data->data->result = array();
		$return_data->data->map = new stdClass();
		$obj = array();
		$i = 0;
		$pricestr = '';
		if(!empty($returnjson)){
			if(!empty(json_decode($returnjson)->result->list)){
				foreach (json_decode($returnjson)->result->list as $value){
					$obj[$value->from_station_code] = $value->from_station_name;
					$obj[$value->to_station_code] = $value->to_station_name;
					$seat_type = '';
					if(isset($value->gjrw_price)){
						if($value->gjrw_price > 0){
							$gjrwPrice = $value->gjrw_price * 10;
							$seat_type .= '"6":"'.$gjrwPrice.'","A6":"¥'.$value->gjrw_price.'",';
						}
					}
					if(isset($value->qtxb_price)){
						if($value->qtxb_price > 0){
							$seat_type .= '"H":"¥'.$value->qtxb_price.'",';
						}
					}
					if(isset($value->rw_price)){
						if($value->rw_price > 0){
							$rwPrice = $value->rw_price * 10;
							$seat_type .= '"4":"'.$rwPrice.'","A4":"¥'.$value->rw_price.'",';
						}
					}
					if(isset($value->rz_price)){
						if($value->rz_price > 0){
							$rzPrice = $value->rz_price * 10;
							$seat_type .= '"2":"'.$rzPrice.'","A2":"¥'.$value->rz_price.'",';
						}
					}
					if(isset($value->tdz_price)){
						if($value->tdz_price > 0){
							$seat_type .= '"P":"¥'.$value->tdz_price.'",';
						}
					}
					if(isset($value->wz_price)){
						if($value->wz_price > 0){
							$wzPrice = $value->wz_price * 10;
							$seat_type .= '"WZ":"¥'.$value->wz_price.'",';
						}
					}
					if(isset($value->yw_price)){
						if($value->yw_price > 0){
							$ywPrice = $value->yw_price * 10;
							$seat_type .= '"3":"'.$ywPrice.'","A3":"¥'.$value->yw_price.'",';
						}
					}
					if(isset($value->yz_price)){
						if($value->yz_price > 0){
							$yzPrice = $value->yz_price * 10;
							$seat_type .= '"1":"'.$yzPrice.'","A1":"¥'.$value->yz_price.'",';
						}
					}
					if(isset($value->edz_price)){
						if($value->edz_price > 0){
							$seat_type .= '"O":"¥'.$value->edz_price.'",';
						}
					}
					if(isset($value->ydz_price)){
						if($value->ydz_price > 0){
							$seat_type .= '"M":"¥'.$value->ydz_price.'",';
						}
					}
					if(isset($value->swz_price)){
						if($value->swz_price > 0){
							$swzPrice = $value->swz_price * 10;
							$seat_type .= '"9":"'.$swzPrice.'","A9":"¥'.$value->swz_price.'",';
						}
					}
					if(isset($value->dw_price)){
						if($value->dw_price > 0){
							$seat_type .= '"F":"¥'.$value->dw_price.'",';
						}
					}
					$pricestr = $seat_type.'"train_no":'.'"'.$value->train_no.'"';
					//余票字符串
					$this->can_buy_now = $value->can_buy_now;
					$return_data->data->result[$i] = '|预定|'.$value->train_no.'|'.$value->train_code.'|'.$value->from_station_name.'|'.$value->to_station_name.'|'.$value->from_station_code.'|'.$value->to_station_code.'|'.$value->start_time.'|'.$value->arrive_time.'|'.$value->run_time.'|'.$value->can_buy_now.'||'.$this->train_date.'||||||||'.ticket_exchange($value->gjrw_price,$value->gjrw_num,$this->seveth).'|'.ticket_exchange($value->qtxb_price,$value->qtxb_num,$this->seveth).'|'.ticket_exchange($value->rw_price,$value->rw_num,$this->seveth).'|'.ticket_exchange($value->rz_price,$value->rz_num,$this->seveth).'|'.ticket_exchange($value->tdz_price,$value->tdz_num,$this->seveth).'|'.ticket_exchange($value->wz_price,$value->wz_num,$this->seveth).'||'.ticket_exchange($value->yw_price,$value->yw_num,$this->seveth).'|'.ticket_exchange($value->yz_price,$value->yz_num,$this->seveth).'|'.ticket_exchange($value->edz_price,$value->edz_num,$this->seveth).'|'.ticket_exchange($value->ydz_price,$value->ydz_num,$this->seveth).'|'.ticket_exchange($value->swz_price,$value->swz_num,$this->seveth).'|'.ticket_exchange($value->dw_price,$value->dw_num,$this->seveth).'||';
					$data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$pricestr.'},"messages":[],"validateMessages":{}}';
					$return_data->data->price[$i] = $data;
					$i++;
				}
			}

		}
		$return_data->data->map = (object)$obj;
		return json_encode($return_data);
	}

	
	
 /**
  * @description: 新的携程（顺途）火车票查询接口
  * @param {*} $date  日期
  * @param {*} $from  出发站点三字码
  * @param {*} $to    到达站点三字码
  * @param {*} $return
  * @return {*}
  * @Date Changed: 
  */
	public function NewCtripApi($date=null,$from=null,$to=null,$return=false){
		if(!$date || !$from || !$to){
			header("HTTP/1.1 404 Not Found");
			exit('{"httpstatus":404,"data":{"seven":false,"cache":false,"result":[],"map":{}}}');
		}else{
			$isCache = false;
			$iseven = false;
			$cachedata = false;
			$now_time = time();
			$differ = (strtotime($date) - $now_time) / 86400;
			//屏蔽掉超过预售期的搜索，提高速度,2021-7-5 zp修改预售期为15天，不再是30天
			if($differ > 14){
				$date = date('Y-m-d',strtotime('+7day'));
				$iseven = true;
			}

			//转换三字码为中文
			$this->load->model("ctrip_model");
			$FromName = $this->ctrip_model->ReplaceCodeToName($from);
			$ToName = $this->ctrip_model->ReplaceCodeToName($to);


			if(!$FromName || !$ToName){
				header("HTTP/1.1 404 Not Found");
				exit('{"httpstatus":404,"data":{"seven":false,"cache":false,"result":[],"map":{}}}');
			}else{
				$FromName = $FromName->station_name;
				$ToName = $ToName->station_name;
			}

			//生成请求链接
			$TimeStamp = date("Y-m-d H:i:s");
			$serviceName = "train.searchTrainsBetweenStations"; //每个接口都有自己名称
			$strSign = $TimeStamp.$serviceName.COMPANYID.CTRIPKEY; //加密前的令牌
			$Sign = md5($strSign);
			$url = CTRIPURL."/searchTrainsBetweenStations";

			//构建接口参数，POST方式JSON格式
			$post_data = new stdClass();
			$post_data->authentication = new stdClass(); 
			$post_data->authentication->timeStamp = $TimeStamp;
			$post_data->authentication->serviceName = $serviceName;
			$post_data->authentication->companyId = COMPANYID;
			$post_data->authentication->messageIdentity = $Sign;
			$post_data->searchTrainsParam = new stdClass();
			$post_data->searchTrainsParam->departDate = $date;
			$post_data->searchTrainsParam->fromStationName = $FromName;
			$post_data->searchTrainsParam->toStationName = $ToName;

			$api_start_time = microtime(true);

			// //获取数据
			// echo ($url . "<hr/>");
			// echo(json_encode($post_data) . "<hr/>");
			$ResponseJson = GetPost_http($url,json_encode($post_data),'json');

			$api_end_time = microtime(true);

			$ResponseData = json_decode($ResponseJson);

			// echo($ResponseJson);

			$api_responsive_time = $api_end_time - $api_start_time;

			if(empty($ResponseData->trainList)){
				log_message('error','ctrip_trian|status:trains is empty |相应时间:'.$api_responsive_time);
			}else{
				log_message('error','ctrip_trian|status:'.$ResponseData->ResponseStatus->Ack.'|相应时间:'.$api_responsive_time);
			}

			//定义返回的json
			$ReturnData = new stdClass();
			$ReturnData->httpstatus = 200;
			$ReturnData->data = new stdClass();
			$ReturnData->data->seven = $iseven;
			$ReturnData->data->cache = $isCache;
			$ReturnData->data->supplier = 'newctrip';
			$ReturnData->data->result = array();
			$ReturnData->data->map = new stdClass();
			$obj = array();
			$i = 0;
			$PriceStr = '';

			//数据解析
			if(!empty($ResponseData->trainList)){
				foreach ($ResponseData->trainList as $TrainInfo){
					$obj[$TrainInfo->fromTelCode] = $TrainInfo->fromStationName;
					$obj[$TrainInfo->toTelCode] = $TrainInfo->toStationName;
					$SeaType = '';
					//余数初始化为空
					$gjrwNum = $rwNum = $rzNum = $tdzNum = $wzNum = $yzNum = $edzNum = $ydzNum = $swzNum = $ywNum = $dwNum = $ydwNum = $edzNum = null;
					foreach($TrainInfo->seatList as $Seats){
						//从香港出发的 D/G 火车加价3%
						if($from == 'XJA'){
							if(stripos($TrainInfo->trainNo, "G") !== false || stripos($TrainInfo->trainNo, "D") !== false){
								$Seats->maxPrice = $Seats->maxPrice * 1.03;
							}
						}

						if($Seats->seatName == '高级软卧上' || $Seats->seatName == '高级软卧'){
							$gjrwXiaPrice = $Seats->maxPrice * 10;
							$SeaType .= '"6":"'.$gjrwXiaPrice.'","A6":"¥'.$Seats->maxPrice.'",';
							$gjrwNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '软卧上' || $Seats->seatName == '软卧' ){
							$rwPrice = $Seats->maxPrice * 10;
							$SeaType .= '"4":"'.$rwPrice.'","A4":"¥'.$Seats->maxPrice.'",';
							$rwNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '一等双软上' || $Seats->seatName=='一等卧'){
							$SeaType .= '"YDW":"¥'.$Seats->maxPrice.'",';
							$ydwNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '软座'){
							$rzPrice = $Seats->maxPrice * 10;
							$SeaType .= '"2":"'.$rzPrice.'","A2":"¥'.$Seats->maxPrice.'",';
							$rzNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '特等座'){
							$SeaType .= '"P":"¥'.$Seats->maxPrice.'",';
							$tdzNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '无座'){
							$SeaType .= '"WZ":"¥'.$Seats->maxPrice.'",';
							$wzNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '硬座'){
							$yzPrice = $Seats->maxPrice * 10;
							$SeaType .= '"1":"'.$yzPrice.'","A1":"¥'.$Seats->maxPrice.'",';
							$yzNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '二等座'){
							$SeaType .= '"O":"¥'.$Seats->maxPrice.'",';
							$edzNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '一等座'){
							$SeaType .= '"M":"¥'.$Seats->maxPrice.'",';
							$ydzNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '商务座'){
							$swzPrice = $Seats->maxPrice * 10;
							$SeaType .= '"9":"'.$swzPrice.'","A9":"¥'.$Seats->maxPrice.'",';
							$swzNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '硬卧上' || $Seats->seatName == '硬卧'){
							$ywPrice = $Seats->maxPrice * 10;
							$SeaType .= '"3":"'.$ywPrice.'","A3":"¥'.$Seats->maxPrice.'",';
							$ywNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '二等双软上' || $Seats->seatName == '二等卧'){
							$SeaType .= '"EDW":"¥'.$Seats->maxPrice.'",';
							$erwNum = $Seats->ticketLeft;
						}

						if($Seats->seatName == '动卧上' || $Seats->seatName == '动卧'){
							$SeaType .= '"F":"¥'.$Seats->maxPrice.'",';
							$dwNum = $Seats->ticketLeft;
						}

						$PriceStr = $SeaType.'"train_no":'.'"'.$TrainInfo->trainNo.'"';

					}

					//对返回的数据进行容错处理
					$gjrwNum = isset($gjrwNum) ? ticket_exchange($Seats->maxPrice,$gjrwNum,$iseven) : '';
					$rwNum = isset($rwNum) ? ticket_exchange($Seats->maxPrice,$rwNum,$iseven) : '';
					$rzNum = isset($rzNum) ? ticket_exchange($Seats->maxPrice,$rzNum,$iseven) : '';
					$tdzNum = isset($tdzNum) ? ticket_exchange($Seats->maxPrice,$tdzNum,$iseven) : '';
					$wzNum = isset($wzNum) ? ticket_exchange($Seats->maxPrice,$wzNum,$iseven) : '';
					$ywNum = isset($ywNum) ? ticket_exchange($Seats->maxPrice,$ywNum,$iseven) : '';
					$yzNum = isset($yzNum) ? ticket_exchange($Seats->maxPrice,$yzNum,$iseven) : '';
					$edzNum = isset($edzNum) ? ticket_exchange($Seats->maxPrice,$edzNum,$iseven) : '';
					$ydzNum = isset($ydzNum) ? ticket_exchange($Seats->maxPrice,$ydzNum,$iseven) : '';
					$swzNum = isset($swzNum) ? ticket_exchange($Seats->maxPrice,$swzNum,$iseven) : '';
					$dwNum = isset($dwNum) ? ticket_exchange($Seats->maxPrice,$dwNum,$iseven) : '';
					$ydwNum = isset($ydwNum) ? ticket_exchange($Seats->maxPrice,$ydwNum,$iseven) : '';
					$erwNum = isset($erwNum) ? ticket_exchange($Seats->maxPrice,$erwNum,$iseven) : '';

					$runMin = $TrainInfo->durationMinutes % 60;
					$runHour = ($TrainInfo->durationMinutes - $runMin) / 60;

					$ReturnData->data->result[$i] = '|预定|'.$TrainInfo->train12306No.'|'.$TrainInfo->trainNo.'|'.$TrainInfo->fromStationName.'|'.$TrainInfo->toStationName.'|'.$TrainInfo->fromTelCode.'|'.$TrainInfo->toTelCode.'|'.$TrainInfo->startTime.'|'.$TrainInfo->arriveTime.'|'.$runHour.':'.$runMin.'|'.$TrainInfo->canWebBuy.'||'.date('Ymd',strtotime($date)).'||||||||'.$gjrwNum.'||'.$rwNum.'|'.$rzNum.'|'.$tdzNum.'|'.$wzNum.'||'.$ywNum.'|'.$yzNum.'|'.$edzNum.'|'.$ydzNum.'|'.$swzNum.'|'.$dwNum.'|||'.$ydwNum.'|'.$erwNum;

					$data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$PriceStr.'},"messages":[],"validateMessages":{}}';
					$ReturnData->data->price[$i] = $data;
					$i++;
				}
			}
			//print_r($ResponseData);die();
			$ReturnData->data->map = (object)$obj;
			if($return){
				return json_encode($ReturnData);
			}else{
				echo json_encode($ReturnData);
			}

		}
	}

 /**
  * @description: 三亚澜海商务的火车票查询接口
  * @param {*} $data
  * @param {*} $from
  * @param {*} $to
  * @param {*} $return
  * @return {*}
  * @Date Changed: 
  */	
	public function LanHaiApi($date=null,$from=null,$to=null,$return=false){
		if(!$date || !$from || !$to){
			header("HTTP/1.1 404 Not Found");
			exit('{"httpstatus":404,"data":{"seven":false,"cache":false,"result":[],"map":{}}}');
		}else{
			$isCache = false;
			$iseven = false;
			$cachedata = false;
			$now_time = time();
			$differ = (strtotime($date) - $now_time) / 86400;
			//屏蔽掉超过预售期的搜索，提高速度,2021-7-5 zp修改预售期为15天，不再是30天
			if($differ > 14){
				$date = date('Y-m-d',strtotime('+7day'));
				$iseven = true;
			}


			//生成请求链接
			$url = "http://ctrip.lhticket.com/api/open/query/train";

			//构建接口参数，POST方式JSON格式
			$post_data = new stdClass();
			$post_data->date = $date; 
			$post_data->fromStationCode = $from;
			$post_data->toStationCode = $to;

			$api_start_time = microtime(true);

			// //获取数据
			// echo ($url . "<hr/>");
			// echo(json_encode($post_data) . "<hr/>");
			$ResponseJson = GetPost_http($url,json_encode($post_data),'json');

			$api_end_time = microtime(true);

			$ResponseData = json_decode($ResponseJson);

			// 			// 获取 microtime
			// $microtime = microtime(true);

			// // 将时间戳转换为毫秒
			// $timestampInMilliseconds = round($microtime * 1000);

			// echo $timestampInMilliseconds;

			//  echo($ResponseJson);
			//  die();

			$api_responsive_time = $api_end_time - $api_start_time;

			if(empty($ResponseData->data)){
				log_message('error','lanhai_train|status:trains is empty |相应时间:'.$api_responsive_time);
			}else{
				log_message('error','lanhai_train|status:'.$ResponseData->success.'|相应时间:'.$api_responsive_time);
			}

			//定义返回的json
			$ReturnData = new stdClass();
			$ReturnData->httpstatus = 200;
			$ReturnData->data = new stdClass();
			$ReturnData->data->seven = $iseven;
			$ReturnData->data->cache = $isCache;
			$ReturnData->data->supplier = 'lanhai';
			$ReturnData->data->result = array();
			$ReturnData->data->map = new stdClass();
			$obj = array();
			$i = 0;
			$PriceStr = '';

			//数据解析
			if(!empty($ResponseData->data)){
				foreach ($ResponseData->data as $TrainInfo){
					$obj[$TrainInfo->fromStationCode] = $TrainInfo->fromStationName;
					$obj[$TrainInfo->toStationCode] = $TrainInfo->toStationName;
					$SeaType = '';
					//余数初始化为空
					$gjrwNum = $rwNum = $rzNum = $tdzNum = $wzNum = $yzNum = $edzNum = $ydzNum = $swzNum = $ywNum = $dwNum = $ydwNum = $edzNum = null;
					foreach($TrainInfo->seats as $Seats){
						
						$ticketLeft = 0 ; //澜海余票会传有和无的中文
						if ($Seats->count == "有"){
							$ticketLeft = 99;
						}else if ($Seats->count == "无"){
							$ticketLeft = 0;
						}else {
							$ticketLeft = $Seats->count;
						}

						//从香港出发的 D/G 火车加价3%
						if($from == 'XJA'){
							if(stripos($TrainInfo->trainCode, "G") !== false || stripos($TrainInfo->trainCode, "D") !== false){
								$Seats->ticketPrice = $Seats->ticketPrice * 1.03;
							}
						}

						if(strstr($Seats->seatTypeName,'高级软卧')){
							$gjrwXiaPrice = $Seats->ticketPrice * 10;
							$SeaType .= '"6":"'.$gjrwXiaPrice.'","A6":"¥'.$Seats->ticketPrice.'",';
							$gjrwNum = $ticketLeft;
						}

						if(strstr($Seats->seatTypeName,'软卧') ){
							$rwPrice = $Seats->ticketPrice * 10;
							$SeaType .= '"4":"'.$rwPrice.'","A4":"¥'.$Seats->ticketPrice.'",';
							$rwNum = $ticketLeft;
						}

						if(strstr($Seats->seatTypeName,'一等卧')){
							$SeaType .= '"YDW":"¥'.$Seats->ticketPrice.'",';
							$ydwNum = $ticketLeft;
						}

						if($Seats->seatTypeName == '软座'){
							$rzPrice = $Seats->ticketPrice * 10;
							$SeaType .= '"2":"'.$rzPrice.'","A2":"¥'.$Seats->ticketPrice.'",';
							$rzNum = $ticketLeft;
						}

						if($Seats->seatTypeName == '特等座'){
							$SeaType .= '"P":"¥'.$Seats->ticketPrice.'",';
							$tdzNum = $ticketLeft;
						}

						if($Seats->seatTypeName == '无座'){
							$SeaType .= '"WZ":"¥'.$Seats->ticketPrice.'",';
							$wzNum = $ticketLeft;
						}

						if($Seats->seatTypeName == '硬座'){
							$yzPrice = $Seats->ticketPrice * 10;
							$SeaType .= '"1":"'.$yzPrice.'","A1":"¥'.$Seats->ticketPrice.'",';
							$yzNum = $ticketLeft;
						}

						if($Seats->seatTypeName == '二等座'){
							$SeaType .= '"O":"¥'.$Seats->ticketPrice.'",';
							$edzNum = $ticketLeft;
						}

						if($Seats->seatTypeName == '一等座'){
							$SeaType .= '"M":"¥'.$Seats->ticketPrice.'",';
							$ydzNum = $ticketLeft;
						}

						if($Seats->seatTypeName == '商务座'){
							$swzPrice = $Seats->ticketPrice * 10;
							$SeaType .= '"9":"'.$swzPrice.'","A9":"¥'.$Seats->ticketPrice.'",';
							$swzNum = $ticketLeft;
						}

						if(strstr($Seats->seatTypeName,'硬卧')){
							$ywPrice = $Seats->ticketPrice * 10;
							$SeaType .= '"3":"'.$ywPrice.'","A3":"¥'.$Seats->ticketPrice.'",';
							$ywNum = $ticketLeft;
						}

						if(strstr($Seats->seatTypeName, '二等卧')){
							$SeaType .= '"EDW":"¥'.$Seats->ticketPrice.'",';
							$erwNum = $ticketLeft;
						}

						if(strstr($Seats->seatTypeName,'动卧')){
							$SeaType .= '"F":"¥'.$Seats->ticketPrice.'",';
							$dwNum = $ticketLeft;
						}

						$PriceStr = $SeaType.'"train_no":'.'"'.$TrainInfo->trainCode.'"';

					}

					//对返回的数据进行容错处理
					$gjrwNum = isset($gjrwNum) ? ticket_exchange($Seats->ticketPrice,$gjrwNum,$iseven) : '';
					$rwNum = isset($rwNum) ? ticket_exchange($Seats->ticketPrice,$rwNum,$iseven) : '';
					$rzNum = isset($rzNum) ? ticket_exchange($Seats->ticketPrice,$rzNum,$iseven) : '';
					$tdzNum = isset($tdzNum) ? ticket_exchange($Seats->ticketPrice,$tdzNum,$iseven) : '';
					$wzNum = isset($wzNum) ? ticket_exchange($Seats->ticketPrice,$wzNum,$iseven) : '';
					$ywNum = isset($ywNum) ? ticket_exchange($Seats->ticketPrice,$ywNum,$iseven) : '';
					$yzNum = isset($yzNum) ? ticket_exchange($Seats->ticketPrice,$yzNum,$iseven) : '';
					$edzNum = isset($edzNum) ? ticket_exchange($Seats->ticketPrice,$edzNum,$iseven) : '';
					$ydzNum = isset($ydzNum) ? ticket_exchange($Seats->ticketPrice,$ydzNum,$iseven) : '';
					$swzNum = isset($swzNum) ? ticket_exchange($Seats->ticketPrice,$swzNum,$iseven) : '';
					$dwNum = isset($dwNum) ? ticket_exchange($Seats->ticketPrice,$dwNum,$iseven) : '';
					$ydwNum = isset($ydwNum) ? ticket_exchange($Seats->ticketPrice,$ydwNum,$iseven) : '';
					$erwNum = isset($erwNum) ? ticket_exchange($Seats->ticketPrice,$erwNum,$iseven) : '';

					//时间处理
					$startTime =  date("H:i",  strtotime($TrainInfo->startTime));;
					$arrivalTime =  date("H:i",  strtotime($TrainInfo->arrivalTime));;
					$runMin = $TrainInfo->travelTime % 60;
					$runHour = ($TrainInfo->travelTime - $runMin) / 60;

					$ReturnData->data->result[$i] = '|预定|'.$TrainInfo->trainNo.'|'.$TrainInfo->trainCode.'|'.$TrainInfo->fromStationName.'|'.$TrainInfo->toStationName.'|'.$TrainInfo->fromStationCode.'|'.$TrainInfo->toStationCode.'|'.$startTime.'|'.$arrivalTime.'|'.$runHour.':'.$runMin.'|Y||'.date('Ymd',strtotime($date)).'||||||||'.$gjrwNum.'||'.$rwNum.'|'.$rzNum.'|'.$tdzNum.'|'.$wzNum.'||'.$ywNum.'|'.$yzNum.'|'.$edzNum.'|'.$ydzNum.'|'.$swzNum.'|'.$dwNum.'|||'.$ydwNum.'|'.$erwNum;

					$data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$PriceStr.'},"messages":[],"validateMessages":{}}';
					$ReturnData->data->price[$i] = $data;
					$i++;
				}
			}
			//print_r($ResponseData);die();
			$ReturnData->data->map = (object)$obj;
			if($return){
				return json_encode($ReturnData);
			}else{
				echo json_encode($ReturnData);
			}

		}
	}

	//App项目组的接口 暂时不用
	public function appGroupApi($train_date=null,$fromStation=null,$toStation=null,$return=false){
		set_time_limit(0);

		//判断数据是否合法
		if(!$train_date || !$fromStation || !$toStation){
			exit('传参不能为空！');
		}else{
			if(strlen($fromStation) != 3){
				exit('fromStation:参数错误！');
			}
			if(strlen($toStation) != 3){
				exit('toStation:参数错误！');
			}

			$api_start_time = microtime(true);
			$train_date = date('Y-m-d', strtotime($train_date)); // 调整格式:YYYY-mm-dd
			//调用查询模块
			$this->train_date = $train_date;
			$now_time = time();
			$this->differ_time = (strtotime($train_date) - $now_time) / 86400;
			$this->seveth = false;
			//屏蔽掉超过预售期的搜索 ,修改预售期为15天，不再是30天
			if($this->differ_time > 14){
				$train_date = date('Y-m-d',strtotime('+7day'));
				$this->seveth = true;
			}
			$appGroupUrl = "http://106.14.5.249:8016/otn/leftTicket/query?leftTicketDTO.train_date=2022-07-15&leftTicketDTO.from_station=GLZ&leftTicketDTO.to_station=NNZ&purpose_codes=ADULT";
			//$appGroupUrl = "http://106.14.5.249:8185/otn/leftTicket/query?leftTicketDTO.train_date=2022-07-15&leftTicketDTO.from_station=GLZ&leftTicketDTO.to_station=NNZ&purpose_codes=ADULT";
			$this->url = $appGroupUrl;
			$train_info = GetPost_http($this->url);

			//$search_return = $train_info;
			$api_end_time = microtime(true);

			//调用拼接处理模块
			$trainjson = json_encode($train_info);
			$end_time = microtime(true);

			//聚合接口相应时间
			$api_responsive_time = $api_end_time - $api_start_time;

			//我们的接口相应时间
			$responsive_time = $end_time - $this->start_time;
			log_message('error','TRAINSPEED|请求url:'.$this->url.'|APP项目组相应时间:'.$api_responsive_time.'s|接口运行时间:'.$responsive_time.'s|+7天:'.$this->seveth);

			if($return){
				return ($trainjson);
			}else{
				echo ($trainjson);
			}

		}
	}

	//查询经停站(基于携程api) 
	public function getstopstation(){
		//接收参数
		$DepartDate = $this->input->get_post('DepartDate');
		$TrainNo = $this->input->get_post('TrainNo');

		if(!$DepartDate || !$TrainNo){
			exit('传参错误！');
		}else{
			//生成请求链接
			$TimeStamp = time();
			$Sign = md5($TimeStamp.DATAKEY);

			$url = JSONRETURN.'GetStopStations/?DepartDate='.$DepartDate.'&TrainNo='.$TrainNo.'&User='.DATAUSER.'&TimeStamp='.$TimeStamp.'&Sign='.$Sign;

			$ResponseJson = GetPost_http($url,'','GET');
			$ResponseData = json_decode($ResponseJson);

			//构造12306格式
			$ReturnData = array();
			$ReturnData['validateMessagesShowId'] = '_validatorMessage';
			$ReturnData['status'] = true;
			$ReturnData['httpstatus'] = 200;
			$ReturnData['data'] = array();
			$i = 0;
			$Last_num = count($ResponseData->StopStations);
			foreach($ResponseData->StopStations as $items){
				if($i == 0){
					$ReturnData['data']['data'][$i]['start_station_name'] = $items->StationName;
					$ReturnData['data']['data'][$i]['station_train_code'] = $TrainNo;
					$ReturnData['data']['data'][$i]['end_station_name'] = $ResponseData->StopStations[$Last_num-1]->StationName;
				}
				$ReturnData['data']['data'][$i]['arrive_time'] = $items->ArrivalTime;
				$ReturnData['data']['data'][$i]['station_name'] = $items->StationName;
				$ReturnData['data']['data'][$i]['start_time'] = $items->StartTime;
				$ReturnData['data']['data'][$i]['stopover_time'] = $items->StopMinutes;
				$ReturnData['data']['data'][$i]['station_no'] = $items->StationNo;
				$ReturnData['data']['data'][$i]['isEnabled'] = true;
				$i++;
			}
			$ReturnData['messages'] = array();
			$ReturnData['validateMessages'] = new stdClass();
			print_r(json_encode($ReturnData));
		}
	}

	//获取所有站点信息(基于新携程接口 2022-7-18）
	public function getallstation(){
		ini_set('max_execution_time', 600);
		//生成请求参数
		$TimeStamp = date("Y-m-d H:i:s");
		$serviceName = "train.getAllStations";
		$strSign = $TimeStamp.$serviceName.COMPANYID.CTRIPKEY; //加密前的令牌
		$Sign = md5($strSign);
		$url = CTRIPURL."/getAllStations";

		//构建接口参数，POST方式JSON格式
		$post_data = new stdClass();
		$post_data->authentication = new stdClass(); 
		$post_data->authentication->timeStamp = $TimeStamp;
		$post_data->authentication->serviceName = $serviceName;
		$post_data->authentication->companyId = COMPANYID;
		$post_data->authentication->messageIdentity = $Sign;

		//获取数据
		$ResponseJson = GetPost_http($url,json_encode($post_data),'json');
		//echo $ResponseJson;
		$ResponseData = json_decode($ResponseJson);

		$data = array();
		foreach ($ResponseData->stationList as $items){
			$data['StationName'] = isset($items->stationName) ? checkNull($items->stationName) : '';
			$data['PinYin'] = isset($items->pinYin) ? checkNull($items->pinYin) : '';
			$data['Telecode'] = isset($items->telecode) ? checkNull($items->telecode) : '';
			$data['Address'] = isset($items->address) ? checkNull($items->address) : '';
			$data['Geography'] = isset($items->shortPinYin) ? checkNull($items->shortPinYin) : '';
			$this->load->model("ctrip_model");
			$this->ctrip_model->AddOrUpdate($data);
		}

		echo("所有站点数据更新完毕！");
	}

	public function testgetstation(){
		$TimeStamp = date("Y-m-d H:i:s");
		//$TimeStamp = time();
		$serviceName = "train.getAllStations";
		$companyId = COMPANYID;
		$key = CTRIPKEY;

		// /** 测试服务器 */
		// $companyId = "1503141150";
		// $key = "hajkrtbcc";


		$strSign = $TimeStamp.$serviceName.$companyId.$key;
		$sign = md5($strSign);
		$E_sign = md5(urlencode($strSign));
		//echo($strSign . "<hr/>");
		echo($sign. "<hr/>");
		echo($E_sign. "<hr/>");
		//$JSONRETURN = "http://apiproxy-uat.ctripqa.com/apiproxy/soa2/19946";
        $JSONRETURN = "https://apiproxy.ctrip.com/apiproxy/soa2/19946";
		$data["TimeStamp"] = $TimeStamp;
		$data["serviceName"] = $serviceName;
		$data["companyId"] = $companyId;
		$data["messageIdentity"] = $sign;
		//$post_data = 'timeStamp='.$TimeStamp.'&serviceName='.$serviceName.'&companyId='.$companyId.'&messageIdentity='.$sign; //array("TimeStamp"=>$TimeStamp,"serviceName"=>$serviceName,"companyId"=>$companyId,"messageIdentity"=>$sign);
		// $url = $JSONRETURN.'/getAllStations/?&timeStamp='.$TimeStamp.'&serviceName='.$serviceName.'&companyId='.$companyId.'&messageIdentity='.$sign;
		//echo ($post_data);

		$post_data = new stdClass();
		$post_data->authentication = new stdClass(); 
		$post_data->authentication->timeStamp = $TimeStamp;
		$post_data->authentication->serviceName = $serviceName;
		$post_data->authentication->companyId = $companyId;
		$post_data->authentication->messageIdentity = $sign;

		$url = $JSONRETURN."/getAllStations";
		 echo json_encode($post_data)."<hr/>"   ;


		$serviceName_Trains = "train.searchTrainsBetweenStations";
		$sign_train = md5($TimeStamp.$serviceName_Trains.$companyId.$key);
		$post_data_train = new stdClass();
		$post_data_train->authentication = new stdClass(); 
		$post_data_train->authentication->timeStamp = $TimeStamp;
		$post_data_train->authentication->serviceName = $serviceName_Trains;
		$post_data_train->authentication->companyId = $companyId;
		$post_data_train->authentication->messageIdentity = $sign_train;
		$post_data_train->searchTrainsParam = new stdClass();
		$post_data_train->searchTrainsParam->departDate = date("Y-m-d",strtotime("3 day")); //"2022-07-22";
		$post_data_train->searchTrainsParam->fromStationName = "北京";
		$post_data_train->searchTrainsParam->toStationName = "深圳";

		echo json_encode($post_data_train);


		//$ResponseJson = GetPost_http($url,json_encode($post_data),'json');
		//echo $ResponseJson;
		//echo json_decode($ResponseJson);

	}

	//查询中转方案(基于携程api)
	public function gettraintrainsfer(){
		$FromCode = $this->input->get_post('FromCode');
		$ToCode = $this->input->get_post('ToCode');
		$DepartDate = $this->input->get_post('DepartDate');

		if(!$FromCode || !$ToCode || !$DepartDate){
			exit('传参错误！');
		}else{
			$TimeStamp = time();
			$Sign = md5($TimeStamp.DATAKEY);
			$this->load->model("ctrip_model");
			$FromName = $this->ctrip_model->ReplaceCodeToName($FromCode);
			$ToName = $this->ctrip_model->ReplaceCodeToName($ToCode);

			$From = $FromName->station_name;
			$To = $ToName->station_name;

			$url = JSONRETURN.'GetTrainTransfer?User='.DATAUSER.'&TimeStamp='.$TimeStamp.'&Sign='.$Sign.'&From='.urlencode($From).'&To='.urlencode($To).'&DepartDate='.$DepartDate;


			$ResponseJson = GetPost_http($url,'','GET');
			$ResponseData = json_decode($ResponseJson);

			$priceAddSeats = ['软卧','硬卧'];
			foreach ($ResponseData->TransferLines as $methodsItems){
				foreach($methodsItems->Trains as $trainsItems){
					foreach ($trainsItems->Seats as $seatsItems){
						if(in_array($seatsItems->SeatName,$priceAddSeats)){
							$seatsItems->Price = ceil($seatsItems->Price * 1.2);
						}
					}
				}
			}
			print_r(json_encode($ResponseData));
		}
	}

	//火车移动端数据接口
	public function get_train(){
		$train_date = $this->input->get('date');
		$fromStation = $this->input->get('from');
		$toStation = $this->input->get('to');
		if(!$train_date || !$fromStation || !$toStation){
			exit('传参错误！');
		}

		//$back_json = $this->NewCtripApi($train_date,$fromStation,$toStation,true);
		$back_json = $this->LanHaiApi($train_date,$fromStation,$toStation,true);
		$train_info = new stdClass();
		$train_info = json_decode($back_json);

		$return_data = new stdClass();
		$return_data->status = 200;
		$return_data->search = new stdClass();
		$return_data->search->FromStationCode = $fromStation;
		$return_data->search->ToStationCode = $toStation;
		$return_data->search->StartDate = $train_date;

		$return_data->CardFee = 3;
		//MainlandChina:大陆内地寄送费用
		//HMT:港澳台寄送费用
		//寄送费按订单收，无论多少张票，没有取票费
		//$return_data->MainlandChina = 6;
		//$return_data->HMT = 15;
		$this->load->library('Currency');
		define("CONST_SITE_CURRENCY","USD");
		$return_data->ExRate = $this->currency->get_site_currencyrate();
		$return_data->result = array();
		$i = 0;

		foreach ($train_info->data->result as $value){
			$return_data->result[$i] = new StdClass();
			$data = explode('|', $value);
			$return_data->result[$i]->TrainNo = $data[2];
			$return_data->result[$i]->TrainCode = $data[3];
			$return_data->result[$i]->TrainType = substr(ucwords($data[3]), 0, 1);
			$return_data->result[$i]->StartStationCode = $data[4];
			$return_data->result[$i]->EndStationCode = $data[5];
			$return_data->result[$i]->DepartStation = '';
			$return_data->result[$i]->DepartStationCode = $data[6];
			$return_data->result[$i]->DepartStationNo = '';
			$return_data->result[$i]->ArriveStation = '';
			$return_data->result[$i]->ArriveStationCode = $data[7];
			$return_data->result[$i]->ArriveStationNo = '';
			$return_data->result[$i]->DepartTime = $data[8];
			$return_data->result[$i]->ArriveTime = $data[9];
			$return_data->result[$i]->RunTime = $data[10];
			$return_data->result[$i]->IsBookable = true;
			//$return_data->result[$i]->SeatType = '123456789OMPA';
			$return_data->result[$i]->cached = 1;
			//$return_data->result[$i]->CheapSeat = new stdClass();
			$price = json_decode($train_info->data->price[$i]);

			$return_data->result[$i]->CheapSeat = null;
			$return_data->result[$i]->SeatList = array();
			//做一个数据存储所有的座位类型的余票
			$seat_yp_map = array(
				'A' => $data[21], //高级软卧
				'A6' => $data[21], //高级软卧
				'H' => $data[22], //其他
				'A4' => $data[23], //软卧
				'A2' => $data[24], //软座
				'P' => $data[25], //特等座
				'WZ' => $data[26],//无座
				'X' => $data[26], //无座
				'A3' => $data[28], //硬卧
				'A1' => $data[29], //硬座
				'O' => $data[30], //二等座
				'A8' => $data[30], //二等座
				'A7' => $data[31], //一等座
				'M' => $data[31], //一等座
				'A9' => $data[32],  //商务座
				'F' => $data[33],  //动卧
				'YDW' => $data[36], //一等卧 // 聚合接口缺少值
				'EDW' => $data[37], //二等卧
			);
			$j=0;
			$soldoutnums = 0;
			foreach($price->data as $key=>$value){
				if($key != 'train_no'){
					if(!is_numeric($key)){
						$return_data->result[$i]->SeatList[$j] = new StdClass();
						$return_data->result[$i]->SeatList[$j]->SeatCode = $key;
						$return_data->result[$i]->SeatList[$j]->SeatName = get_name($key);
						$return_data->result[$i]->SeatList[$j]->SeatNameCn = get_name_cn($key); //中文名称
						$value = str_replace('¥','',$value);

						if(in_array($key, array('A3', 'A4', 'A5', 'A6', '3', '4', '5', '6', 'A', 'F', 'S','YDW','EDW'))){
							if (stripos($return_data->result[$i]->TrainNo, "D") !== false || stripos($return_data->result[$i]->TrainNo, "G") !== false) {
								//$value = $value * 1.2;     //直接读取接口的最高价，无需再自己定义下铺价格。 2022-7-21 zp
								$return_data->result[$i]->SeatList[$j]->ChildDiscut = 1;
							}else{
								//$value = $value * 1.15;    //直接读取接口的最高价，无需再自己定义下铺价格。 2022-7-21 zp
								$return_data->result[$i]->SeatList[$j]->ChildDiscut = 0.75;
							}
						}else{
							$return_data->result[$i]->SeatList[$j]->ChildDiscut = 0.5;
						}
						/*
						if (stripos($value, ".")) {//判断是否是整数,避免类似ecil(9.9*10)=100
							$value = ceil($value);
						}*/
						$return_data->result[$i]->SeatList[$j]->SeatPrice = $this->currency->GetSiteMoney($value);
						$return_data->result[$i]->SeatList[$j]->SeatPriceRMB = $value;
						if($fromStation == 'LSO' || $toStation == 'LSO'){
							$return_data->result[$i]->SeatList[$j]->ServiceCharge = 33;
						}else{
							$return_data->result[$i]->SeatList[$j]->ServiceCharge = 6;
						}
						if($fromStation == 'XJA' && stripos($return_data->result[$i]->TrainNo, "G") && $value > 500){
							$return_data->result[$i]->SeatList[$j]->ServiceCharge = 10;
						}
						$return_data->result[$i]->SeatList[$j]->ServiceChargeRMB = $return_data->result[$i]->SeatList[$j]->ServiceCharge * $return_data->ExRate;

						$return_data->result[$i]->SeatList[$j]->SeatInventory = $seat_yp_map[$key];
						if($return_data->result[$i]->SeatList[$j]->SeatInventory == '有'){
							$return_data->result[$i]->SeatList[$j]->SeatInventory = 99;
						}elseif($return_data->result[$i]->SeatList[$j]->SeatInventory == '无'){
							$return_data->result[$i]->SeatList[$j]->SeatInventory = 0;
							$soldoutnums++;
						}

						$j++;
					}
				}
			}
			if($soldoutnums >= $j){
				 $return_data->result[$i]->soldout = true;
			}else{
				 $return_data->result[$i]->soldout = false;
			}
			$i++;
		}
		print_r(json_encode($return_data));
	}

	#region  旧接口
		//携程数据接口(旧)
		// public function ctripApi($date=null,$from=null,$to=null,$return=false){
		// 	if(!$date || !$from || !$to){
		// 		header("HTTP/1.1 404 Not Found");
		// 		exit('{"httpstatus":404,"data":{"seven":false,"cache":false,"result":[],"map":{}}}');
		// 	}else{
		// 		$isCache = false;
		// 		$iseven = false;
		// 		$cachedata = false;
		// 		$now_time = time();
		// 		$differ = (strtotime($date) - $now_time) / 86400;
		// 		//屏蔽掉超过预售期的搜索，提高速度,2021-7-5 zp修改预售期为15天，不再是30天
		// 		if($differ > 14){
		// 			$date = date('Y-m-d',strtotime('+7day'));
		// 			$iseven = true;
		// 		}

		// 		//转换三字码为中文
		// 		$this->load->model("ctrip_model");
		// 		$FromName = $this->ctrip_model->ReplaceCodeToName($from);
		// 		$ToName = $this->ctrip_model->ReplaceCodeToName($to);


		// 		if(!$FromName || !$ToName){
		// 			header("HTTP/1.1 404 Not Found");
		// 			exit('{"httpstatus":404,"data":{"seven":false,"cache":false,"result":[],"map":{}}}');
		// 		}else{
		// 			$FromName = $FromName->station_name;
		// 			$ToName = $ToName->station_name;
		// 		}

		// 		//生成请求链接
		// 		$TimeStamp = time();
		// 		$Sign = md5($TimeStamp.DATAKEY);

		// 		$url = JSONRETURN.'SearchS2S/?From='.urlencode($FromName).'&To='.urlencode($ToName).'&DepartDate='.$date.'&User='.DATAUSER.'&TimeStamp='.$TimeStamp.'&Sign='.$Sign;

		// 		$api_start_time = microtime(true);

		// 		//获取数据
		// 		$ResponseJson = GetPost_http($url,'','GET');

		// 		$api_end_time = microtime(true);

		// 		$ResponseData = json_decode($ResponseJson);

		// 		$api_responsive_time = $api_end_time - $api_start_time;

		// 		if(empty($ResponseData->Trains)){
		// 			log_message('error','ctrip_trian|status:trains is empty |相应时间:'.$api_responsive_time);
		// 		}else{
		// 			log_message('error','ctrip_trian|status:'.$ResponseData->ResponseStatus->Ack.'|相应时间:'.$api_responsive_time);
		// 		}

		// 		//定义返回的json
		// 		$ReturnData = new stdClass();
		// 		$ReturnData->httpstatus = 200;
		// 		$ReturnData->data = new stdClass();
		// 		$ReturnData->data->seven = $iseven;
		// 		$ReturnData->data->cache = $isCache;
		// 		$ReturnData->data->supplier = 'ctrip';
		// 		$ReturnData->data->result = array();
		// 		$ReturnData->data->map = new stdClass();
		// 		$obj = array();
		// 		$i = 0;
		// 		$PriceStr = '';

		// 		//数据解析
		// 		if(!empty($ResponseData->Trains)){
		// 			foreach ($ResponseData->Trains as $TrainInfo){
		// 				$obj[$TrainInfo->FromTelcode] = $TrainInfo->FromStationName;
		// 				$obj[$TrainInfo->ToTelcode] = $TrainInfo->ToStationName;
		// 				$SeaType = '';
		// 				//余数初始化为空
		// 				$gjrwNum = $rwNum = $rzNum = $tdzNum = $wzNum = $yzNum = $edzNum = $ydzNum = $swzNum = $ywNum = $dwNum = $ydwNum = $edzNum = null;
		// 				foreach($TrainInfo->Seats as $Seats){
		// 					//从香港出发的 D/G 火车加价3%
		// 					if($from == 'XJA'){
		// 						if(stripos($TrainInfo->TrainNo, "G") !== false || stripos($TrainInfo->TrainNo, "D") !== false){
		// 							$Seats->Price = $Seats->Price * 1.03;
		// 						}
		// 					}

		// 					if($Seats->SeatName == '高级软卧上'){
		// 						$gjrwXiaPrice = $Seats->Price * 10;
		// 						$SeaType .= '"6":"'.$gjrwXiaPrice.'","A6":"¥'.$Seats->Price.'",';
		// 						$gjrwNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '软卧上'){
		// 						$rwPrice = $Seats->Price * 10;
		// 						$SeaType .= '"4":"'.$rwPrice.'","A4":"¥'.$Seats->Price.'",';
		// 						$rwNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '一等双软上'){
		// 						$SeaType .= '"YDW":"¥'.$Seats->Price.'",';
		// 						$ydwNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '软座'){
		// 						$rzPrice = $Seats->Price * 10;
		// 						$SeaType .= '"2":"'.$rzPrice.'","A2":"¥'.$Seats->Price.'",';
		// 						$rzNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '特等座'){
		// 						$SeaType .= '"P":"¥'.$Seats->Price.'",';
		// 						$tdzNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '无座'){
		// 						$SeaType .= '"WZ":"¥'.$Seats->Price.'",';
		// 						$wzNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '硬座'){
		// 						$yzPrice = $Seats->Price * 10;
		// 						$SeaType .= '"1":"'.$yzPrice.'","A1":"¥'.$Seats->Price.'",';
		// 						$yzNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '二等座'){
		// 						$SeaType .= '"O":"¥'.$Seats->Price.'",';
		// 						$edzNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '一等座'){
		// 						$SeaType .= '"M":"¥'.$Seats->Price.'",';
		// 						$ydzNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '商务座'){
		// 						$swzPrice = $Seats->Price * 10;
		// 						$SeaType .= '"9":"'.$swzPrice.'","A9":"¥'.$Seats->Price.'",';
		// 						$swzNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '硬卧上'){
		// 						$ywPrice = $Seats->Price * 10;
		// 						$SeaType .= '"3":"'.$ywPrice.'","A3":"¥'.$Seats->Price.'",';
		// 						$ywNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '二等双软上'){
		// 						$SeaType .= '"EDW":"¥'.$Seats->Price.'",';
		// 						$erwNum = $Seats->TicketLeft;
		// 					}

		// 					if($Seats->SeatName == '动卧上'){
		// 						$SeaType .= '"F":"¥'.$Seats->Price.'",';
		// 						$dwNum = $Seats->TicketLeft;
		// 					}

		// 					$PriceStr = $SeaType.'"train_no":'.'"'.$TrainInfo->TrainNo.'"';

		// 				}

		// 				//对返回的数据进行容错处理
		// 				$gjrwNum = isset($gjrwNum) ? ticket_exchange($Seats->Price,$gjrwNum,$iseven) : '';
		// 				$rwNum = isset($rwNum) ? ticket_exchange($Seats->Price,$rwNum,$iseven) : '';
		// 				$rzNum = isset($rzNum) ? ticket_exchange($Seats->Price,$rzNum,$iseven) : '';
		// 				$tdzNum = isset($tdzNum) ? ticket_exchange($Seats->Price,$tdzNum,$iseven) : '';
		// 				$wzNum = isset($wzNum) ? ticket_exchange($Seats->Price,$wzNum,$iseven) : '';
		// 				$ywNum = isset($ywNum) ? ticket_exchange($Seats->Price,$ywNum,$iseven) : '';
		// 				$yzNum = isset($yzNum) ? ticket_exchange($Seats->Price,$yzNum,$iseven) : '';
		// 				$edzNum = isset($edzNum) ? ticket_exchange($Seats->Price,$edzNum,$iseven) : '';
		// 				$ydzNum = isset($ydzNum) ? ticket_exchange($Seats->Price,$ydzNum,$iseven) : '';
		// 				$swzNum = isset($swzNum) ? ticket_exchange($Seats->Price,$swzNum,$iseven) : '';
		// 				$dwNum = isset($dwNum) ? ticket_exchange($Seats->Price,$dwNum,$iseven) : '';
		// 				$ydwNum = isset($ydwNum) ? ticket_exchange($Seats->Price,$ydwNum,$iseven) : '';
		// 				$erwNum = isset($erwNum) ? ticket_exchange($Seats->Price,$erwNum,$iseven) : '';

		// 				$runMin = $TrainInfo->DurationMinutes % 60;
		// 				$runHour = ($TrainInfo->DurationMinutes - $runMin) / 60;

		// 				$ReturnData->data->result[$i] = '|预定|'.$TrainInfo->Train12306No.'|'.$TrainInfo->TrainNo.'|'.$TrainInfo->FromStationName.'|'.$TrainInfo->ToStationName.'|'.$TrainInfo->FromTelcode.'|'.$TrainInfo->ToTelcode.'|'.$TrainInfo->StartTime.'|'.$TrainInfo->ArriveTime.'|'.$runHour.':'.$runMin.'|'.$TrainInfo->CanWebBuy.'||'.date('Ymd',strtotime($date)).'||||||||'.$gjrwNum.'||'.$rwNum.'|'.$rzNum.'|'.$tdzNum.'|'.$wzNum.'||'.$ywNum.'|'.$yzNum.'|'.$edzNum.'|'.$ydzNum.'|'.$swzNum.'|'.$dwNum.'|||'.$ydwNum.'|'.$erwNum;

		// 				$data = '{"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":{'.$PriceStr.'},"messages":[],"validateMessages":{}}';
		// 				$ReturnData->data->price[$i] = $data;
		// 				$i++;
		// 			}
		// 		}
		// 		//print_r($ResponseData);die();
		// 		$ReturnData->data->map = (object)$obj;
		// 		if($return){
		// 			return json_encode($ReturnData);
		// 		}else{
		// 			echo json_encode($ReturnData);
		// 		}
		// 	}
		// }



		// //获取所有站点信息(基于携程api)旧接口
		// public function getallstation(){
		// 	//生成请求参数
		// 	$TimeStamp = time();
		// 	$Sign = md5($TimeStamp.DATAKEY);

		// 	$url = JSONRETURN.'/GetAllStations/?&User='.DATAUSER.'&TimeStamp='.$TimeStamp.'&Sign='.$Sign;

		// 	$ResponseJson = GetPost_http($url,'','GET');

		// 	$ResponseData = json_decode($ResponseJson);

		// 	$data = array();
		// 	foreach ($ResponseData->Stations as $items){
		// 		$data['StationName'] = isset($items->StationName) ? checkNull($items->StationName) : '';
		// 		$data['PinYin'] = isset($items->PinYin) ? checkNull($items->PinYin) : '';
		// 		$data['Telecode'] = isset($items->Telecode) ? checkNull($items->Telecode) : '';
		// 		$data['Address'] = isset($items->Address) ? checkNull($items->Address) : '';
		// 		$data['Geography'] = isset($items->Geography) ? checkNull($items->Geography) : '';
		// 		$this->load->model("ctrip_model");
		// 		$this->ctrip_model->AddOrUpdate($data);
		// 	}
		// }
	
	#endregion

}
