<?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
 */

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

/**
 * Rent_reduce_base_model CLASS
 *
 * 出租立减标签
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          xz
 */
//加载父类
load_m("rent_attr_model");

class Rent_reduce_base_model extends Rent_attr_model {
    
    /**
     * 立减房源标识对应值
     * @var const
     */
    const  ISREAL_VALUE = 1;
    
    
    /**
     * 类初始化
     */
    public function __construct() 
    {
        parent::__construct();
    }
    
    
    /**
     * 判断房源是否为立减房源
     *
     * @access	public
     * @return	boolean  是否为立减房源
     */
    public function is_reduce()
    {
        $is_reduce = FALSE;
        
        $house_info = $this->init_houseinfo();
        
        if( is_array( $house_info ) && !empty( $house_info ) )
        {
            $is_reduce = self::ISREAL_VALUE == $house_info['isreal'] 
                    ? TRUE : FALSE;
        }
        
        return $is_reduce;
    }
    
    
    /**
     * 设置立减房源
     *
     * @access  public
     * @return  boolean  是否设置成功
     */
    public function set_reduce()
    {   
        $result = FALSE;
        
        $house_info = $this->init_houseinfo();
        $rowid = intval($house_info['id']);

        if( $rowid > 0)
        {
            $update_info = array();

            //需要设置的房源属性值
            $update_info['isreal'] = self::ISREAL_VALUE;

            //设置多图房源
            $result = $this->update_house_atrr_val( $update_info , $rowid );
        }
        
        return $result;
    }
    
    
    /**
     * 取消立减标签
     *
     * @access  protected
     * @return  boolean  取消标签是否成功
     */
    public function remove_reduce()
    {   
        $result = FALSE;
        
        $house_info = $this->init_houseinfo();
        $rowid = intval($house_info['id']);
        
        if( $rowid > 0 )
        {   
            $update_info = array();
            
            //需要设置的房源属性值
            $update_info['isreal'] = 0;
            
            //取消Real房属性
            $result = $this->update_house_atrr_val( $update_info , $rowid );
        }
        
        return $result;
    }
    
    
    /**
     * 检测房源是否符合立减房源的标准
     *
     * @access  public
     * @return  boolean  是否符合立减房源的标准
     */
    public function check_real_standard()
    {   
        //房源信息
        $houseinfo = $this->init_houseinfo();
        $result = array();
        $uid = intval($houseinfo['uid']);
        
        $this->load->model('broker_model');
        $this->broker_model->set_uid($uid);
        $broker_info = $this->broker_model->get_broker_info();
        
        //是否已经是立减房源
        $is_reduce = $this->is_reduce();
        if($is_reduce)
        {
            //冻结状态
            $result['is_ok'] = 0;
            $result['msg'] = '该房源已设置立减标签';
            return  $result;
        }
        
        /*** 经纪人不能再黑名单 ***/
        if( is_array( $broker_info ) && !empty( $broker_info ) )
        {   
            if(isset($broker_info['festa']) && $broker_info['festa'] == 1 )
            {
                //冻结状态
                $result['is_ok'] = 0;
                $result['msg'] = '经纪人在黑名单中，无法设置立减标签';
                return  $result;
            }
        }
        else
        {
            //经纪人信息为空
            $result['is_ok'] = 0;
            $result['msg'] = '经纪人信息为空';
            return  $result;
        }
        
        //租房立减上限
        $favtag_num_limit = intval($broker_info['favtag_num']);
        
        //当前立减标签房源个数
        $favtag_num = $this->get_reduce_num_by_uid($uid);
        if( $favtag_num >= $favtag_num_limit)
        {
            $result['is_ok'] = 0;
            $result['msg'] = '立减标签已用完';
            return  $result;
        }
        
        /*** 非多图房源不可设置立减标签 ***/
        $this->load->model('sell_star_base_model');
        $isstar_value = $this->sell_star_base_model->get_isstar_value();
        if( $houseinfo['isstar'] != $isstar_value )
        {
            $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;
    }
    
    
    /**
     * 根据条件查到Real房记录条数
     *
     * @access  protected
     * @param   string $cond_where 查询条件
     * @return  boolean  是否符合Real房源的标准
     */
    public function get_real_num_by_cond($cond_where)
    {
        $real_num = 0;
       
        if( $cond_where != '')
        {
            $this->dbback->where($cond_where);
            $real_num = $this->dbback->count_all_results(self::REAL_TBL);
        }
        
        return $real_num;
    }
    
    
    /**
     * 根据UID查询待审核和通过的审核的立减房源个数
     *
     * @access  public
     * @param   int  $uid 经纪人编号
     * @return  int  立减房源个数
     */
    public function get_reduce_num_by_uid( $uid )
    {   
        $real_num = 0;
        
        if( $uid > 0)
        {   
            $this->load->model('rent_base_model');
            $cond_where = "uid = $uid AND esta = 1 AND infofrom = '5' AND isreal = 1 ";
            $real_num = $this->rent_base_model->find_count_by($cond_where);
        }
        
        return $real_num;
    }
}
/* End of file rent_favtag_base_model.php */
/* Location: ./applications/models/rent_favtag_base_model.php */