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