<?php

/**
 * core\lib\growth\GrowthCreditWay.php
 */

/**
 * 积分的获取方式
 *
 * @author Administrator
 */
class GrowthCreditWay {
    
    /**
     *
     * @var Mixed 主数据库连接
     */
    private $db;
    /**
     *
     * @var Mixed 从数据库连接
     */
    
    private $dbback;
    /**
     *
     * @var Memcache memcache对象
     */
    private $mc;
    /**
     *
     * @var array 表中包含的字段
     */
    private $fields = array('id','type','name','alias','is_display','description','repeatable');
    
    /**
     *
     * @var String 表名
     */
    private $table;
    
    /**
     * 获取方式的分类
     * @var 1套餐，100认证，200活跃度，300房源，400贡献，500系统赠送，1000竞投，1500兑换礼品，2000违规 
     */
    public static $type = array(
        '1'=>'套餐',
        '100'=>'认证',
        '200'=>'活跃度',
        '300'=>'房源',
        '400'=>'贡献',
        '500'=>'系统赠送',
        '1000'=>'竞投',
        '1500'=>'兑换礼品',
        '2000'=>'违规',);
    
    /**
     * 构造函数，初始化db和表名等
     */
    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = 'growth_credit_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 $isDisplay 1表示显示，0，表示不显示
     * @return array 成功返回数组
     */
    public function findByDisplayable($isDisplay = 1) {
        //$sql = "select ".  implode(",", $this->fields)." from $this->table where is_display =$isDisplay";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "is_display ='$isDisplay' ORDER BY type,id asc");
        if($res = $this->mc->get($sql)) {
            return $res;
        }
        $res = $this->dbback->getAll($sql);
        $this->mc->set($key,$res,true,5);

        return $res;
    }
    
    /**
     * 根据id获得积分获取方式
     * @param type $id id
     * @return array 返回获取方式的数组
     */
    public function findById($id) {
        //$sql = "select ".  implode(",", $this->fields)." from $this->table where id =$id";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "id ='$id'");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据获取方式类型获得积分获取方式
     * @param int $type 获取方式类型
     * @return Mixed 返回数组
     */
    public function findByType($type) {
        //$sql = "select ".  implode(",", $this->fields)." from $this->table where type =$type";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "type ='$type'");
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据获取方式名字查找获取方式
     * @param String $name
     * @return Mixed 返回数组
     */
    public function findByName($name) {
        //$sql = "select ".  implode(",", $this->fields)." from $this->table where name =$name";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "name ='$name'");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据别名获得获取方式
     * @param String $alias
     * @return Mixed 返回数组
     */
    public function findByAlias($alias) {
        //$sql = "select ".  implode(",", $this->fields)." from $this->table where name =$name";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "alias ='$alias'");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 更新积分获取方式
     * @param array $fields 包含获取方式的数组
     * @return boolean
     */
    public function save($fields) {
        //$keys = array_keys($fields);
        //$sql = "replace into $this->table (".  implode(",", $keys).") value ('".  implode("','", $fields)."')";
        $sql = SqlCombinHelper::replace_sql($this->table, $fields);
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取别名的描述文字
     * @return string 别名的描述文字
     */
    public function findNameByAlias($alias) {
        $arrRow = $this->findByAlias($alias);
        return $arrRow['name'];
    }
    
    /**
     * 插入一条获取方式
     * @param array $fields 包含获取方式的数组
     * @return Mixed 成功返回插入的id，失败返回false
     */
    public function insert($fields) {
        $sql = SqlCombinHelper::insert_sql($this->table, $fields);
        if($this->db->execute($sql)) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }
    
    /**
     * 清空积分获取方式表
     */
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
}

?>
