<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * zsb
 *
 * 业务类库
 *
 * @package         zsb
 * @author          EllisLab Dev Team
 * @copyright       Copyright (c) 2006 - 2014, HOUSE365, Inc.
 * @link            http://nj.sell.house.com
 * @since           Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------
/**
 * 小区类
 *
 * 小区类文件功能描述
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          fisher
 */
class Block_base_xiao_model extends MY_Model 
{ 
    /**
     * 小区表
     * @var string 
     */
    private $_block_tbl = 'block';
    
    /**
     * 小区图库表
     * @var string 
     */
    private $_block_img_tbl = 'block_img';
    
    
    /**
     * 查询字段
     * @var string 
     */
    public $select_fields = '';
    
    /**
     * 设置需要查询的字段
     * @param array $select_fields
     */
    public function set_select_fields($select_fields)
    {
        $select_fields_str = '';
        if(isset( $select_fields ) && !empty($select_fields))
        {   
            $select_fields_str = implode( ',', $select_fields) ;
        }
        $this->select_fields = $select_fields;
    }
    
    /**
     * 获取需要查询的字段
     * @return string
     */
    public function get_select_fields()
    {
        return $this->select_fields;
    }
    
   /**
    * 类初始化
    */
    public function __construct()
    {
        parent::__construct(1,1,2);
    }
    
    /**
     * 以小区编号获取小区详情页的url地址
     * @param int $id 小区编号
     * @return string 小区在web端的地址
     */
    public function find_blockurl_by_id($id)
    {
        return GB_XIAOQU_URL . $id . '/';
    }
    
    /**
     * 获取小区状态描述
     * @param iny $esta 状态位
     * @return string 描述
     */
    public function find_blocktype_by_esta($esta)
    {
        $esta_block = '';
        if($esta == 2)
        {
            $esta_block = "正式小区";
        }
        elseif($esta == 1)
        {
            $esta_block = "临时小区";
        }
        elseif($esta == 0)
        {
            $esta_block = "待审小区";
        }
        return $esta_block;
    }
    
    /**
     * 根据小区id号获取基本信息
     * @param int $id 小区编号
     * @return array 小区基本信息组成的一维数组
     */
    public function find_by_id($id)
    {
        //查询字段
        if ($this->get_select_fields())
        {
            $this->dbback->select($this->get_select_fields());
        }
        //查询条件
        $this->dbback->where('id', $id);
        return $this->dbback->get($this->_block_tbl)->row_array();
    }
    
    /**
     * 根据小区名称号获取基本信息
     * @param string $blockname 小区名称
     * @return int
     */
    public function find_by_blockname($blockname)
    {
        //查询字段
        if ($this->get_select_fields())
        {
            $this->dbback->select($this->get_select_fields());
        }
        $this->dbback->where('blockname', $blockname);
        return $this->dbback->get($this->_block_tbl)->row_array();
    }
    
    
    /**
     * 根据条件获取小区信息
     * @param string $cond_where 查询条件
     * @return array 小区数组
     */
    public function find_by_cond($cond_where)
    {   
        $block_arr = array();
        
        if( $cond_where != '' )
        {
            //查询字段
            if ( $this->get_select_fields() )
            {
                $this->dbback->select($this->get_select_fields());
            }
            
            $this->dbback->where($cond_where);
            $block_arr =  $this->dbback->get($this->_block_tbl)->result_array();
        }
        
        return $block_arr;
    }
    
    /**
     * 根据条件查找户型图的总数
     * @param string $blockname 小区名称
     * @param int $room 室
     * @param int $hall 厅
     * @return int
     */
    public function find_room_img_count($blockname, $room, $hall)
    {
        $where = "b.blockname = '".$blockname. "' AND bi_room = '$room' "
                . "AND bi_hall = '$hall' AND (bi.bi_type = 0 OR bi.bi_type = 9)";
        $this->dbback->where($where);
        $this->dbback->join($this->_block_tbl . ' b', 'bi.bi_blockid = b.id');
        return $this->dbback->count_all_results($this->_block_img_tbl . ' bi');
    }
    
