<?php

class BIZ_train_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
        $this->INFO = $this->load->database('INFO', TRUE);
    }
	
	//测试
	function test_myslef(){
		$sql = "UPDATE BIZ_JuheOrderList SET JOL_SendMail = 1";
		$query = $this->HT->query($sql);
		//eturn $query->result();
	}
	
	//自动获取符合自动出票要求的订单的coli_sn
	function auto_check_ticket(){
		$sql = "SELECT distinct COLD_SN ,coli_id,COLD_SPFS,COLI_State
        FROM   BIZ_ConfirmLineInfo bcli
        inner join BIZ_ConfirmLineDetail bcld on COLD_COLI_SN=COLI_SN
             LEFT JOIN BIZ_GroupAccountInfo bgai
              ON  bcli.COLI_SN = bgai.GAI_COLI_SN
        WHERE  bcli.COLI_ServiceType = '2'
             AND bcli.COLI_State in ('11','13','8','63')
             AND bcli.COLI_WebCode in ('cht', 'JP', 'train_it', 'VC', 'train_ru','GM-Train','SHT','CT')
             AND (bcli.COLI_Price - bgai.GAI_SQJE) <= 20
             AND (bcli.COLI_Price - bgai.GAI_SQJE) >= 0
             AND bcli.DeleteFlag = 0
             AND bgai.DeleteFlag = 0
			 AND bcld.DeleteFlag = 0
			 --AND COLD_SPFS<=2
             AND NOT EXISTS (
                 SELECT TOP 1 1
                 FROM   BIZ_JuheOrderList
                 WHERE  JOL_COLD_SN = COLD_SN
               )
            and (((COLI_State<>8 and COLI_State<>63) and COLD_StartDate < CONVERT(varchar(100),GETDATE()+29,23)) or ((COLI_State=8 or COLI_State=63)  and COLD_StartDate between CONVERT(varchar(100),GETDATE()+29,23) and CONVERT(varchar(100),GETDATE()+29,23)+' 23:59'))
		";
		$query = $this->HT->query($sql);
		return $query->result();
	}
	
	
	//筛选符合发送邮件的订单
	public function auto_sendmail(){
		$sql = "SELECT
					JOL_COLD_SN,
					JOL_JuheOrder,
					JOL_Status,
					JOL_RebackMsg
				FROM
					BIZ_JuheOrderList bjo
				left join
					BIZ_ConfirmLineDetail bcld
				on 
					bcld.COLD_SN = bjo.JOL_COLD_SN
				left join
					BIZ_ConfirmLineInfo bcli
				on 
					bcld.COLD_COLI_SN = bcli.COLI_SN			 
				WHERE 
					JOL_SendMail = 0
				AND
					JOL_IsAuto = 1
				AND 
					JOL_Status != '0'
				AND
					JOL_Status != 'e'
				AND
					JOL_Status != '2'
				AND 
					bcli.COLI_WebCode = 'cht'";
		$query = $this->HT->query($sql);
		return $query->result();	
	}
	
	//筛选符合发送邮件的订单
	public function auto_sendmailtest(){
		$sql = "SELECT
					JOL_COLD_SN,
					JOL_JuheOrder,
					JOL_Status,
					JOL_RebackMsg
				FROM
					BIZ_JuheOrderList bjo
				left join
					BIZ_ConfirmLineDetail bcld
				on 
					bcld.COLD_SN = bjo.JOL_COLD_SN
				left join
					BIZ_ConfirmLineInfo bcli
				on 
					bcld.COLD_COLI_SN = bcli.COLI_SN			 
				WHERE 
					JOL_SendMail = 0
				AND
					JOL_IsAuto = 1
				AND 
					bcli.COLI_WebCode = 'cht'";
		$query = $this->HT->query($sql);
		return $query->result();	
	}
	
	//获取失败的订单请求了多少次接口
	/*
		状态4为成功出票
		状态2为等待回调
		状态7为线上退票
	*/
	function get_count_jol($cold_sn){
		$sql = "select 
						count(JOL_JuheOrder) as count 
				  from 
						BIZ_JuheOrderList 
				 where 
						JOL_COLD_SN = ? 
					and 
						JOL_Status not in (4,2,7)";
		$query = $this->HT->query($sql,$cold_sn);
		return $query->row();
	}
	
	//成功出票后，更新汉特订单中，special request的值
	function update_special_request($coli_id){
		$sql = "update 
					BIZ_ConfirmLineInfo 
				set 
					COLI_OrderDetailText = '已经自动出票---' + (select COLI_OrderDetailText from BIZ_ConfirmLineInfo where COLI_ID = '$coli_id')
				where 
					COLI_ID = '$coli_id'";
		$query = $this->HT->query($sql);
		return $query;
	}
	
	//邮件使用
	function get_user_info($jh_order){
		$sql = "select 
						* 
				from 
						BIZ_ConfirmLineDetail 
				where 
						COLD_SN = (
						select 
								top 1 JOL_COLD_SN 
						from 
								BIZ_JuheOrderList 
						where 
								JOL_JuheOrder = ?
						)";
		$query = $query = $this->HT->query($sql, $jh_order);	
		if ($query->num_rows() > 0) {
			return $query->row();
		} else {
			return false;
		}		
	}
	
	//获取paypal付款记录
	function get_paypal($coli_id){
		$sql = "select top 1 GAI_SQJE from BIZ_GroupAccountInfo where GAI_COLI_ID = ?";
		$query = $query = $this->HT->query($sql, $coli_id);	
		if ($query->num_rows() > 0) {
			return $query->row();
		} else {
			return false;
		}		
	}
	
    function biz_order_detail($cold_sn) {
        $sql = "        
                    SELECT TOP 1 bfoi.FOI_SN
                          ,bfoi.FOI_COLD_SN
                          ,bfoi.DepartAirport
                          ,bfoi.ArrivalAirport
                          ,bfoi.FlightsNo
                          ,bfoi.Aircraft
                          ,bfoi.DepartureDate
						  ,bfoi.FOI_SelectedSeat 
                          ,(
                               SELECT TOP 1 TRS_StationCN
                               FROM   TrainStation
                               WHERE  TRS_Code = DepartAirport
							   and ISNULL(TRS_StationCN,'')<>''
                           )  AS DepartAirport_cn
                          ,(
                               SELECT TOP 1 TRS_StationCN
                               FROM   TrainStation
                               WHERE  TRS_Code = ArrivalAirport
							   and ISNULL(TRS_StationCN,'')<>''
                           )  AS ArrivalAirport_cn,
                           FOI_TrainNetOrderNo,
                           bfoi.adultcost,
						   bfoi.childcost,
						   ArrivalTime,
						   DepartureTime
                    FROM   BIZ_FlightsOrderInfo bfoi
                    WHERE  bfoi.FOI_COLD_SN = ?
                 ";
        $query = $this->HT->query($sql, $cold_sn);
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    //传入一组BPE_SN获取乘客信息
    function in_bpesn_people_info($bpe_sn){
        $sql = "
                SELECT bbp.BPE_SN
                    ,bbp.BPE_FirstName
                    ,bbp.BPE_MiddleName
                    ,bbp.BPE_LastName
                    ,bbp.BPE_GuestType
                    ,bbp.BPE_Passport
                    ,bbp.BPE_PassportType
					,bbp.BPE_SEX 
					,bbp.BPE_BirthDate
					,bbp.BPE_PassExpdate 
              FROM   BIZ_BookPeople bbp
              WHERE  BPE_SN in(".$bpe_sn.")          
              ";
        $query = $this->HT->query($sql);
        return $query->result();
    }
    //传入子订单COLD_SN，获取子订单对应的乘客信息
    function biz_people($cold_sn) {
        $sql = "
                SELECT bbp.BPE_SN
                    ,bbp.BPE_FirstName
                    ,bbp.BPE_MiddleName
                    ,bbp.BPE_LastName
                    ,bbp.BPE_GuestType
                    ,bbp.BPE_Passport
					,bbp.BPE_PassportType
              FROM   BIZ_BookPeople bbp
              WHERE  EXISTS(
                         SELECT TOP 1 1
                         FROM   BIZ_BookPeopleList bbpl
                         WHERE  bbpl.BPL_BPE_SN = bbp.BPE_SN
                                AND bbpl.BPL_COLD_SN = ?
                     )          
              ";
        $query = $this->HT->query($sql, $cold_sn);
        return $query->result();
    }

    //添加聚合订单记录，BIZ_JuheOrderList
    function add_biz_jol($data,$isauto){
		$sql="
			  INSERT INTO BIZ_JuheOrderList(
				  JOL_SubTime,
				  JOL_COLD_SN,
				  JOL_JuheOrder,
				  JOL_Status,
				  JOL_RebackMsg,
				  JOL_FromStation,
				  JOL_ToStation,
				  JOL_FromStationCode,
				  JOL_ToStationCode,
				  JOL_TrainCode,
				  JOL_BackTxt,
				  JOL_IsAuto
				  )
			  VALUES(getdate(),?,?,?,?,?,?,?,?,?,?,?)  
		   ";
      $query = $this->HT->query($sql, array($data->JOL_COLD_SN,$data->JOL_JuheOrder,$data->JOL_Status,$data->JOL_RebackMsg,$data->JOL_FromStation,$data->JOL_ToStation,$data->JOL_FromStationCode,$data->JOL_ToStationCode,$data->JOL_TrainCode,$data->JOL_BackTxt,$isauto
        ));
      return $query;
    }
    /* 以下为get_ht_order优化代码 */

    //传入主订单翰特订单号COLI_ID（161014006M），获取子订单中火车订单的COLD_SN
    function get_biz_cold($cols_id) {
        $sql = "SELECT COLD_SN
            FROM BIZ_ConfirmLineDetail bcld 
            WHERE bcld.COLD_COLI_SN=(
              SELECT COLI_SN FROM BIZ_ConfirmLineInfo bcli WHERE bcli.COLI_ID=?)
            AND bcld.DeleteFlag=0 AND bcld.COLD_ServiceType='2'";
        $query = $this->HT->query($sql, $cols_id);
        return $query->result();
    }

    //传入COLD_SN，获取火车车次等信息
    function get_biz_foi($cold_sn) {
        $sql = "
            SELECT FOI_COLD_SN,
                   FlightsNo,
                   Cabin,
				   Aircraft,
                   DepartureCity,
				   DepartAirport,
				   ArrivalAirport,
                   ArrivalCity,
                   DepartureDate,
                   DepartureTime,
                   ArrivalTime,
                   adultcost,
				   FOI_SelectedSeat,
				   FOI_TrainNetOrderNo,
				   FOI_SaleDate,
				   (
                               SELECT TOP 1 TRS_StationCN
                               FROM   TrainStation
                               WHERE  TRS_Code = DepartAirport
                           )  AS DepartAirport_cn
                          ,(
                               SELECT TOP 1 TRS_StationCN
                               FROM   TrainStation
                               WHERE  TRS_Code = ArrivalAirport
                           )  AS ArrivalAirport_cn
            FROM   BIZ_FlightsOrderInfo
            WHERE  FOI_COLD_SN = ?
            ";
        $query = $this->HT->query($sql, $cold_sn);
        return $query->result();
    }

    //传入COLD_SN，获取BIZ_JuheOrderList是否存在此子订单，用来判断是否提交过给聚合
    function get_biz_jol($cold_sn) {
        $sql = "SELECT top 1 JOL_SN FROM BIZ_JuheOrderList WHERE JOL_COLD_SN= ?";
        $query = $this->HT->query($sql, $cold_sn);
		if($query->num_rows() == 0){
			return true;
		}else{
			return false;
		}	
    }

    //传入COLI_ID，获取外联名
    function get_operatorInfo($cols_id) {
        $sql = "
			   SELECT
					   Name,
					   OPI_Name,
					   case when OPI_SN=375 then OPI_EmailBak else OPI_Email end as OPI_Email,
					   tel,
					   Mobile,
					   Email
				FROM   OperatorInfo
				left join agenter_user
				on AU_OPI_SN = OPI_SN
				WHERE  OPI_SN = (
						   SELECT COLI_OPI_ID
						   FROM   BIZ_ConfirmLineInfo bcli
						   WHERE  bcli.COLI_ID = ?
					   )
					   and agenter in ('cht', 'train_vac', 'jp', 'train_it', 'vc', 'ru')
        ";
        $query = $this->HT->query($sql, $cols_id);
        return $query->result();
    }
	
	
	
    /* 以上为get_ht_order优化代码 */

    //修改BIZ_JuheOrderList
    function update_biz_jol($where,$data){
      return $this->HT->where($where)->update("BIZ_JuheOrderList", $data);
    }

    //获取站点简码
    function get_train_station_code($where){
       $data=$this->HT->where($where)->select("TRS_Code")->from("TrainStation")->get(); 
       return $data->result(); 
    }

    //接收聚合订单号，获取翰特订单号，即BIZ_ConfirmLineInfo的COLI_ID
    function jh_order_get_coli_id($jh_order){
	  $sql="SELECT 
				COLI_ID,COLI_SN,COLI_OPI_ID 
			FROM 
				BIZ_ConfirmLineInfo bcli 
			WHERE 
				bcli.COLI_SN=
			  (SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail bcld WHERE bcld.COLD_SN=
				(SELECT JOL_COLD_SN FROM BIZ_JuheOrderList bjol WHERE bjol.JOL_JuheOrder= ? ))
			";
	  $query = $this->HT->query($sql, $jh_order);
	  return $query->result();
    }
	
    //通过COLI_ID获取团名 即 GroupInfo的GRI_No 
    function get_gri_no($coli_id){
      $sql="SELECT GRI_No FROM GroupInfo 
              WHERE GRI_SN=(
                  SELECT COLI_GRI_SN FROM BIZ_ConfirmLineInfo WHERE COLI_ID=?
                )
      ";
      $query = $this->HT->query($sql, $coli_id);
      return $query->result();
    }
	
	//获取跟踪号
	public function getTrackingCode(){
		include('c:/database_conn.php');
        $connection = array(
        	'UID' => $db['HT']['username'],
        	'PWD' => $db['HT']['password'],
        	'Database' =>  'tourmanager',
        	'ConnectionPooling' => 1,
        	'CharacterSet' => 'utf-8',
        	'ReturnDatesAsStrings' => 1
        );
        $conn = sqlsrv_connect($db['HT']['hostname'], $connection);
        $stmt = sqlsrv_query($conn, "exec dbo.SP_getTrackingCode;");
		if ($stmt === false) {
            echo "Error in executing statement 3.\n";
            die(print_r(sqlsrv_errors(), true));
        }else{
			//存储过程中每一个select都会产生一个结果集，取某个结果集就需要从第一个移动到需要的那个结果集
            //如果结果集为空就移到下一个
            while (sqlsrv_has_rows($stmt) !== TRUE) {
                sqlsrv_next_result($stmt);
            }
            
            $result_object = array();
            while ($row = sqlsrv_fetch_object($stmt)) {
                $result_object[] = $row;
            }
            
            sqlsrv_free_stmt($stmt);
            sqlsrv_close($conn);
            
            return($result_object[0]->TrackingCode);
        }
	}
	
	//根据coli_sn判断订单是否关联
	public function islink($coli_sn){
		$sql = "select * from CK_GroupInfo left join BIZ_ConfirmLineInfo on CGI_GRI_SN=COLI_GRI_SN where COLI_SN = '$coli_sn'";
		$query = $this->HT->query($sql);
		if($query->num_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
	//跟踪号与订单关联
	public function linkTrackingCode($coli_sn,$TrackCode){
		include('c:/database_conn.php');
        $connection = array(
        	'UID' => $db['HT']['username'],
        	'PWD' => $db['HT']['password'],
        	'Database' =>  'tourmanager',
        	'ConnectionPooling' => 1,
        	'CharacterSet' => 'utf-8',
        	'ReturnDatesAsStrings' => 1
        );
        $conn = sqlsrv_connect($db['HT']['hostname'], $connection);
        $stmt = sqlsrv_query($conn, "exec dbo.SP_recordTrackingCode '$coli_sn', '$TrackCode'");
		if ($stmt === false) {
            echo "Error in executing statement 3.\n";
            die(print_r(sqlsrv_errors(), true));
        }else{
			//存储过程中每一个select都会产生一个结果集，取某个结果集就需要从第一个移动到需要的那个结果集
            //如果结果集为空就移到下一个
			/*
            while (sqlsrv_has_rows($stmt) !== TRUE) {
                sqlsrv_next_result($stmt);
            }
            
            $result_object = array();
            while ($row = sqlsrv_fetch_object($stmt)) {
                $result_object[] = $row;
            }
            */
            sqlsrv_free_stmt($stmt);
            sqlsrv_close($conn);
            
            
        }
	}
	
    //通过COLI_ID获取我的支付 BIZ_TrainOrderCost
    function get_train_order_cost($coli_id){
      $sql="SELECT TOC_Memo,TOC_TrainNumber,TOC_DepartureDate,TOC_TicketCost
            FROM BIZ_TrainOrderCost btoc 
            WHERE btoc.TOC_COLI_SN=
              (SELECT COLI_SN FROM BIZ_ConfirmLineInfo bcli 
                  WHERE bcli.COLI_ID=?)";
      $query = $this->HT->query($sql, $coli_id);
      return $query->result();
    }

    //-----------------------------自动出票 begin-----------
    //用于自动出票，传入主订单翰特订单号COLI_SN，获取子订单中火车订单的COLD_SN和COLD_SPFS(寄送票标示，大于1的为寄送)
    public function get_cold_sn($COLI_SN){
      $sql="SELECT COLD_SN,COLD_SPFS
          FROM BIZ_ConfirmLineDetail bcld 
          WHERE bcld.COLD_COLI_SN=?
          AND bcld.DeleteFlag=0 AND bcld.COLD_ServiceType='2'";
      $query = $this->HT->query($sql,$COLI_SN);
      return $query->result();
    }  
    //用于自动出票,传入主订单翰特订单号 COLI_ID ，获取客人的姓名和邮箱
    public function get_guest_info($COLI_ID){
      $sql = "SELECT GUT_FirstName,GUT_LastName,GUT_Email FROM BIZ_GUEST bg WHERE bg.GUT_SN =        
              ( SELECT COLI_GUT_SN FROM BIZ_ConfirmLineInfo bcli WHERE bcli.COLI_ID = ?)
            ";
      $query = $this->HT->query($sql,$COLI_ID);
      return $query->result();
    }
    //通过 JOL_JuheOrder 获取 BIZ_JuheOrderList 获取聚合订单详情
    public function get_biz_jol_info($cold_sn,$jol_jo){
      $sql = "SELECT top 1 
                JOL_SN,
                JOL_COLD_SN,
                JOL_JuheOrder,
                JOL_SubTime,
                JOL_Status,
                JOL_RebackMsg,
                JOL_FromStation,
                JOL_ToStation,
                JOL_FromStationCode,
                JOL_ToStationCode,
                JOL_TrainCode,
                JOL_Price,
                JOL_PayTime,
                JOL_BackTxt
               FROM BIZ_JuheOrderList 
                WHERE JOL_COLD_SN = ? AND JOL_JuheOrder = ? 
            ";
      $query = $this->HT->query($sql,array($cold_sn,$jol_jo));
      return $query->row();      
    }
    // 传入coli_sn获取订单号
    public function coli_sn_get_coli_id($coli_sn){
      $sql="SELECT COLI_ID,COLI_WebCode FROM BIZ_ConfirmLineInfo WHERE COLI_SN = ? ";
      $query = $this->HT->query($sql,array($coli_sn));
      return $query->result();       
    }
    // 传入 cold_sn 获取订单号
    public function cold_sn_get_coli_id($cold_sn){
      $sql="SELECT COLI_ID FROM BIZ_ConfirmLineInfo
             WHERE COLI_SN = (
                SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail WHERE COLD_SN = ?
              )
          ";
      $query = $this->HT->query($sql,array($cold_sn));
      return $query->result();       
    }
    //-----------------------------自动出票 end-----------
	
	 function SendMail($fromName, $fromEmail, $toName, $toEmail, $subject, $body) {
        $toName = str_replace("'","''",$toName);
        $body = str_replace("'","''",$body);
        $sql = "INSERT INTO Email_AutomaticSend 
                  ( 
                    M_ReplyToName, M_ReplyToEmail, M_ToName, M_ToEmail, M_Title, M_Body, M_Web,  
                    M_FromName, M_State 
                  ) 
                VALUES 
                  (  
                    ?, ?, N'{$toName}', ?, N'{$subject}', N'{$body}', ?, ?, 0
                  ) ";	
        $query = $this->HT->query($sql, array($fromName, $fromEmail, $toEmail, 'cht', 'information'));
        echo $query;
		
		//return $query;
     }
	 
	 public function get_mail($m_sn){
		 $sql = "SELECT * FROM Email_AutomaticSend WHERE M_SN = ?";
		 $query = $this->HT->query($sql,$m_sn);
		 return $query->row();
	 }
	 
	 
	 
	 public function get_ht_order(){
		 $sql = "select 
						COLI_ID,
						COLD_SN,
						FOI_SelectedSeat 
						from 
							BIZ_ConfirmLineInfo bcli
						left join BIZ_ConfirmLineDetail bgai on COLI_SN = COLD_COLI_SN	
						left join BIZ_FlightsOrderInfo bfoi on FOI_COLD_SN = COLD_SN			 
						where 
							COLI_ServiceType = '2'
						AND 
							COLI_WebCode in ('cht')
						and
							COLI_ApplyDate > '2018-02-01'
						AND bcli.DeleteFlag = 0
						AND bgai.DeleteFlag = 0
						and COLI_State != 30
						and COLI_State != 50
						and COLI_State != 40
						and COLI_State != 60
						and FOI_SelectedSeat != 'NULL'
						and FOI_SelectedSeat != ''
						order by COLI_ApplyDate asc	";
		 $query = $this->HT->query($sql);
		 return $query->result();
	 }
	 
	 public function get_juhe_select(){
		$sql = "select 
					FOI_SelectedSeat,
					JOL_BackTxt,
					JOL_JuheOrder,
					JOL_IsAuto
				from 
					BIZ_JuheOrderList	
				left join BIZ_FlightsOrderInfo on FOI_COLD_SN = JOL_COLD_SN
				where JOL_SubTime > '2018-02-01'
				and JOL_Status = '4'
				and FOI_SelectedSeat != 'NULL'
				and FOI_SelectedSeat != ''
			   "; 
		$query = $this->HT->query($sql);
		return $query->result();	   
	 }
	 
	 //更新当前订单
	 public function update_cold_state($state,$cold_sn){
		$sql="update BIZ_ConfirmLineDetail set COLD_State = ? where COLD_SN = ?";
		$query = $this->HT->query($sql,array($state,$cold_sn));
		return $query;
	 }
	 
	 public function update_coli_state($state,$coli_sn){
		 $sql="update BIZ_ConfirmLineInfo set COLI_State = ? where COLI_SN = ?";
		 $query = $this->HT->query($sql,array($state,$coli_sn));
	 }
	 
	 public function cold_sn_get_coli_sn($cold_sn){
		 $sql = "select COLD_COLI_SN from BIZ_ConfirmLineDetail where COLD_SN = ?";
		 $query = $this->HT->query($sql,$cold_sn);
		 return $query->result();
	 }
	 
	 public function get_alltrain($coli_sn){
		 $sql = "select * from BIZ_ConfirmLineDetail where COLD_COLI_SN = ?";
		 $query = $this->HT->query($sql,$coli_sn);
		 return $query->result();
	 }
	 
	 public function get_order_webcode($coli_sn){
		 $sql = "select COLI_WebCode from BIZ_ConfirmLineInfo where COLI_SN = ?";
		 $query = $this->HT->query($sql,$coli_sn);
		 return $query->row();
	 }
	 
	 public function sale_time_station($station,$time){
		 $sql = 'INSERT INTO TrainSaleTime (TST_station_cn,TST_saletime) VALUES (?,?) ';
		 $query = $this->HT->query($sql,array($station,$time));
		 return $query;
	 }
	 
	 public function update_sale_time($time,$update_time){
		 $sql = "update TrainSaleTime set TST_saletime = '{$time}' WHERE TST_saletime = '{$update_time}'";
		 $query = $this->HT->query($sql);
		 return $query;
	 }
	 
	 public function get_saletime($station){
		 $sql = 'select TST_saletime from TrainSaleTime where TST_station_cn = ?';
		 $query = $this->HT->query($sql,$station);
		 return $query->row();
	 }
	 
	 //
	 public function addseatinfo($seat_info,$cold_sn){
		 $sql = "if EXISTS(select FOI_BookSeat from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}' and FOI_BookSeat = '{$seat_info}' or FOI_BookSeat = 'NULL')
					update BIZ_FlightsOrderInfo set FOI_BookSeat = '{$seat_info}' where FOI_COLD_SN = '{$cold_sn}'
				 else
					if(select CHARINDEX('{$seat_info}',FOI_BookSeat) from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}') = 0
						update BIZ_FlightsOrderInfo set FOI_BookSeat = (select FOI_BookSeat from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}') + ',{$seat_info}' where FOI_COLD_SN = '{$cold_sn}'";
		 $query = $this->HT->query($sql,array($seat_info,$cold_sn));
	 }
	 
	 public function test(){
		 
		 $seat_info = 'Coach 09,Seat 05A,05C';
		 $cold_sn = '488110751';
		 
		 //$sql = "update BIZ_FlightsOrderInfo set FOI_BookSeat = '{$seat_info}' where FOI_COLD_SN = '{$cold_sn}'";
		 
		 /*$sql = "IF (select FOI_BookSeat from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}' and FOI_BookSeat like '%{$seat_info}%')
					update BIZ_FlightsOrderInfo set FOI_BookSeat = '{$seat_info}' where FOI_COLD_SN = '{$cold_sn}'
				 else 
					update BIZ_FlightsOrderInfo set FOI_BookSeat = (select FOI_BookSeat from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}') + ',{$seat_info}' where FOI_COLD_SN = '{$cold_sn}'	
				";*/
		  $sql = "if EXISTS(select FOI_BookSeat from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}' and FOI_BookSeat = '{$seat_info}' or FOI_BookSeat = 'NULL')
					update BIZ_FlightsOrderInfo set FOI_BookSeat = '{$seat_info}' where FOI_COLD_SN = '{$cold_sn}'
				 else
					if(select CHARINDEX('{$seat_info}',FOI_BookSeat) from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}') = 0
						update BIZ_FlightsOrderInfo set FOI_BookSeat = (select FOI_BookSeat from BIZ_FlightsOrderInfo where FOI_COLD_SN = '{$cold_sn}') + ',{$seat_info}' where FOI_COLD_SN = '{$cold_sn}'	
				 ";		
		 
		 
		 $query = $this->HT->query($sql);
		 
	 }
	 
	 public function get_price($fromStationCode,$toStationCode,$trainCode){
		$sql = "SELECT 
					TPL_Price 
				FROM 
					TrainPriceList 
				WHERE 
					TPL_Train_Code = '$trainCode'
				AND 
					TPL_From_Station_Code = '$fromStationCode'
				AND	
					TPL_To_Station_Code = '$toStationCode'";
		$query = $this->INFO->query($sql);		
		return $query->row();
	}
}
