<?php

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

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

	//获取订单(用于订单列表)
	public function get_order($pagesize=2,$page=0,$where="1=1"){
		$data=new StdClass();
		//获取总条数
		$sql="SELECT COUNT(*) AS count FROM InfoManager.dbo.trainsystem
					 LEFT JOIN 
				   		 BIZ_ConfirmLineInfo 
				   ON
				   		BIZ_ConfirmLineInfo.COLI_SN=(SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail WHERE COLD_SN = InfoManager.dbo.trainsystem.ts_cold_sn)
				   WHERE 
			   		  {$where}
			   ";
		$query = $this->HT->query($sql);
		$count=$query->result();
		$data->count=$count[0]->count;
		
		$sql="SELECT TOP {$pagesize} InfoManager.dbo.trainsystem.ts_subtime,
					 InfoManager.dbo.trainsystem.ts_cold_sn,
					 InfoManager.dbo.trainsystem.ts_ordernumber,
					 InfoManager.dbo.trainsystem.ts_status,
					 InfoManager.dbo.trainsystem.ts_errormsg,
					 InfoManager.dbo.trainsystem.ts_fromstationame,
					 InfoManager.dbo.trainsystem.ts_tostationame,
					 InfoManager.dbo.trainsystem.ts_checi,
					 InfoManager.dbo.trainsystem.ts_orderamount,
					 InfoManager.dbo.trainsystem.ts_isauto,
					 InfoManager.dbo.trainsystem.ts_sendmail,
					 InfoManager.dbo.trainsystem.ts_m_sn,
					 InfoManager.dbo.trainsystem.ts_channel,
					 InfoManager.dbo.trainsystem.ts_autotimes,
					 BIZ_ConfirmLineInfo.COLI_ID,
					 BIZ_ConfirmLineInfo.COLI_WebCode
					 ,(select top 1 OPI2_Name from OperatorInfo2 where OPI2_OPI_SN = coli_opi_id and OPI2_LGC=2) as OPIName
			   FROM 
			 		 InfoManager.dbo.trainsystem
			   LEFT JOIN 
			   		 BIZ_ConfirmLineInfo 
			   ON
			   		BIZ_ConfirmLineInfo.COLI_SN=(SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail WHERE COLD_SN = InfoManager.dbo.trainsystem.ts_cold_sn)
			   WHERE 
			   		InfoManager.dbo.trainsystem.ts_id NOT IN(
					SELECT 
						TOP {$page} ts_id
					FROM 
						InfoManager.dbo.trainsystem 
					LEFT JOIN 
						BIZ_ConfirmLineInfo 
					ON
						BIZ_ConfirmLineInfo.COLI_SN=(SELECT COLD_COLI_SN FROM BIZ_ConfirmLineDetail WHERE COLD_SN = InfoManager.dbo.trainsystem.ts_cold_sn)
						where {$where}
					ORDER BY ts_subtime DESC)		
			   	AND
			   		{$where}
			   ORDER BY InfoManager.dbo.trainsystem.ts_subtime DESC";
		// echo($sql);
		// die();

		$query = $this->HT->query($sql);
		$data->list=$query->result();
		return $data;

	}

	//获取指定订单信息
	public function get_passager_details($ordernumber){
		$sql = "select * from trainsystem_tickets where tst_ordernumber = '{$ordernumber}'";
		$query = $this->INFO->query($sql);
		return $query->result();
	}
	
	//添加订单
	function add_orders($data){
		$sql="
			  INSERT INTO trainsystem(
				  ts_cold_sn,
				  ts_ordernumber,
				  ts_subtime,
				  ts_returncode,
				  ts_status,
				  ts_errormsg,
				  ts_fromstationame,
				  ts_fromstationcode,
				  ts_tostationame,
				  ts_tostationcode,
				  ts_startdate,
				  ts_startime,
				  ts_endtime,
				  ts_runtime,
				  ts_checi,
				  ts_channel,
				  ts_isauto,
				  ts_autotimes,
				  ts_autolasttime,
				  ts_lanhaiData
				  )
			  VALUES(
				  '{$data->cold_sn}',
				  '{$data->ordernumber}',
				  getdate(),
				  '{$data->returncode}',
				  '{$data->status}',
				  '{$data->errormsg}',
				  '{$data->fromstationame}',
				  '{$data->fromstationcode}',
				  '{$data->tostationame}',
				  '{$data->tostationcode}',
				  '{$data->startdate}',
				  '{$data->startime}',
				  '{$data->endtime}',
				  '{$data->runtime}',
				  '{$data->checi}',
				  '{$data->channel}',
				  '{$data->isauto}',
				  '{$data->ts_autotimes}',
				  getdate(),
				  '{$data->lanhaiData}'
			  )  
		   ";
      //echo $sql;
	  $query = $this->INFO->query($sql);
    }
	
	public function ticketfrom($ts_ordernumber){
		$sql = "select ts_channel,ts_cold_sn,ts_ordernumber,ts_lanhaiData from trainsystem where ts_ordernumber = ?";
		$query = $this->INFO->query($sql,array($ts_ordernumber));
		return $query->row();
	}
	
	public function get_passenger_info($ordernumber,$passportname,$passportno){
		$sql = "select * from trainsystem_tickets left join trainsystem on tst_ordernumber = ts_ordernumber where tst_realname = ? and tst_numberid = ? and tst_ordernumber = ?";
		$query = $this->INFO->query($sql,array($passportname,$passportno,$ordernumber));
		return $query->row();
	}
	
	//新增乘客
	function add_passagers($data){
		$sql = "IF EXISTS (select * from trainsystem_tickets where tst_ordernumber = '{$data->ordernumber}' and tst_numberid = '{$data->numberid}' and tst_realname = '{$data->realname}')
				update 
					trainsystem_tickets 
				set 
					tst_identitytype = '{$data->identitytype}',
					tst_numberid = '{$data->numberid}',
					tst_ticketype = '{$data->ticketype}',
					tst_ticketprice = '{$data->ticketprice}',
					tst_seatstype = '{$data->seatype}',
					tst_seatdetail = '{$data->seatdetail}',
					tst_status = '{$data->status}',
					tst_LongTrainNo = '{$data->longtrainno}',
					tst_lanhaiData = '{$data->lanhaiData}'
				where
					tst_ordernumber = '{$data->ordernumber}' 
				and 
					tst_numberid = '{$data->numberid}'
				and 
					tst_realname = '{$data->realname}'
			else
				INSERT INTO trainsystem_tickets (
					tst_ordernumber,
					tst_status,
					tst_realname,
					tst_identitytype,
					tst_numberid,
					tst_ticketype,
					tst_ticketprice,
					tst_seatstype,
					tst_seatdetail,
					tst_LongTrainNo,
					tst_lanhaiData 
				)VALUES(
					'{$data->ordernumber}',
					'{$data->status}',
					'{$data->realname}',
					'{$data->identitytype}',
					'{$data->numberid}',
					'{$data->ticketype}',
					'{$data->ticketprice}',
					'{$data->seatype}',
					'{$data->seatdetail}',
					'{$data->longtrainno}',
					'{$data->lanhaiData}'
				)
			";
		$query =$this->INFO->query($sql);
	}
	
	//更新订单信息
	public function update_orders($data){
		$where = '';
		if(!empty($data->bookcallback)){
			$where .= "
						ts_seatsinfo = '{$data->seatsinfo}',
						ts_checkdoor = '{$data->TicketCheck}',
						ts_elecnumber = '{$data->ElectronicOrderNumber}',	
					    ts_orderamount = '{$data->OrderTotleFee}',
						ts_operateTradeNo = '{$data->operateTradeNo}',
					    ts_bookcallback = '{$data->bookcallback}',";
						//ts_startime = '{$data->OrderTicketTime}',
		}else if(!empty($data->ticketresultcallback)){
			$where .= "ts_ticketresultcallback = '{$data->ticketresultcallback}',";
		}else if(!empty($data->confirmcallback)){
			$where .= "ts_confirmcallback = '{$data->confirmcallback}',";
		}else if(!empty($data->refundcallback )){
			$where .= "ts_refundcallback = '{$data->refundcallback }',";
		}else if(!empty($data->returncallback)){
			$where .= "ts_returncallback = '{$data->returncallback}',";
			if (!empty($data->operateTradeNo)){
				$where .= "ts_operateTradeNo = '{$data->operateTradeNo}',";
			}
		}else if(!empty($data->reschedulecallback)){
			$where .= "ts_reschedulecallback = '{$data->reschedulecallback}',";
		}
		$sql ="
			  update trainsystem
				set 
					ts_status = '{$data->OrderStatus}',
					ts_errormsg = '{$data->ErrorMsg}',
					".substr($where,0,strlen($where)-1)."
			    where 
					ts_ordernumber = '{$data->ordernumber}'
			  ";
		//echo $sql;die();	  
		$query = $this->INFO->query($sql);
	}

	//更新订单信息，下订单和支付保存状态
	public function update_orders_msg($data){
		$where = '';
		if(!empty($data->bookcallback)){
			$where .= " ts_bookcallback = '{$data->bookcallback}',";
		}else if(!empty($data->confirmcallback)){
			$where .= "ts_confirmcallback = '{$data->confirmcallback}',";
		}else if(!empty($data->returncallback)){
			$where .= "ts_returncallback = '{$data->returncallback}',";
		}else if(!empty($data->reschedulecallback)){
			$where .= "ts_reschedulecallback = '{$data->reschedulecallback}',";
		}

		if (!empty($data->addOrderData)) {
			//失败了保存提交的订单数据
			$where .= " ts_addOrderData = '{$data->addOrderData}',";
		}

		$sql ="
			  update trainsystem
				set 
					ts_status = '{$data->OrderStatus}',
					ts_errormsg = '{$data->ErrorMsg}',
					".substr($where,0,strlen($where)-1)."
			    where 
					ts_ordernumber = '{$data->ordernumber}'
			  ";
		//echo $sql;die();	  
		$query = $this->INFO->query($sql);

	}

	
	//更新乘客表信息
	public function update_passpager_info($data){
		$sql = "update 
					trainsystem_tickets 
				set 
					tst_status = '{$data->status}', 
					tst_returncallback = '{$data->returncallback}', 
					tst_lasteditdate = getdate()
				where 
					tst_ordernumber = '{$data->ordernumber}'
				and
					tst_realname = '{$data->realname}'
				and 
					tst_numberid = '{$data->numberid}'
				";
		$query = $this->INFO->query($sql);			
	}


	//更新乘客表信息,顺途新退票不再返回用户姓名及证件号码，用longElecNo长电子订单号来判断  2023-4-28 zp
	public function return_update_passpager_info($data){
		$sql = "update 
					trainsystem_tickets 
				set 
					tst_status = '{$data->status}', 
					tst_returncallback = '{$data->returncallback}', 
					tst_lasteditdate = getdate()
				where 
					tst_ordernumber = '{$data->ordernumber}'
				and 
					tst_LongTrainNo = '{$data->longElecNo}'
				";
		$query = $this->INFO->query($sql);			
	}
	
	/**
	 * 更新退票后的乘客信息，澜海退票返回乘客的ID，其它信息没有。
	 */
	public function return_update_passager_lanhai($data){

		$sql = "update 
				trainsystem_tickets 
			set 
				tst_status = '{$data->status}', 
				tst_returncallback = '{$data->returncallback}', 
				tst_lasteditdate = getdate()
			where 
				tst_id = '{$data->tst_id}'
			";
		$query = $this->INFO->query($sql);	
	}

	//获取订单出票状态
	public function get_tickets_info($cold_sn){
		$sql = "select top 1 ts_status,ts_ordernumber,ts_elecnumber,ts_seatsinfo from trainsystem where ts_cold_sn = ? and ts_status in ('4','7') order by ts_subtime desc";
		$query = $this->INFO->query($sql,array($cold_sn));
		return $query->row();
	}
	
	//获取所有订单
	public function getallorders(){
		$sql = "select * from Tourmanager.dbo.BIZ_JuheOrderList where JOL_SubTime > '2019-03-01' and (JOL_Status = '4' or JOL_Status = '7')";
		$query = $this->HT->query($sql);
		return $query->result();
	}
	
	//根据cold_sn 获取出票情况(仅限出票成功)
	public function get_ticketinfos($cold_sn){
		$sql = "select * from trainsystem where ts_cold_sn = ? and ts_status = '4'";
		$query = $this->INFO->query($sql,array($cold_sn));
		return $query->row();
	}
	
	//获取订单信息 根据第三方订单号查询
	public function get_order_info($ordernumber){
		$sql = "select * from trainsystem where ts_ordernumber = ?";
		$query = $this->INFO->query($sql,array($ordernumber));
		return $query->row();
	}
	
	//获取携程出票成功的订单详情
	public function ctripOrderDetail($ordernumber){
		$sql = "select * from trainsystem left join trainsystem_tickets on ts_ordernumber = tst_ordernumber where ts_ordernumber = ? and ts_channel = 'ctrip' and ts_status = 4";
		$query = $this->INFO->query($sql,array($ordernumber));
		return $query->result();
	}

	//传入COLD_SN，获取trainsystem是否存在此订单，用来判断是否提交过
	function get_submitStatus($cold_sn) {
		$sql = "SELECT top 1 ts_cold_sn FROM trainsystem WHERE ts_cold_sn= ?";
		$query = $this->INFO->query($sql, $cold_sn);
		if($query->num_rows() == 0){
			return true;
		}else{
			return false;
		}	
	}

	//获取出票次数，不管是自动还是手动。
	public function get_autotimes($cold_sn){
		$sql = "select COUNT(*) as countauto from InfoManager.dbo.trainsystem where ts_cold_sn = ? and ts_status=5";
		$query = $this->INFO->query($sql, $cold_sn);
		return $query->row();
	}

	/**
	 * 根据客人ID获取客人信息及出票信息
	 */
	public function get_passenger_byId($tst_id){
		$sql = "select top 1 * from trainsystem_tickets inner join trainsystem on  tst_ordernumber=ts_ordernumber and  tst_id = ?";
		$query = $this->INFO->query($sql,array($tst_id));
		return $query->row();
	}

	//根据订单号获取外联名称
	public function get_OPIName($orderNumber){
		$sql = "select top 1		
		(SELECT TOP 1 OPI2_Name
				FROM OperatorInfo2
				WHERE OPI2_OPI_SN = coli_opi_id
					AND OPI2_LGC = 2
				) AS OPIName,
				(select top 1 Gri_No from GRoupInfo where GRI_SN=BIZ_ConfirmLineInfo.COLI_GRI_SN) as griName
				
		FROM InfoManager.dbo.trainsystem
		INNER JOIN BIZ_ConfirmLineInfo ON BIZ_ConfirmLineInfo.COLI_SN = (
				SELECT COLD_COLI_SN
				FROM BIZ_ConfirmLineDetail
				WHERE COLD_SN = InfoManager.dbo.trainsystem.ts_cold_sn
				)
		where ts_ordernumber=?
		";

		$query = $this->HT->query($sql,array($orderNumber));
		return $query->row();
	}
}