    /**
     * 根据条件查找户型图的记录
     * @param string $blockname 小区名称
     * @param int $room 室
     * @param int $hall 厅
     * @param int $start 查询开始行
     * @param int $limit 数据偏移量
     * @return array
     */
    public function find_room_img($blockname, $room, $hall, $start = 0, $limit = 20)
    {
        //查询字段
        if ($this->select_fields)
        {
            $this->dbback->select($this->select_fields);
        }
        $where = "b.blockname = '".$blockname. "' AND bi_room = '$room' "
                . "AND bi_hall = '$hall' AND (bi.bi_type = 0 OR bi.bi_type = 9)";
        $this->dbback->where($where);
        $this->dbback->join($this->_block_tbl . ' b', 'bi.bi_blockid = b.id');
        return $this->dbback->get($this->_block_img_tbl . ' bi', $start, $limit)
                ->result_array();
    }

    /**
     * 根据条件查找外景图的总数
     * @param string $blockname 小区名称
     * @return int
     */
    public function find_outdoor_img_count($blockname)
    {
        $where = "b.blockname = '".$blockname.
                "' AND ( bi.bi_type = 51  OR bi.bi_type = 52  OR bi.bi_type = 53)";
        $this->dbback->where($where);
        $this->dbback->join($this->_block_tbl . ' b', 'bi.bi_blockid = b.id');
        return $this->dbback->count_all_results($this->_block_img_tbl . ' bi');
    }
    
    /**
     * 根据条件查找外景图的记录
     * @param string $blockname 小区名称
     * @param int $start 查询开始行
     * @param int $limit 数据偏移量
     * @return array
     */
    public function find_outdoor_img($blockname, $start = 0, $limit = 20)
    {
        //查询字段
        /*
        if ($this->select_fields)
        {
            $this->dbback->select($this->select_fields);
        }
        $where = "b.blockname = '".$blockname.
                "' AND ( bi.bi_type = 51  OR bi.bi_type = 52  OR bi.bi_type = 53)";
        $this->dbback->where($where);
        $this->dbback->join($this->_block_tbl . ' b', 'bi.bi_blockid = b.id');
        $this->dbback->order_by('locate(52,bi_type) desc',false);
        print_r($this->dbback);die;
        return $this->dbback->get($this->_block_img_tbl . ' bi', $start, $limit)
                ->result_array();
        */

        $sql = "SELECT bi.id,bi.bi_img,bi.bi_type FROM block_img bi JOIN block b ON b.id = bi.bi_blockid WHERE b.blockname = '".$blockname."' AND ( bi_type = 51 OR bi_type = 52 OR bi_type = 53 ) ORDER BY locate(52, bi_type) DESC LIMIT ".$limit.",".$start;

        $result = $this->dbback->query($sql)->result_array();
        return $result;
    }
    
    /**
     * 以小区名称模糊查询出小区的记录
     * @param string $blockname 小区名称
     * @param int $num 显示数量
     * @return array 以小区信息组成的多维数组
     */
    public function find_like_by_blockname($blockname, $num = 10)
    {
        //查询字段
        if ($this->get_select_fields())
        {
            $this->dbback->select($this->get_select_fields());
        }
        $this->dbback->like('blockname', $blockname, 'after');
        $this->dbback->or_like('bi_s', $blockname, 'after');
        $this->dbback->or_like('bi_spell', $blockname, 'after');
		//$this->dbback->last_query();
        return $this->dbback->get($this->_block_tbl, $num)->result_array();
    }
    
    /**
     * 查找某个小区名称的个数
     * @param string $blockname 小区名称.
     * @return int 总数
     */
    public function find_count_by_blockname($blockname)
    {
        $this->dbback->where('blockname', $blockname);
        return $this->dbback->count_all_results($this->_block_tbl);
    }
   
    
    /**
     * 查找某个小区地址的个数
     * @param string $address 小区地址
     * @return int 总数
     */
    public function find_count_by_address($address)
    {
        $this->dbback->where('address', $address);
        return $this->dbback->count_all_results($this->_block_tbl);
    }
    
    /**
     * 插入小区记录
     * @param array $insert_data 插入数据源数组
     * @return int 成功 返回插入成功后的房源id 失败 false
     */
    public function insert($insert_data)
    {
        if (isset($insert_data[0]) && is_array($insert_data[0]))
        {
            //批量插入
            if ($this->db->insert_batch($this->_block_tbl, $insert_data))
            {
                return $this->db->insert_id();
            }
        } 
        else 
        {
            //单条插入
            if ($this->db->insert($this->_block_tbl, $insert_data))
            {
                return $this->db->insert_id();
            }
        }
        return false;
    }
}

/* End of file block_model.php */
/* Location: ./models/block_model.php */
