<?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();
	}

	//获取价格信息
	function get_priceinfo($train_no){
        $sql = "SELECT seatPriceInfo2 FROM trainPrice WHERE train_no = '{$train_no}'";
        $query = $this->HT->query($sql);
        return $query->row();
    } 
}
