整理火车搜索接口到一个目录下

mobile-first
cyc 6 years ago
parent 16bf3af463
commit 6f515d6f8a

@ -367,7 +367,6 @@ class search extends CI_Controller{
$rule_arr = $this->BIZ_intel_train_model->get_train_rules($item->station_id);
if(!empty($rule_arr)){
$return_json['TrainList'][$i]['FromStation'] = $item->s_ename;
$return_json['TrainList'][$i]['FromCountry'] = $item->s_country;
$return_json['TrainList'][$i]['ToStations'] = array();

@ -8,9 +8,17 @@
define("TUNIU_URL","https://open.tuniu.cn");
define("TUNIU_KEY","AILvoDj8El7KCSMe25");
//携程订单接口
define("ORDERUSER","guilintravel");
define("ORDERKEY","07f811fe29f04008a8fcc86e81c012b9");
//携程数据接口
define("DATAUSER","guilintravel");
define("DATAKEY","0dbf1652763c2289533e27437a971854");
//携程接口地址
define("JSONRETURN","http://m.ctrip.com/restapi/soa2/12976/json/");
//聚合火车订票API key
define("JUHE_TRAIN_API_KEY","79f03107b921ef31310bd40a1415c1cb");

@ -0,0 +1,478 @@
<?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 juheApi($train_date=null,$fromStation=null,$toStation=null){
//定义一些变量
$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);
//调用查询模块
$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);
print_r($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;
//屏蔽掉超过预售期的搜索,提高速度
if($this->differ_time > 29){
$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->httpstatus = 200;
$return_data->seveth = $this->seveth;
$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);
}
public function ctripApi($date=null,$from=null,$to=null){
//接收参数
if(!$date && !$from && !$to){
$date = $this->input->get_post('date');
$from = $this->input->get_post('from');
$to = $this->input->get_post('to');
}
$TrainNo = $this->input->get_post('TrainNo');
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;
if($differ > 29){
$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->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;
print_r(json_encode($ReturnData));
}
}
//查询经停站(基于携程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));
}
}
//获取所有站点信息(基于携程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);
}
}
//查询中转方案(基于携程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));
}
}
}

