<?php !defined('APP_DIR') ? die('acceess not defined') : '';
class BM{
    //默认到对象
    private $_default_dao = null;
    private $_defaule_db = 'nj';
    
    public static $instance = array();
    
    public function __construct($default_dao=''){
        $this->_default_dao = $default_dao;
    }
    
    /**
     * BM单例调用函数
     *
     * @param string $bm_name
     * @return BM_Object
     */
    public function instance($bm_name){
        if(isset(self::$instance[$bm_name])){
            return self::$instance[$bm_name];
        }else {
			
            $bm_obj = "BM_$bm_name";
            if( class_exists($bm_obj) ){
                self::$instance[$bm_name] = new $bm_obj();
            }else {
                self::$instance[$bm_name] = new BM($bm_name);
            }
            return self::$instance[$bm_name];
        }
    }
    
    /**
     * 根据查询条件获取所有数据
     *
     * @param array $query_struct
     * @param sting $index
     * @return array
     */
    public function index($query_struct=array(), $index=NULL){
        return $this->dao($this->_default_dao)->get_all($query_struct,$index);
    }
    
    /**
     * 根据ID或者查询结构体获取一条结果
     *
     * @param unknown_type $id
     * @return unknown
     */
    public function get($id){
        return $this->dao($this->_default_dao)->get($id);
    }
    
    /**
     * 添加一条数据
     *
     * @param array $data
     * @return int
     */
    public function add($data){
        if (isset($data['id'])) {
            return false;
        }
        return $this->dao($this->_default_dao)->add($data);
    }
    
    /**
     * 更新一条数据
     *
     * @param array $data
     * @return bool
     */
    public function edit($data){
        if (!isset($data['id'])) {
            return false;
        }
        return $this->dao($this->_default_dao)->edit($data);
    }


    
    /**
     * 删除一条数据
     *
     * @param int $id
     * @return bool
     */
    public function delete($id){
        return $this->dao($this->_default_dao)->delete($id);
    }
    
    /**
     * DAO的实例化函数
     *
     * @param string $dao_name
     * @return DAO_Object
     */
    public function dao($dao_name, $city_code=null, $database=null, $table=null, $primaryKey=null){
        return DAO::instance($dao_name, $city_code, $database, $table, $primaryKey);
    }
}
?>