<?php

/**
 * 前台 首页 模块 Model
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhaojianjun $  <zwttmac@qq.com>
 * $Id: index.class.php 79 2011-12-15 06:36:06Z zhaojianjun $
 * http://un.zhuna.cn；
 */
class index extends Model
{

    private $db;
    private $hotel_table;
    private $ad_add_table;
    private $article_table;
    private $flink_table;
    private $hotel_left_model;
    private $liansuohotel_model;

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

    /**
     * 取酒店总数/评价总数
     * @return<String>
     */
    public function getHotelCount()
    {
        $sql = "SELECT COUNT(*) AS count FROM $this->hotel_table";
        $hotelcount = $this->db->cacheGetOne($sql, 604800);
        //评价数
        $comment_count = $hotelcount * 5 + 553;
        $result['hotelcount'] = $hotelcount;
        $result['comment_count'] = $comment_count;
        return $result;
    }

    /**
     * 设置首页默认第一的搜索城市
     * @global <type> $cfg_indexCitylist
     * @return <type>
     */
    public function setDefaultCity()
    {
        $cfg_indexCitylist = Tmac::config('configcache.config.cfg_indexCitylist', APP_NAME, '.inc.php');
        $uncityinfo_ary = explode(',', $cfg_indexCitylist);
        $uncityinfo = explode('|', $uncityinfo_ary[0]);
        foreach ($uncityinfo_ary AS $k => $v) {
            if ($k > 8)
                break;
            $unarray = explode('|', $v);
            $uncityinfo_array[$k]['cityname'] = $unarray[1];
            $uncityinfo_array[$k]['cityid'] = $unarray[0];
        }
        $result['uncityinfo_array'] = $uncityinfo_array;
        $result['uncityinfo'] = $uncityinfo;
        return $result;
    }

