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

    /**
     *
     * @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 = 'payment_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'");
        $way = $this->mc->get($sql);
        if ($way === false) {
            $way = $this->dbback->getRow($sql);
            $this->mc->set($sql, $way, 3600);
        }
        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'];
    }
    
    /**
     * 获取别名的名字信息
     * @param string $wayAlias 别名
     * @return string
     */
    public function getNameByWayAlias($wayAlias) {
        $wayOne = $this->getByWayAlias($wayAlias);
        return $wayOne['name'];
    }
    
    /**
     * 根据操作方式获取相应的信息
     * @param string $method
     * @return array
     */
    public function getByMethod($method = null) {
        $where = 'discard = 0';
        if ($method != null) {
            $where .= ' and method = "' . $method . '"';
        }
        $sql = SqlCombinHelper::select_sql($this->table, 'name, alias', $where);
        $method = $this->mc->get($sql);
        if ($method === false) {
            $method = $this->dbback->getAll($sql);
            $this->mc->set($sql, $method, 3600);
        }
        return $method;
    }
    
    /**
     * 清空房豆获取方式表
     */
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
}
