<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Trippest_model extends CI_Model {

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


    /**
     * @description: 根据信息标题获取对应的翰特包价线路内容
     * @param {string} $pagecode 
     * @return: 
     */
    function get_pagdetail($pagcode){
        $sql = " SELECT top 1 P1.PAG_ExtendType, P1.PAG_NeedTime,p2.PAG2_Name,P1.PAG_SN,p2.PAG2_Attraction,
		     P1.PAG_Code,P1.PAG_Title, CItyInfo2.CII2_Name
			,P1.PAG_Type 
			,PAG_Scheme 
			,PAG_GuideLan 
			,PAG2_TimeDetail 
			,p1.PAG_SourceType
		 FROM dbo.BIZ_PackageInfo P1 INNER JOIN
		      dbo.BIZ_PackageInfo2 p2 ON P1.PAG_SN = p2.PAG2_PAG_SN INNER JOIN
		      dbo.CItyInfo2 ON P1.PAG_CII_SN = dbo.CItyInfo2.CII2_CII_SN AND 
		       dbo.CItyInfo2.CII2_LGC=1 
		WHERE (p1.pag_dei_sn=32) AND
		      (p2.PAG2_LGC = 1)  
              AND (isnull(P1.DeleteFlag,0)<>1) 
            AND  (p1.PAG_Code = ?) ";
            
			// AND (p2.PAG2_Check = 2) 
            // AND (isnull(P1.DeleteFlag,0) <>1) ";
        $query = $this->HT->query($sql,array($pagcode));
        if ($query->num_rows()>0){
            return $query->row();
        }else{
            return null;
        }

    }

    /**
     * @description: 根据一日游code获取对应人数的成人价格
     * @param {string} $pagecode 线路代码
     * @param int $personnum 人数 
     * @return: 
     */
    function get_price($pagcode,$personnum){
        $sql = "select top 1 isnull(PKP_AdultNetPrice,0) as PKP_AdultNetPrice,isnull(PKP_AdultSpecialPrice,0) as PKP_AdultSpecialPrice
        from BIZ_PackagePrice 
        inner join BIZ_PackageInfo on BIZ_PackageInfo.PAG_SN = BIZ_PackagePrice.PKP_PAG_SN 
        where BIZ_PackagePrice.Checked=2
        and (BIZ_PackageInfo.PAG_Code = ?) 
        AND (pag_dei_sn=32) 
        and ? between PKP_PersonStart and PKP_PersonStop 
        and CONVERT(varchar(12),getdate(),111) between pkp_validdate and pkp_invaliddate 
        order by PKP_PriceGrade  ";

        $param = array($pagcode,$personnum);

        $query = $this->HT->query($sql,$param);

        if ($query->num_rows()>0){
            return $query->row();
        }else{
          $sql = " select top 1 isnull(PKP_AdultNetPrice,0) as PKP_AdultNetPrice,isnull(PKP_AdultSpecialPrice,0) as PKP_AdultSpecialPrice

          from BIZ_PackagePrice 
          inner join BIZ_PackageInfo on BIZ_PackageInfo.PAG_SN = BIZ_PackagePrice.PKP_PAG_SN 
          where BIZ_PackagePrice.Checked=2
          and (BIZ_PackageInfo.PAG_Code = ?) 
          AND (pag_dei_sn=32) 
          and ? between PKP_PersonStart and PKP_PersonStop 
          order by PKP_AdultNetPrice desc  ";  
          $query = $this->HT->query($sql,$param);
          if ($query->num_rows()>0){
              return $query->row();
          }else
          {
              return null;
          }

        }

    }

    /**
     * @description: 获取对应的接送产品列表
     * @param {type} 
     * @return: 
     */
    function GetTransfer($PagSn){
        $sql = "SELECT p2.PAG2_Name
                    ,P1.PAG_SN
                    ,p2.PAG2_Attraction
                    ,P1.PAG_Code
                FROM dbo.BIZ_PackageInfo P1
                INNER JOIN dbo.BIZ_PackageInfo2 p2 ON P1.PAG_SN = p2.PAG2_PAG_SN
                WHERE p2.PAG2_LGC = 108
                    AND P1.PAG_SN IN (
                        SELECT BTE_Trade
                        FROM BIZ_PackageTree
                        WHERE BTE_TradeType = 11
                            AND isnull(BTE_ParentID, 0) = 0
                            AND BTE_Trade = ?                        
                        UNION                        
                        SELECT BTE_Trade
                        FROM BIZ_PackageTree
                        WHERE BTE_ParentID = (
                                SELECT BTE_ID
                                FROM BIZ_PackageTree
                                WHERE BTE_TradeType = 11
                                    AND isnull(BTE_ParentID, 0) = 0
                                    AND BTE_Trade = ?
                                )
                        )  order by PAG2_Order desc ";
                
        $param = array($PagSn, $PagSn);
        $query = $this->HT->Query($sql,$param);                        
        return $query->result();

    }
}

/* End of file Trippest_model.php */
