<?php
/**
 * 操作房豆的方法
 * @author sun
 */
class PayHousebeanWay {

    /**
     *
     * @var TINY_ADODB 主数据库 
     */    
    private $db;
    
    /**
     *
     * @var type 从数据库
     */
    private $dbback;
    
    /**
     *
     * @var type 缓存对象
     */
    private $mc;

    /**
     *
     * @var String 关联的表名 
     */
    private $table;
    
    /**
     * 构造函数，初始化db memcache 等。
     * @param string $table 表名，可以使用默认值。
     */
    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = 'pay_housebean_way';
        $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 array $fields 字段
     * @return boolean
     */
    public function initData($fields) {
        $sql = SqlCombinHelper::insert_sql($this->table, $fields);
        return $this->db->execute($sql);
    }
    
    /**
     * 根据别名获得获取方式
     * @param string $wayAlias 别名
     * @return Mixed 返回数组
     */
    public function getByWayAlias($wayAlias) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,
                "alias ='$wayAlias'");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据别名获取操作的房豆
     * @param string $wayAlias 别名
     * @return int
     */
    public function getNumByWayAlias($wayAlias) {
        $wayOne = $this->getByWayAlias($wayAlias);
        return $wayOne['num'];
    }
    
    /**
     * 获取别名的描述信息
     * @param string $wayAlias 别名
     * @return string
     */
    public function getDescriptionByWayAlias($wayAlias) {
        $wayOne = $this->getByWayAlias($wayAlias);
        return $wayOne['description'];
    }
    
    /**
     * 获取别名的描述信息
     * @param string $wayAlias 别名
     * @return string
     */
    public function getTypeByWayAlias($wayAlias) {
        $wayOne = $this->getByWayAlias($wayAlias);
        return $wayOne['type'];
    }
    
    /**
     * 清空房豆获取方式表
     */
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
}
