<?php

/**
 * core\lib\block\BlockLevelHeat.php
 */

/**
 * 小区热度等级
 *
 * @author Administrator
 */
class BlockLevelHeat {
    //put your code here
    /**
     *
     * @var TINY_ADODB 主数据库
     */
    private $db;
    /**
     *
     * @var TINY_ADODB 从数据库
     */
    private $dbback;
    /**
     *
     * @var Memcache 缓存 
     */
    private $mc;
    /**
     *
     * @var string 表名
     */
    private $table;
    /**
     *
     * @var array 表中包含字段
     */
    public static $fields = array('id', 'block_id', 'level', 'utime');

    /**
     * 构造函数 ，初始化db，memcache等
     * @param string $table 表名
     */
    public function __construct($table = 'block_level_heat') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = $table;
    }
    
    /**
     * 增加一条小区等级数据
     * @param array $fieldData 包含block_id,level的数组
     * @return int 返回插入数据的id
     */
    public function insert($fieldData) {
        $sql = SqlCombinHelper::insert_sql($this->table, $fieldData);
        $this->db->Execute($sql);
        return $this->db->Insert_ID();
    }
    
    /**
     * 一次性增加多条小区等级数据
     * @param array $fieldsData 包含block_id,level的二维数组
     * @return int 插入的条数
     */
    public function insertMutl($fieldsData) {
        $sql = SqlCombinHelper::insertMutil_sql($this->table, $fieldsData);
        //exit;
        $this->db->Execute($sql);
        return $this->db->Affected_Rows();
    }

    /**
     * 根据小区id查找小区热度等级
     * @param int $blockId 小区id
     * @return array 包含小区等级的数组
     */
    public function findByBlockId($blockId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, "block_id='$blockId'");
        return $this->dbback->GetRow($sql);
    }
    
    
    public function findAll() {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields);
        return $this->dbback->getAll($sql);
    }

        /**
     * 查找等级为$level的小区
     * @param  mixed $level 正整数或者A,B,C,D
     * @return array 返回包含小区id的二维数组
     */
    public function findByLevel($level) {
        if(is_numeric($level)) {
            $where = "level=$level";
        } else {
            $where = "level='$level'";
        }
        $sql = SqlCombinHelper::select_sql($this->table, $where);
        return $this->db->Execute($sql);
    }
    
    /**
     * 清空小区热度等级表
     */
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
    
    public function findBy($select,$conf,$arrayType=1) {
        $sql = SqlCombinHelper::select_sql($this->table, $select, $conf);
        if($arrayType) {
            return $this->dbback->GetAll($sql);
        } else {
            return $this->dbback->GetAssoc($sql);
        }
        
    }

    /**
     * 重新刷新小区等级数据
     * @param array $data 包含小区等级的数组,如array(0=>array('block_id'=>10,'level'=>'A'))
     * @return int 返回刷新的条数
     */
    public function refreshData($data) {
        $this->emptyTable();
        return $this->insertMutl($data);
    }
    
    public static function friendlyShow($blockLevel) {
        
        $levelDef = array('A'=>4,'B'=>3,'C'=>2,'D'=>1);
        $re = array();
        if($blockLevel&&is_array($blockLevel)) {
            foreach($blockLevel as $v) {
                $width = $levelDef[$v['level']]*20;
                $re[$v['block_id']] = '<div l="'.$v['level'].'" style="width:'.$width.'px;" class="hot_bg_inner"></div>';
            }
            return $re;
        } else {
            $width = $levelDef[$blockLevel]*20;
            return '<div l="'.$blockLevel.'" style="width:'.$width.'px;" class="hot_bg_inner"></div>';
        }
    }
}

?>
