<?php
/**
 * 经纪人使用竞投的数据
 * type 字段，1，小区专家；2，房源竞投，3，求购匹配，4，求租匹配
 * @author user
 */
class GrowthBidUserBroker {
    /**
     * 主数据库
     */
    private $db;

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

    /**
     * 缓存对象
     */
    private $mc;
    
    /**
     * 关联的表名
     * @var string
     */
    private $table;
    
    /**
     * 经纪人使用竞投的类型
     * @var type 
     */
    private static $brokerUserType = array(
        'blockexpert' => 1,
        'house' => 2,
        'buy'=>3,//求购
        'rent'=>4//求租
    );
    
    /**
     * 表中包含字段
     * @var array
     */
    private static $fields = array('id','type', 'broker_id','ctime');

    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = 'growth_bid_broker_user';
    }
    
    /**
     * 获取某个类型的数据的总数
     * @param type $type
     * @return type
     */
    public function findCountByType($type) {
        $sql = SqlCombinHelper::count_sql($this->table, 'type = ' . $type);
        $bidNum = $this->mc->get($sql);
        if ($bidNum === false) {
            $bidNum = $this->dbback->getOne($sql);
        }
        return $bidNum;
    }
    
    /**
     * 获取竞投小区专家经纪人数量
     * @return type
     */
    public function findCountBidBlockExpert() {
        return $this->findCountByType(self::$brokerUserType['blockexpert']);
    }
    
    /**
     * 获取竞投房源经纪人数量
     * @return type
     */
    public function findCountBidHouse() {
        return $this->findCountByType(self::$brokerUserType['house']);
    }
    
    public function addJoinBidRecord($type, $brokerId) {
        $fieldData = array();
        $fieldData['type'] = $type;
        $fieldData['broker_id'] = $brokerId;
        $sql = SqlCombinHelper::replace_sql($this->table, $fieldData);
        return $this->db->execute($sql);
    }
    
    /**
     * 
     * @param type $brokerId
     * @return type
     */
    public function addJoinBidHouseRecord($brokerId) {
        return $this->addJoinBidRecord(self::$brokerUserType['house'], $brokerId);
    }
    
    /**
     * 
     * @param type $brokerId
     * @return type
     */
    public function addJoinBidBlockExpertRecord($brokerId) {
        return $this->addJoinBidRecord(self::$brokerUserType['blockexpert'], $brokerId);
    }
}
