<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 特权类
 */

/**
 * Description of TeQuan
 *
 * @author Administrator
 */
class TeQuan {
    private $brokerLevel;
    private $brokerType;
    private $brokerCredit;
    private $packageId;
    private $mc;
    public function __construct() {
        $ci =& get_instance();
        $ci->load->library('My_memcached',null,'memcached');
        $this->mc = $ci->memcached;
    }

    public function init($brokerLevel,$brokerType,$brokerCredit,$packageId) {
        $this->brokerLevel = $brokerLevel;
        $this->brokerType = $brokerType;
        $this->brokerCredit = $brokerCredit;
        $this->packageId = $packageId;
        return $this;
    }
    public function getAll() {
        $privilege = array();
        $privilege[] = $this->getPrivilegeBlockExpert();
        $privilege[] = $this->getPrivilegeBidHouse();
        $privilege[] = $this->getPrivilegeBidRent();
        //$privilege[] = $this->getPrivilegeSetPromot();
        $privilege[] = $this->getPrivilegePackingHouse();
        if(GB_CITY == 'nj'){
		    $privilege[] = $this->getPrivilegeTagSale();
            $privilege[] = $this->getPrivilegeGuideProject();
        }
        //$privilege[] = $this->getPrivilegeBuy();
        // $privilege[] = $this->getPrivilegeRent();
        return $privilege;
    }
    
   

