You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/third_party/train/controllers/search.php

687 lines
23 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class search 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->start_time = microtime(true);
$this->load->model("BIZ_train_model");//加载模型
$this->load->model("BIZ_intel_train_model");//国际火车模型
}
//查询聚合余票接口,对返回的数据进行处理
public function index($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);
//调用数据处理模块
$returnJson = $this->dataOperate($search_return,$fromStation,$toStation);
//调用拼接处理模块
$trainjson = $this->createTrainJson($returnJson);
$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|缓存:'.$this->cache.'|+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 = 'no';
//屏蔽掉超过预售期的搜索,提高速度
if($this->differ_time > 29){
$train_date = date('Y-m-d',strtotime('+7day'));
$this->seveth = 'yes';
}
$this->url = JUHE_TRAIN_CX_API.'?key='.JUHE_TRAIN_API_KEY.'&train_date='.$train_date.'&from_station='.$fromStation.'&to_station='.$toStation;
$train_info = $this->get_http($this->url);
return $train_info;
}
//缓存处理模块(包含缓存处理)
function dataOperate($search_return,$fromStation,$toStation){
//首先判断聚合返回的数据是否为空
if(empty($search_return)){
$this->reason = '聚合接口没有响应';
$this->cache = 'yes';
}else{
$this->reason = json_decode($search_return)->reason;
$this->cache = 'no';
}
if(!empty(json_decode($search_return)->result)){
//$this->BIZ_train_model->addOrUpdate($fromStation,$toStation,$search_return);
$operate_data = $search_return;
}else{
$operate_data = null;
//添加缓存调用时间判断
/*if($this->differ_time <= 5){
$cache_train_info = NULL;
}else{
$cache_train_info = $this->BIZ_train_model->get_train_cache($fromStation,$toStation);
}
//判断缓存是否为空
if(empty($cache_train_info)){
log_message('error','TRAIN 查询为空的链接:'.$this->url.'|出错的原因:'.$this->reason.'|缓存为空');
$operate_data = NULL;
}else{
$cache_time = $cache_train_info->tpc_datetime;
$now_time = time();
$cache_differ_time = ($now_time - strtotime($cache_time)) / 86400;
if($cache_differ_time >= 3){
$this->BIZ_train_model->delete_traincache($fromStation,$toStation);
log_message('error','TRAIN 查询为空的链接:'.$this->url.'|出错的原因:'.$this->reason.'|缓存超时,已经删除');
$operate_data = NULL;
}else{
$this->cache = 'yes';
$operate_data = $cache_train_info->tpc_content;
log_message('error','TRAIN 查询为空的链接:'.$this->url.'|出错的原因:'.$this->reason.'|调用缓存号:'.$cache_train_info->tpc_sn.'|缓存生成的时间:'.$cache_train_info->tpc_datetime);
}
}*/
}
return $operate_data;
}
//字符串拼接模块
function createTrainJson($returnjson){
$return_data = new stdClass();
$return_data->data = new stdClass();
$return_data->httpstatus = 200;
$return_data->reason = $this->reason;
$return_data->cache = $this->cache;
$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.'||||||||'.$this->ticket_exchange($value->gjrw_price,$value->gjrw_num).'|'.$this->ticket_exchange($value->qtxb_price,$value->qtxb_num).'|'.$this->ticket_exchange($value->rw_price,$value->rw_num).'|'.$this->ticket_exchange($value->rz_price,$value->rz_num).'|'.$this->ticket_exchange($value->tdz_price,$value->tdz_num).'|'.$this->ticket_exchange($value->wz_price,$value->wz_num).'||'.$this->ticket_exchange($value->yw_price,$value->yw_num).'|'.$this->ticket_exchange($value->yz_price,$value->yz_num).'|'.$this->ticket_exchange($value->edz_price,$value->edz_num).'|'.$this->ticket_exchange($value->ydz_price,$value->ydz_num).'|'.$this->ticket_exchange($value->swz_price,$value->swz_num).'|'.$this->ticket_exchange($value->dw_price,$value->dw_num).'||';
$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 search_international_train(){
$trainFrom = $this->input->get_post('trainFrom');
$trainTo = $this->input->get_post('trainTo');
$trainDate = $this->input->get_post('trainDate');
if(empty($trainFrom) || empty($trainTo)){
header("HTTP/1.1 404 Not Found");
exit('{"status":"201","reason":"车站查询失败","data":""}');
}
//根据传过来的城市名称获取城市代码
$trainFromCode = $this->BIZ_intel_train_model->get_code_by_name($trainFrom);
$trainToCode = $this->BIZ_intel_train_model->get_code_by_name($trainTo);
if(empty($trainFromCode) || empty($trainToCode)){
header("HTTP/1.1 404 Not Found");
exit('{"status":"202","reason":"站点名转换失败","data":""}');
}
//获取车次信息
$obj = $this->BIZ_intel_train_model->get_train_info($trainFromCode->s_code,$trainToCode->s_code);
//如果结果为空则直接返回为空的结果
if(empty($obj)){
header("HTTP/1.1 404 Not Found");
exit('{"status":"203","reason":"无车次信息!","data":""}');
}
//处理返回的字符串
$return_json = new StdClass();
$return_json->status = '200';
$return_json->reason = '成功的返回';
$return_json->rate = $this->GetCurrencyRate();
$return_json->mainland_deliver = 9;
$return_json->others_deliver = 12;
$return_json->data = array();
$return_json->endata = array();
//如果有结果则进行字符串处理
foreach($obj as $key=>$item){
//发车规则处理
$item->trainrule = array();
$item->trainrule['include'] = array();
$item->trainrule['exclusive'] = array();
foreach(json_decode($item->DateRule)->DateRule as $key_rule=>$rules){
if(!empty($rules)){
if($key_rule == 'include'){
$item->trainrule['include'] = $rules;
}else if($key_rule == 'exclusive'){
$item->trainrule['exclusive'] = $rules;
}
}
}
//价格处理
$item->adult_seat = array();
$item->child_seat = array();
$price_arr = json_decode($item->seatPriceInfo2);
$audlt = $price_arr->price;
$child = $price_arr->cprice;
if(!empty($audlt)){
$i=0;
foreach($audlt as $key_adult=>$value){
$seat_name = $this->BIZ_intel_train_model->get_seatname($key_adult)->Seat_EName1;
$item->adult_seat[$i]['name'] = $seat_name;
$item->adult_seat[$i]['code'] = $key_adult;
$item->adult_seat[$i]['price'] = $value;
$item->adult_seat[$i]['pic'] = $this->BIZ_intel_train_model->get_seatname($key_adult)->Seat_Pic;
$i++;
}
}
if(!empty($child)){
$j=0;
foreach($child as $key_child=>$value){
$seat_name = $this->BIZ_intel_train_model->get_seatname($key_child)->Seat_EName1;
$item->child_seat[$j]['name'] = $seat_name;
$item->child_seat[$j]['code'] = $key_child;
$item->child_seat[$j]['price'] = $value;
$j++;
}
}
//最后进行当前日期判断,对数据进行分组
$flag = $this->checkDateRule($trainDate,$item->DateRule);
if(!$flag){
//将不符合发车日期的列车放入到另外一个数组中存储起来
array_push($return_json->endata,$obj[$key]);
}else{
array_push($return_json->data,$obj[$key]);
}
}
print_r(json_encode($return_json));
}
//获取某一条列车的信息
public function search_one_train($train_no){
$obj = $this->BIZ_intel_train_model->get_one_train($train_no);
$seat_code = json_decode($obj->seatPriceInfo2)->price;
$obj->seat_info = new stdClass();
foreach ($seat_code as $key=>$code){
$seat_info = $this->BIZ_intel_train_model->get_seat_info($key);
$obj->seat_info->$key = $seat_info;
}
$obj->arriveTime = $obj->localarriveTime;
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_all_inteltrain(){
$obj = $this->BIZ_intel_train_model->get_allinteltrain();
print_r(json_encode($obj));
}
//获取国际火车出发规则
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));
}
//获取价格(废弃)
/*
fromStationCode出发站三字码
toStationCode:终点站三字码
trainCode:车次号
*/
public function get_price($fromStationCode=null,$toStationCode=null,$trainCode=null){
if(!$fromStationCode || !$toStationCode || !$trainCode){
exit('传参错误!');
}else{
$return_data = $this->BIZ_train_model->get_price($fromStationCode,$toStationCode,$trainCode);
if(!empty($return_data)){
print_r(json_decode($return_data->TPL_Price));
}else{
print_r('没有数据返回');
}
}
}
//余票转换
function ticket_exchange($price,$num){
//echo $this->can_buy_now;
if($this->cache == 'yes' || $this->seveth == 'yes'){
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 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;
}
//发送请求
function get_http($url, $data = '', $method = 'GET') {
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
//curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
if ($method == 'POST' && !empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
}
curl_setopt($curl, CURLOPT_TIMEOUT, 45); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
$errno = curl_errno($curl);
if ($errno !== 0) {
return false;
$error_message = $errno . ' ' . curl_error($curl); //记录错误日志
log_message('error', "third_party/train_app/index/get_http curl {$error_message}");
}
curl_close($curl); //关闭CURL会话
return $tmpInfo; //返回数据
}
function GetCurrencyRate() {
$this->HT = $this->load->database('HT', TRUE);
$sql = "SELECT CRI_BuyIn / 100.00 AS tmpExRate, \n"
. " CRI_Code \n"
. "FROM tourmanager.dbo.CurrencyRateInfo \n"
. "WHERE CRI_Code IN ('USD', 'EUR', 'RUB') \n"
. " AND GETDATE() BETWEEN CRI_Start AND CRI_Stop";
$query = $this->HT->query($sql);
foreach ($query->result() as $item) {
switch ($item->CRI_Code) {
case 'USD':
$this->USD_Rate = $item->tmpExRate;
break;
case 'EUR':
$this->EUR_Rate = $item->tmpExRate;
break;
case 'RUB':
$this->RUB_Rate = $item->tmpExRate;
break;
}
}
// echo $this->USD_Rate . '<br/>';
// echo $this->EUR_Rate . '<br/>';
// echo $this->RUB_Rate . '<br/>';
return $this->USD_Rate;
}
}
?>