<?php

/**
 * 前台 HotelLeft Sidebar 模块 Model
 * ============================================================================
 * ZhunaCMS 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: hotelLeft.class.php 50 2011-12-05 02:56:32Z zhangwentao $
 * http://un.zhuna.cn；
 */
class hotelLeft extends Model
{

    private $db;
    private $point_class_table;   //地标分类
    private $point_info_table;    //地标信息
    private $cbd_table;           //商圈
    private $chain_table;         //连锁酒店信息表
    private $chain_num_table;     //连锁酒店数量信息表
    private $hotel_table;
    private $article_table;
    private $city_table;
    private $keywords_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->point_class_table = DB_PREFIX . 'point_class';
        $this->point_info_table = DB_PREFIX . 'point_info';
        $this->cdb_table = DB_PREFIX . 'cbd';
        $this->chain_table = DB_PREFIX . 'chain';
        $this->chain_num_table = DB_PREFIX . 'chainnum';
        $this->hotel_table = DB_PREFIX . 'hotel';
        $this->article_table = DB_PREFIX . 'article';
        $this->city_table = DB_PREFIX . 'city';
        $this->keywords_table = DB_PREFIX . 'keywords';
    }

    /**
     * 取商业区cbd的
     * @param <int> $cityid
     * @param <int> $limit
     */
    public function cbdList($cityid, $limit)
    {
        $cbd_rs = $this->db->getAll("SELECT * FROM $this->cdb_table WHERE cityid = $cityid ORDER BY hotelnum DESC LIMIT 0, $limit");
        return $cbd_rs;
    }

    /**
     * 取地标附近酒店 api lable_json.php
     * @param <int> $cityid
     * @param <int> $limit
     * @param <string> $name
     * @param <int> $pg
     */
    public function point_info_list($cityid, $limit, $class_id = 0, $pg = 1)
    {
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');
        $xml_url = $cfg_apiurl . "lable_json.php?cityid=$cityid&classid={$class_id}&pg={$pg}";
        $xml_root = VAR_ROOT . "json/citylable/" . $cityid . '/';
        $xml_name = "lable_{$cityid}_{$class_id}_{$pg}.json";
        $this->createXML($xml_url, $xml_root, $xml_name);
        $xmlstring = $xml_root . $xml_name;

        $lable_result = json_decode(file_get_contents($xmlstring), true);
        $lable_ary = array();
        if ($lable_result['retmsg'] == 'ok') {
            foreach ($lable_result['reqdata'] as $k => $basexml) {
                if ($k == $limit)
                    break;
                $lable_ary[$k]['key'] = $basexml['name'];
                $lable_ary[$k]['key_urlencode'] = urlencode($basexml['name']);
                $lable_ary[$k]['cityid'] = $basexml['cityid'];
                $lable_ary[$k]['classid'] = $basexml['classid'];
                $lable_ary[$k]['classname'] = $basexml['classname'];
            }
            shuffle($lable_ary);    //随机排序       
        }
        return $lable_ary;
    }

    /**
     * 创建xml
     * @param <type> $xml_root  //创建的xml本地路径
     * @param <type> $xml_url   //接口xml的地址
     */
    public function createXML($xml_url, $xml_root, $xml_name, $checktime = 2592000)
    {
        $xml_root_all = $xml_root . $xml_name;  //xml接口的本地路径+文件全名
        if ($this->checkXML($xml_root_all, $checktime)) {
            Functions::CreateFolder($xml_root);
            $xml_content = Functions::GetHttps($xml_url);
            if (strlen($xml_content) > 0) {
                $rs = file_put_contents($xml_root_all, $xml_content);
                return $rs;
            } else {
                return false;
            }
        }
    }

    /**
     * 检查xml接口是否已经生成 是否过期
     * @param <type> $xml_root_all
     * @param <type> $time
     * @return <type>
     */
    public function checkXML($xml_root_all, $time)
    {
        if (file_exists($xml_root_all)) {
            if (filesize($xml_root_all) == 0)
                return true;
            $creat_time = filemtime($xml_root_all);    //查看本地xml接口文件的修改时间            
            $time_diff = time() - $creat_time;         //与现在时间的时差
            //如果时间差大于2592000 (1个月)就重新生成一下
            if ($time_diff > $time) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    /**
     * 取 cityid 地标分类
     * @param <type> $limit
     */
    public function pointClassList($parentid = 0)
    {
        $sql = "SELECT class_id, classname, parentid FROM $this->point_class_table ";
        $sql .= "WHERE ParentID = $parentid ORDER BY class_id ASC";
        $point_class_rs = $this->db->cacheGetAll($sql, 86400);
        return $point_class_rs;
    }

    /**
     * 获得某个城市的连锁品牌
     * @param <type> $cityid 城市id
     * @param <type> $limit 取的数量
     * @param <type> $jibie 高端/普通连锁品牌[1/0]
     */
    public function getChain($cityid = 0, $limit = 10, $jibie = 0)
    {
        $sql = "SELECT * FROM $this->chain_table";
        $sql .= " WHERE chain_id IN(SELECT chain_id FROM $this->chain_num_table WHERE cityid=$cityid)";
        $sql .= " AND jibie = $jibie";
        $sql .= " ORDER BY hotelnum DESC";
        $sql .= " LIMIT 0, $limit";
        $chain_rs = $this->db->getAll($sql);
        return $chain_rs;
    }

    /**
     * 获取连锁酒店数量信息表
     * @param int $cityid 默认0全取
     * return array;
     */
    public function getChainnumAry($cityid = 0)
    {
        $sql = "SELECT * FROM $this->chain_num_table";
        if ($cityid != 0)
            $sql .= " WHERE CityId=$cityid";
        $sql .= " ORDER BY chain_num DESC";
        $rs = $this->db->cacheGetAll($sql, 86400);
        return $rs;
    }

    /**
     * 取最受欢迎酒店列表数据
     * @param <type> $cityid
     * @param <type> $limit
     */
    public function getHotelWelcome($cityid=0, $limit=10)
    {
        $sql = "SELECT hotel_id,hotelname,min_price,cityid FROM $this->hotel_table";
        if ($cityid != 0)
            $sql .= " WHERE cityid=$cityid";
        $sql .= " ORDER BY Px_hot DESC";
        $sql .= " LIMIT 0, $limit";
        $hotel_welcome_rs = $this->db->getAll($sql);
        return $hotel_welcome_rs;
    }

    /**
     * 热门旅游资讯
     * @param <type> $cityid
     * @param <type> $title
     * @param <type> $limit
     * @param newstype 0 停止  1正常  2推荐 3图片推荐
     * @return <type>
     */
    public function getNewsView($cityid = 0, $title = null, $limit = 10, $newstype=3)
    {
        $sql = "SELECT title, content, class_id, aid, img FROM $this->article_table WHERE ";
        if ($cityid <> 0)
            $sql .= " CityID=$cityid";
        if ($title != null)
            $sql .= " OR title LIKE '%{$title}%'";
        if ($cityid <> 0 || $title <> null)
            $sql .= " and state_radio=$newstype";
        else
            $sql .= " state_radio=$newstype";
        $sql .= " ORDER BY view_num ASC, aid ASC";
        $sql .= " LIMIT 0, $limit";
        $article_side_rs = $this->db->cacheGetAll($sql, 86400);
        return $article_side_rs;
    }

    /**
     * 取当前城市的cityid
     * @return array
     */
    public function getCityinfo()
    {
        $get = $_GET['cityid'] = empty($_GET['cityid']) ? 0 : intval($_GET['cityid']);
        $post = $_POST['cityid'] = empty($_POST['cityid']) ? 0 : intval($_POST['cityid']);
        $common_model = Tmac::model('common');
        $city = array();
        if (!empty($get) || !empty($post)) {      //附近地标
            $cityid = !empty($get) ? $get : $post;
            $city['cityid'] = (int) $cityid;
            $cityinfo = $common_model->getCityinfo($cityid, 'c');
            $city['cityname'] = $cityinfo['CityName'];
        } else {
            $cityid = HttpResponse::getCookie('cityid');
            if (empty($cityid)) {
                //取默认配置文件中的cityinfo
                $cityinfo_cfg = $common_model->getCfgcity();
                $city['cityid'] = $cityinfo_cfg['cityid'];
                $city['cityname'] = $cityinfo_cfg['cityname'];
            } else {
                $city['cityid'] = HttpResponse::getCookie('cityid');
                $city['cityname'] = HttpResponse::getCookie('cityname');
            }
        }
        return $city;
    }

    /**
     * 取 入店时间 默认时间是今天后的3天
     * @param <type> $tm
     */
    public function getTms($tm)
    {
        if (empty($tm)) { //如果tm1 POST值为空就默认是今天的
            $time_now = date('Y-m-d', time());
            $time_default = date('Y-m-d', time() + 0);
            $tm1_cookies = HttpResponse::getCookie('tm1');
            if (empty($tm1_cookies) || ($tm1_cookies < $time_now)) {  //检测是否有cookies时间 并且时间要大于等于今天
                $rstime = $time_default;    //如果没有cookes tm1 就等于现在的时间
            } else {
                $rstime = $tm1_cookies;     //如果有cookes tm1 就用cookies 里的时间
            }
        } else {
            $rstime = $tm;
        }
        HttpResponse::setCookie('tm1', $rstime);
        return $rstime;
    }

    /**
     * 取 离店时间 默认时间是今天后的6天
     * @param <type> $tm
     */
    public function getTme($tm)
    {
        if (empty($tm)) { //如果tm1 POST值为空就默认是今天的
            $time_now = date('Y-m-d', time());
            $time_default = date('Y-m-d', time() + 259200);
            $tm1_cookies = HttpResponse::getCookie('tm2');
            if (empty($tm1_cookies) || ($tm1_cookies < $time_now)) {  //检测是否有cookies时间 并且时间要大于等于今天
                $rstime = $time_default;    //如果没有cookes tm1 就等于现在的时间
            } else {
                $rstime = $tm1_cookies; //如果有cookes tm1 就用cookies 里的时间
            }
        } else {
            $rstime = $tm;
        }
        HttpResponse::setCookie('tm2', $rstime);
        return $rstime;
    }

    /**
     * 设置左上 搜索块默认值
     */
    public function setLeftSearch()
    {
        $tm1 = $this->getParam('tm1');
        $tm2 = $this->getParam('tm2');
        $tm1 = $this->getTms($tm1);
        $tm2 = $this->getTme($tm2);
        $key = $this->getParam('key');
        if (!empty($key)) {      //附近地标            
            $key = $this->H($key);
        }

        $minprice = $this->getParam('minprice');
        $maxprice = $this->getParam('maxprice');

        $hn = $this->getParam('hn');
        if (!empty($hn)) {
            $hn = $this->H($hn);
        }

        $cityinfo = $this->getCityinfo();
        $minprice = is_numeric($minprice) ? $minprice : '';
        $maxprice = is_numeric($maxprice) ? $maxprice : '';
        if ($maxprice <= $minprice) {
            $minprice = '';
            $maxprice = '';
        }
        //取left sider搜索串 字段的cookies
        $Searchinfo = array(
            'cityid' => $cityinfo['cityid'],
            'cityname' => $cityinfo['cityname'],
            'tm1' => $tm1,
            'tm2' => $tm2,
            'key' => $key,
            'minprice' => $minprice,
            'maxprice' => $maxprice,
            'hn' => $hn
        );
        return $Searchinfo;
    }

    /**
     * @param array $searchinfo默认
     * 设置left sider的默认搜索cookies
     */
    public function getSearchCookies($searchinfo)
    {
        $sk_array = array();
        $sk_array['cityid'] = empty($searchinfo['cityid']) ? HttpResponse::getCookie('cityid') : $searchinfo['cityid'];
        $sk_array['cityname'] = empty($searchinfo['cityname']) ? HttpResponse::getCookie('cityname') : $searchinfo['cityname'];
        $sk_array['tm1'] = empty($searchinfo['tm1']) ? HttpResponse::getCookie('tm1') : $searchinfo['tm1'];
        $sk_array['tm2'] = empty($searchinfo['tm2']) ? HttpResponse::getCookie('tm2') : $searchinfo['tm2'];
        $sk_array['key'] = empty($searchinfo['key']) ? '' : $searchinfo['key'];  //附近地标        
        $sk_array['minprice'] = $searchinfo['minprice'];    //房价范围 min                        
        $sk_array['maxprice'] = $searchinfo['maxprice'];    //房价范围 max        
        $sk_array['hn'] = empty($searchinfo['hn']) ? '' : $searchinfo['hn'];  //酒店名称 max
//        print_r($sk_array);        
        return $sk_array;
    }

    /**
     * 最近浏览酒店 hotel_cookie_array
     */
    public function getHotelCookie()
    {
        $hotel_cookie_array_have = HttpResponse::getCookie('hotel_cookie_array');
        $hotel_cookie_array = explode('{$}', $hotel_cookie_array_have);
        if ($hotel_cookie_array_have <> '') {
            foreach ($hotel_cookie_array AS $k => $v) {
                $hotel_cookie_info = explode('{|}', $v);
                $hotel_cookie_info_array[$hotel_cookie_info[0]] = $hotel_cookie_info[1];
            }
            $hotel_cookie_nofind = '';
        } else {
            $hotel_cookie_info_array = array();
            $hotel_cookie_nofind = '还没有浏览过的酒店!';
        }
        $result['rs'] = $hotel_cookie_info_array;
        $result['nofind'] = $hotel_cookie_nofind;
        return $result;
    }

    /**
     * 根据页面名取页面keywords_info信息
     * @param <type> $k_id
     * @return <type>
     */
    public function getKeywordsInfo($k_id)
    {
        $cfg_webname = Tmac::config('configcache.config.cfg_webname', APP_NAME, '.inc.php');

        $sql = "SELECT k_pagename, k_page, k_title, k_keywords, k_description FROM $this->keywords_table WHERE k_id = $k_id LIMIT 0, 1";
        $keywords_info = $this->db->cacheGetRow($sql, 86400);
        return array($cfg_webname, $keywords_info);
    }

    /**
     * 通过cityname取到cityid
     * @param <type> $cityname
     * @return <type>
     */
    public function getCityid($cityname)
    {
        $sql = "SELECT cityid FROM $this->city_table WHERE cityname like '%{$cityname}%' LIMIT 0,1";
        $cityid = $this->db->cacheGetOne($sql, 432000);
        return $cityid;
    }

}