<?php

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


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

	//根据公司名或者人名查找联系人信息
	public function get_list($map)
	{
        if (!$map['CompanyBN'] && !$map['name']) {
            return array();
        }
        if ($map['CompanyBN']) {
            $map_company=" b.VEI2_CompanyBN like '%".$map['CompanyBN']."%' and ";
        }else{
            $map_company='';
        }
        if ($map['name']) {
            $map_name=" and a.LMI2_Name like '%".$map['name']."%' ";
            $map_name2=" and a.TGI2_Name like '%".$map['name']."%' ";
        }else{
            $map_name='';
            $map_name2='';
        }
        
	   $sql="SELECT b.VEI_SN as companyID,
                    '联系人' as VType,
                    a.LMI_SN,
                    b.VEI2_CompanyBN,
                    a.LMI2_Name as name,
                    a.LMI2_Department as  department,
                    a.LMI_Mobile as mobile,
                    a.LMI_Telephone as telephone,
                    a.LMI_AutoFax as fax,
                    isnull(LMI_Receiver,'') Receiver,
                    isnull(LMI_DefaultContactor,'') DefaultContactor,
                    '' as LMI_QQMSN
               FROM V_Vendor_Info as b 
                    LEFT JOIN V_Link_man_Info as a 
                         on b.VEI_SN = a.LMI_VEI_SN and 
                         a.LGC_LGC = 2  
              WHERE b.DeleteFlag=0 and 
                    b.LGC_LGC = 2  and 
                    $map_company 
                    LMI_SN is not null  
                    $map_name 
             UNION 
            SELECT  b.VEI_SN as companyID,
                    '导游' as VType,
                    a.TGI_SN,
                    b.VEI2_CompanyBN, 
                    a.TGI2_Name as name,
                    '' as department,
                    a.TGI_Mobile as mobile,
                    a.TGI_HomePhone as telephone,
                    '' as fax,
                    '' as Receiver,
                    '' as DefaultContactor,
                    '' as LMI_QQMSN
               FROM V_Vendor_Info as b 
                    LEFT JOIN V_Tourist_guide_Info as a 
                         on b.VEI_SN = a.TGI_VEI_SN  
              WHERE b.DeleteFlag=0 and 
                    b.LGC_LGC = 2 and 
                    $map_company 
                    a.LGC_LGC = 2   
                    $map_name2 
              ORDER BY b.VEI2_CompanyBN,VType";
		
		//$query = $this->ht->query($sql, array($email,$this->site_code));
        $query = $this->ht->query($sql);
		$result=$query->result();
		return $result;
	}

    //获取个人最近订单
    public function get_orders($GRI_Operator,$status,$date1=false,$date2=false,$COLI_ID=false)
    {
        $sended_sql=" AND isnull(COLI_Sended,0)=$status ";
        if ($status=='all') {
            $sended_sql='';
        }
        $ApplyDate_sql='';
        if ($date1) {
            //$ApplyDate_sql=" AND COLI_ApplyDate Between '$date1' AND '$date2' ";
            $ApplyDate_sql=" AND COLI_OrderStartDate Between '$date1' AND '$date2' ORDER BY COLI_OrderStartDate ASC";
        }elseif(!$date1 && $status==5) {
            $default_date=date("Y-m-d",strtotime("-15 day"));
            $ApplyDate_sql=" AND COLI_OrderStartDate>'$default_date' ORDER BY COLI_OrderStartDate ASC";
        }
        $map=" AND (COLI_OPI_ID=$GRI_Operator or exists(SELECT GRI_SN FROM GroupInfo WHERE GRI_SN=COLI_GRI_SN AND GRI_Operator =$GRI_Operator)) ";
        if ($COLI_ID!==false) {
            $sended_sql='';
            $ApplyDate_sql='';
            $map=" AND COLI_ID like '%$COLI_ID%' ";
        }

        $sql="SELECT top 20 COLI_SN As Vkey,
                            COLI_ID,
                            COLI_GRI_SN,
                            (SELECT PTI_Name FROM PriceTimeInfo WHERE PTI_SN=COLI_PTI_SN) PTI_Name,
                            (SELECT SYC2_CodeDiscribe FROM V_System_Code WHERE SYC_SN=COLI_OrderType AND LGC_LGC=2) As COLI_OrderType, 
                            COLI_PersonNum,
                            dbo.GetOrderState(COLI_Sended) COLI_State, 
                            COLI_ApplyDate,
                            COLI_OrderStartDate,
                            DBO.GetOrderSrouce(COLI_WebCode,COLI_SourceType,2) As COLI_OrderSource,
                            COLI_Days,
                            (SELECT OPI_Name FROM OperatorInfo WHERE OPI_SN=isnull(COLI_MovePeople,0)) as COLI_MovePeople ,
							SFI_Location,
							SFI_FileName
                      FROM  ConfirmLineInfo 
							LEFT JOIN SystemFileInfo on SFI_COLI_SN=COLI_SN AND SFI_Type=1018002
                     WHERE  isnull(DeleteFlag,0)=0  
                            AND isnull(COLI_Sended,0)<>0 
                            $map
                            $sended_sql
                            $ApplyDate_sql
                     ";
        $query = $this->ht->query($sql);
        $result=$query->result();
        return $result;
    }

    //地接及导游信息
    public function get_guides($GRI_SN)
    {
        $sql="  SELECT  b.EOI_SN as GuideEOI_SN,
                        a.EOI_SN ,
                        a.EOI_GRI_SN,
                        a.EOI_CII_SN,
                        a.EOI_ObjSN,
                        b.EOI_ObjSN as GuideSN,
                        TGI_Mobile,
                        TGI_HomePhone,
                        dbo.GetGroupName(a.EOI_GRI_SN,2) GroupName,
                        dbo.GetCityName(a.EOI_CII_SN,2) as CityName,
                        dbo.GetVendorName(a.EOI_ObjSN,2) as VendorName,
                        dbo.GetGuideName(b.EOI_ObjSN,2) as GuideName,
                        a.EOI_GetDate,
                        a.EOI_Date
                  FROM  Eva_ObjectInfo as a 
                        left join Eva_ObjectInfo as b on b.EOI_Type=3 and b.EOI_GRI_SN=a.EOI_GRI_SN and b.EOI_VEI_SN = a.EOI_ObjSN and b.EOI_CII_SN=a.EOI_CII_SN 
                        LEFT JOIN V_Tourist_guide_Info on TGI_SN=b.EOI_ObjSN and LGC_LGC=2
                 WHERE  a.EOI_Type = 1 and a.EOI_GRI_SN=$GRI_SN order by a.EOI_GetDate";
        $query = $this->ht->query($sql);
        $result=$query->result();
        return $result;
    }

    //团信息
    public function get_group_info($GRI_SN)
    {
        $sql="SELECT    GRI_SN,
                        GRI_No,
                        GRI_Name,
                        GRI_PersonNum,
                        GRI_Country,
                        GRI_Days,
                        GRI_State,
                        GRI_EntranceDate,
                        GRI_PricePersonNum,
                        GRI_Type,
                        COLI_PersonNum,
                        COLI_ChildNum,
                        COLI_BabyNum,
                        COLI_TwoRoom,
                        COLI_ThreeRoom,
                        COLI_AddBed,
                        COLI_Accompany,
                        COLI_OneRoom,
                        COLI_ApartmentNum,
                        COLI_CruiseOneRoom,
                        COLI_CruiseTwoRoom,
                        COLI_CruiseThreeRoom,
                        COLI_CruiseApartmentNum,
                        COLI_CruiseAddBed
                FROM    GRoupInfo LEFT JOIN ConfirmLineInfo on COLI_GRI_SN=GRI_SN
                WHERE   COLI_GRI_SN=?";
        $query = $this->ht->query($sql, array($GRI_SN));
        $result=$query->result();
        return $result;
    }

    //客人信息
    public function get_customer_info($COLI_SN)
    {
        $sql="SELECT    CUL_IsLinkMan,
                        MEI_FirstName,
                        MEI_LastName,
                        MEI_Gender,
                        MEI_Country,
                        COI2_Country,
                        MEI_PassportNo,
                        MEI_Phone,
                        MEI_Birthday,
                        MEI_ServiceLang,
                        SYC2_CodeDiscribe
                FROM    CUstomerList INNER JOIN MEmberInfo ON CUL_CUI_SN=MEI_SN
                        LEFT JOIN COuntryInfo2 ON COI2_COI_SN=MEI_Country AND COI2_LGC=2
                        LEFT JOIN V_System_Code ON MEI_ServiceLang=SYC_SN AND SYC_Type=102 AND LGC_LGC=2
                WHERE   CUL_COLI_SN=?";
        $query = $this->ht->query($sql, array($COLI_SN));
        $result=$query->result();
        return $result;
    }
	
}