<?php
/**
 * 成长值等级类
 *
 * @author sun
 */
class GrowthLevel {
    
    /**
     * 主数据库
     */
    private $db;

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

    /**
     * 缓存对象
     */
    private $mc;
    
    /**
     * 关联的表名
     * @var string
     */
    private $table;
    
    /**
     * 表中包含字段
     * @var array
     */
    private static $fields = array('level_id','name','down_exp','up_exp');
    
    /**
     * 经纪人成长值类
     * @var object
     */
    private $exp;
    
    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = ' growth_exp_level';
        $this->exp = new GrowthExp();
    }
    
    /**
     * 初始化成长值等级及分值区间值
     * @param type $arrFiled
     * @return type
     */
    public function initData($arrFiled) {
        $sql = SqlCombinHelper::insert_sql($this->table, $arrFiled);
        return $this->db->execute($sql);
    }
    
    /**
     * 返回成长值等级表的所有数据
     * @return array
     */
    public function findAllLevelData() {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields);
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据等级ID号获取一条记录
     * @param int $levelId 等级ID号
     * @return array
     */
    public function findOneByLevelId($levelId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, 
                "level_id = $levelId");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据等级名称获取一条记录
     * @param string $levelName 等级名称
     * @return array
     */
    public function findOneByLevelName($levelName) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, 
                "name = '".$levelName."'");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据成长值分值获取满足条件的经纪人总数
     * @param type $down_exp 成长值介于下限边值
     * @param type $up_exp 成长值介于上限边值
     * @return int 满足条件经纪人数目
     */
    public function findBrokerNumByBetweenExp($down_exp, $up_exp) {
        $brokerNum = 0;
        if ($down_exp >= 0 && $up_exp >= 0 && $up_exp != null) {
            $brokerNum = $this->exp->findBrokerNumByBetweenExp($down_exp, $up_exp);
        } else if ($down_exp >= 0  && $up_exp == null) {
            $brokerNum = $this->exp->findBrokerNumByGlExp($down_exp);
        } else if ($down_exp == null  && $up_exp >= 0) {
            $brokerNum = $this->exp->findBrokerNumByLtExp($up_exp);
        }
        return $brokerNum;
    }
    
    /**
     * 根据等级ID获取满足条件的经纪人总数
     * @param int $levelId  等级ID值
     * @return array
     */
    public function findBrokerNumByLevelId($levelId) {
        $arrLevelOne = $this->findOneByLevelId($levelId);
        return $this->findBrokerNumByBetweenExp($arrLevelOne['down_exp'], 
                $arrLevelOne['up_exp']);
    }

    /**
     * 根据等级名称获取满足条件的经纪人总数
     * @param string $levelName  等级名称
     * @return array
     */
    public function findbrokerNumByLevelName($levelName) {
        $arrLevelOne = $this->findOneByLevelName($levelName);
        return $this->findBrokerNumByBetweenExp($arrLevelOne['down_exp'], 
                $arrLevelOne['up_exp']);
    }
    
    /**
     * 根据成长值分值获取满足条件的经纪人数组
     * @param type $down_exp 成长值介于下限边值
     * @param type $up_exp 成长值介于上限边值
     * @return int 满足条件经纪人数组
     */
    public function findAllBrokerByBetweenExp($down_exp, $up_exp) {
        $arrBroker = array();
        if ($down_exp >= 0 && $up_exp >= 0 && $up_exp != null) {
            $arrBroker = $this->exp->findAllBrokerByBetweenExp($down_exp, $up_exp);
        } else if ($down_exp >= 0  && $up_exp == null) {
            $arrBroker = $this->exp->findAllBrokerByGlExp($down_exp);
        } else if ($down_exp == null  && $up_exp >= 0) {
            $arrBroker = $this->exp->findAllBrokerByLeExp($up_exp);
        }
        return $arrBroker;
    }

    /**
     * 根据等级ID获取满足条件的经纪人数组
     * @param int $levelId  等级ID值
     * @return array 返回经纪人数组基本信息
     */
    public function findAllBrokerByLevelId($levelId) {
        $arrLevelOne = $this->findOneByLevelId($levelId);
        return $this->findAllBrokerByBetweenExp($arrLevelOne['down_exp'], 
                $arrLevelOne['up_exp']);
    }
    
    /**
     * 根据等级名称获取满足条件的经纪人数组
     * @param string $levelName  等级名称
     * @return array 返回经纪人数组基本信息
     */
    public function findAllBrokerByLevelName($levelName) {
        $arrLevelOne = $this->findOneByLevelName($levelName);
        return $this->findAllBrokerByBetweenExp($arrLevelOne['down_exp'], 
                $arrLevelOne['up_exp']);
    }
    
    /**
     * 根据经纪人成长值获取等级名称
     * @param int $exp 成长值
     * @return string 返回成长值所对应的等级 如:Lv4
     */
    public function findLevelNameByExp($exp) {
        $arrExpOne = $this->findOneByExp($exp);
        return $arrExpOne['name'];
    }
    
     /**
     * 根据经纪人成长值返回一条与之匹配的记录
     * @param int $exp 成长值
     * @return string 返回一条与之匹配的记录
     */   
    public function findOneByExp($exp) {
        $arrLevelData = $this->findAllLevelData();
        foreach($arrLevelData as $value) {			
            if (($exp >= $value['down_exp'] && $exp <= $value['up_exp']) 
                    || ($value['up_exp'] == 0 && $exp >= $value['down_exp'])) {				
                return $value;
            } 
        }
    }
    
    /**
     * 根据经纪人成长值返回一条与之匹配的记录
     * @param type $exp
     */
    public function findLevelIdByExp($exp) {
        $arrExpOne = $this->findOneByExp($exp);
        return $arrExpOne['level_id'];
    }
    
    /**
     * 根据等级ID获取等级名称
     * @param int $exp 成长值
     * @return string 返回成长值所对应的等级 如:Lv4
     */
    public function findLeveNameByLevelId($levelId) {
        $arrLevelRow = $this->findOneByLevelId($levelId);
        return $arrLevelRow['name'];
    }
    
    /**
     * 清空
     */
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->execute($sql);
    }

	public function test(){
		echo "test";
	}
}
