<?php

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

/**
 * 按资料完善度（未完善项和小区图片数量）分小区等级的设置 类
 *
 * @author Wxj
 */
class BlockLevelInfoFullSetting {
    
    /**
     *
     * @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', 'not_filled_items_number', 'image_number', 'credit_way_alias', 'exp_way_alias');

     /**
     *
     * @var array 分级符号
     */
    public static $levels = array('1'=>'★','2'=>'★★','3'=>'★★★','4'=>'★★★★',);
    /**
     * 构造函数 ，初始化db，memcache等
     * @param string $table 表名
     */
    public function __construct($table = 'block_level_info_full_setting') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = $table;
    }

    /**
     * 查找所有小区分级数据，包含*,**,***,****4个分级数据
     * @return array 包含小区等级的数组
     */
    public function findAll() {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields);
        $sql .= "order by id asc, 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('not_filled_items_number', $fields) && array_key_exists('image_number', $fields)) {
            $not_filled_items_number = $fields['not_filled_items_number'];
            $imageNumber = $fields['image_number'];
            $sql = "insert into $this->table (" . implode(",", $keys) . ") value ('" . implode("','", $fields) . "')";
            $sql .= " ON DUPLICATE KEY UPDATE not_filled_items_number='$not_filled_items_number',image_number='$imageNumber'";
            if ($this->db->execute($sql)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

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

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

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

    /**
     * 根据等级查找积分获取方式的别名
     * @param int $level 小区等级
     * @param int $type 默认值1, 表示修改资料，2表示修改图片
     * @return string  积分获取方式别名
     */
    public function getCreditWayAlias($level,$type = 1) {
        $sql = SqlCombinHelper::select_sql($this->table, 'credit_way_alias', "level = '$level' and type='$type'");
        return $this->dbback->getOne($sql);
    }

    /**
     * 根据等级查找经验获取方式的别名
     * @param int $level 小区等级
     * @param int $type 默认值1, 表示修改资料，2表示修改图片
     * @return string 经验获取方式的别名
     */
    public function getExpWayAlias($level,$type = 1) {
        $sql = SqlCombinHelper::select_sql($this->table, 'exp_way_alias', "level = '$level' and type='$type'");
        return $this->dbback->getOne($sql);
    }
    
    /**
     * 清空小区等级设置表
     */
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
}

?>
