<?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', 'pv', '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('pv', $fields) && array_key_exists('house_number', $fields)) {
            $pv = $fields['pv'];
            $houseNumber = $fields['house_number'];
            $sql = "insert into $this->table (" . implode(",", $keys) . ") value ('" . implode("','", $fields) . "')";
            $sql .= " ON DUPLICATE KEY UPDATE pv='$pv',house_number='$houseNumber'";
            if ($this->db->execute($sql)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 更新$pv,和$houseNumber
     * @param int $pv 为null时不更新
     * @param int $houseNumber 为null时不更新
     * @return int 返回更新的记录数目
     */
    public function update($pv, $houseNumber) {
        
        $fields = array();
        if($pv===null&&$houseNumber===null) {
            return false;
        } else {
            if($pv!==null) {
                $fields['pv'] = $pv;
            }
            if($houseNumber!==null) {
                $fields['house_number'] = $houseNumber;
            }
            $sql = SqlCombinHelper::update_sql($this->table, $fields, '');
            $this->db->execute($sql);
            return $this->db->affected_Rows();
        }
    }

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

    /**
     * 更新房源量
     * @param int $houseNumber 房源量
     * @return int 返回更新的记录数目
     */
    public function updateHouseNumber($houseNumber) {
        $sql = SqlCombinHelper::update_sql($this->table, array('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);
    }
}

?>
