<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GrowBrokerCredit
 *
 * @author Administrator
 */
class GrowthBrokerCredit {
    //put your code here
    /**
     *
     * @var GrowthCredit 
     */
    private $credit;
    
    /**
     *
     * @var GrowthCreditDayCount 
     */
    private $creditDayCount;
    /**
     *
     * @var GrowthCreditRecord 
     */
    private $creditRecord;
    
    /**
     *
     * @var type 经纪人数组
     */
    private $broker;
    
    /**
     * 
     * @param array $broker 经纪人数组
     * @param type $weekday 周几 ，$weekday 等于null时会去当天的周几。
     */
    public function __construct($broker,$weekday = null) {
        $this->broker = $broker;
        $this->credit = new GrowthCredit();
        $this->creditDayCount = new GrowthCreditDayCount();
        $this->creditRecord = new GrowthCreditRecord($weekday);
    }

    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, $ishidden = 1, $transactionId = null) {
        $bean = new PaymentBrokerHousebean($this->broker);

        return $bean->increase($wayAlias, $description, $score,$ishidden,$transactionId);
        /*
        if ($wayAlias == null) {
            return array('status' => false, 'msg' => 'wayAlias字段不能为空');
        }
        if($score == null) {
            $score = $this->getScoreByWayAlias($wayAlias);
        } else {
            $score = abs($score);
        }
        if($description == null) {
            $description = $this->getWayAliasName($wayAlias);
           
        }
        $description = $this->getTime() . ' ' . $description . '获得'
                         . $score . '积分';
        
        if ($wayAlias == 'false') {
            $updateScoreStatus = $this->credit->insertData($this->getBrokerId(), $score);
        } else {
            $updateScoreStatus = $this->credit->addCreditToOne($this->getBrokerId(), $score);
        }
        $arrRtnResult = array();
    //    echo $wayAlias."--".$this->getBrokerId()."--".$score."--".$description;
     // exit;
        if ($updateScoreStatus) {
            //修改每日积累
            $ctime = date("Y-m-d");//2014-02-13
            $this->creditDayCount->increase($ctime, $wayAlias, $this->getBrokerId(), $score);
            //添加记录
            $this->creditRecord->insertData($this->getBrokerId(), $wayAlias, $score, $description);
            $arrRtnResult['status'] = 1;
            $arrRtnResult['score'] = $score;
        } else {
            $arrRtnResult['status'] = 0;
            $arrRtnResult['msg'] = '增加积分失败';
        }
        return $arrRtnResult;
        */
    }
    
    /**
     * 减少积分
     * @param string $wayAlias 获取积分的方式
     * @param string $description 获取积分方式的描述或者自定义
     * @param type 返回该方式的积分或者自定义
     * @param int $type 1,用户可见；2，系统可见
     */
    public function reduce($wayAlias,$description = null,$score = null,$type=1) {
        $bean = new PaymentBrokerHousebean($this->broker);
        //$wayAlias,$description = null, $num = null, $ishidden, $orderId = null
        return $bean->reduce($wayAlias, $description, $score, $type);
        /*
        if ($wayAlias == null) {
            return array('status' => false, 'msg' => 'wayAlias字段不能为空');
        }
        if($score == null) {
            $score = $this->getScoreByWayAlias($wayAlias);
        } else {
            $score = '-'.abs($score);
        }

        if ($wayAlias == 'openPayPackage') {
            $updateScoreStatus = $this->credit->insertData($this->getBrokerId(), $score);
        } elseif($wayAlias == 'accountFailure') {
			//经纪人积分数组
			$brokerCreditArr = $this->getBrokerCredit();
			$score = '-'.abs($brokerCreditArr['credit']);   
			//积分清为0
            $updateScoreStatus = $this->credit->setCreditToOne($this->getBrokerId(), 0);
            
        }else{
            $updateScoreStatus = $this->credit->addCreditToOne($this->getBrokerId(), $score);
        }

        if($wayAlias != 'accountFailure') {
            if ($description == null) {
                $description = $this->getWayAliasName($wayAlias);
                $description = $this->getTime() . ' ' . $description . '扣除' . abs($score) . '积分';
            }
        }
		
        
        $arrRtnResult = array();
        if ($updateScoreStatus) {
            //修改每日积累
            $ctime = date("Y-m-d");//2014-02-13
            $this->creditDayCount->increase($ctime, $wayAlias, $this->getBrokerId(), $score);
            //添加记录
            $this->creditRecord->insertData($this->getBrokerId(), $wayAlias, $score, $description,$type);
            $arrRtnResult['status'] = 1;
            $arrRtnResult['score'] = $score;
        } else {
            $arrRtnResult['status'] = 0;
            $arrRtnResult['msg'] = '扣除积分失败';
        }
        return $arrRtnResult;
        */
    }
    
    private function getBrokerId() {
        return $this->broker['id'];
    }
    
    /**
     * 经纪人的套餐类型
     * @return int 
     */
    private function getBrokerPackage() {
        return $this->broker['groupid'];
    }
    
    /**
     * 
     * @return array  获得经纪人当前积分的数组。
     */
    public function getBrokerCredit() {
        return $this->credit->findByBrokerId($this->broker['id']);
    }
    
    /**
     * 获取积分获取方式的分值。表示 通过该方式可以增加或消耗的分值。
     * @param int $wayAlias
     * @return int 积分获取方式的分值。表示 通过该方式可以增加或消耗的分值。
     */
    public function getScoreByWayAlias($wayAlias) {
        //根据wayid查积分 经纪人套餐id 查找对应积分
        $wayPackageScore = new GrowthCreditWayPackage();
        $packageId = $this->getBrokerPackage();
        $score = $wayPackageScore->getScoreByWayAliasAndPackageId($wayAlias, $packageId);
        return $score;
    }
    
    /**
     * 获取别名的项目名称
     * @return string 别名的项目名称
     */
    public function getWayAliasName($alias) {
        $creditWay = new GrowthCreditWay();
        return $creditWay->findNameByAlias($alias);
    }
    
    /**
     * 返回可用积分数
     */
    public function getValidCredit() {
        $res = $this->credit->findByBrokerId($this->broker['id']);
        if($res) {
            return $res['credit']-$res['frozen'];
        } else {
            return 0;
        }
    }
    
    /**
     * 冻结积分
     * @param type $score 分数
     * @param type $brokerId 经纪人id
     * @param type $alisWay 获取方式别名
     * @param type $info 要记录的信息
     * @param type $type 要记录的消息类型，1，用户可见；2，系统可见
     * @return type
     */
    public function frozenCredit($score,$brokerId=null,$alisWay='',$info='',$type=1) {
        $bean = new PaymentBrokerHousebean($this->broker);
        return $bean->frozen($score, $brokerId, 'bidFrozenCredit', $type, $info);
        /*
        if($score>0) { $x = -$score;}if($score<=0){$x =  abs($score); }
        if($brokerId===null) { $brokerId = $this->getBrokerId();}
        $this->creditRecord->insertData($brokerId, 'bidFrozenCredit', $x, $info,$type);
        return $this->credit->frozen($brokerId,$score);
         * 
         */
    }
    
    /**
     * 
     * @return GrowthCredit
     */
    public function getGrowthCredit() {
        return $this->credit;
    }
}

?>
