<?php

/*!
 * 查询
 */

class Orders_query extends CI_Model {

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

    /** 获取海纳团的发团人 */
    public function get_gri_opi_id($code)
    {
      $gri_sql = "SELECT top 1 GRI_SN,GRI_OPI_ID,isnull(GRI_operator,0) GRI_operator,GRI_No,GRI_Name
                    from GRoupInfo
                    where GRI_Name like '%$code%' ";
      $gri_query = $this->HT->query($gri_sql);
      if ($gri_query->num_rows() > 0) {
        return $gri_query->row()->GRI_operator;
      }
      return 0;
    }

    public function get_order_vendorplan($COLI_ID)
    {
        $sql = "SELECT COLI_GroupCode,COLI_OPI_ID,vas.*
                from BIZ_ConfirmLineInfo coli
                left join VendorArrangeState vas on VAS_GRI_SN=COLI_GRI_SN
                where COLI_GroupCode like '%" . $this->HT->escape_like_str($COLI_ID) . "%'
                            OR COLI_ID like '%" . $this->HT->escape_like_str($COLI_ID) . "%'
                            OR COLI_PriceMemo like '%" . $this->HT->escape_like_str($COLI_ID) . "%'
                ";
        return $this->HT->query($sql)->result();
    }

    public function get_order_detail($GRI_SN)
    {
        $sql = "SELECT
                pag2.PAG2_Name
                ,(select PAG_code from biz_packageinfo where pag_sn=COLD_ServiceSN) as pag_code
                ,pags.PAGS_Direction,pags.PAGS_describ
                ,poi.POI_Hotel,poi.POI_HotelAddress,poi.POI_HotelPhone
                ,poi.POI_AirPort,poi.POI_FlightsNo
                ,GUT_FirstName,GUT_LastName
                ,cold.*,coli.*
                from BIZ_ConfirmLineDetail cold
                inner join BIZ_ConfirmLineInfo coli on COLI_SN=COLD_COLI_SN and cold.DeleteFlag=0
                inner join BIZ_PackageOrderInfo poi on poi.POI_COLD_SN=COLD_SN
                inner join BIZ_GUEST g on g.GUT_SN=COLI_GUT_SN
                left join BIZ_PackageInfo2 pag2 on pag2.PAG2_PAG_SN=COLD_ServiceSN and pag2.PAG2_LGC=1
                left join BIZ_PackageInfoSub pags on pags.PAGS_SN=cold.COLD_ServiceSN2
                where COLI_GRI_SN=$GRI_SN
                order by COLD_StartDate ";
        return $this->HT->query($sql)->result();
    }

    public function get_plan_tourguide($GRI_SN=0)
    {
        $GRI_SN = $GRI_SN==NULL ? 0 : $GRI_SN;
        $sql = "SELECT tgi_info.TGI_SN,tgi_info.TGI2_Name,tgi_info.TGI_Mobile
                        ,eoi.EOI_GetDate,eoi.EOI_Date,eoi.EOI_VEI_SN
                from Eva_ObjectInfo eoi
                left join
                    ( select TGI_SN,TGI_Mobile,TGI2_Name from TouristGuideInfo tgi
                        left join TouristGuideInfo2 tgi2 on TGI2_TGI_SN=TGI_SN and TGI2_LGC=1
                    ) as tgi_info on tgi_info.TGI_SN=eoi.EOI_ObjSN
                where eoi.EOI_Type=3 and EOI_gri_sn=$GRI_SN
                order by eoi.EOI_GetDate ";
        return $this->HT->query($sql)->result();
    }

    function get_package_order($COLI_ID)
    {
      $order_info_sql = "SELECT
                          GCI_SN,GCI_VendorOrderId,GCI_combineNo
                          ,(select PAG_code from biz_packageinfo where pag_sn=COLD_ServiceSN) as pag_code
                          ,COLI_SN,COLI_ID,COLD_SN,COLI_GroupCode,COLI_OPI_ID,COLI_OrderDetailText,COLI_PriceMemo
                          ,COLD_ServiceSN,COLD_PersonNum,COLD_ChildNum,COLD_StartDate,COLD_EndDate,cold.COLD_MemoText
                          ,pags.PAGS_Direction,pags.PAGS_describ
                          ,pag2.PAG2_Name
                          ,poi.POI_Hotel,poi.POI_HotelAddress,poi.POI_HotelPhone
                          ,poi.POI_AirPort,poi.POI_FlightsNo
                          ,GUT_FirstName,GUT_LastName
                          FROM BIZ_ConfirmLineInfo coli
                          inner join GroupCombineInfo on COLI_GRI_SN=GCI_GRI_SN --and GCI_combineNo<>'cancel'
                          inner join BIZ_ConfirmLineDetail cold on COLD_COLI_SN=COLI_SN
                          inner join BIZ_PackageOrderInfo poi on poi.POI_COLD_SN=COLD_SN
                          inner join BIZ_GUEST g on g.GUT_SN=COLI_GUT_SN
                          inner join BIZ_PackageInfo2 pag2 on pag2.PAG2_PAG_SN=COLD_ServiceSN and pag2.PAG2_LGC=1
                          left join BIZ_PackageInfoSub pags on pags.PAGS_SN=cold.COLD_ServiceSN2
                          where COLI_GroupCode like '%" . $this->HT->escape_like_str($COLI_ID) . "%'
                            OR COLI_ID like '%" . $this->HT->escape_like_str($COLI_ID) . "%'
                            OR COLI_PriceMemo like '%" . $this->HT->escape_like_str($COLI_ID) . "%'
                          order by COLD_StartDate asc";
                          // OR COLI_ID like '%" . $this->HT->escape_like_str($COLI_ID) . "%'
      $order_info_query = $this->HT->query($order_info_sql);
      $ret = $order_info_query->result();
      return $ret;
    }

    function get_operator($OPI_SN=0)
    {
      $operator_sql = "SELECT opi.OPI_SN,opi.OPI_Name,opi.OPI_FirstName,OPI_MoveTelephone,OPI_Email,opi2.OPI2_Name
                        from OperatorInfo opi
                        left join OperatorInfo2 opi2 on opi2.OPI2_OPI_SN=OPI_SN and opi2.OPI2_LGC=1
                        where OPI_SN=" . $OPI_SN . " AND OPI_SN<>435";
      return $this->HT->query($operator_sql)->row();
    }

    function get_operation($combineNo)
    {
      $combineNos = my_implode("'",",",$combineNo);
      $operation_sql = "SELECT gcod.*
                        from GroupCombineOperationDetail gcod
                        where GCOD_GCI_combineNo in ($combineNos)
                        and gcod.GCOD_operationType in ('touristCarOperations','guiderOperations')
                        order by GCOD_startDate";
      $operation_info = $this->HT->query($operation_sql, array($combineNo));
      return $operation_info->result();
    }

}
