<?php

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

/**
 * 按热度（pv和房源量）分小区等级的设置类
 *
 * @author Administrator
 */
class BlockLevelHeatSetting {

    /**
     *
     * @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', 'level', 'min_house_number', 'max_house_number', 'credit_way_alias', 'exp_way_alias');

    /**
     *
     * @var array 分级字母
     */
    public static $levels = array('1'=>'A','2'=>'B','3'=>'C','4'=>'D',);

    /**
     * 构造函数 ，初始化db，memcache等
     * @param string $table 表名
     */
    public function __construct($table = 'block_level_heat_setting') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = $table;
    }

    /**
     * 查找所有小区分级数据，包含A,B,C,D4个分级数据
     * @return array 包含小区等级的数组
     */
    public function findAll() {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields);
        $sql .= "order by level";
        $res = $this->db->getAll($sql);
        if ($res) {
            foreach ($res as $key => $value) {
                $res[$key] = $this->removeNumberKey($value);
            }
        }
        return $res;
    }

    /**
     * 去除数组中键值为数组的数组元素
     * @param array $arr 包含数字和字符键值的数组
     * @return array 键值数组
     */
    private function removeNumberKey($arr) {
        $rarr = array();
        foreach ($arr as $k => $val) {
            if (!is_int($k)) {
                $rarr[$k] = $val;
            }
        }
        return $rarr;
    }

    /**
     * 插入或者更新小区等级数据
     * @param array $fields 小区等级数据数组
     * @return boolean 成功返回true，失败返回false
     */
    public function save($fields) {
        $keys = array_keys($fields);
        if (array_key_exists('min_house_number', $fields) && array_key_exists('max_house_number', $fields)) {
            $min_house_number = $fields['min_house_number'];
            $max_house_number = $fields['max_house_number'];
            $sql = "insert into $this->table (" . implode(",", $keys) . ") value ('" . implode("','", $fields) . "')";
            $sql .= " ON DUPLICATE KEY UPDATE min_house_number = '$min_house_number',max_house_number='$max_house_number'";
            if ($this->db->execute($sql)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 更新$min_house_number,和$max_house_number
     * @param int $pv 为null时不更新
     * @param int $houseNumber 为null时不更新
     * @return int 返回更新的记录数目
     */
    public function update($housenumber) {        
        $fields = array();
        $affected_row = 0;
        if(is_array($housenumber)&&!empty($housenumber)) {
            foreach ($housenumber as $value){
                if($value['min']!==null) {
                    $fields['min_house_number'] = $value['min'];
                }
                if($value['max']!==null) {
                    $fields['max_house_number'] = $value['max'];
                }
                $where = "level='".$value['level']."'";
                $sql = SqlCombinHelper::update_sql($this->table, $fields, $where);               
                $this->db->execute($sql);
                
                $affected_row += $this->db->affected_Rows();
                
            }        
            return $affected_row;
           
        } else {
             return false;
        }
    }
    
    


    /**
     * 更新最小房源量
     * @param int $houseNumber 房源值
     * @return int 返回更新的记录数目
     */
    public function updateMinHouseNumber($houseNumber) {
        $sql = SqlCombinHelper::update_sql($this->table, array('min_house_number' => $houseNumber));
        $this->db->execute($sql);
        return $this->db->affected_Rows();
    }

    /**
     * 更新房源量
     * @param int $houseNumber 房源量
     * @return int 返回更新的记录数目
     */
    public function updateMaxHouseNumber($houseNumber) {
        $sql = SqlCombinHelper::update_sql($this->table, array('max_house_number' => $houseNumber));
        $this->db->execute($sql);
        return $this->db->affected_Rows();
    }

    /**
     * 根据等级查找积分获取方式的别名
     * @param int $level 小区等级
     * @return string  积分获取方式别名
     */
    public function getCreditWayAlias($level) {
        $sql = SqlCombinHelper::select_sql($this->table, 'credit_way_alias', "level = '$level'");
        return $this->dbback->getOne($sql);
    }

    /**
     * 根据等级查找经验获取方式的别名
     * @param int $level 小区等级
     * @return string 经验获取方式的别名
     */
    public function getExpWayAlias($level) {
        $sql = SqlCombinHelper::select_sql($this->table, 'exp_way_alias', "level = '$level'");
        return $this->dbback->getOne($sql);
    }

    /**
     * 清空小区等级设置表
     */
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
}

?>
