<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GrowthBidHouseLowLevel
 *
 * @author user
 */
class GrowthBidRentLevel {
    /**
     *
     * @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_rent_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);
        }
    }
    
    public function findLevelByType($type) {
        $sql = SqlCombinHelper::select_sql($this->table, 'levels', "type = $type");
        return $this->dbback->getOne($sql);
    }
    
    public function findAll() {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields);
        return $this->dbback->getAll($sql);
    }
    
    public function updateBlockLevel($levelname) {
        $fieldValues['levels'] = $levelname;
		$fieldValues['type'] = 1;
        $sql = SqlCombinHelper::replace_sql($this->table, $fieldValues);
        return $this->db->execute($sql);
    }
    
    public function updateHotSearchLevel($levelname) {
        $fieldValues['levels'] = $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);
    }
}
