<?php

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

    private $db;
    private $chain_table;
    private $chainnum_table;
    private $hotel_table;

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

    /**
     * 取连锁酒店信息
     * @param <type> $chain_id 
     */
    public function getChainInfo($chain_id)
    {
        $sql = "SELECT * FROM $this->chain_table WHERE chain_id = $chain_id LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        return $rs;
    }

    /**
     * 取城市所有连锁酒店列表
     * @param <type> $cityid
     * @param <type> $limit
     * @return <Array>
     */
    public function liansuoCityList($cityid, $limit = 0)
    {
        $sql = "SELECT a.chain_id, a.chain_num, b.chain_name FROM $this->chainnum_table a LEFT JOIN $this->chain_table b ON a.chain_id = b.chain_id WHERE a.cityid = $cityid AND a.chain_num > 0 ORDER BY a.chain_num DESC, b.hotelnum desc";
        if ($limit <> 0)
            $sql .= " LIMIT 0, $limit";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

    /**
     * 取城市取所有连锁酒店列表
     * @param <type> $chain_id
     * @param <type> $cityid
     * @return <Array>
     */
    public function gethotellistByCity($chain_id, $cityid)
    {
        if ($chain_id == 0) {
            $chain_id_sql = '';
        } else {
            $chain_id_sql = " AND b.chain_id = $chain_id";
        }
        $sql = "SELECT a.chain_id, a.chain_num, b.chain_name, b.picture FROM $this->chainnum_table a LEFT JOIN $this->chain_table b ON a.chain_id = b.chain_id WHERE a.cityid = $cityid {$chain_id_sql} AND a.chain_num > 0 ORDER BY a.chain_num DESC, b.hotelnum desc";
        $chain_rs = $this->db->getAll($sql);
        foreach ($chain_rs AS $k => $v) {
            $sql = "SELECT hotel_id, hotelname, picture, min_price, address, haoping, cityid FROM $this->hotel_table ";
            $sql .= "WHERE Auditing = 1 AND cityid = $cityid AND chain_id = {$v['chain_id']} ORDER BY px_index DESC ";
            $sql .= "LIMIT 0, 4";
            $chain_rs[$k]['hotel_array'] = $this->db->getAll($sql);
        }
        return $chain_rs;
    }

    /**
     * 取连锁酒店的所有城市列表
     * @param <type> $chain_id
     * @return <type>
     */
    public function liansuoCityListByChainid($chain_id)
    {
        $sql = "SELECT cityid, cityname, chain_num FROM $this->chainnum_table WHERE chain_id=$chain_id AND chain_num>0 ORDER BY chain_num DESC";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

    /**
     * 取连锁酒店所有城市列表
     * @param <type> $chain_id
     * @param <type> $cityid
     * @return <array>
     */
    public function gethotellistByChain_id($chain_id, $cityid)
    {
        if ($cityid == 0) {
            $cityid_sql = '';
        } else {
            $cityid_sql = " AND a.cityid = $cityid";
        }

        if ($cityid <> 0 && $chain_id <> 0) {
            $limit = "";
        } else {
            $limit = "LIMIT 0, 4";
        }
        $sql = "SELECT a.chain_id, a.chain_num, a.cityid, a.cityname, b.chain_name, b.chain_shortname, b.picture FROM $this->chainnum_table a LEFT JOIN $this->chain_table b ON a.chain_id = b.chain_id WHERE a.chain_id = $chain_id {$cityid_sql} AND a.chain_num > 0 ORDER BY a.chain_num DESC";
        $chain_rs = $this->db->getAll($sql);
        foreach ($chain_rs AS $k => $v) {
            $sql = "SELECT hotel_id, hotelname, picture, min_price, address, haoping, cityid FROM $this->hotel_table ";
            $sql .= "WHERE Auditing = 1 AND cityid = {$v['cityid']} AND chain_id = {$v['chain_id']} ORDER BY px_index DESC ";
            $sql .= $limit;
            $chain_rs[$k]['hotel_array'] = $this->db->getAll($sql);
        }
        return $chain_rs;
    }

}