<?php

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

    private $db;
    private $city_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->city_table = DB_PREFIX . 'city';
    }

    /**
     * 取城市酒店问答 api question_json.php
     * @param <int> $cityid     
     */
    public function question_info($cityid)
    {
        $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_wenda = '';
        $pages = $this->P('Pages_Ajax');
        $page = $pages->getNowPage();

        $url = array('question', $cityid);
        $xml_url = $cfg_apiurl . "question_json.php?cityid=$cityid&pg={$page}";
        $xml_root = VAR_ROOT . "json/question/" . $cityid . '/';
        $xml_name = "question_{$cityid}_0_{$page}.json";
        $this->M('hotelLeft')->createXML($xml_url, $xml_root, $xml_name, 604800);
        $xmlstring = $xml_root . $xml_name;
        $page = '';
        $lable_result = json_decode(file_get_contents($xmlstring), true);
        $wenda_ary = array();
        if ($lable_result['retmsg'] == 'ok') {
            $totalput = $lable_result['retHeader']['totalput'];
            $pages->setTotal($totalput);
            $pages->setUrl($url);
            $pages->setPrepage(10);
            $pages->getSqlLimit();
            $page = $pages->show();
            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('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']);  
                }
            } else {
                $nofind_wenda = "暂无酒店问答!";
            }
        } else {
            $nofind_wenda = "接口读取失败，请重试！";
        }

        $result['rs'] = $wenda_ary;
        $result['nofind'] = $nofind_wenda;
        $result['page'] = $page;
        return $result;
    }

    /**
     * 取所有城市字母开头
     * @return <type>
     */
    public function getCityABCD()
    {
        $sql = "SELECT DISTINCT ABCD FROM $this->city_table ORDER BY ABCD ASC";
        $rs = $this->db->cacheGetAll($sql, 108000);
        return $rs;
    }

    public function getCityAll()
    {
        $sql = "SELECT cityid, cityname, abcd FROM $this->city_table ORDER BY ABCD ASC, hotelnum DESC";
        $rs = $this->db->cacheGetAll($sql, 108000);
        $city_array = array();
        foreach ($rs AS $k => $v) {
            $city_array[$v['abcd']][$k]['cityname'] = $v['cityname'];
            $city_array[$v['abcd']][$k]['cityid'] = $v['cityid'];
        }
        return $city_array;
    }

}