@ -0,0 +1,266 @@
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class intelTrainSearch 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 get_all_inteltrain(){
$obj = $this->BIZ_intel_train_model->get_allinteltrain();
print_r(json_encode($obj));
}
//查询某一趟列车的途径站
public function search_train_station($train_no){
if(!$train_no){
header("HTTP/1.1 404 Not Found");
exit('{"status":"404","reason":"列车编号为空"}');
}
$arr = $this->BIZ_intel_train_model->get_train_station($train_no);
if(!empty($arr)){
echo json_encode($arr);
}else{
header("HTTP/1.1 404 Not Found");
exit('{"status":"404","reason":"途径站为空"}');
}
}
//获取国际火车出发规则
public function get_station_rules(){
//获取所有站点的列表
$return_json = array();
$return_json['TrainList'] = array();
$all_stations = $this->BIZ_intel_train_model->get_allstations();
$i = 0;
foreach($all_stations as $item){
$rule_arr = $this->BIZ_intel_train_model->get_train_rules($item->station_id);
if(!empty($rule_arr)){
$return_json['TrainList'][$i]['FromStation'] = $item->s_ename;
$return_json['TrainList'][$i]['FromCountry'] = $item->s_country;
$return_json['TrainList'][$i]['ToStations'] = array();
$j = 0;
foreach($rule_arr as $rule_info){
$return_json['TrainList'][$i]['ToStations'][$j]['Message'] = $rule_info->tsi_Message;
$return_json['TrainList'][$i]['ToStations'][$j]['Station'] = $rule_info->S_ename;
$return_json['TrainList'][$i]['ToStations'][$j]['StationCountry'] = $rule_info->s_country;
$j++;
}
$i++;
}
}
//print_r($return_json);
header('Content-type: application/json');
print_r(json_encode($return_json));
}
//获取国际火车出发规则
public function ch_station_rules(){
//获取所有站点的列表
$return_json = array();
$return_json['TrainList'] = array();
$all_stations = $this->BIZ_intel_train_model->ch_allstations();
$i = 0;
//print_r($all_stations);
$data = array();
foreach($all_stations as $item){
$todata = new stdClass();
if($item->fromStationName == 'Beijing West'){
$item->fromStationName = 'Beijing';
}
if(isset($data[$item->fromStationName])){
$todata->Message = $item->tsi_Message;
$todata->Station = $item->toStationName;
$todata->StationCountry = $item->toStationCountry;
array_push($data[$item->fromStationName]->ToStations,$todata);
}else{
$data[$item->fromStationName] = new stdClass();
$data[$item->fromStationName]->FromCountry = $item->fromStationCountry;
$data[$item->fromStationName]->FromStation = $item->fromStationName;
$data[$item->fromStationName]->ToStations = array();
$todata->Message = $item->tsi_Message;
$todata->Station = $item->toStationName;
$todata->StationCountry = $item->toStationCountry;
array_push($data[$item->fromStationName]->ToStations,$todata);
}
}
foreach ($data as $rules){
array_push($return_json['TrainList'],$rules);
}
header('Content-type: application/json');
print_r(json_encode($return_json));
}
function checkDateRule($strDate, $jsonDate){
if (!strtotime($strDate)) {
return false;
}
$strDate_t = strtotime($strDate);
$strDate = date("Y-m-d", $strDate_t);
$dYear = date("Y", $strDate_t);
$dMonth = date("m", $strDate_t);
$dDay = date("d", $strDate_t);
$dWeek = date("w", $strDate_t); //星期的第几天数组0-6
//单双日
if ($dDay % 2 == 0) {
$dSingle = 2;
} else {
$dSingle = 1;
}
$jObject = json_decode($jsonDate);
$result = false;
//先判断排除规则有一条符合就返回false
if (isset($jObject->DateRule->exclusive) ) {
$jObject_exclusive = $jObject->DateRule->exclusive;
foreach ($jObject_exclusive as $key => $val) {
if ($val->dateType=="EveryDay" ) { //每天,这个基本不会出现,设置每天排除和关闭没区别
return false;
} elseif ($val->dateType=="WeekDay" ) { //按星期
//先判断是否在日期范围内,再判断星期是否对应
$bw_CheckRange = false; //是否需要判断星期几
if ($val->dateRange == "*") { //每天
$bw_CheckRange = true;
} else {
$dateRange = explode("|", $val->dateRange); //日期范围
if (count($dateRange) == 2) {
if ($strDate_t >= strtotime($dateRange[0]) and $strDate_t <= strtotime($dateRange[1])) {
$bw_CheckRange = true; //在范围内
}
}
}
if ($bw_CheckRange) {
if ($dWeek==0) {
$dWeek=7; //星期0为星期天我们保存的星期天是7
}
$weekArr = explode(",", $val->dateData); //星期数据1,2,3,4,5,6,7这种
foreach ($weekArr as $week) {
if ($week == $dWeek) {
return false;
}
}
}
} elseif ($val->dateType == "SelectDay") { //固定日期
if ($val->dateRange=="*") {
$SelectDayArr = explode(",", $val->dateData);
foreach ($SelectDayArr as $sDay) {
if (strtotime($sDay) == $strDate_t) {
return false;
}
}
}
} elseif ($val->dateType == "RangeDay") { //连续日期
if ($val->dateRange == "*") {
$RangeDateArr = explode("|", $val->dateData);
if (count($RangeDateArr)==2) {
if ($strDate_t >= strtotime($RangeDateArr[0]) and $strDate_t <= strtotime($RangeDateArr[1])) {
return false ; //在范围内
}
}
}
}
}
}
//再判断包含规则有一条符合就为true
if (isset($jObject->DateRule->include)) {
$jObject_include = $jObject->DateRule->include;
foreach ($jObject_include as $key => $val) {
if ($val->dateType == "EveryDay") { //每天
return true;
} elseif ($val->dateType == "WeekDay") { //按星期
//先判断是否在日期范围内,再判断星期是否对应
$bw_CheckRange = false; //是否需要判断星期几
if ($val->dateRange == "*") { //每天
$bw_CheckRange = true;
} else {
$dateRange = explode("|", $val->dateRange); //日期范围
if (count($dateRange) == 2) {
if ($strDate_t >= strtotime($dateRange[0]) and $strDate_t <= strtotime($dateRange[1])) {
$bw_CheckRange = true; //在范围内
}
}
}
if ($bw_CheckRange) {
if ($dWeek == 0) {
$dWeek = 7; //星期0为星期天我们保存的星期天是7
}
$weekArr = explode(",", $val->dateData); //星期数据1,2,3,4,5,6,7这种
foreach ($weekArr as $week) {
if ($week == $dWeek) {
return true;
}
}
}
} elseif ($val->dateType=="MonthDay") { //每月单双
if ($val->dateRange=="*") {
$MonthDayArr = explode(",", rtrim($val->dateData, ',')); //月单双数据m3:2,m4:1
foreach ($MonthDayArr as $MonthKey) {
$MonthArr = explode(":", $MonthKey);
$Month = str_pad(str_replace("m", "", $MonthArr[0]), 2, "0", STR_PAD_LEFT); //存储的月份补0好比较
if ($dMonth == $Month) { //月份相同比较
if ($dSingle == $MonthArr[1]) {
return true;
}
}
}
}
} elseif ($val->dateType == "SelectDay") { //固定日期
if ($val->dateRange=="*") {
$SelectDayArr = explode(",", $val->dateData);
foreach ($SelectDayArr as $sDay) {
if (strtotime($sDay) == $strDate_t) {
return true;
}
}
}
} elseif ($val->dateType == "RangeDay") { //连续日期
if ($val->dateRange == "*") {
$RangeDateArr = explode("|", $val->dateData);
if (count($RangeDateArr)==2) {
if ($strDate_t >= strtotime($RangeDateArr[0]) and $strDate_t <= strtotime($RangeDateArr[1])) {
return true ; //在范围内
}
}
}
}
}
}
return $result;
}
}

