<?php

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

class BIZorder_model extends CI_Model {

    var $topnum = false;
    var $orderby = false;
    var $has_pay = false;
    var $search = false;

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

    public function init() {
        $this->topnum = false;
        $this->has_pay = false;
        $this->search = false;
        $this->orderby = ' ORDER BY bcli.COLI_SN DESC ';
    }
	
	//获取首页订单列表ID
    public function get_order_list_ids($coli_id = false, $startdate_bettwen = false, $startdate_and = false) {
		$this->init();
		$this->topnum = 300;
        $this->search = $coli_id == false ? false : " AND bcli.COLI_ID like '%$coli_id%' ";
        $this->search .=($startdate_bettwen == false)&&($startdate_and == false) ? false : " AND (bcld.COLD_StartDate BETWEEN '$startdate_bettwen' AND '$startdate_and') ";
        return $this->get_list();
    }

    public function search_date($date) {
        $this->init();
        $first_date = date('Y-m-01', strtotime($date));
        $last_date = date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day'));
        $this->search = " AND bcli.COLI_ApplyDate BETWEEN '$first_date 00:00:00' AND '$last_date 23:59:59'  ";
        return $this->get_list();
    }

    public function detail_by_cold($cold_sn) {
        $this->init();
        $this->topnum = 1;
        $this->search = " AND bcld.COLD_SN=  " . $this->HT->escape($cold_sn);
        return $this->get_list();
    }

    public function get_list() {

        $this->topnum ? $sql = "SELECT TOP " . $this->topnum : $sql = "SELECT ";
        $sql .= "    
                          bcli.COLI_ID
                          ,bcli.COLI_State
                          ,bcli.COLI_ApplyDate
                          ,bcli.COLI_WebCode
                          ,bcld.COLD_SN
                          ,bcld.COLD_MemoText
                          ,bcld.COLD_StartDate
                          ,bcld.COLD_PersonNum
                          ,bcld.COLD_ChildNum
                          ,bcld.COLD_BabyNum
                          ,bcld.COLD_ServiceSN
                          ,bpi.PAG_Code
                          ,bpi.PAG_NeedTime
                          ,bpoi.POI_Hotel
                            ,bg.GUT_Email
                            ,bg.GUT_FirstName
                            ,bg.GUT_LastName
							,bg.GUT_SEX
                    FROM   BIZ_ConfirmLineInfo bcli
                           LEFT JOIN BIZ_ConfirmLineDetail bcld
                                ON  bcli.COLI_SN = bcld.COLD_COLI_SN
                           LEFT JOIN BIZ_PackageInfo bpi
                                ON  bpi.PAG_SN = bcld.COLD_ServiceSN
                           LEFT JOIN BIZ_PackageOrderInfo bpoi
                                ON  bpoi.POI_COLD_SN = bcld.COLD_SN
                           LEFT JOIN BIZ_GUEST bg
                                ON  bg.GUT_SN = bcli.COLI_GUT_SN
                    WHERE 1=1
                           -- AND bcli.COLI_sourcetype = 32090
                           AND bcli.DeleteFlag = 0
                           AND bcli.COLI_WebCode IN ('CHT')
                 ";
        $this->search ? $sql.=$this->search : false;
        $this->has_pay ? $sql.="   AND EXISTS ( SELECT TOP 1 1
                            FROM   BIZ_GroupAccountInfo  bgai
                            WHERE  bgai.GAI_COLI_SN = bcli.COLI_SN
                                  )
                    ;" : false;
        $this->orderby ? $sql.=$this->orderby : false;
        $query = $this->HT->query($sql);
        //print_r($this->HT->queries);
        if ($this->topnum === 1) {
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row;
            } else {
                return FALSE;
            }
        } else {
            return $query->result();
        }
    }

    function save_mail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $frominfo = '中华游在线', $M_Web = 'chtcdn') {
        $sql = "INSERT INTO Email_AutomaticSend 
                        (
                            M_ReplyToName, 
                            M_ReplyToEmail, 
                            M_ToName, 
                            M_ToEmail, 
                            M_Title, 
                            M_Body, 
                            M_Web,
                            M_FromName, 
                            M_State
                        )
                        VALUES(?, ?, ?, ?, ?, ?, ?, ?, 0) ";
        $query = $this->HT->query($sql, array($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_Web, $frominfo));
        return $query;
    }

}
