<?php
/**
 * Description of GrowthBrokerExp
 *
 * @author sun
 */
class GrowthBrokerExp {
    
    /**
     *
     * @var GrowthExp 
     */
    private $exp;
    
    /**
     *
     * @var GrowthExpDayCount 
     */
    private $expDayCount;
    /**
     *
     * @var GrowthExpRecord 
     */
    private $expRecord;
    
    /**
     *
     * @var GrowthExpControllPackage 
     */
    private $expControllPackage;
    
    /**
     *
     * @var type 经纪人数组
     */
    private $broker;
    
    
    public function __construct($broker, $strDate = '') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->exp = new GrowthExp();
        $this->expDayCount = new GrowthExpDayCount();
        $this->expRecord = new GrowthExpRecord($strDate);
        $this->expControllPackage = new GrowthExpControllPackage();
        $this->broker = $broker;
    }
    
    /**
     * 经纪人编号
     * @return int
     */
    private function getBrokerId() {
        return $this->broker['id'];
    }
    
    /**
     * 经纪人套餐类型
     * @return int
     */
    private function getBrokerPackageId() {
        return $this->broker['groupid'];
    }
    
    /**
     * 根据成长值获取方式的描述
     * @param type $wayAlias
     * @return type
     */
    private function getWayAliasName($wayAlias) {
        $expWay = new GrowthExpWay();
        return $expWay->findNameByAlias($wayAlias);
    }
    
    /**
     * 根据成长值获取方式的增加或者减少的分值
     * @param string $wayAlias 获取成长值的方式
     * @return int 返回成长值
     */
    public function getScoreByWayAlias($wayAlias) {
        
        $packageId = $this->getBrokerPackageId();
        $wayPackageScore = new GrowthExpWayPackage();
        $score = $wayPackageScore->findScoreByWayAliasAndPackageId($wayAlias,
                $packageId);       
        return $score;
    }
    
    private function getTime() {
        return date('Y') . '年' . date('m') . '月' . date('d') . '日';
    }
    
    /**
     * 增加成长值
     * @param string $wayAlias 获取成长值的方式
     * @param string $description 获取成长值方式的描述或者自定义
     * @param type 返回该方式的成长值或者自定义
     */
    public function increase($wayAlias, $description = null, $score = null) {
        if ($wayAlias == null) {
            return array('status' => false, 'msg' => 'wayAlias字段不能为空');
        }
        if($score == null) {
            $score = $this->getScoreByWayAlias($wayAlias);
        } else {
            $score = abs($score);
        }
        $updateScoreStatus = '';
        $brokerId = $this->getBrokerId();
        //更新经纪人分值
        $ctime = date("Y-m-d");
        if ($wayAlias == 'false'/**'openPayPackage'**/ ) {
            $updateScoreStatus = $this->exp->insertData($brokerId, $score);
			$status = 1; 
        } else {
            //获取此套餐每天的成长值上限
            $upperLimitScore = $this->expControllPackage->findScoreByWayAliasPackageId(
                    'dayExpLimit', $this->getBrokerPackageId());
            //获取今天此纪纪人已经获取多少成长值
            $wayArr = array('systemReward','openPayPackage','birthdayReward','accountFailure');//排除项目
            $sumScoreDay = $this->expDayCount->findCountExpByDayExOther($ctime, $brokerId,$wayArr);
            $arrRtnResult = array();
            if ($sumScoreDay >= $upperLimitScore && !in_array($wayAlias, $wayArr)) {//系统赠送不限制峰值
                $status = 2;
		        $isFull = TRUE;
            }elseif ($sumScoreDay+$score > $upperLimitScore && !in_array($wayAlias, $wayArr)){//系统赠送不限制峰值
                $miusScore = $upperLimitScore - $sumScoreDay;
                $updateScoreStatus = $this->exp->addExpToOneBrokerId($brokerId, $miusScore);
				$score = $miusScore;
				$status = 2;               
            } else {
                $updateScoreStatus = $this->exp->addExpToOneBrokerId($brokerId, $score);
                $status = 1;
            }
        }
		if($description == null) {
            $description = $this->getWayAliasName($wayAlias);            
        } 
        $description = $this->getTime() . ' ' . $description . '获得'
                         . $score . '成长值';
        if ($updateScoreStatus) {
            //修改每天某项的记录
            $this->expDayCount->increase($ctime, $wayAlias, $brokerId, $score);
            //添加操作记录
            $this->expRecord->insertData($brokerId, $wayAlias, $score, $description);
            $arrRtnResult['status'] = $status;
	    }elseif ($isFull){
            $arrRtnResult['status'] = $status;
            $arrRtnResult['score'] = $score;
	    } else {
            $arrRtnResult['status'] = 0;
            $arrRtnResult['msg'] = '增加成长值失败';
	    }
        return $arrRtnResult;
    }
    
    /**
     * 减少成长值
     * @param string $wayAlias 获取成长值的方式
     * @param string $description 获取成长值方式的描述或者自定义
     * @param type 返回该方式的成长值或者自定义
     */
    public function reduce($wayAlias, $description = null, $score = null) {
        if ($wayAlias == null) {
            return array('status' => false, 'msg' => 'wayAlias字段不能为空');
        }
        if($score == null) {
            $score = $this->getScoreByWayAlias($wayAlias);
        } else {
            $score = '-' . abs($score);
        }

        $brokerId = $this->getBrokerId();       

		 //更新经纪人分值
         $updateScoreStatus = $this->exp->addExpToOneBrokerId($brokerId, $score);
	

        if($description == null) {
            $description = $this->getWayAliasName($wayAlias);           
        }
        $description = $this->getTime() . ' '. $description. '扣除'
                         . abs($score) . '成长值';
     
       
        $arrRtnResult = array();
        if ($updateScoreStatus) {
            //修改每天某项的记录
            $ctime = date("Y-m-d");
            $this->expDayCount->reduce($ctime, $wayAlias, $brokerId, $score);

            //添加操作记录
            $this->expRecord->insertData($brokerId, $wayAlias, $score, $description);
            $arrRtnResult['status'] = 1;
            $arrRtnResult['score'] = $score;
        } else {
            $arrRtnResult['status'] = 0;
            $arrRtnResult['msg'] = '扣除成长值失败';
        }
        return $arrRtnResult;
    }
    
    /**
     * 获取经纪人等级
     */
    public function getLevel($resultType=1) {
        $exp = $this->getExp();
        //等级
        $growthLevel = new GrowthLevel();
        $brokerLevel = $growthLevel->findLevelNameByExp($exp);
        if($resultType===2) {
            $brokerLevel = intval(substr($brokerLevel,2));
        }
        return $brokerLevel;
    }
    
    public function getExp() {
        $brokerId = $this->broker['id'];
        $brokerExp = $this->exp->findOneByBrokerId($brokerId);
        if($brokerExp) {
         $brokerExp['exp'] = $brokerExp['exp'] == '' ? 0 : $brokerExp['exp'];
        } else {
            $brokerExp['exp'] = 0;
        }
        return $brokerExp['exp'];
    }
    
    /**
     * 名次
     */
    public function getRank($allBrokerNumber,$exp) {
        $container = Container::getInstance();
        $mc = $container->get('mc');
        $key = 'nj_growth_broker_less_exp'.$this->getBrokerId();
        $lessBrokerExpNum = $mc->get($key);
        if($lessBrokerExpNum === false) {
            $lessBrokerExpNum = $this->exp->findCountBrokerByLtExp($exp);
            //$lessBrokerExpNum = count($lessBrokerExpInfo);
            $mc->set($key,$lessBrokerExpNum,1800);
        }
        return $allBrokerNumber-$lessBrokerExpNum;
    }
    
    /**
     * 返回经纪人数量
     * @return type
     */
    public function getBrokerNumber() {
        return $this->exp->findCountBrokerAndExpInfo();
        $container = Container::getInstance();
        $mc = $container->get('mc');
        $key = 'nj_growth_all_broker_number';
        $allBrokerExpNum = $mc->get($key);
        if($allBrokerExpNum === false){
            $allBrokerExpInfo = $this->exp->findAllBrokerAndExpInfo();
            $allBrokerExpNum = count($allBrokerExpInfo);
            $mc->set($key,$allBrokerExpNum,300); 
        }
        return $allBrokerExpNum;
    }
}