    /**
     * 取城市地标
     * @param <type> $cityid
     * @param <type> $limit
     * @param <type> $pg
     * @return <type>
     */
    public function citylable($cityid, $limit, $pg = 1)
    {
        $nofind_lable = '';
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');
        $xml_url = $cfg_apiurl . "lable_json.php?cityid=$cityid&classid=0&pg={$pg}";
        $xml_root = VAR_ROOT . "json/citylable/" . $cityid . '/';
        $xml_name = "lable_{$cityid}_0_{$pg}.json";
        $this->hotel_left_model->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'];
            }
        } else {
            $nofind_lable = "暂无此城市地标列表!";
        }

        $result['rs'] = $lable_ary;
        $result['nofind'] = $nofind_lable;
        return $result;
    }

    /**
     * 推荐酒店
     * @param <type> $cityid
     * @param <type> $type
     * @param <type> $limit
     * @return <type> 
     */
    public function getIndexHotelArray($cityid, $type = 'liansuo', $limit = 8)
    {
        $sql = "SELECT hotel_id, hotelname, min_price, cbd_name, cityid, chain_name, chain_id, cbd_id FROM $this->hotel_table ";
        $sql .= "WHERE cityid = $cityid ";
        $sql .= " AND min_price > 0 ";
        if ($type == 'liansuo') {
            $chain_id_array = $this->liansuohotel_model->liansuoCityList($cityid, 8);
            $chain_id_string = '';
            foreach ($chain_id_array AS $k => $v) {
                $chain_id_string .= ',' . $v['chain_id'];
            }
            $chain_id_string = substr($chain_id_string, 1);
            $sql .= " AND chain_id IN($chain_id_string)";
        }
        $sql .= "AND Auditing=1 ";
        if ($type == 'min_price' || $type == 'haohua') {
            $sql .= "AND cbd_id>0 ";
        }
        if ($type == 'min_price') {
            $sql .= "GROUP BY chain_id ";
        }
        if ($type != 'haohua') {
            $sql .= "ORDER BY min_price ASC ";
        } else {
            $sql .= "ORDER BY star, px_index DESC, hotel_id DESC ";
        }

        $sql .= "LIMIT 0, $limit";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

    /**
     * 最新酒店点评
     * @param <type> $limit
     * @return <type>
     */
    public function indexPinglun($limit = 4, $cityid = 0, $isPic = 0)
    {
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');

        $nofind_comment = '';
        $hotel_id_string = '';

        $xml_url = $cfg_apiurl . "comment_json.php?cityid=$cityid";
        $xml_root = VAR_ROOT . "json/comment/{$cityid}/";
        $xml_name = "comment_{$cityid}_0.json";
        $this->hotel_left_model->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) {
                    if ($k == $limit)
                        break;
                    $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 ($isPic == 1) {
                        $hotel_id_string .= ',' . $basexml['hid'];
                    }
                }
            } else {
                $nofind_comment = '此酒店暂无客人评价!';
            }
        } else {
            $nofind_comment = '接口出错,请重试!';
        }

        if ($isPic == 1) {
            $hotel_id_string = substr($hotel_id_string, 1);
            $hotel_pic_array = $this->M('hotelinfo')->getHotelPicArray($hotel_id_string);
            foreach ($hotel_pic_array AS $kk => $vv) {
                $hotel_id = $vv['hotel_id'];
                $result['pic']['' . $hotel_id . ''] = $vv['picture'];
            }
        }

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

    /**
     * 最新酒店问答
     * @global <type> $cfg_webname
     * @param <type> $limit
     * @return <type> 
     */
    public function indexWenda($limit = 4, $cityid = 0)
    {
        $cfg_apiurl = Tmac::config('configcache.config.cfg_apiurl', APP_NAME, '.inc.php');
        $cfg_webname = Tmac::config('configcache.config.cfg_webname', APP_NAME, '.inc.php');
        $cfg_indexurl = Tmac::config('configcache.config.cfg_indexurl', APP_NAME, '.inc.php');
        $nofind_qa = '';
        $xml_url = $cfg_apiurl . "question_json.php?cityid=$cityid";
        $xml_root = VAR_ROOT . "json/question/{$cityid}/";
        $xml_name = "question_{$cityid}_0.json";
        $this->hotel_left_model->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) {
                    if ($k == $limit)
                        break;
                    $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']);
                    }
                }
            } else {
                $nofind_qa = "暂无酒店问答!";
            }
        } else {
            $nofind_qa = "接口读取失败，请重试！";
        }
        $result['nofind'] = $nofind_qa;
        $result['rs'] = $wenda_ary;
        return $result;
    }

    /**
     * 取友情链接
     * @param <type> $type     
     * @param <type> $limit
     * @return <type>
     */
    public function getFlink($type = 'img', $limit = 8)
    {
        $time = time();
        $sql = "SELECT * FROM $this->flink_table ";
        $sql .= "WHERE IF(flink_state_radio = 1, flink_starttime<$time AND flink_endtime>$time, flink_state_radio <> 3) ";
        if ($type == 'img') {
            $sql .= " AND flink_type_radio <> 1 ";
        } else {
            $sql .= " AND flink_type_radio = 1 ";
        }

        $sql .= "ORDER BY flink_order DESC ";
        if ($limit > 0)
            $sql .= "LIMIT 0, $limit";
        $rs = $this->db->getAll($sql);
        if ($type == 'img') {   //如果是图片类型的友链
            foreach ($rs AS $k => $v) {
                if ($v['flink_type_radio'] == 3) {
                    $rs[$k]['img'] = $v['flink_externallinks'];
                } else {
                    $rs[$k]['img'] = $v['flink_uploadfile'];
                }
            }
        }
        return $rs;
    }
    
    /**
     * 淡蓝色模板首页酒店
     */
    public function getOderList($limit = 5)
    {

        $tel_a = array('150', '153', '152', '151', '186');
        for ($j = 0; $j <= 9; $j++) {
            $tel_a[] = '13' . $j;
        }
        $hotel_arr = array(
                           array('hotelname'=>'北京右安门速8酒店','hotelid'=>'21673'),
                           array('hotelname'=>'上海静安宾馆','hotelid'=>'6584'),
                           array('hotelname'=>'上海延安饭店','hotelid'=>'6181'),
                           array('hotelname'=>'深圳富临大酒店','hotelid'=>'5830'),
                           array('hotelname'=>'深圳大梅沙芭堤雅酒店','hotelid'=>'7565'),
                           array('hotelname'=>'杭州卡地亚大酒店','hotelid'=>'7903'),
                           array('hotelname'=>'惠州江景商务酒店','hotelid'=>'9479'),
                           array('hotelname'=>'武汉新龙商务酒店','hotelid'=>'5670'),
                           array('hotelname'=>'武汉纽宾凯国际酒店','hotelid'=>'4521'),
                           array('hotelname'=>'广州南方毅源大酒店','hotelid'=>'4379'),              
        );
        $orderlist = array();
        for ($i = 0; $i < $limit; $i++) {
            $rand_a = rand(0, count($tel_a) - 1);
            $hotel = rand(0, count($hotel_arr) - $i+1);
            if($hotel > 9)
                $hotel = 0 + $i;
            $tel_b = rand(10, 99);
            $tel_c = rand(10, 99);
            $jiang = rand(80, 250);
            
            $tel = $tel_a[$rand_a] . $tel_b . $tel_c . '****';
            $time = date('m-d', time());
            $orderlist[$i]['tel'] = $tel;
            $orderlist[$i]['jiang'] = $jiang;;
            $orderlist[$i]['time'] = $time;
            $orderlist[$i]['hotelid'] = $hotel_arr[$hotel]['hotelid'];
            $orderlist[$i]['hotelname'] = $hotel_arr[$hotel]['hotelname'];
        }
        $date = date('Y-m-d');

        return $orderlist;
    }
}