<?php

/**
 * 小区
 */

/**
 * 小区
 *
 * @author Administrator
 */
class Block {
    /**
     *
     * @var TINY_ADODB 主数据库
     */
    private $db;
    /**
     *
     * @var TINY_ADODB 从数据库
     */
    private $dbback;
    /**
     *
     * @var Memcache 缓存
     */
    private $mc;
    /**
     *
     * @var string 表名
     */
    private $table;
    /**
     *
     * @var 表中字段数组 
     */
    private $fields;
    
    /**
     * 初始化类
     * @param string $table 表名，默认为growth_bid_periods
     */
    public function __construct($table = 'block') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = $table;
        $sqlfield = "DESC $this->table";
        $fields = $this->dbback->getAll($sqlfield);
        foreach ($fields as $field) {
            $this->fields[] = $field['Field'];
        }
    }

    /**
     * 
     * @param type $blockId
     * @return type
     */
    public function findOneBy($blockId) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,"id='$blockId'");
        return $this->dbback->GetRow($sql);
    }
    
    /**
     * 
     * @param type $blockname
     * @return type
     */ 
    public function findAllEstaBlockId() { 
        $sql = SqlCombinHelper::select_sql($this->table, 'id', 'esta = 2');
        $blockIdNew = $this->mc->get($sql);
        if (!$blockIdNew) {
            $blockId = $this->dbback->GetAll($sql);
            $blockIdNew = array();
            foreach($blockId as $v) {
                $blockIdNew[] = $v['id'];
            }
            $this->mc->set($sql, $blockIdNew, 86400);
        }
        return $blockIdNew;
    }
    
    /**
     * 
     * @param type $blockname
     * @return type
     */
    public function findAllIdByLikeName($blockname) {
        $sql = SqlCombinHelper::select_sql($this->table, 'id',"esta = 2 "
                . "AND blockname like '%{$blockname}%'");
        $arrBlockIdOne = $this->mc->get($sql);
        if (!$arrBlockIdOne) {
            $arrBlockId = $this->dbback->getAll($sql);
            $arrBlockIdOne = array();
            if (!CommonBase::isFullArray($arrBlockId)) {return $arrBlockIdOne;}
            foreach($arrBlockId as $k => $v) {
                $arrBlockIdOne[] = $v['id'];
            }
            $this->mc->set($sql, $arrBlockIdOne, 86400);
        }
        return $arrBlockIdOne;
    }
    
    public function findAllBy($conf) {
        $sql = SqlCombinHelper::select_sql($this->table,$this->fields,$conf);
        return $this->dbback->getAll($sql);
    }
    
    
}

?>
