<?php
/**
 * 经纪人增减房豆
 *
 * @author sun
 */
class PayBrokerHousebean {
    
    /**
     * 积分对象
     * @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 PayHousebeanRecord();
        $this->housebeanWay = new PayHousebeanWay();
        $this->broker = $broker;
    }
    
    /**
     * 按照规定的格式显示，不足的补0
     * @param int $value 值
     * @param int $length 长度
     * @return string
     */
    private function formatValue($value, $length) {
        return sprintf("%0{$length}d", $value);
    }
    
    /**
     * 随机产生的订单号后四位数
     * 1-9999
     */
    private function randValue() {
        return rand(1, 9999);
    }
    
    /**
     * 获取订单号
     * ?时间14位+类型代号2位+随机数4位
     * @param string $wayAlias 别名
     */
    public function getOrderId($wayAlias) {
        //类型代号
        $type = $this->housebeanWay->getTypeByWayAlias($wayAlias);
        $formatType = $this->formatValue($type, 2);
        //随机数
        $randValue = $this->formatValue($this->randValue(), 4);
        //订单号
        return date('YmdHis') . $formatType . $randValue;
    }
    
    /**
     * 获取经纪人编号
     * @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);
    }
    
    /**
     * 增加房豆
     * @param string $wayAlias 获取房豆的方式
     * @param string $description 获取房豆方式的描述或者自定义
     * @param int $num 房豆数量
     * @param string $orderId 订单号
     * @return array
     */
    public function increase($wayAlias, $description = null,
            $num = null, $ishidden = 1, $orderId = null) {
        $brokerId = $this->getBrokerId();
        //操作来源
        $source = PayHousebeanSource::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 ($orderId == null) {
            $orderId = $this->getOrderId($wayAlias);
        }
        //增加房豆
        $updateNumStatus = $this->credit->addCreditToOne($brokerId, $num);
        if ($updateNumStatus) {
            //添加记录
            $this->housebeanRecord->insert($brokerId, $orderId, $wayAlias, 
                    $source, $num, $description, $ishidden);
            $arrRtnResult['status'] = 1;
            $arrRtnResult['num'] = $num;
        } else {
            $arrRtnResult['status'] = 0;
            $arrRtnResult['msg'] = '增加房豆失败';
        }
        return $arrRtnResult;
    }
    
    /**
     * 减少房豆
     * @param string $wayAlias 获取房豆的方式
     * @param string $description 获取房豆方式的描述或者自定义
     * @param int $num 房豆数量
     * @param string $orderId 订单号
     * @return array
     */
    public function reduce($wayAlias,$description = null, 
            $num = null, $ishidden, $orderId = null) {
        $brokerId = $this->getBrokerId();
        //操作来源
        $source = PayHousebeanSource::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 ($orderId == null) {
            $orderId = $this->getOrderId($wayAlias);
        }
        //增加房豆
        $updateNumStatus = $this->credit->addCreditToOne($brokerId, $num);
        if ($updateNumStatus) {
            //添加记录
            $this->housebeanRecord->insert($brokerId, $orderId, $wayAlias, 
                    $source, $num, $description, $ishidden);
            $arrRtnResult['status'] = 1;
            $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 = PayHousebeanSource::getSource();
        if($num > 0) { 
            $x = -$num;
        } else if($num < 0) {
            $x =  abs($num);
        }
        //订单号
        $orderId = $this->getOrderId($wayAlias);
        if($brokerId === null) { $brokerId = $this->getBrokerId();}
        $status = $this->credit->frozen($brokerId, $num);
        //添加记录
        $this->housebeanRecord->insert($brokerId, $orderId, $wayAlias, $source, 
                $x, $description, $ishidden);
        return $status;
    }
}