@ -79,4 +79,191 @@ function get_microtime (){
return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}
//余票转换
function ticket_exchange($price,$num,$iseven){
if($iseven){
if(isset($price) && is_numeric($num)){
if($price > 0){
return '有';
}else{
return '';
}
}
return '';
}else{
if(is_numeric($num)){
if($num == 0){
return '无';
}elseif($num >= 99){
return '有';
}else{
return $num;
}
}else{
if($num == '--'){
return '';
}
}
}
}
function checkNull($info){
if(empty($info)){
return '';
}else{
return $info;
}
}
function checkDateRule($strDate, $jsonDate){
if (!strtotime($strDate)) {
return false;
}
$strDate_t = strtotime($strDate);
$strDate = date("Y-m-d", $strDate_t);
$dYear = date("Y", $strDate_t);
$dMonth = date("m", $strDate_t);
$dDay = date("d", $strDate_t);
$dWeek = date("w", $strDate_t); //星期的第几天数组0-6
//单双日
if ($dDay % 2 == 0) {
$dSingle = 2;
} else {
$dSingle = 1;
}
$jObject = json_decode($jsonDate);
$result = false;
//先判断排除规则有一条符合就返回false
if (isset($jObject->DateRule->exclusive) ) {
$jObject_exclusive = $jObject->DateRule->exclusive;
foreach ($jObject_exclusive as $key => $val) {
if ($val->dateType=="EveryDay" ) { //每天,这个基本不会出现,设置每天排除和关闭没区别
return false;
} elseif ($val->dateType=="WeekDay" ) { //按星期
//先判断是否在日期范围内,再判断星期是否对应
$bw_CheckRange = false; //是否需要判断星期几
if ($val->dateRange == "*") { //每天
$bw_CheckRange = true;
} else {
$dateRange = explode("|", $val->dateRange); //日期范围
if (count($dateRange) == 2) {
if ($strDate_t >= strtotime($dateRange[0]) and $strDate_t <= strtotime($dateRange[1])) {
$bw_CheckRange = true; //在范围内
}
}
}
if ($bw_CheckRange) {
if ($dWeek==0) {
$dWeek=7; //星期0为星期天我们保存的星期天是7
}
$weekArr = explode(",", $val->dateData); //星期数据1,2,3,4,5,6,7这种
foreach ($weekArr as $week) {
if ($week == $dWeek) {
return false;
}
}
}
} elseif ($val->dateType == "SelectDay") { //固定日期
if ($val->dateRange=="*") {
$SelectDayArr = explode(",", $val->dateData);
foreach ($SelectDayArr as $sDay) {
if (strtotime($sDay) == $strDate_t) {
return false;
}
}
}
} elseif ($val->dateType == "RangeDay") { //连续日期
if ($val->dateRange == "*") {
$RangeDateArr = explode("|", $val->dateData);
if (count($RangeDateArr)==2) {
if ($strDate_t >= strtotime($RangeDateArr[0]) and $strDate_t <= strtotime($RangeDateArr[1])) {
return false ; //在范围内
}
}
}
}
}
}
//再判断包含规则有一条符合就为true
if (isset($jObject->DateRule->include)) {
$jObject_include = $jObject->DateRule->include;
foreach ($jObject_include as $key => $val) {
if ($val->dateType == "EveryDay") { //每天
return true;
} elseif ($val->dateType == "WeekDay") { //按星期
//先判断是否在日期范围内,再判断星期是否对应
$bw_CheckRange = false; //是否需要判断星期几
if ($val->dateRange == "*") { //每天
$bw_CheckRange = true;
} else {
$dateRange = explode("|", $val->dateRange); //日期范围
if (count($dateRange) == 2) {
if ($strDate_t >= strtotime($dateRange[0]) and $strDate_t <= strtotime($dateRange[1])) {
$bw_CheckRange = true; //在范围内
}
}
}
if ($bw_CheckRange) {
if ($dWeek == 0) {
$dWeek = 7; //星期0为星期天我们保存的星期天是7
}
$weekArr = explode(",", $val->dateData); //星期数据1,2,3,4,5,6,7这种
foreach ($weekArr as $week) {
if ($week == $dWeek) {
return true;
}
}
}
} elseif ($val->dateType=="MonthDay") { //每月单双
if ($val->dateRange=="*") {
$MonthDayArr = explode(",", rtrim($val->dateData, ',')); //月单双数据m3:2,m4:1
foreach ($MonthDayArr as $MonthKey) {
$MonthArr = explode(":", $MonthKey);
$Month = str_pad(str_replace("m", "", $MonthArr[0]), 2, "0", STR_PAD_LEFT); //存储的月份补0好比较
if ($dMonth == $Month) { //月份相同比较
if ($dSingle == $MonthArr[1]) {
return true;
}
}
}
}
} elseif ($val->dateType == "SelectDay") { //固定日期
if ($val->dateRange=="*") {
$SelectDayArr = explode(",", $val->dateData);
foreach ($SelectDayArr as $sDay) {
if (strtotime($sDay) == $strDate_t) {
return true;
}
}
}
} elseif ($val->dateType == "RangeDay") { //连续日期
if ($val->dateRange == "*") {
$RangeDateArr = explode("|", $val->dateData);
if (count($RangeDateArr)==2) {
if ($strDate_t >= strtotime($RangeDateArr[0]) and $strDate_t <= strtotime($RangeDateArr[1])) {
return true ; //在范围内
}
}
}
}
}
}
return $result;
}
?>

