<?php
/**
junjun.php 
用于测试自动出票中出现的问题。
查询coli_sn来测试。
*/
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class junjun extends CI_Controller{
	
	public function __construct() {
        header("Content-Type: text/html;charset=utf-8");       
        parent::__construct();
        $this->config->load('config');
        $this->order_status_msg=$this->config->item('train_order_status_msg');
        $this->key=JUHE_TRAIN_API_KEY;
        $this->cx_api=JUHE_TRAIN_CX_API;
        $this->dp_api=JUHE_TRAIN_DP_API;
        $this->qxdd_api=JUHE_TRAIN_CANCEL_API;
        $this->pay_api=JUHE_TRAIN_PAY_API;
        $this->refund_api=JUHE_TRAIN_REFUND_API;
        $this->status_api=JUHE_TRAIN_STATUS_API;
        $this->code_zw=$this->config->item('train_zw');
        $this->piaotype=$this->config->item('train_piaotype');
        $this->passportty=$this->config->item('train_passportty');
		$this->balance_api = "http://op.juhe.cn/trainTickets/balance.php";//余额
        $this->load->model("BIZ_train_model");//加载模型	 
    } 
	
	public function test(){
		$arr = array('','');
		print_r($arr);
		echo count($arr);
		if(!empty($arr)){
			echo '123';	
		}
	}
	
	
	public function index(){
		$this->ticketype = 1;
		//筛选出能自动出票的订单
		$auto_pool = $this->BIZ_train_model->auto_check_ticket();
		//print_r($auto_pool);
		$auto_pool = array('0'=>(object)array('COLD_SN'=>'488096935','coli_id'=>'180824444','COLI_State'=>'13'));
		//print_r($auto_pool);
		//创建一个不允许自动出票的国际火车票数组
		$nation_train = array('K19', 'K23', 'Z8701', 'Z8702', 'Z97', 'Z98', 'Z99', 'Z100', 'K9795');
		
		//创建黑名单
		$black_list = $this->config->item('black_list');
		$string = '';
		foreach($auto_pool as $item){
			$this->ticketype = 1;
			$back_message = '';
			$cold_sn = $item->COLD_SN;
			$coli_id = $item->coli_id;
			$back_data = 1;
			
			$people_arr = $this->BIZ_train_model->biz_people($cold_sn);
			$train_info = $this->BIZ_train_model->get_biz_foi($cold_sn);
			/*
			if($item->COLD_SPFS > 1){
				//寄送票
				$back_data = 0;
				$back_message .= '-邮寄不自动出票';
			}
			*/
			//乘客人数大于5人不出票
			if(count($people_arr) > 5){
				$back_data = 0;
				$back_message .= '-乘客人数大于5不自动出票';
			}
			
			//护照号如果在黑名单的就不自动出票
			foreach($people_arr as $people_info){
				if(in_array($people_info->BPE_Passport,$black_list)){
					$back_data = 0;
					$back_message .= '-此用户为黑名单用户，不自动出票';
				}
				
				if(strlen($people_info->BPE_Passport) >= 18){
					$back_data = 0;
					$back_message .= '-护照位数大于18不自动出票';
				}
			}
			
			//单张票价不能大于1000人民币
			if($train_info[0]->adultcost > 1000){
				$back_data = 0;
				$back_message .= '-单价大于1000不自动出票'; 
			}
			
			//如果为国际火车票就不出票
			if(in_array($train_info[0]->FlightsNo, $nation_train)){
				$back_data = 0;
				$back_message .= '-国际火车票不自动出票';
			}
			
			//无座的订单不做出票
			if($train_info[0]->Aircraft == 'WZ'){
				$back_data = 0;
				$back_message .= '-无座不自动出票';
			}
			
			//香港火车不自动出票
			if($train_info[0]->ArrivalAirport == 'XJA' || $train_info[0]->DepartAirport == 'XJA'){
				$back_data = 0;
				$back_message .= '-香港火车不自动出票';
			}
			//print_r($train_info);
			
			//如果刚好是第三十天的订单
			if(($item->COLI_State == '8' || $item->COLI_State == '63')){
				$this->ticketype = 3;
				$time_obj = $this->BIZ_train_model->get_saletime($train_info['0']->DepartAirport_cn);
				if(!empty($time_obj)){
					$saletime = strtotime($time_obj->TST_saletime);
					$now_time = time();
					$sale_diff = (time() - $saletime) / 3600;
					if($sale_diff > 1){
						$back_data = 0;
						$back_message .= '-超过抢票时间';
					}else if($sale_diff <0){
						$back_data = 0;
						$back_message .= '-未到抢票时间';
					}
				}	
			}
			
			if($back_data == 0){
				$string .= '<tr><td>汉特订单号：'.$coli_id.'('.$cold_sn.')'.$back_message.'</td></tr>';
			}else{
				//单个订单提交
				//$this->submit_juhe_order($cold_sn,$coli_id);
				$string .= '<tr><td>汉特订单号：'.$coli_id.'('.$cold_sn.')可以自动出票</td></tr>';
			}
		}
		print_r('<table border="1">'.$string.'</table>');
	}
	
	
	 public function submit_juhe_order($cold_sn,$coli_id) {
		$this->load->model("BIZ_train_model");
        $cold_sn='488079918';//488084043
		//$cold_sn=$this->input->get("order");
        //$bpe_sn=$this->input->get("people");
		//$selectseat=$this->input->get("selectseat");
		//$bpe_sn = '(473118360);
        $data = array();
        $rebakc=array();//返回数据
        $rebakc["status"]=0;
        $rebakc["mes"]="";
        if(!is_numeric($cold_sn)){
            $rebakc["mes"]="订单号是数字";
            echo json_encode($rebakc);
            return false; 
        }
        if(empty($bpe_sn)){
            $rebakc["mes"]="请选择乘客";
            echo json_encode($rebakc);
            return false; 
        }

        $data['train'] = $this->BIZ_train_model->biz_order_detail($cold_sn);
        $data['people_list']=$this->BIZ_train_model->in_bpesn_people_info($bpe_sn);
        if (empty($data['train'])) {
            //显示错误,找不到车次
            $rebakc["mes"]="找不到车次";
            echo json_encode($rebakc);
            return false;

        }
        if (empty($data['people_list'])) {
            //显示错误，找不到用户信息
            $rebakc["mes"]="找不到乘客信息";
            echo json_encode($rebakc);
            return false;
        }

        if (count($data['people_list']) > 5) {
            //显示错误，用户超过五个
            $rebakc["mes"]="乘客不能超过五个";
            echo json_encode($rebakc);
            return false;
        }
        $db_train_zw = $this->config->item('db_train_zw');
        $train_zw = $this->config->item('train_zw');
        $zwcode = $db_train_zw[$data['train']->Aircraft]; //座位简码
        $zwname = $train_zw[$db_train_zw[$data['train']->Aircraft]]; //座位名称
		$black_list = $this->config->item('black_list');
		
        $passengers="";
        foreach ($data['people_list'] as $key => $item) {
            
            //乘客姓名
            $passengersename = $item->BPE_FirstName.$item->BPE_MiddleName.$item->BPE_LastName;
            //将特殊字符转换为正常字符以便于出票
			$passengersename = $this->chk_sp_name($passengersename);
			//乘客类型
            switch ($item->BPE_GuestType) {
                case 1:
                    $piaotype = 1;
                    $piaotypename = "成人票";
                    break;
                case 2:
                    $piaotype = 2;
                    $piaotypename = "儿童票";
                    break;
                default://外国人应该就两种票吧
                    $piaotype = 1;
                    $piaotypename = "成人票";
                    break;
            }
			
			switch ($item->BPE_PassportType){
				case 'Chinese ID':
					$passporttypeseid = "1";
					$passporttypeseidname = "二代身份证";
					break;
				case 'Travel Permit from Hong Kong / Macau':
					$passporttypeseid = "C";
					$passporttypeseidname = "港澳通行证";
					break;
				case 'Travel Permit from Taiwan':
					$passporttypeseid = "G";
					$passporttypeseidname = "台湾通行证";
					break;
				default :
					$passporttypeseid = "B";
					$passporttypeseidname = "护照";
					break;
			}
			
			
            $passportseno = str_replace(' ','',$item->BPE_Passport);
			
			//添加一个判断护照号是否在黑名单
			if(in_array($passportseno,$black_list)){
				$rebakc["mes"]="乘客为黑名单用户";
				echo json_encode($rebakc);
				return false;
			}
			
            $passengers.=',{"passengerid":' . ( ++$key) . ',"passengersename":"' . $passengersename . '","piaotype":"' . $piaotype . '","piaotypename":"' . $piaotypename . '","passporttypeseid":"' . $passporttypeseid . '","passporttypeseidname":"' . $passporttypeseidname . '","passportseno":"' . $passportseno . '","price":"1","zwcode":"' . $zwcode . '","zwname":"' . $zwname . '"}';
        }
        $passengers.="]";
        $passengers = substr($passengers, 1);
        $passengers = "[" . $passengers;
        $url=$this->dp_api;
		if(empty($selectseat)){
			 $post_data=array(
                "key"=>$this->key,
                "user_orderid"=>$cold_sn,//自定义订单号
                "train_date"=>substr($data["train"]->DepartureDate, 0, 10),
                "is_accept_standing"=>"no",
                "from_station_name"=>$data["train"]->DepartAirport_cn,
                "from_station_code"=>$data["train"]->DepartAirport,
                "to_station_code"=>$data["train"]->ArrivalAirport,
                "to_station_name"=>$data["train"]->ArrivalAirport_cn,
                "passengers"=>$passengers,
                "checi"=>$data["train"]->FlightsNo
            );	
		}else{
			$post_data=array(
                "key"=>$this->key,
                "user_orderid"=>$cold_sn,//自定义订单号
                "train_date"=>substr($data["train"]->DepartureDate, 0, 10),
                "is_accept_standing"=>"no",
                "choose_seats"=>$selectseat,
                "from_station_name"=>$data["train"]->DepartAirport_cn,
                "from_station_code"=>$data["train"]->DepartAirport,
                "to_station_code"=>$data["train"]->ArrivalAirport,
                "to_station_name"=>$data["train"]->ArrivalAirport_cn,
                "passengers"=>$passengers,
                "checi"=>$data["train"]->FlightsNo
            );	
		}
        
        return $coli_id.'('.$cold_sn.')可以自动出票';
    } 

	//
	public function count_select(){
		$obj = $this->BIZ_train_model->get_juhe_select();
		//print_r($obj);
		$html = '';
		$html .= '<table border="1">';
		$html .= '<tr><th>序号</th><th>聚合订单号</th><th>出票后信息</th><th>订单原信息</th><th>是否自动出票</th></tr>';
		$i = 1;
		foreach($obj as $item){
			$html .= '<tr><td>'.$i.'</td><td>'.$item->JOL_JuheOrder.'</td>';
			if(isset(json_decode($item->JOL_BackTxt)->passengers)){
				$passengers = json_decode($item->JOL_BackTxt)->passengers;
			}else{
				$passengers = '';
			}
			$ex_obj = '';
			if(!empty($passengers)){
				foreach($passengers as $pass_tiem){
					$ex_obj .= $pass_tiem->cxin;
				}
			}
			if($item->JOL_IsAuto == '1'){
				$item->JOL_IsAuto ='是';
			}else{
				$item->JOL_IsAuto ='否';
			}
			$html .= '<td>'.$ex_obj.'</td><td>'.$item->FOI_SelectedSeat.'</td><td>'.$item->JOL_IsAuto.'</td></tr>';
			$i++;
		}
		$html .= '</table>';
		echo $html;
	}
	
	public function update_juheorder(){
		print_r($this->BIZ_train_model->test());
	}
	
	public function update_state($cold_sn){
		//先更新当前订单
		$flag = $this->BIZ_train_model->update_cold_state($cold_sn);
		if(!$flag){
			log_message('error','状态更新失败:'.$cold_sn);
		}else{
			$coli_sn = $this->BIZ_train_model->cold_sn_get_coli_sn($cold_sn);
			$coli_sn = $coli_sn[0]->COLD_COLI_SN;
			$all_train = $this->BIZ_train_model->get_alltrain($coli_sn);
			$all_count = count($all_train);
			$success_count = 0;
			foreach($all_train as $value){
				if($value->COLD_State == '61'){
					$success_count++;
				}
			}
			if($all_count == $success_count){
				$this->BIZ_train_model->update_coli_state('61',$coli_sn);
			}else{
				$this->BIZ_train_model->update_coli_state('62',$coli_sn);
			}
		}
	}
	
	//测试发送邮件
	public function test_send(){
		$phone = '18677367018';
		$name = 'sw';
		$coli_id = '780258';
		$email = 'sw@hainatravel.com';
		
		$mail_data = array();
		$mail_data['name'] = $name;
		$mail_data['phone'] = $phone;
		$mailtitle = 'Signup successfully on China Highlights Customer Center';
		$mail_body = $this->load->view('train_help',$mail_data,true);
		$fromName = 'China Highlights Customer Center';
		$fromEmail = 'sharon@chinahighlights.net';
		$toName = $name;
		$toEmail = $email;
		$this->load->model("Sendmail_model");
		$this->Sendmail_model->SendMailToTable($fromName, $fromEmail, $toName, $toEmail, $mailtitle, $mail_body);
	}
	
}
?>