<?php

class Order_people_model extends CI_Model {
	private $order="";//订单号

    function __construct() {
        parent::__construct();
        $this->HT=$this->load->database('HT', TRUE);
    }

	function biz_people($order){
		//订单信息
		$sql="SELECT COLD_ServiceType,COLD_StartDate FROM BIZ_ConfirmLineDetail WHERE COLD_SN= ?";

		$data=new StdClass();
		$query_cold=$this->HT->query($sql,$order);
		$data->cold=$query_cold->result(); 

		//城市名称
		if(!empty($data->cold)){

			//车次信息
			$sql="SELECT DepartAirport,ArrivalAirport,FlightsNo,Aircraft,DepartureDate FROM BIZ_FlightsOrderInfo WHERE FOI_COLD_SN= ?";
			$query_zdjm=$this->HT->query($sql,(int)$order);
			$query_zdjm=$query_zdjm->result();
			if(!empty($query_zdjm)){
				$data->cold[0]->DepartAirport=$query_zdjm[0]->DepartAirport;
				$data->cold[0]->ArrivalAirport=$query_zdjm[0]->ArrivalAirport;
				$data->cold[0]->FlightsNo=$query_zdjm[0]->FlightsNo;
				$data->cold[0]->Aircraft=$query_zdjm[0]->Aircraft;

				//站点中文名称
				$sql="SELECT TRS_StationCN AS LeaveStation FROM TrainStation WHERE TRS_Code='".$query_zdjm[0]->DepartAirport."'";
				$query_LeaveStation=$this->HT->query($sql);
				$LeaveStation=$query_LeaveStation->result();

				$sql="SELECT TRS_StationCN AS ServerStation FROM TrainStation WHERE TRS_Code='".$query_zdjm[0]->ArrivalAirport."'";
				$query_ServerStation=$this->HT->query($sql);
				$ServerStation=$query_ServerStation->result();

				$data->cold[0]->LeaveStation=$LeaveStation[0]->LeaveStation;
				$data->cold[0]->ServerStation=$ServerStation[0]->ServerStation;

			}			
		}

		$sql="SELECT BPE_FirstName,BPE_MiddleName,BPE_LastName,BPE_GuestType,BPE_Passport
			  FROM BIZ_BookPeople
			  WHERE BPE_SN in (
					SELECT BPL_BPE_SN FROM BIZ_BookPeopleList WHERE BPL_COLD_SN=?
			  	)";
		$query_people=$this->HT->query($sql,$order);  
		$data->people=$query_people->result();
		return $data;	  

	}
	//添加聚合订单记录，BIZ_JuheOrderList
	public function add_jh_order($data){
		$sql="IF NOT EXISTS(
					SELECT TOP 1 1 FROM BIZ_JuheOrderList
					WHERE JOL_COLD_SN=? AND JOL_JuheOrder=?
				)	
			  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
			  			)
					VALUES(getdate(),?,?,?,?,?,?,?,?,?,?)
			  ELSE UPDATE BIZ_JuheOrderList
			  		SET 
				  		JOL_SubTime=getdate(),
						JOL_JuheOrder=?,
						JOL_Status=?,
						JOL_RebackMsg=?,
						JOL_FromStation=?,
						JOL_ToStation=?,
						JOL_FromStationCode=?,
						JOL_ToStationCode=?,
						JOL_TrainCode=?,
						JOL_BackTxt=?
					WHERE 
						JOL_COLD_SN=?			
			 ";
		$query = $this->HT->query($sql, array($data->JOL_COLD_SN,$data->JOL_JuheOrder,$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,
			$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,$data->JOL_COLD_SN));
        
        return $query;
	}
	public function update_jh_order($data){
		$sql="UPDATE BIZ_JuheOrderList
				SET JOL_RebackMsg=?,
					JOL_BackTxt=?,
					JOL_Status=?,
					JOL_Price=?
				WHERE 
					JOL_JuheOrder=?	
					";
		$query = $this->HT->query($sql,array($data->JOL_RebackMsg,$data->JOL_BackTxt,$data->JOL_Status,$data->JOL_Price,$data->JOL_JuheOrder));
		return $query;
	}

		
	//BIZ_TrainOrderCost,我的支付
	//BIZ_FlightsOrderInfo.FOI_TrainNetOrderNo,更新取票号
	public function add_train_order($data){
		//主表ID，下面两个地方用到，所以先筛选出来，不知道能不能通过合并提高效率
		$sql="SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail WHERE COLD_SN=?";
		$query=$this->HT->query($sql,$data->TOC_COLD_SN);
		$query=$query->result();
		$CCSN=$query[0]->COLD_COLI_SN;
		if(empty($data->FOI_TrainNetOrderNo)){
			//退票
			$sql="IF NOT EXISTS(
						SELECT TOP 1 1 FROM BIZ_TrainOrderCost
						WHERE TOC_COLD_SN = ? AND TOC_Memo like ?
					) 
					INSERT INTO BIZ_TrainOrderCost(
								TOC_Memo,
								TOC_CreateDate,
								TOC_COLI_SN,
								TOC_COLD_SN,
								TOC_TrainNumber,
								TOC_DepartureDate,
								TOC_TicketCost,
								TOC_WL
								)
							VALUES(?,getdate(),{$CCSN},?,?,?,?,(SELECT COLI_OPI_ID FROM BIZ_ConfirmLineInfo WHERE COLI_SN={$CCSN}))";
			$query = $this->HT->query($sql,array($data->TOC_COLD_SN,"%".$data->TOC_Memo."%","聚合退票费 ".$data->TOC_Memo,$data->TOC_COLD_SN,$data->TOC_TrainNumber,$data->TOC_DepartureDate,$data->TOC_TicketCost));	
		}else{
			//出票
			//BIZ_FlightsOrderInfo.FOI_TrainNetOrderNo,更新取票号
			$sql="UPDATE BIZ_FlightsOrderInfo
					SET
						FOI_TrainNetOrderNo=?
					WHERE
						FOI_COLD_SN=?";
			$this->HT->query($sql,array($data->FOI_TrainNetOrderNo,$data->TOC_COLD_SN));
			$sql="IF NOT EXISTS(
						SELECT TOP 1 1 FROM BIZ_TrainOrderCost
						WHERE TOC_COLD_SN = ? AND TOC_Memo like ?
					) 
					INSERT INTO BIZ_TrainOrderCost(
								TOC_Memo,
								TOC_CreateDate,
								TOC_COLI_SN,
								TOC_COLD_SN,
								TOC_TrainNumber,
								TOC_DepartureDate,
								TOC_TicketCost,
								TOC_WL,
								TOC_OtherCost
								)
							VALUES(?,getdate(),{$CCSN},?,?,?,?,(SELECT COLI_OPI_ID FROM BIZ_ConfirmLineInfo WHERE COLI_SN={$CCSN}),null),(?,getdate(),{$CCSN},?,?,?,?,(SELECT COLI_OPI_ID FROM BIZ_ConfirmLineInfo WHERE COLI_SN={$CCSN}),1)";
			$query = $this->HT->query($sql,array($data->TOC_COLD_SN,"%".$data->TOC_Memo."%",$data->TOC_Memo." 聚合自动出票",$data->TOC_COLD_SN,$data->TOC_TrainNumber,$data->TOC_DepartureDate,$data->TOC_TicketCost,$data->TOC_Memo." 手续费",$data->TOC_COLD_SN,$data->TOC_TrainNumber,$data->TOC_DepartureDate,$data->poundage));			
		}
		return $query;

	}
	public function get_order($pagesize=2,$page=0,$where="1=1"){
		$data=new StdClass();
		//获取总条数
		$sql="SELECT COUNT(*) AS count FROM BIZ_JuheOrderList
					 LEFT JOIN 
				   		 BIZ_ConfirmLineInfo 
				   ON
				   		BIZ_ConfirmLineInfo.COLI_SN=(SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail WHERE COLD_SN=BIZ_JuheOrderList.JOL_COLD_SN)
				   WHERE 
			   		  {$where}
			   ";
		$query = $this->HT->query($sql);
		$count=$query->result();
		$data->count=$count[0]->count;
		
		$sql="SELECT TOP {$pagesize} BIZ_JuheOrderList.JOL_SubTime,
					 BIZ_JuheOrderList.JOL_COLD_SN,
					 BIZ_JuheOrderList.JOL_JuheOrder,
					 BIZ_JuheOrderList.JOL_Status,
					 BIZ_JuheOrderList.JOL_RebackMsg,
					 BIZ_JuheOrderList.JOL_FromStation,
					 BIZ_JuheOrderList.JOL_ToStation,
					 BIZ_JuheOrderList.JOL_TrainCode,
					 BIZ_JuheOrderList.JOL_Price,
					 BIZ_ConfirmLineInfo.COLI_ID
			   FROM 
			 		 BIZ_JuheOrderList
			   LEFT JOIN 
			   		 BIZ_ConfirmLineInfo 
			   ON
			   		BIZ_ConfirmLineInfo.COLI_SN=(SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail WHERE COLD_SN=BIZ_JuheOrderList.JOL_COLD_SN)
			   WHERE 
			   		BIZ_JuheOrderList.JOL_SN NOT IN(SELECT TOP {$page} BIZ_JuheOrderList.JOL_SN FROM BIZ_JuheOrderList ORDER BY BIZ_JuheOrderList.JOL_SubTime DESC)		
			   	AND
			   		{$where}
			   ORDER BY BIZ_JuheOrderList.JOL_SubTime DESC";
		$query = $this->HT->query($sql);
		$data->list=$query->result();
		return $data;

	}
	//外联
	public function get_wl($select="*",$where="1=1"){
		// $sql="SELECT COLI_OPI_ID FROM BIZ_ConfirmLineInfo bcli WHERE bcli.COLI_ID=?";
		$sql="SELECT {$select} FROM OperatorInfo WHERE {$where}";
		

	}
	//获取火车订票的相应信息 
    public function get_ht_order($order=""){
    	$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,$order);
		$cold_sn=$query->result();
		

		$data=new StdClass();
		
		if(!empty($cold_sn)){
			// $cold_sn_str="";
			$i=0;
			foreach ($cold_sn as $value) {
				// $cold_sn_str.=",".$value->COLD_SN;
				//乘客信息
				$sql="SELECT BPE_FirstName,BPE_MiddleName,BPE_LastName,BPE_GuestType,BPE_Passport
				  FROM BIZ_BookPeople
				  WHERE BPE_SN IN (
						SELECT BPL_BPE_SN FROM BIZ_BookPeopleList WHERE BPL_COLD_SN = ".$value->COLD_SN."
				  	)";
				$query=$this->HT->query($sql,$order);  
				$data->info[$i]->people=$query->result();

				//火车订票信息
				$sql="SELECT * FROM BIZ_FlightsOrderInfo WHERE FOI_COLD_SN = ".$value->COLD_SN;
				$query = $this->HT->query($sql);
				$data->info[$i]->train=$query->result();
				

				//BIZ_JuheOrderList是否有JOL_COLD_SN记录 ,返回1（已提交过）
				$sql="SELECT JOL_SN FROM BIZ_JuheOrderList WHERE JOL_COLD_SN=".$value->COLD_SN;
				$query = $this->HT->query($sql);

				$o=$query->result();
				if(!empty($o)){
					$data->info[$i]->status=1;
				}else{
					$data->info[$i]->status=0;
				}
				// $data->info[$i]->status= $query->result();
				$i++;	
			}
			// $cold_sn_str=substr($cold_sn_str, 1);
			//火车订票信息
			// $sql="SELECT * FROM BIZ_FlightsOrderInfo WHERE FOI_COLD_SN IN(".$cold_sn_str.")";
			// $query = $this->HT->query($sql);
			// $data->train=$query->result();
			$sql="SELECT  OPI_Name FROM OperatorInfo WHERE OPI_SN=(SELECT COLI_OPI_ID FROM BIZ_ConfirmLineInfo bcli WHERE bcli.COLI_ID=?)";
			$query = $this->HT->query($sql,$order);	
			$data->wl=$query->result();
		}
		
		return $data;		
    }

