<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * zsb
 *
 * 求购匹配类
 *
 * @package         zsb
 * @author          EllisLab Dev Team
 * @copyright      Copyright (c) 2006 - 2014, HOUSE365, Inc.
 * @link            http://nj.sell.house.com
 * @since           Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * sell_nofee_base_model CLASS
 *
 * 二手房新房直售属性类
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          xz
 */
//加载父类
load_m("sell_attr_model");
class Sell_nofee_base_model extends Sell_attr_model{
    
    /**
     * Real房标识对应值
     * @var const
     */
    const  NOFEE_VALUE = 1;

    /**
     * 缓存标识
     * @var private
     */
    private $_mem_key;
    
    /*
     * 构造函数
     */
    public function __construct() 
    {
        parent::__construct();

		$this->_mem_key = GB_CITY.'sell_nofee_base_model';
    }
    
    
    /**
     * 获取新房直售属性值
     * @var const
     */
    public function get_nofee_value()
    {
        return self::NOFEE_VALUE;
    }
    
    /**
     * 判断房源是否为新房直售房源
     *
     * @access	public
     * @return	boolean  是否为新房直售房源
     */
    public function is_nofee()
    {
        $is_nofee = FALSE;
        
        $house_info = $this->init_houseinfo();
        
        if( is_array( $house_info ) && !empty( $house_info ) )
        {   
            $is_nofee = self::NOFEE_VALUE == $house_info['nofee'] 
                    ? TRUE : FALSE;
        }
        
        return $is_nofee;
    }
    
    
    /**
     * 设置nofee房源
     *
     * @access  public
     * @return  boolean  是否设置成功
     */
    public function set_nofee()
    {   
        $result = FALSE;
        
        $house_info = $this->init_houseinfo();
        $rowid = intval($house_info['id']);
        
        if( $rowid > 0 )
        {   
            $update_info = array();
            
            //需要设置的房源属性值
            $update_info['nofee'] = self::NOFEE_VALUE;
            
            //设置多图房源
            $result = $this->update_house_atrr_val( $update_info , $rowid );
        }
        
        return $result;
    }
    
    
    /**
     * 设置nofee房源
     *
     * @access  public
     * @return  boolean  是否设置成功
     */
    public function remove_nofee()
    {   
        $result = FALSE;
        
        $house_info = $this->init_houseinfo();
        $rowid = intval($house_info['id']);
        
        if( $rowid > 0 )
        {   
            $update_info = array();
            
            //需要设置的房源属性值
            $update_info['nofee'] = 0;
            
            //设置多图房源
            $result = $this->update_house_atrr_val( $update_info , $rowid );
        }
        
        return $result;
    }
    
    
    /**
     * 检测是房源是否符合新房直售设置的标准
     *
     * @access  protected
     * @return  boolean  是否符合新房直售房源标准
     */
    public function check_nofee_standard()
    {   
        $result = array();
        
        $houseinfo = $this->init_houseinfo();
        $rowid = $houseinfo['id'];
        $blockid = intval($houseinfo['blockid']);
        $this->load->model('broker_model');
        
        /** 非多图房源不可设置 **/
        $this->load->model('sell_star_base_model');
        $star_value = $this->sell_star_base_model->get_isstar_value();

        if( $houseinfo['isstar'] != $star_value )
        {
            //新房房源
            $result['is_ok'] = 0;
            $result['msg'] = '非多图房源不可设置';
            return  $result; 
        }
        
        /** 新房直售数量已满 **/
        $uid = intval($houseinfo['uid']);
        $nofee_limit = $this->get_nofee_num_limit($uid);
        $nofee_today = $this->get_nofee_num_by_uid($uid);
        if( $nofee_limit <= $nofee_today)
        {
            $result['is_ok'] = 0;
            $result['msg'] = '新房直售数量已满';
            return  $result;
        }
        
        /*** 非新楼盘房源不可设置 ***/
        $this->load->model('block_model');
        $nofee_block = $this->block_model->get_nofee_block();
        $nofee_blockid = array();
        if( is_array( $nofee_block ) && !empty( $nofee_block ))
        {
            foreach($nofee_block as $v) {
                $nofee_blockid[] = $v['id'];
            }
        }
        if(!in_array( $blockid , $nofee_blockid ) )
        {
            $result['is_ok'] = 0;
            $result['msg'] = '非新楼盘房源不可设置';
            return  $result;
        }
        
        /*** 已设置急推标签 ***/
        $this->load->model('sell_tag_base_model');
        $tag_value = $this->sell_tag_base_model->get_istag_value();
        if( $houseinfo['istag'] == $tag_value )
        {
            //新房房源
            $result['is_ok'] = 0;
            $result['msg'] = '已设置急推标签';
            return  $result; 
        }
        
        /*** 已设置急推标签 ***/
        $this->load->model('sell_real_base_model');
        $isreal_value = $this->sell_real_base_model->get_isreal_value();
        if( $houseinfo['isreal'] == $isreal_value )
        {
            //新房房源
            $result['is_ok'] = 0;
            $result['msg'] = '已设置REAL房标签';
            return  $result; 
        }
        
        $this->broker_model->set_uid($houseinfo['uid']);
        $broker_info = $this->broker_model->get_broker_info();
        
        /*** 经纪人不能再黑名单 ***/
        if( is_array( $broker_info ) && !empty( $broker_info ) )
        {
            if( $broker_info['festa'] == 1 )
            {
                //冻结状态
                $result['is_ok'] = 0;
                $result['msg'] = '经纪人在黑名单中，无法设置标签房源';
                
                return  $result;
            }
        }
        else
        {
            //经纪人信息为空
            $result['is_ok'] = 0;
            $result['msg'] = '经纪人信息为空';
            return  $result;
        }

        /*** 多图房源、状态只能为有效状态 ***/
        if( $houseinfo['esta'] != 1 )
        {
            $result['is_ok'] = 0;
            $result['msg'] = '房源非激活状态，无法设置新房直售';
            return  $result;
        }
        
        $result['is_ok'] = 1;
        $result['msg'] = '设置新房直售';
        
        return $result;
    }
    
    
    /**
     * 根据UID查询新房直售房源的个数
     *
     * @access  public
     * @param   int  $uid 经纪人编号
     * @return  boolean  新房直售房源的个数
     */
    public function get_nofee_num_by_uid($uid)
    {   
        $nofee_num = 0;
        
        if( $uid > 0)
        {	
            $mem_key = $this->_mem_key.'_get_nofee_num_by_uid_'.$uid;
            $cache = $this->mc->get($mem_key);

            if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
            {
                $nofee_num = $cache['data'];
            }
            else
            {
                $this->load->model('sell_base_model');
                $cond_where = "uid = $uid  AND esta IN (1,5) AND  infofrom = '5' AND nofee = '".self::NOFEE_VALUE."'";
                $nofee_num = $this->sell_base_model->find_count_by($cond_where,1);

                $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$nofee_num),  10);
            }
        }

        return $nofee_num;
    }
    
    
    /**
     * 根据UID查询新房直售房源的个数上限
     *
     * @access  public
     * @param   int  $uid 经纪人编号
     * @return  boolean  新房直售房源的个数上限
     */
    public function get_nofee_num_limit($uid)
    {   
        $nofee_num_limit = 0;
        
        if( $uid > 0)
        { 
            $this->load->model('broker_model');
            $this->broker_model->set_uid($uid);
            $broker_info = $this->broker_model->get_broker_info();
            $nofee_num_limit = (is_array($broker_info) && !empty($broker_info)) ?
                    $broker_info['nofee_num'] : 0 ;
        }
        
        return $nofee_num_limit;
    }
}
