<?php

/**
 * 前台 酒店祥细信息 模块 Model
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhaojianjun $  <zwttmac@qq.com>
 * $Id: hotelinfo.class.php 156 2012-02-15 02:03:13Z zhaojianjun $
 * http://un.zhuna.cn；
 */
class hotelinfo extends Model
{

    private $db;
    private $hotel_table;
    private $hotel_left_model;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->hotel_table = DB_PREFIX . 'hotel';
        $this->hotel_left_model = Tmac::model('hotelLeft');
    }

    /**
     * 获取资讯栏目
     * @param <int> $hotel_id
     * return $rs
     */
    public function getHotelInfo($hotel_id)
    {
        $sql = "SELECT * FROM $this->hotel_table WHERE hotel_id = '$hotel_id' LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        if (!$rs) {
            $this->redirect('对不起！您所查到酒店不存在！', PHP_SELF);
            exit;
        }
        return $rs;
    }

    /**
     * 酒店图片组合成数组
     * @param <type> $hotelpictures
     * @return <type>
     */
    public function hpicshow($hotelpictures,$limit=5)
    {
    	$limit = 5;
        $hotelpictures_array = explode('@_@', $hotelpictures);
        $pic_ary = explode(',', $hotelpictures_array[0]);
        $result = array();
        foreach ($pic_ary AS $k => $v) {
            if ($k >= $limit)
                continue;        	
            $pic_array = explode(':', $v);
            $pic_all_array = explode('|', $pic_array[1]);
            $result[$k]['pic_name'] = $pic_array[0];
            $result[$k]['pic_thumb_url'] = $pic_all_array[0];
            $result[$k]['pic_url'] = $pic_all_array[1];

        }
        return $result;
    }

    public function tags_ary($tags)
    {
        if (!empty($tags)) {
            $tags = str_replace('$', ',', $tags);
            $tags_ary = explode(',', $tags);
        } else {
            $tags_ary = '';
        }
        return $tags_ary;
    }

    /**
     * 格式化服务设施
     * @param <string> $idstr
     */
    public function Select_Info($idstr)
    {
        $str_count = strlen($idstr);
        $find_str = strrpos($idstr, ",");
        if ($find_str + 1 == $str_count) {
            $idstr = substr($idstr, 0, $str_count - 1);
        }
        $idstr = str_replace(",,", "", $idstr);
        $idstr = str_replace(",", "、", $idstr);
        echo $idstr;
    }

    /**
     * 取酒店评论 api comment_json.php
     * @param <int> $hotel_id
     * @param <int> $limit
     * @param <string> $name
     */
    public function comment_list($hotel_id, $limit)
    {
        $nofind_comment = '';
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');

        $xml_url = $cfg_apiurl . "comment_json.php?hid=$hotel_id";
        $xml_root = VAR_ROOT . "json/comment/hotel/";
        $xml_name = "comment_{$hotel_id}.json";
        $this->M('hotelLeft')->createXML($xml_url, $xml_root, $xml_name, 604800);
        $xmlstring = $xml_root . $xml_name;

        $comment_ary = array();
        $lable_result = json_decode(file_get_contents($xmlstring), true);
        if ($lable_result['retmsg'] == 'ok') {
            if (count($lable_result['reqdata']) > 0) {
                foreach ($lable_result['reqdata'] as $k => $basexml) {
                    $comment_ary[$k]['yonghu'] = $basexml['username'];
                    $comment_ary[$k]['addtime'] = date('Y-m-d H:i:s', $basexml['time']);
                    $comment_ary[$k]['hotel_id'] = $basexml['hid'];
                    $comment_ary[$k]['hotelname'] = $basexml['hotelname'];
                    $comment_ary[$k]['cityid'] = $basexml['cityid'];
                    $comment_ary[$k]['content'] = $basexml['content'];
                    if ($k == $limit)
                        break;
                }
            } else {
                $nofind_comment = '此酒店暂无客人评价!';
            }
        } else {
            $nofind_comment = '接口出错,请重试!';
        }

        $result['rs'] = $comment_ary;
        $result['nofind'] = $nofind_comment;
        return $result;
    }

    /**
     * 取附近酒店hotel_id
     * @param <type> $NearbyHotels
     * @param <type> $limit
     * @param <type> $asc
     */
    public function getNearbyHotels($NearbyHotels, $limit, $type='asc')
    {
        $NearbyHotels_array = explode(',', $NearbyHotels);
        $NearbyHotels_ary = array();
        $NearbyHotels_info_array = array();
        foreach ($NearbyHotels_array AS $k => $v) {
            $NearbyHotels_ary = explode(':', $v);
            $NearbyHotels_info_array[$k]['hotel_id'] = $NearbyHotels_ary[0];
            $NearbyHotels_info_array[$k]['juli'] = $NearbyHotels_ary[1];
            $NearbyHotels_hotel_id_ary[] = $NearbyHotels_ary[0];
            $NearbyHotels_juli_ary[] = $NearbyHotels_ary[1];
            $juli = round(($NearbyHotels_ary[1] / 1000), 2);
            $NearbyHotels_array_key[$NearbyHotels_ary[0]] = $juli;
        }
        array_multisort($NearbyHotels_juli_ary, SORT_ASC, $NearbyHotels_info_array);
        $NearbyHotels_hotel_id = implode(',', $NearbyHotels_hotel_id_ary);
        $NearbyHotels_juli = implode(',', $NearbyHotels_juli_ary);
        $sql = "SELECT hotel_id, hotelname, address, picture, mappoint, min_price, cityid FROM $this->hotel_table WHERE hotel_id in($NearbyHotels_hotel_id) ";
        if ($type == 'asc') {
            $sql .= "ORDER BY instr('{$NearbyHotels_hotel_id}',`hotel_id`) ";
        }
        $sql .= "LIMIT 0,{$limit}";
        $rs = $this->db->getAll($sql);
        $NearbyHotels_hotel_id_string = implode(',', $NearbyHotels_hotel_id_ary);
        //jsItem js数据
        $jsItem = null;
        $cfg_rewrite_pic = Tmac::config('configcache.config.cfg_rewrite_pic', APP_NAME, '.inc.php');
        $picurl = !empty($cfg_rewrite_pic) ? 'http://' . $_SERVER['HTTP_HOST'] . '/pic.php?p=' : $GLOBALS['TmacConfig']['config']['picurl'];
        foreach ($rs as $kk => $vv) {
            $nearbyHotels_ary_b[$kk]['hotel_id'] = $hotel_id = $vv['hotel_id'];
            $nearbyHotels_ary_b[$kk]['hotelname'] = $hotelname = $vv['hotelname'];
            $nearbyHotels_ary_b[$kk]['hotelname_strlen'] = Functions::cut_str($vv['hotelname'], 12);
            $nearbyHotels_ary_b[$kk]['picture'] = $picture = $picurl . $vv['picture'];
            $nearbyHotels_ary_b[$kk]['mappoint'] = $mappoint = $vv['mappoint'];
            $nearbyHotels_ary_b[$kk]['address'] = $address = $vv['address'];
            $nearbyHotels_ary_b[$kk]['min_price'] = $min_price = $vv['min_price'];
            $nearbyHotels_ary_b[$kk]['cityid'] = $min_price = $vv['cityid'];
            $nearbyHotels_ary_b[$kk]['juli'] = $NearbyHotels_array_key[$hotel_id];
            $jsItem .= ",[{$hotel_id},'{$hotelname}','{$picture}','{$mappoint}','{$address}','{$min_price}','9']";
        }
        if ($type == 'asc') {
            $jsItem = substr($jsItem, 1);
            $result['jsItem'] = $jsItem;
            $result['NearbyHotels_array_key'] = $NearbyHotels_array_key;
            $result['NearbyHotels_hotel_id_string'] = $NearbyHotels_hotel_id_string;
            $result['rs'] = $nearbyHotels_ary_b;
        } else {
            $result['rs'] = $nearbyHotels_ary_b;
        }
        return $result;
    }

    /**
     * 获取酒店问答
     * @param <type> $hotel_id
     * @param <type> $limit     
     * @return string
     */
    public function qa_list($hotel_id, $limit)
    {
        $nofind_qa = '';
        $cfg_webname = Tmac::config('configcache.config.cfg_webname', APP_NAME, '.inc.php');
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');
        $cfg_indexurl = Tmac::config('configcache.config.cfg_indexurl', APP_NAME, '.inc.php');

        $xml_url = $cfg_apiurl . "question_json.php?hid=$hotel_id";
        $xml_root = VAR_ROOT . "json/question/hotel/";
        $xml_name = "question_{$hotel_id}.json";
        $this->M('hotelLeft')->createXML($xml_url, $xml_root, $xml_name, 604800);
        $xmlstring = $xml_root . $xml_name;
        $lable_result = json_decode(file_get_contents($xmlstring), true);
        $wenda_ary = array();
        if ($lable_result['retmsg'] == 'ok') {
            if (count($lable_result['reqdata']) > 0) {
                foreach ($lable_result['reqdata'] as $k => $basexml) {
                    $wenda_ary[$k]['yonghu'] = $basexml['username'];
                    $wenda_ary[$k]['addtime'] = date('Y-m-d H:i:s', $basexml['updatetime']);
                    $wenda_ary[$k]['hotel_id'] = $basexml['hid'];
                    $wenda_ary[$k]['hotelname'] = $basexml['hotelname'];
                    $wenda_ary[$k]['cityid'] = $basexml['cityid'];
                    $content = $basexml['content'];
                    $content = explode('{un}', $content);
                    $wenda_ary[$k]['question'] = $content[0];
                    $check_have_webname = strpos($content[1], '住哪网');
                    if ($check_have_webname !== false) {
                        $wenda_ary[$k]['answer'] = str_replace('住哪网', $cfg_webname, $content[1]);
                    } else {
                        $wenda_ary[$k]['answer'] = $content[1];
                    }
                    $check_have_url = strpos($wenda_ary[$k]['answer'], 'www.zhuna.cn');
                    if ($check_have_webname !== false) {
                        $wenda_ary[$k]['answer'] = str_replace('http://www.zhuna.cn', $cfg_indexurl, $wenda_ary[$k]['answer']);
                    }
                    $wenda_ary[$k]['answer'] = str_replace('400-6665511', '400-601-6535', $wenda_ary[$k]['answer']);
                    $wenda_ary[$k]['answer'] = str_replace('400-666-5511', '400-601-6535', $wenda_ary[$k]['answer']);                     
                    if ($k == $limit)
                        break;
                }
            } else {
                $nofind_qa = "暂无此酒店问答!";
            }
        } else {
            $nofind_qa = "接口读取失败，请重试！";
        }
        $result['rs'] = $wenda_ary;
        $result['nofind'] = $nofind_qa;
        return $result;
    }

    public function cookiehotel($hotel_id, $hotelname)
    {
        //取cookie中的hotel_cookie_array
        $hotel_cookie_array_have = HttpResponse::getCookie('hotel_cookie_array');

        $hotel_cookie = $hotel_id . '{|}' . $hotelname;

        if ($hotel_cookie_array_have <> '') {
            $hotel_cookie_array_string = $hotel_cookie . '{$}' . $hotel_cookie_array_have;
        } else {
            $hotel_cookie_array_string = $hotel_cookie;
        }
        $hotel_cookie_array = explode('{$}', $hotel_cookie_array_string);

        foreach ($hotel_cookie_array AS $k => $v) {
            $hotel_cookie_info = explode('{|}', $v);
            $hotel_cookie_info_array[$hotel_cookie_info[0]] = $hotel_cookie_info[1];
        }

        $i = 1;
        $hotel_cookie_info_string = '';
        foreach ($hotel_cookie_info_array AS $k => $v) {
            if ($i > 10)
                break;
            $hotel_cookie_info_string .= '{$}' . $k . '{|}' . $v;
            $hotel_array[$k] = $v;
            $i++;
        }
        $hotel_cookie_info_string = substr($hotel_cookie_info_string, 3);
        HttpResponse::setCookie('hotel_cookie_array', $hotel_cookie_info_string);
    }

    /**
     * 根据酒店ID取酒店图片
     * @param type $hotel_id_string
     * @return type 
     */
    public function getHotelPicArray($hotel_id_string)
    {
        $sql = "SELECT hotel_id, picture FROM $this->hotel_table WHERE hotel_id IN($hotel_id_string)";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

}