<?php

/**
 * model\growth\GrowthBlockImprove.php
 */

/**
 * 小区类
 *
 * @author bsw
 */
class GrowthBlock {
    /**
     *
     * @var TINY_ADODB 主数据库 
     */    
    private $db;
    
    /**
     *
     * @var type 从数据库
     */
    private $dbback;
    
    /**
     *
     * @var type 缓存对象
     */
    private $mc;

    /**
     *
     * @var String 关联的表名 
     */
    private $table;

    /**
     * 构造函数，初始化db memcache 等。
     * @param string $table 表名，可以使用默认值。
     */
    public function __construct($table=null) {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = 'block';
    }


	/**
     * 获取小区完善已经开放的属性数据
	 * @param array  $attrs  小区属性字段 
     * @param int  $id  小区id  
     * return array
	 */
	 function getBlockAtttOpen($attrs,$id){
		if(!$attrs || !is_array($attrs) || !$id) return array();
        
		$attr_info = array();
		$block_info = $this->getBlockInfoById($id);
		foreach($attrs as $val){
			$attr_info[$val] = $block_info[$val];
		}
		return $attr_info;
	 }


	 	/**
     * 查询小区总锁定信息
     * @param int  $block_id  小区id  
     * return array
	 */
	 function getBlockLockInfo($block_id){
		if(!$block_id) return array();
		$sql = SqlCombinHelper::select_sql('block',array('id,lock_flag,lock_opennum'), "id = '".$block_id."'");
		$result = $this->dbback->getRow($sql);
        $result = $result ? $result : array();
		return $result;
	 }

	/**
     * 获取我熟悉的小区总数 
     * @param array  $blockIds  小区id  
     * return int
	 */
	 function getfamiliarBlockCount($blockIds){
		if(!is_array($blockIds) || !$blockIds) return 0;

		$block_ids_str = implode(',',$blockIds);
		if(!$block_ids_str) return 0;

		$sql = SqlCombinHelper::select_sql('block',array('count(id) as num'), " id in ($block_ids_str) and esta = 2 ");
		$result = $this->dbback->getOne($sql);
        $result = $result ? $result : 0;
		return $result;
	 }

	/**
     * 根据板块id获取小区板块 
     * @param int  $district  区属id  
     * return array
	 */
	 function getStreetByDistrict($district,$type=null){
		if(!$district) return array();
 
		$sql = SqlCombinHelper::select_sql('street',array('id,streetname'), " district='{$district}' ");
		$result = $this->dbback->getAll($sql);
		$street = array();
		if($type == 1){
			$street[''] = '请选择';
		}
        if(is_array($result)){
			foreach($result as $val){
				$street[$val['id']] = $val['streetname'];
			}
		}
		return $street;
	 }

	/**
     * 根据id获取小区信息
     * @param int  $id  小区id  
     * @param array $attr 小区属性
     * return array
	 */
	 function getBlockInfoById($id,$attr=null){
		if(!$id) return array();
		$attr = $attr ? $attr : array('*');
		$sql = SqlCombinHelper::select_sql('block',$attr, "id = '".$id."'");
		$result = $this->dbback->getRow($sql);
		return $result;
	 }


    /**
     * 根据经纪人agentcode获取板块
     * @param int  $agentcode   经纪人agentcode  
     * return int string
	 */
	function getBlockStreetByAgentCode($agentcode){
		if(!$agentcode) return '';
		$sql = "select streetid from agency  where  agentcode='{$agentcode}'";

         //获取缓存
		$result = $this->mc->get($sql);
		if($result === false){
			$result = $this->dbback->getOne($sql);
			$this->mc->set($sql,$result,3600);
		}
		return $result;
	 }

	/**
     * 根据板块id获取相应区属小区总数(排查我有房源小区)
     * @param int  $streetid   板块id 
	 * @param string  $block_ids_str   我有房源小区id
     * return int
	 */
	 function getBlockCountByStreetId($streetid,$block_ids_str){
		if(!$streetid) return 0;
		$sql_in = $block_ids_str ? " and id not in ($block_ids_str) " : "";

		$sql = "select count(id) as num from block where streetid='{$streetid}' and esta=2 {$sql_in} ";
		$result = $this->dbback->getOne($sql);
		return $result;
	 }

	/**
     * 根据板块id获取相应区属小区列表(排查我有房源小区)
     * @param int  $streetid   板块id 
	 * @param string  $block_ids_str   我有房源小区id
     * return int
	 */
	 function getBlockByStreetId($streetid,$block_ids_str,$start,$limit){
		if(!$streetid) return array();
		$sql_in = $block_ids_str ? " and id not in ($block_ids_str) " : "";

		$sql = "select id,blockname,district,lock_opennum,lock_outside,lock_flag from block where   streetid = '{$streetid}' and esta=2 {$sql_in} order by lock_opennum desc,id desc limit {$start},{$limit}";
		$result = $this->dbback->getAll($sql);
		return $result;
	 }

	/**
     * 根据板块id获取版块信息
     * @param int  $streetid   板块id 
     * return array
	 */
	 function getStreetById($streetid){
		if(!$streetid) return array();
		$sql = "select streetname  from street where  id = '{$streetid}'";

        //获取缓存
		$result = $this->mc->get($sql);
		if($result === false){
			$result = $this->dbback->getRow($sql);
			$this->mc->set($sql,$result,3600);
		}
		return $result;
	 }


         
}

?>