@ -0,0 +1,196 @@
<?php
class BIZ_intel_train_model extends CI_Model {
function __construct() {
parent::__construct();
$this->HT = $this->load->database('HT', TRUE);
}
//查询火车列表信息
public function get_train_info($trainFromCode,$trainToCode){
if($trainFromCode == 'CHBJ'){
$add = "OR tl.trainFromCode = 'CHPW'";
}else{
$add = "";
}
$sql = "
SELECT
tl.trainNo,
tl.trainFrom,
tl.trainFromCode,
(select s_country from TrainStation_intel where s_code = tl.trainFromCode) as from_country,
tl.trainTo,
tl.trainToCode,
(select s_country from TrainStation_intel where s_code = tl.trainToCode) as to_country,
tl.trainType,
tl.train_no,
tl.trainUse,
tl.RunTime,
tl.departTime,
(select top 1 trainArrive from trainlistDetail where train_no = tl.train_no order by trainOrder desc) as arriveTime,
tl.UseDay,
tl.DateRule,
tl.TrainMessage,
tp.seatPriceInfo2
FROM
trainlist tl
left join
trainPrice tp
on
tl.train_no = tp.train_no
WHERE
tl.isGlobal = 1
AND
(tl.trainFromCode = '{$trainFromCode}' {$add})
AND
tl.trainToCode = '{$trainToCode}'
AND
tl.DateRule != ''
AND
tp.seatPriceInfo2 IS NOT NULL
order by departTime asc
";
$query = $this->HT->query($sql);
return $query->result();
}
public function get_code_by_name($name){
$sql = "select s_code from TrainStation_intel where s_ename = '{$name}'";
$query = $this->HT->query($sql);
return $query->row();
}
public function get_seatname($seat_code){
$sql = "select Seat_EName1,Seat_Pic from TrainSeat_Intel where Seat_code = '{$seat_code}'";
$query = $this->HT->query($sql);
return $query->row();
}
//获取一列指定火车数据
public function get_one_train($train_no){
$sql = "
SELECT
*,
(select s_country from TrainStation_intel where trainFromCode = s_code ) as From_country,
(select s_country from TrainStation_intel where trainToCode = s_code ) as To_country,
(select top 1 trainArrive from trainlistDetail where train_no = tl.train_no order by trainOrder desc) as localarriveTime
FROM
trainlist tl
LEFT JOIN
trainPrice tp
ON
tl.train_no = tp.train_no
WHERE
tl.train_no = '{$train_no}'
AND
isGlobal = 1
";
$query = $this->HT->query($sql);
return $query->row();
}
public function get_allinteltrain(){
$sql = "
SELECT
*
FROM
TrainStation_intel
LEFT JOIN
trainlist
ON
s_code = trainFromCode
WHERE
trainFrom != 'NULL'
";
$query = $this->HT->query($sql);
return $query->result();
}
public function get_seat_info($seat_code){
$sql = "SELECT Seat_CName,Seat_EName1 FROM TrainSeat_Intel WHERE Seat_Code = '{$seat_code}'";
$query = $this->HT->query($sql);
return $query->row();
}
public function get_train_station($train_no){
$sql = "SELECT * FROM trainlistDetail WHERE train_no = '{$train_no}' order by trainOrder asc";
$query = $this->HT->query($sql);
return $query->result();
}
public function get_allstations(){
$sql = "SELECT * FROM TrainStation_intel where station_id != 1";
$query = $this->HT->query($sql);
return $query->result();
}
public function ch_allstations(){
$sql = "select
tsi1.s_ename as fromStationName,
tsi1.s_country as fromStationCountry,
tsi2.s_ename as toStationName,
tsi2.s_country as toStationCountry,
tsi_Message
from TrainSearch_intel
left join
TrainStation_intel tsi1
ON
tsi1.station_id = tsi_FromStation
left join
TrainStation_intel tsi2
ON
tsi2.station_id = tsi_ToStation
where tsi1.s_country = 'china' or tsi2.s_country = 'china'
";
$query = $this->HT->query($sql);
return $query->result();
}
public function get_train_rules($station_id){
if($station_id == 11){
$station = "or tsi_FromStation = 10";
}elseif($station_id == 2){
$station = "or tsi_FromStation = 9";
}else{
$station = "";
}
$sql = "SELECT
S_ename,s_country,tsi_Message
FROM
TrainSearch_intel
LEFT JOIN
TrainStation_intel
ON
tsi_ToStation = station_id
where
tsi_FromStation = '{$station_id}'";
$sql .= $station;
$query = $this->HT->query($sql);
return $query->result();
}
public function ch_train_rules($station_id){
if($station_id == 11){
$station = "or tsi_FromStation = 10";
}elseif($station_id == 2){
$station = "or tsi_FromStation = 9";
}else{
$station = "";
}
$sql = "SELECT
S_ename,s_country,tsi_Message
FROM
TrainSearch_intel
LEFT JOIN
TrainStation_intel
ON
tsi_ToStation = station_id
where
(tsi_FromStation = ?)
";
$sql .= $station;
$query = $this->HT->query($sql,array($station_id));
return $query->result();
}
}

