<?php
/**
 * 经纪人增减房豆
 *
 * @author sun
 */
class PaymentBrokerHousebean {
    
    /**
     * 积分对象
     * @var object 
     */ 
    private $credit;
    
    /**
     * 房豆操作记录对象
     * @var object 
     */
    private $housebeanRecord;
    
    /**
     * 房豆方法对象
     * @var object 
     */
    private $housebeanWay;
    
    /**
     * 经纪人数组
     * @var array
     */
    private $broker;
    
    public function __construct($broker = array()) {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->credit = new GrowthCredit();
        $this->housebeanRecord = new PaymentHousebeanRecord();
        $this->housebeanWay = new PaymentHousebeanWay();
        $this->broker = $broker;
    }
    
    /**
     * 获取经纪人编号
     * @return int
     */
    private function getBrokerId() {
        return $this->broker['id'];
    }
    
    /**
     * 获取别名下房豆数量
     * @param string $wayAlias 别名
     * @return int
     */
    private function getNumByWayAlias($wayAlias) {
        return $this->housebeanWay->getNumByWayAlias($wayAlias);
    }
    
    /**
     * 获取别名下描述信息
     * @param string $wayAlias 别名
     * @return int
     */
    private function getDescriptionByWayAlias($wayAlias) {
        //未加东西
        return $this->housebeanWay->getDescriptionByWayAlias($wayAlias);
    }
    
    /**
     * 获得经纪人当前积分的数组。
     * @return array  
     */
    public function getBrokerCredit($brokerId) {
        $credit = new GrowthCredit();
        return $credit->getValidCredit($brokerId);
    }
    
    /**
     * 增加房豆
     * @param string $wayAlias 获取房豆的方式
     * @param string $description 获取房豆方式的描述或者自定义
     * @param int $num 房豆数量
     * @param string $transactionId 交易号
     * @return array
     */
    public function increase($wayAlias, $description = null,
            $num = null, $ishidden = 1, $transactionId = null) {
        $brokerId = $this->getBrokerId();
        //操作来源
        $source = PaymentHousebeanSource::getSource();
        //别名参数的判断
        if ($wayAlias == null) {
            return array('status' => false, 'msg' => 'wayAlias字段不能为空');
        }
        //房豆
        if($num == null) {
            $num = $this->getNumByWayAlias($wayAlias);
        } else {
            $num = abs($num);
        }
        //操作描述
        if($description == null) {
            $description = $this->getDescriptionByWayAlias($wayAlias);
        }
        //交易号
        if ($transactionId == null) {
            $transactionId = PaymentTransactionId::getTransactionId($wayAlias);
        }
        //增加房豆
        $updateNumStatus = $this->credit->addCreditToOne($brokerId, $num);

        if ($updateNumStatus) {
            //添加记录
            $this->housebeanRecord->insert($brokerId, $transactionId, $wayAlias, 
                    $source, $num, $description, $ishidden);
            $arrRtnResult['status'] = 1;
            $arrRtnResult['transaction_id'] = $transactionId;
            $arrRtnResult['num'] = $num;
        } else {
            $arrRtnResult['status'] = 0;
            $arrRtnResult['msg'] = '增加房豆失败';
        }
        return $arrRtnResult;
    }
    
    
    /**
     * 减少房豆
     * @param string $wayAlias 获取房豆的方式
     * @param string $description 获取房豆方式的描述或者自定义
     * @param int $num 房豆数量
     * @param string $transactionId 交易号
     * @return array
     */
    public function reduce($wayAlias,$description = null, 
            $num = null, $ishidden = 1, $transactionId = null) {
        $arrRtnResult = array();
        $brokerId = $this->getBrokerId();
        //操作来源
        $source = PaymentHousebeanSource::getSource();
        //别名参数的判断
        if ($wayAlias == null) {
            return array('status' => false, 'msg' => 'wayAlias字段不能为空');
        }
        //房豆
        if($num == null) {
            $num = $this->getNumByWayAlias($wayAlias);
        } else {
            $num = '-'. abs($num);
        }
        $credit = $this->getBrokerCredit($brokerId);
        //查找经纪人目前房豆数量
        if ($wayAlias != 'systemReduce' && $credit < abs($num)) {
            $arrRtnResult['status'] = 2;
            $arrRtnResult['msg'] = '房豆数量不足';
        } else {
            if ($wayAlias == 'systemReduce' && abs($num) > $credit) {
                $num = '-'. abs($credit);
            }
            //操作描述
            if($description == null) {
                $description = $this->getDescriptionByWayAlias($wayAlias);
            }
            //订单号
            if ($transactionId == null) {
                $transactionId = PaymentTransactionId::getTransactionId($wayAlias);
            }
            //扣除房豆
            $updateNumStatus = $this->credit->addCreditToOne($brokerId, $num);
            if ($updateNumStatus) {
                //添加记录
                $this->housebeanRecord->insert($brokerId, $transactionId, $wayAlias, 
                        $source, $num, $description, $ishidden);
                $arrRtnResult['status'] = 1;
                $arrRtnResult['transaction_id'] = $transactionId;
                $arrRtnResult['num'] = $num;
            } else {
                $arrRtnResult['status'] = 0;
                $arrRtnResult['msg'] = '扣除房豆失败';
            }
        }
        return $arrRtnResult;
    }
    
    /**
     * 冻结或者返还房豆
     * @param int $num 房豆数量 
     * @param int $brokerId 经纪人编号
     * @param string $wayAlias 获取房豆的方式
     * @param string $description 描述
     * @return true or false
     */
    public function frozen($num, $brokerId, $wayAlias, $ishidden, $description='') {
        //操作来源
        $source = PaymentHousebeanSource::getSource();
        if($num > 0) { 
            $x = -$num;
        } else if($num < 0) {
            $x =  abs($num);
        }
        //交易号
        $transactionId = PaymentTransactionId::getTransactionId($wayAlias);
        if($brokerId === null) { $brokerId = $this->getBrokerId();}
        $status = $this->credit->frozen($brokerId, $num);
        //添加记录
        $this->housebeanRecord->insert($brokerId, $transactionId, $wayAlias, $source, 
                $x, $description, $ishidden);
        return $status;
    }
}
