<?php
/**
 * Description of GrowthBidLowLevel
 *
 * @author user
 */
class GrowthBidLowLevel {
    /**
     *
     * @var TINY_ADODB 主数据库
     */
    private $db;
    /**
     *
     * @var TINY_ADODB 从数据库
     */
    private $dbback;
    /**
     *
     * @var Memcache 缓存
     */
    private $mc;
    /**
     *
     * @var string 表名
     */
    private $table;
    /**
     *
     * @var 表中字段数组 
     */
    private $fields;
    
    /**
     * 初始化类
     * @param string $table 表名，默认为growth_bid_periods
     */
    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = 'growth_bid_low_level';
        $this->mc = $container->get('mc');
        $sqlfield = "DESC $this->table";
        $fieldsMKey = $sqlfield;
        $this->fields = $this->mc->get($fieldsMKey);
        if($this->fields === false) {
            $fields = $this->dbback->getAll($sqlfield);
            foreach ($fields as $field) {
                $this->fields[] = $field['Field'];
            }
            $this->mc->set($fieldsMKey, $this->fields, 3600);
        }
    }
    
    /**
     * 获取某个类型的访问等级
     * @param type $type
     * @return type
     */
    public function findLevelByType($type) {
        $lowLevel = $this->findAll();
        $lowLevel = SqlCombinHelper::changeToKeyArray($lowLevel, 'type');
        $lowLevel[$type]['level'] = isset($lowLevel[$type]['level']) ? $lowLevel[$type]['level'] : '';
        return $lowLevel[$type]['level'];
    }
    
    /**
     * 获取竞投的等级
     * @param type $type
     * @return type
     */
    public function findAll() {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields);
        return $this->dbback->getAll($sql);
    }
    
    public function updateBlockExpertLevel($levelname) {
        $fieldValues['level'] = $levelname;
		$fieldValues['type'] = 1;
        $sql = SqlCombinHelper::replace_sql($this->table, $fieldValues);
        return $this->db->execute($sql);
    }
    
    public function updateHouseLevel($levelname) {
        $fieldValues['level'] = $levelname;
		$fieldValues['type'] = 2;
        $sql = SqlCombinHelper::replace_sql($this->table, $fieldValues);
        return $this->db->execute($sql);
    }

    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
}