/*以下为get_ht_order优化代码*/ 
    //传入主订单翰特订单号COLI_ID（161014006M），获取子订单中火车订单的COLD_SN
    public function get_biz_cold($order){
    	$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,$order);
		return $query->result();
    }
    //传入子订单COLD_SN，获取子订单对应的乘客信息
    public function get_biz_people($cold_sn){
    	$sql="SELECT BPE_FirstName,BPE_MiddleName,BPE_LastName,BPE_GuestType,BPE_Passport
			  FROM BIZ_BookPeople
			  WHERE BPE_SN IN (
					SELECT BPL_BPE_SN FROM BIZ_BookPeopleList WHERE BPL_COLD_SN = ?
			  	)";
		$query=$this->HT->query($sql,$cold_sn);  
		return $query->result();
    }
    //传入COLD_SN，获取火车车次等信息
    public function get_biz_foi($cold_sn){
    	$sql="SELECT FOI_COLD_SN,
    			 	 FlightsNo,
    				 Cabin,
    				 DepartureCity,
    				 ArrivalCity,
    				 DepartureDate,
    				 DepartureTime,
    				 ArrivalTime,
    				 adultcost
    			FROM BIZ_FlightsOrderInfo WHERE FOI_COLD_SN = ?";
		$query = $this->HT->query($sql,$cold_sn);
		return $query->result();
    }	

    //传入COLD_SN，获取BIZ_JuheOrderList是否存在此子订单，用来判断是否提交过给聚合
    public 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);
		return $query->result();	
    }

    //传入COLI_ID，获取外联名
    public function get_operatorinfo($order){
    	$sql="SELECT  OPI_Name FROM OperatorInfo WHERE OPI_SN=(SELECT COLI_OPI_ID FROM BIZ_ConfirmLineInfo bcli WHERE bcli.COLI_ID=?)";
		$query = $this->HT->query($sql,$order);	
		return $query->result();
    }
/*以上为get_ht_order优化代码*/    


}