    /**
     * 
     * @param type $brokerLevel 经纪人等级
     * @param type $brokerType 经纪人类型，付费or免费
     * @param type $brokerCredit 经纪人房豆
     * @param type $packageid 经纪人套餐类型
     */
    public function getPrivilegeBlockExpert() {
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
  
        $p = array();
        $permi = new GrowthLevelWayPermission();
        
        $minLevel = $this->level_require(1);

        $p['name'] = '小区专家';
        $minLevel = substr($minLevel, 2);
        $p['level'] = $minLevel;//等级要求
        if($brokerType==1) {
            $p['allow'] = TRUE;//是否允许
        } else {
            $p['allow'] = FALSE;//是否允许
        }
         //竞投期限类
        $growthBidPeriods = new GrowthBidPeriods();
        //竞投期限的控制
        $p['perioids'] = $growthBidPeriods->currentBidPeriods(1);
        $gbuserbroker = new GrowthBidUserBroker();
        $p['using'] = $gbuserbroker->findCountByType(1);//正在使用人数
        $p['intro'] = '经纪人扎堆热门小区，做小区专家脱颖而出。客户来电接不停！开单太容易了！小区专家每隔3天开放一次，9点至17点开放。';//介绍
        $p['img'] = $p['allow']?'tqXqzjIco.png':'tqXqzjIco_none.png';//tqqgppIco.png,tqqzppIco.png
        $p['show_time'] = 1;
        $p['levelnoenough'] = 0;
        if(GB_CITY == 'nj'){
            $documentation = "http://zsb.house365.com/help/nj/";
        }else{
            $documentation = "http://zsb.house365.com/help/";
        }
        $btn = array();
        if($p['allow']) {
               if($brokerLevel<$minLevel){
                   $p['levelnoenough'] = 1;
                   $btn = array('link'=>$documentation,'text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
               }else{
                   if($p['perioids']['status']==1){
                       $btn = array('link'=>'/market_bid_block_expert/','text'=>'快去竞投','class'=>'btnColor_p');
                   }elseif($p['perioids']['status']==2){
                       $btn = array('link'=>'/market_bid_block_expert/','text'=>'去看看怎么玩','class'=>'btnColor_p');
                   }else{
                       $btn = array('link'=>'/market_bid_block_expert/on/','text'=>'查看竞投结果','class'=>'btnColor_p');
                   } 
               }    
            
        }else{
            $btn = array('link'=>'javascript:void(0)','text'=>'开通付费才可参与','class'=>'btnColor_p_no ');

        }
        $p['btn'] = $btn;
        
        return $p;
    }
    
     /**
     * 
     * @param type $brokerLevel 经纪人等级
     * @param type $brokerType 经纪人类型，付费or免费
     * @param type $brokerCredit 经纪人房豆
     * @param type $packageid 经纪人套餐类型
     */
    public function getPrivilegeBidHouse() {
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
    
        $p = array();
        //竞投经纪人最小等级
        $minLevel = $this->level_require(2);
        $p['name'] = '二手房竞投';
        $minLevel = substr($minLevel, 2);
        $p['level'] = $minLevel;//等级要求
        if($brokerType==1) {
            $p['allow'] = TRUE;//是否允许
        } else {
            $p['allow'] = FALSE;//是否允许
        }

         //竞投期限类
        $growthBidPeriods = new GrowthBidPeriods();
        //竞投期限的控制
        $p['perioids'] = $growthBidPeriods->currentBidPeriods(2);
        if(GB_CITY == 'nj'){
            $documentation = "http://zsb.house365.com/help/nj/";
        }else{
            $documentation = "http://zsb.house365.com/help/";
        }
        $gbuserbroker = new GrowthBidUserBroker();
        $p['using'] = $gbuserbroker->findCountBidHouse();//正在使用人数
        $p['intro'] = '还在苦等刷新？竞投成功，热门推广位属于你，强力曝光，助您立马开单！房源竞投每隔3天开放一次，9点至17点开放。';//介绍
        $p['img'] = $p['allow']?'esfjt_cz.png':'esfjt_cz_none.png';//tqqgppIco.png,tqqzppIco.png
        $btn = array();
        $p['levelnoenough'] = 0;
        $p['show_time'] = 1;
        if($p['allow']) {
            if($brokerLevel<$minLevel) {
                $p['levelnoenough'] = 1;
                $btn = array('link'=>$documentation,'text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
            }else{
                if($p['perioids']['status']==1){
                    $btn = array('link'=>'/market_bid_house_manage/all/','text'=>'快去竞投','class'=>'btnColor_p');
                }elseif($p['perioids']['status']==2){
                    $btn = array('link'=>'/market_bid_house_manage/all/','text'=>'去看看怎么玩','class'=>'btnColor_p');
                }else{
                    $btn = array('link'=>'/market_bid_house_manage/on/','text'=>'查看竞投结果','class'=>'btnColor_p');
                } 
                
            }
        }else{
            $btn = array('link'=>'javascript:void(0)','text'=>'开通付费才可参与','class'=>'btnColor_p_no ');

        }
        $p['btn'] = $btn;
        
        return $p;
    }
  
     /**
     * 
     * @param type $brokerLevel 经纪人等级
     * @param type $brokerType 经纪人类型，付费or免费
     * @param type $brokerCredit 经纪人房豆
     * @param type $packageid 经纪人套餐类型
     */
    public function getPrivilegeBidRent() {
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
    
        $p = array();
        //竞投经纪人最小等级
        $minLevel = $this->level_require(2);
        $p['name'] = '租房竞投';
        $minLevel = substr($minLevel, 2);
        $p['level'] = $minLevel;//等级要求
        if($brokerType==1) {
            $p['allow'] = TRUE;//是否允许
        } else {
            $p['allow'] = FALSE;//是否允许
        }

         //竞投期限类
        $growthBidPeriods = new GrowthBidPeriods();
        //竞投期限的控制
        $p['perioids'] = $growthBidPeriods->currentBidPeriods(3);
        if(GB_CITY == 'nj'){
            $documentation = "http://zsb.house365.com/help/nj/";
        }else{
            $documentation = "http://zsb.house365.com/help/";
        }
        $gbuserbroker = new GrowthBidUserBroker();
        $p['using'] = $gbuserbroker->findCountBidRent();//正在使用人数
        $p['intro'] = '租房竞投全面开放，还在苦等刷新？竞投成功，热门推广位属于你，强力曝光，助您立马开单！每隔3天开放一次，9点至17点开放。';//介绍
        $p['img'] = $p['allow']?'zfjt_cz.png':'zfjt_cz_none.png';//tqqgppIco.png,tqqzppIco.png
        $btn = array();
        $p['levelnoenough'] = 0;
        $p['show_time'] = 1;
        if($p['allow']) {
            if($brokerLevel<$minLevel) {
                $p['levelnoenough'] = 1;
                $btn = array('link'=>$documentation,'text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
            }else{
                if($p['perioids']['status']==1){
                    $btn = array('link'=>'/market_bid_rent_manage/all/','text'=>'快去竞投','class'=>'btnColor_p');
                }elseif($p['perioids']['status']==2){
                    $btn = array('link'=>'/market_bid_rent_manage/all/','text'=>'去看看怎么玩','class'=>'btnColor_p');
                }else{
                    $btn = array('link'=>'/market_bid_rent_manage/on/','text'=>'查看竞投结果','class'=>'btnColor_p');
                } 
                
            }
        }else{
            $btn = array('link'=>'javascript:void(0)','text'=>'开通付费才可参与','class'=>'btnColor_p_no ');

        }
        $p['btn'] = $btn;
        
        return $p;
    }
    
    /**
     * 定投推广特权
     * getPrivilegeSetPromot
     * @param type $brokerLevel 经纪人等级
     * @param type $brokerType 经纪人类型，付费or免费
     * @param type $brokerCredit 经纪人房豆
     * @param type $packageid 经纪人套餐类型
     */
    public function getPrivilegeSetPromot(){
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;    
       
     
        /*
           $this->load->model('broker_model');
        //把SESSION中的用户信息赋给user_arr，方便使用
        $user_arr = $this->broker_model->get_user_session();
        //学区价格限制
        $vasPrice  = new VasPrice; 
        $aSchoolPriceLimit = $vasPrice->ServicePrice('ASchool');
        $bSchoolPriceLimit = $vasPrice->ServicePrice('BSchool');
        $schoolHousePrice = $aSchoolPriceLimit>$bSchoolPriceLimit ? $aSchoolPriceLimit : $bSchoolPriceLimit;
     
        $this->load->model('school_model');
        $allSchoolBlockArr = $this->school_model->getAllSchoolBlock();
        $isEstSchoolHouse = $this->school_model->isEstHouseInSchoolByUid($user_arr['id'],$allSchoolBlockArr);
         */ 
        
        //已使用的人数        
        $vasBroker = new VasBroker();
        $setPromotBrokerNum = $vasBroker->getUsedSetPromotBrokerNum();
        //特权最低等级
        $permi = new GrowthLevelWayPermission();
        $minLevel = $permi->findMinLevelByWayAliasAndType('setPromot',2);
        $p['show_time'] = 0;
        $p['name'] = "定投推广";
        $p['level'] = $minLevel;
        $p['using'] = $setPromotBrokerNum;//正在使用人数
        $p['intro'] = '开放平台最核心推广位，优势资源向您倾斜！日均百万的访问量，高强度长时间的曝光展示，保证有效覆盖意向客户。使用房豆自由定额投放推广，合理管理您的预算。客户来电接不停，助您立马开单！';//介绍
  
        if(GB_CITY == 'nj'){
            $documentation = "http://zsb.house365.com/help/nj/";
        }else{
            $documentation = "http://zsb.house365.com/help/";
        }
        $p['levelnoenough'] = 0;
        
        if($brokerType==1) {
            $p['allow'] = TRUE;//是否允许
        } else {
            $p['allow'] = FALSE;//是否允许
        }
        
        if($p['allow']){
            if($brokerLevel>=$minLevel){
                $p['btn'] = array('link'=>'/vas_set_promot/schoolHouse/','text'=>'快去定投','class'=>'btnColor_p');   
                $p['img'] = 'dt.png';
                $p['allow'] = TRUE;
            }  else {
                $p['levelnoenough'] = 1;
                $p['btn'] = array('link'=>$documentation,'text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
                $p['img'] = 'dtgray.png';
                $p['allow'] = FALSE;
            } 
            
        }else{
             $p['btn'] = array('link'=>'javascript:void(0)','text'=>'开通付费才可参与','class'=>'btnColor_p_no ');
             $p['img'] = 'dtgray.png';
        }
        
        return $p;
        
    }
    
    /**
     * 房源包装特权
     * getPrivilegePackingHouse
     * @param type $brokerLevel 经纪人等级
     * @param type $brokerType 经纪人类型，付费or免费
     * @param type $brokerCredit 经纪人房豆
     * @param type $packageid 经纪人套餐类型
     */
    public function getPrivilegePackingHouse(){
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
        
        //已使用的人数        
        $vasBroker = new VasBroker();
        $PackingHouseBrokerNum = $vasBroker->getUsedPackingHouseBrokerNum();
        //特权最低等级
        $permi = new GrowthLevelWayPermission();
        $minLevel = $permi->findMinLevelByWayAliasAndType('packingHouse',2);
        $p['show_time'] = 0;
        $p['name'] = "房源包装";
        $p['level'] = $minLevel;
        $p['using'] = $PackingHouseBrokerNum;
        $p['intro'] = '百万流量的推广通道为您独家定制，整合平台资源，为您提供全方位的房源包装方案。深度挖掘您的房源卖点，契合买房人偏好，我们致力为您提供专业、优质、省心的房源包装。开单，就这么简单！';//介绍
  
        if($brokerType==1) {
            $p['allow'] = TRUE;//是否允许
        } else {
            $p['allow'] = FALSE;//是否允许
        }
        
        if(GB_CITY == 'nj'){
            $documentation = "http://zsb.house365.com/help/nj/";
        }else{
            $documentation = "http://zsb.house365.com/help/";
        }
        $p['levelnoenough'] = 0;
        if($p['allow']){
            if($brokerLevel>=$minLevel){
                $p['btn'] = array('link'=>'/vas_packing_house/','text'=>'快去购买','class'=>'btnColor_p');
                $p['img'] = 'bz.png';
                $p['allow'] = TRUE;
            }  else {
                $p['levelnoenough'] = 1;
                $p['btn'] = array('link'=>$documentation,'text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
                $p['img'] = 'bzgray.png';
                $p['allow'] = FALSE;
            }       
            
        }else{
             $p['btn'] = array('link'=>'javascript:void(0)','text'=>'开通付费才可参与','class'=>'btnColor_p_no ');
             $p['img'] = 'dtgray.png';
        }
        
        return $p;
        
    }





	 /**
     * 标签零售特权
     * getPrivilegePackingHouse
     * @param type $brokerLevel 经纪人等级
     * @param type $brokerType 经纪人类型，付费or免费
     * @param type $brokerCredit 经纪人房豆
     * @param type $packageid 经纪人套餐类型
     */
    public function getPrivilegeTagSale(){
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
        $CI =& get_instance();
        $CI->load->model('house_tag_record_model');       
		$use_num = $CI->house_tag_record_model->findPersonNum();
        //特权最低等级
        $permi = new GrowthLevelWayPermission();
        $minLevel = $permi->findMinLevelByWayAliasAndType('tagSale',2);
        $p['show_time'] = 0;
        $p['name'] = "标签零售";
        $p['level'] = $minLevel; 
		$p['using'] = $use_num[0]['number'];
        $p['intro'] = '还在担心每月套餐赠送的标签不够用？7天不到急推房就火速成交了，效果这么好还再等什么？急推标签也推叠加包了，即买即用使用更灵活，未使用标签月底不清零，心动不如行动！';//介绍
  
        if($brokerType==1) {
            $p['allow'] = TRUE;//是否允许
        } else {
            $p['allow'] = FALSE;//是否允许
        }
        
        if(GB_CITY == 'nj'){
            $documentation = "http://zsb.house365.com/help/nj/";
        }else{
            $documentation = "http://zsb.house365.com/help/";
        }
        $p['levelnoenough'] = 0;
        if($p['allow']){
            if($brokerLevel>=$minLevel){
                $p['btn'] = array('link'=>'/tag_index/','text'=>'快去购买','class'=>'btnColor_p');
                $p['img'] = 'tagsale2.png';
                $p['allow'] = TRUE;
            }  else {
                $p['levelnoenough'] = 1;
                $p['btn'] = array('link'=>$documentation,'text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
                $p['img'] = 'tagsale_none2.png';
                $p['allow'] = FALSE;
            }       
            
        }else{
             $p['btn'] = array('link'=>'javascript:void(0)','text'=>'开通付费才可参与','class'=>'btnColor_p_no ');
             $p['img'] = 'tagsale_none2.png';
        }
        
        return $p;
        
    }

    /**
     * 放心看房源推广
     */
    public function getPrivilegeGuideProject(){
        $p['name']  = "放心看推广";
        $p['theme'] = "guide_project";
        $p['levelnoenough'] = 0;
        $p['show_time'] = 0;
        $p['intro'] = '针对“放心看”房源进行主题式包装，PC端和APP端多渠道推广，增加房源的曝光量，促进成交！';
        $p['btn'] = array('link'=>'/guide_project/','text'=>'去推广','class'=>'btnColor_p');
        $p['img'] = 'guide_project.png';
        return $p;
    }
    
    

    /**
     * 求购匹配特权
     * @param type $brokerLevel
     * @param type $brokerType
     * @param type $brokerCredit
     * @param type $packageId
     * @return string
     */
    public function getPrivilegeBuy() {
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
        $key = "PrivilegeBuy_".$brokerLevel."_".$brokerType."_".$brokerCredit."_".$packageId;
        $p = $this->mc->get($key);//$p = false;
        if($p===false) {
            $p = array();
            $permi = new GrowthLevelWayPermission();
            $minLevel = $permi->findMinLevelByWayAliasAndType('matchBuy',2);
            $growthCwp = new GrowthCreditWayPackage();
            $minCredit = $growthCwp->getScoreByWayAliasAndPackageId('matchBuy', $packageId);
            $p['name'] = '求购匹配';
            $p['level'] = $minLevel;//等级要求
            if($brokerType==1&&$brokerCredit>=$minCredit&&$brokerLevel>=$minLevel) {
                $p['allow'] = TRUE;//是否允许
            } else {
                $p['allow'] = FALSE;//是否允许
            }
            //$gbuserbroker = new GrowthBidUserBroker();
            //$p['using'] = $gbuserbroker->findCountByType(1);//正在使用人数
            $CI =& get_instance();
            $CI->load->model('buy_match_model');
            $p['using'] = $CI->buy_match_model->get_matchednum();

            
            $p['intro'] = '这里有迫切求购意向的精准客户，这里有细化到区位、面积、价格的真实需求，这里更有100%真实可靠的客户联系方式。使用房豆兑换即可匹配需求，抓住精准用户，迅速开单，从求购匹配开始！';//介绍
            $p['img'] = $p['allow']?'tqqgppIco.png':'tqqgppIco.png';//tqqgppIco.png,tqqzppIco.png
            $btn = array();
            if($p['allow']) {
                $btn = array('link'=>'/house_demands/buy/','text'=>'快去参与','class'=>'btnColor_p');
            }else {
                if($brokerType!=1) {
                    $btn = array('link'=>'javascript:void(0)','text'=>'暂无权限','class'=>'btnColor_p_no btnColor_p_no_w150');
                }elseif ($brokerCredit<500) {
                    $btn = array('link'=>'javascript:void(0)','text'=>'房豆不够','class'=>'btnColor_p_no btnColor_p_no_w150');
                } elseif ($brokerLevel<$minLevel) {
                    $btn = array('link'=>'javascript:void(0)','text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
                }
            }
            $p['btn'] = $btn;
            $this->mc->set($key,$p,6000);
        }
        return $p;
    }

    /**
     * 求租匹配特权
     * @param type $brokerLevel
     * @param type $brokerType
     * @param type $brokerCredit
     * @param type $packageId
     * @return string
     */
    public function getPrivilegeRent() {
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
        $key = "PrivilegeRent_".$brokerLevel."_".$brokerType."_".$brokerCredit."_".$packageId;
        $p = $this->mc->get($key); //$p = false;
        if($p===false) {
            $p = array();
            $permi = new GrowthLevelWayPermission();
            $minLevel = $permi->findMinLevelByWayAliasAndType('matchRent',2);
            $growthCwp = new GrowthCreditWayPackage();
            $minCredit = $growthCwp->getScoreByWayAliasAndPackageId('matchRent', $packageId);
            //var_dump($minCredit);exit;
            //$minCredit = 500;
            $p['name'] = '求租匹配';
            $p['level'] = $minLevel;//等级要求
            if($brokerType==1&&$brokerCredit>=$minCredit&&$brokerLevel>=$minLevel) {
                $p['allow'] = TRUE;//是否允许
            } else {
                $p['allow'] = FALSE;//是否允许
            }
            //$gbuserbroker = new GrowthBidUserBroker();
            //$p['using'] = $gbuserbroker->findCountByType(1);//正在使用人数
            $CI =& get_instance();
            $CI->load->model('rent_match_model');
            $p['using'] = $CI->rent_match_model->get_matchednum();
            $p['intro'] = '这里有迫切求购意向的精准客户，这里有细化到区位、面积、价格的真实需求，这里更有100%真实可靠的客户联系方式。使用房豆兑换即可匹配需求，抓住精准用户，迅速开单，从求购匹配开始！';//介绍
            $p['img'] = $p['allow']?'tqqzppIco.png':'tqqzppIco.png';//tqqgppIco.png,tqqzppIco.png
            $btn = array();
            if($p['allow']) {
                $btn = array('link'=>'/house_demands/rent/','text'=>'快去参与','class'=>'btnColor_p');
            }else {
                if($brokerType!=1) {
                    $btn = array('link'=>'javascript:void(0)','text'=>'暂无权限','class'=>'btnColor_p_no btnColor_p_no_w150');
                }elseif ($brokerCredit<500) {
                    $btn = array('link'=>'javascript:void(0)','text'=>'房豆不够','class'=>'btnColor_p_no btnColor_p_no_w150');
                } elseif ($brokerLevel<$minLevel) {
                    $btn = array('link'=>'javascript:void(0)','text'=>'等级不够','class'=>'btnColor_p_no btnColor_p_no_w150');
                }
            }
            $p['btn'] = $btn;
            $this->mc->set($key,$p,6000);
        }
        return $p;
    }
    
    
    
    
     /**
     * level_require 等级要求
     *@param int $type 1是小区专家，2是房源竞投
     *@return string
     */
    private function level_require($type=1){
        $brokerLevel = $this->brokerLevel;
        $brokerType = $this->brokerType;
        $brokerCredit = $this->brokerCredit;
        $packageId = $this->packageId;
        $key = "level_require_".$brokerLevel."_".$brokerType."_".$brokerCredit."_".$packageId."_".$type;
        $minLevel = $this->mc->get($key);
        if($minLevel===false) {
           //小区专家竞投的等级
           $growthBidLowLevel = new GrowthBidLowLevel();
           $bidLevel = $growthBidLowLevel->findAll();
           if ($bidLevel) {
               foreach($bidLevel as $v) {
                   if ($v['type'] == $type) {
                       $minLevel = $v['level'];
                   }
               }
           }
          $this->mc->set($key,$minLevel,6000); 
        }
        return $minLevel;
    }
}

?>
