<?php

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

/**
 * 学校热度等级
 *
 * @author Administrator
 */
class SchoolLevelHeat {
    //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', 'school_id', 'level', 'utime');

    /**
     * 构造函数 ，初始化db，memcache等
     * @param string $table 表名
     */
    public function __construct($table = 'school_level') {
        $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 包含school_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 $schoolId 学校id
     * @return array 包含小区等级的数组
     */
    public function findBySchoolId($schoolId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, "school_id='$schoolId'");
        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($schoolLevel) {
        
        $levelDef = array('A'=>3,'B'=>2,'C'=>1);
        $re = array();
        if($schoolLevel&&is_array($schoolLevel)) {
            foreach($schoolLevel as $v) {
                $width = $levelDef[$v['level']]*20;
                $re[$v['school_id']] = '<div l="'.$v['level'].'" style="width:'.$width.'px;" class="hot_bg_inner2"></div>';
            }
            return $re;
        } else {
            $width = $levelDef[$schoolLevel]*20;
            return '<div l="'.$schoolLevel.'" style="width:'.$width.'px;" class="hot_bg_inner2"></div>';
        }
    }
}

?>
