<?php
/**
 * 竞投和成长值等级的关联类
 *
 * @author sun
 */
class GrowthLevelPermission {
    /**
     * 主数据库
     */
    private $db;

    /**
     * 从数据库
     */
    private $dbback;

    /**
     * 缓存对象
     */
    private $mc;
    
    /**
     * 关联的表名
     * @var string
     */
    private $table;
    
    /**
     * 表中包含字段
     * @var array
     */
    private static $fields = array('id','way_id','level_id', 'status');

    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = 'growth_level_permission';
    }
    
    /**
     * 根据获取方式别名与等级ID值获取一条记录
     * @param string $wayAlias 获取方式别名
     * @param int $levelId  成长值等级ID值
     * @return array
     */
    public function findOneBy($wayId, $levelId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, 
                "way_id = $wayId AND level_id = $levelId");
        return $this->dbback->getRow($sql);
    }
   
    /**
     * 根据获取方式别名获取所有的等级信息
     * @param string $wayAlias 获取方式别名
     * @param int $levelId  成长值等级ID值
     * @return array
     */
    public function findByWayId($wayId, $status = 1) {
        $where = $status != '' ? " AND status = $status" : '';
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, 
                "way_id = $wayId $where");
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据获取方式别名与等级ID值判断两者是否有关联
     * @param type 获取方式别名
     * @param string $wayAlias 获取方式别名
     * @param int $levelId  成长值等级ID值
     * @return boolean 1 表示绑定 0表示未绑定
     */
    public function findStatusByWayIdAndlevelId($wayId, $levelId) {
        $arrRow = $this->findOneBy($wayId, $levelId);
        return $arrRow['status'] == '' ? 0 : $arrRow['status'];
    }
    
    /**
     * 保存数据
     * @param array $fields 数据结构
     * @return boolean
     */
    public function saveData($fields) {
        $sql = SqlCombinHelper::replace_sql($this->table, $fields);
        return $this->db->execute($sql);
    }
    
    
}