@ -0,0 +1,68 @@
<?php
class ctrip_model extends CI_Model {
function __construct() {
parent::__construct();
$this->INFO = $this->load->database('INFO', TRUE);
}
//新增或更新缓存
public function AddOrUpdate($data){
$sql = "IF NOT EXISTS(
SELECT 1
FROM train_stations
WHERE
station_telecode = '{$data['Telecode']}'
) BEGIN
INSERT INTO train_stations
(
station_name,
station_telecode,
station_geography,
station_pinyin,
station_address
)
VALUES
(
'{$data['StationName']}','{$data['Telecode']}','{$data['Geography']}','{$data['PinYin']}','{$data['Address']}'
)
END
ELSE
BEGIN
UPDATE train_stations
SET station_name = '{$data['StationName']}',
station_geography = '{$data['Geography']}',
station_pinyin = '{$data['PinYin']}',
station_address = '{$data['Address']}'
WHERE
station_telecode = '{$data['Telecode']}'
END
";
$query = $this->INFO->query($sql);
return $query;
}
//获取缓存的火车信息
//如果读取到缓存是7天以前的数据就不返回任何数据并且将其删除。
public function get_train_cache($tpc_from_station,$tpc_to_station){
$sql = "SELECT
*
FROM
TrainPriceCache
WHERE
tpc_from_station = '$tpc_from_station'
AND
tpc_to_station = '$tpc_to_station'";
$query = $this->INFO->query($sql);
return $query->row();
}
//根据三字码获取车站中文名
public function ReplaceCodeToName($code){
$sql = "SELECT station_name from train_stations where station_telecode = '{$code}'";
$query = $this->INFO->query($sql);
return $query->row();
}
}
?>
Loading…
Cancel
Save