<?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_match_base_model CLASS
 *
 * 出售急推标签管理类
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          xz
 */
//加载父类
load_m("sell_attr_model");

class Sell_tag_base_model extends Sell_attr_model {
   
    /**
     * 急推标识对应值
     * @var const
     */
    const  ISTAG_VALUE = 1;
    
    /**
     * 房源标签记录表
     * @var const
     */
    const TAG_TBL = 'house_tag';
    
    /**
     * 缓存key
     * @var const
    */
    private $_mem_key;
    
    /**
     * 类初始化
     */
    public function __construct() 
    {
        parent::__construct();
        
        //设置房源表名称
        $this->set_house_tbl('sell');
        
        //mem_key
        $this->_mem_key = GB_CITY.'sell_tag_base_model';
    }
    
    /**
     * 获取急推属性值
     * @var const
     */
    public function get_istag_value()
    {
        return self::ISTAG_VALUE;
    }
    
    /**
     * 根据房源编号获取急推标签记录信息
     *
     * @access  public
     * @return  array   急推标签信息
     */
    public function get_tag_record($clear_cache = 0)
    {
        $data_info = array();
        
        $house_info = $this->init_houseinfo();
        $rowid = intval($house_info['id']);
        
        if( $rowid > 0 )
        {   
            $mem_key = $this->_mem_key.'_get_tag_record_'.$rowid;
			if( $clear_cache == 1)
			{
				$this->mc->delete($mem_key);
			}
            $cache = $this->mc->get($mem_key);

            if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
            {
                $data_info = $cache['data'];
            }
            else
            {
                $house_kind = $this->get_house_tbl();
                $cond_where = "rowid = '".$rowid."' AND tbl = '".$house_kind."' AND esta = 1 order by id desc";
                $this->dbback->where($cond_where);
                $data_info = $this->dbback->get( self::TAG_TBL )->row_array();
                
                $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$data_info),  600);
            }
        }
        return $data_info;
    }
    
    
    /**
     * 判断房源是否为急售房源
     *
     * @access	public
     * @return	boolean  是否为急推(标签)房源
     */
    public function is_tag()
    {
        $is_tag = FALSE;
        
        $house_info = $this->init_houseinfo();
        
        if( is_array( $house_info ) && !empty( $house_info ) )
        {
            $is_tag = self::ISTAG_VALUE == $house_info['istag'] 
                    ? TRUE : FALSE;
        }
        
        return $is_tag;
    }
    
    
    /**
     * 设置标签房源
     *
     * @access  public
     * @return  boolean  是否设置成功
     */
    public function set_tag()
    {
        $result = FALSE;
        $result_add = 0;
        
        $house_info = $this->init_houseinfo();
        $rowid = intval($house_info['id']);

		/*if($house_info['is360'] == '2')
		{
			$expiretime = strtotime( date("Y-m-d 23:59:59") ) + 86400 * 13;
			
		}
		else
		{*/
			$expiretime = strtotime( date("Y-m-d 23:59:59") ) + 86400 * 6;
		//}

        //标签房源记录
        $tag_info = array();
        $tag_info['rowid']  = $rowid;
        $tag_info['tbl']    = $this->get_house_tbl();
        $tag_info['uid']    = intval($house_info['uid']);
        $tag_info['addtime']    = time();
        //$tag_info['expiretime'] = strtotime( date("Y-m-d 23:59:59") ) + 86400 * 6;
		 $tag_info['expiretime'] = $expiretime;
		 $tag_info['reason'] = 1; //添加
        $tag_info['operatetime'] = time();
        $tag_info['ip'] = GetIP();

        //添加标签房源记录
        $result_add = $this->_add_tag_record($tag_info);
        
       	//修改标签表house_tag_package状态 xue 2015-05-19        
        $this->load->model("tag_package_base_model");
		$result_update = $this->tag_package_base_model->update_tag_record($tag_info['uid']);
        //更改房源属性值

        if( $result_add > 0 && $result_update)
        {   
            $update_info = array();
            
            //需要设置的房源属性值
            $update_info['istag'] = self::ISTAG_VALUE;
            
            //设置多图房源
            $result = $this->update_house_atrr_val( $update_info , $rowid );

			//2016-11-3 经纪人添加标签日志 --- start ------>

			$tag_info = array();
			$tag_info['uid']    = intval($house_info['uid']);
			if($this->get_house_tbl()=='sell')
			{
				$tag_info['tbl']    = '1';
			}else
			{
				$tag_info['tbl']    = '2';
			}
			
			$tag_info['rowid']  = $rowid;
			$tag_info['type']   = 1;
			
			$tag_info['time']    = time();
			$tag_info['ip']		 = get_ip();

			if( is_array($tag_info) && !empty($tag_info) )
			{
				$this->db->insert( 'log_broker_tag' , $tag_info );
				//$result = ($this->db->affected_rows() == 1) ? $this->db->insert_id() : 0;
			}

			//2016-11-3 经纪人添加标签日志 --- end ------>
        }

        return $result;
    }

	/**
     * 获取房源标签数据
	 *
	 */
	public function get_house_tag($data = array())
	{
		$result = array();

		if(empty($data))
		{
			return $result;
		}
		
		$time = time();

		$this->db->select('id,rowid,tbl,uid,addtime,expiretime,esta');
        $this->db->where('rowid', $data['houseId']);
        $this->db->where('tbl', $data['tbl']);
		$this->db->where('esta', $data['esta']);
		$this->db->where('expiretime >=', $time);
		
        return $this->db->get(self::TAG_TBL)->row_array();
	}
	
	/**
     * 更新房源标签过期时间
	 *
	 */
	public function edit_tag_expiretime($data = array())
	{
		$result = false;

		if(empty($data))
		{
			return $result;
		}
		
		$this->db->where("id",$data['id']);
		$this->db->update( self::TAG_TBL, array('expiretime' => $data['expiretime']));
		
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
    
    
    /**
     * 设置标签房源
     *
     * @access  private
     * @return  int  大于0添加成功，等于0插入失败，-1 数据为空
     */
    private function _add_tag_record( $data_info  )
    {   
        $result = -1;
        
        if( is_array($data_info) && !empty($data_info) )
        {
            $this->db->insert( self::TAG_TBL , $data_info );
            $result = ($this->db->affected_rows() == 1) ? $this->db->insert_id() : 0;
        }
        
        return $result;
    }
    
    
    /**
     * 移除标签房源
     *
     * @access  public
     * @param $reason 操作标签的原因，默认是手动失效
     * @return  boolean  是否移除成功
     */
    public function remove_tag($reason = 2)
    {   
        $result = FALSE;
        
        $house_info = $this->init_houseinfo();
        $rowid = intval($house_info['id']);
        
        if( $rowid > 0)
        {
            //失效房源
            $result_invalid = $this->_invalid_tag_by_rowid($rowid,$reason);

            if( $result_invalid === TRUE )
            {   
                $update_info = array();

                //需要设置的房源属性值
                $update_info['istag'] = 0;

                //设置多图房源
                $result = $this->update_house_atrr_val( $update_info , $rowid );

				//2016-11-3 经纪人取消标签日志 --- start ------>

				$tag_info = array();
				$tag_info['uid']    = intval($house_info['uid']);
				if($this->get_house_tbl()=='sell')
				{
					$tag_info['tbl']    = '1';
				}else
				{
					$tag_info['tbl']    = '2';
				}
				
				$tag_info['rowid']  = $rowid;
				$tag_info['type']   = 2;
				
				$tag_info['time']    = time();
				$tag_info['ip']		 = get_ip();

				//添加标签房源记录
				 if( is_array($tag_info) && !empty($tag_info) )
				{
					$this->db->insert( 'log_broker_tag' , $tag_info );
					//$result = ($this->db->affected_rows() == 1) ? $this->db->insert_id() : 0;
				}

				//2016-11-3 经纪人取消标签日志 --- end ------>
            }
        }
        
        return $result;
    }
    
    
    /**
     * 失效房源标签记录
     *
     * @access  private
     * @return  boolean  是否设置成功
     */
    private function _invalid_tag_by_rowid( $rowid , $reason = 2)
    {   
        $result = FALSE;
        
        if( $rowid > 0 )
        {   
            $update_data = array();
            $update_data['esta'] = 2;
            $update_data['operatetime'] = time();
            $update_data['reason'] = $reason;
            $update_data['ip'] = $_SERVER['REMOTE_ADDR'];
            $tbl = $this->get_house_tbl();
            if( $tbl != '')
            {
                $cond_where = "rowid = '".$rowid."' AND tbl = '".$tbl."' AND esta = 1";
                $this->db->where($cond_where);
                $this->db->update( self::TAG_TBL, $update_data);
                $result = $this->db->affected_rows() > 0 ? TRUE : FALSE;
            }
        }
        return $result;
    }
    
    /**
     * 检测是房源是否符合急推房源的标准
     *
     * @access  public
     * @return  boolean  是否符合急推房源标准
     */
    public function check_tag_standard()
    {   
        $result = array();
        
        $houseinfo = $this->init_houseinfo();
        $this->load->model('broker_model');
        
        /** 检查房源是否为新房直售 **/
        if($houseinfo['nofee'] == 1)
        {
            //新房房源
            $result['is_ok'] = 0;
            $result['msg'] = '已设置新房直售';
            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;
        }
        
        /*** 多图房源属性判断 ***/
        $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['isstar'] != $isstar_value && (empty($houseinfo['is360']) || (!empty($houseinfo['is360']) && $houseinfo['is360'] != '2')))
        {
            $result['is_ok'] = 0;
            $result['msg'] = '房源为非多图或全景房源，无法设置标签房源';
            return  $result;
        }

        /***  是否超过标签套餐上限 （因标签零售废除） xue modify 2015-05-16***/
        /*$this->load->model('broker_package_model'); //套餐标签个数
        $package_info = array();
        $package_info = $this->broker_package_model->find_by_id($broker_info['package_id']);
        if( empty($package_info) || $package_info['package_tag_num'] == 0)
        {
            $result['is_ok'] = 0;
            $result['msg'] = '超过标签套餐上限，无法设置标签房源';
            return  $result;
        }*/
        
        /** 查看是否已经是急推房源 **/
        if( self::ISTAG_VALUE == $houseinfo['istag'])
        {
            $result['is_ok'] = 0;
            $result['msg'] = '此房源已是标签房源，无法重复设置标签房源';
            return  $result;
        }
        
          /***  经纪人当月已设置标签个数（因标签零售废除） xue modify 2015-05-16***/ 
       /* $match_num_month = $this->get_month_tagnum_by_uid($broker_info['uid'] , 1 , 1);
        if( empty($package_info) || $match_num_month >= $package_info['package_tag_num'] )
		{
            $result['is_ok'] = 0;
            $result['msg'] = '当月设置标签房源已满，无法设置标签房源';
            return  $result;
        }
		*/

		/**** 经纪人是否还有标签可设xue add 2015-05-16****/ 
	    $this->load->model("tag_package_base_model");
		$tag_valid_num = $this->tag_package_base_model->get_tag_valid_num($broker_info['uid'],1,1);		
		//var_dump($tag_valid_num);
        if($tag_valid_num<1)
		{
            $result['is_ok'] = 0;
            $result['msg'] = '当月设置标签房源已满，无法设置标签房源';
            return  $result;
        }
        
        $result['is_ok'] = 1;
        $result['msg'] = '设置急推';
        
        return $result;
    }





	 /**
     * 检测是房源是否符合急推房源的标准
     *
     * @access  public
     * @return  boolean  是否符合急推房源标准
     */
    public function check_tag_standard_list($tag_valid_num)
    {   
        $result = array();
        
        $houseinfo = $this->init_houseinfo();
        $this->load->model('broker_model');
        
        /** 检查房源是否为新房直售 **/
        if($houseinfo['nofee'] == 1)
        {
            //新房房源
            $result['is_ok'] = 0;
            $result['msg'] = '已设置新房直售';
            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;
        }
        
        /*** 多图房源属性判断 ***/
        $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;
        }
        /***  是否超过标签套餐上限 （因标签零售废除） xue modify 2015-05-16***/
        /*$this->load->model('broker_package_model'); //套餐标签个数
        $package_info = array();
        $package_info = $this->broker_package_model->find_by_id($broker_info['package_id']);
        if( empty($package_info) || $package_info['package_tag_num'] == 0)
        {
            $result['is_ok'] = 0;
            $result['msg'] = '超过标签套餐上限，无法设置标签房源';
            return  $result;
        }*/
        
        /** 查看是否已经是急推房源 **/
        if( self::ISTAG_VALUE == $houseinfo['istag'])
        {
            $result['is_ok'] = 0;
            $result['msg'] = '此房源已是标签房源，无法重复设置标签房源';
            return  $result;
        }
        
          /***  经纪人当月已设置标签个数（因标签零售废除） xue modify 2015-05-16***/ 
       /* $match_num_month = $this->get_month_tagnum_by_uid($broker_info['uid'] , 1 , 1);
        if( empty($package_info) || $match_num_month >= $package_info['package_tag_num'] )
		{
            $result['is_ok'] = 0;
            $result['msg'] = '当月设置标签房源已满，无法设置标签房源';
            return  $result;
        }
		*/

		/**** 经纪人是否还有标签可设xue add 2015-05-16****/ 
	    //$this->load->model("tag_package_base_model");
		//$tag_valid_num = $this->tag_package_base_model->get_tag_valid_num($broker_info['uid'],1,1);		
		//var_dump($tag_valid_num);
        if($tag_valid_num<1)
		{
            $result['is_ok'] = 0;
            $result['msg'] = '当月设置标签房源已满，无法设置标签房源';
            return  $result;
        }
        
        $result['is_ok'] = 1;
        $result['msg'] = '设置急推';
        
        return $result;
    }
    


     /**
     * 检测是房源是否符合急推房源的标准(列表专用)
     *
     * @access  public
     * @return  boolean  是否符合急推房源标准
     */
   /* public function check_tag_standard_list($month_tag_num)
    {   
        $result = array();
        
        $houseinfo = $this->init_houseinfo();
        $this->load->model('broker_model');
        
        /** 检查房源是否为新房直售 **/
      /*  if($houseinfo['nofee'] == 1)
        {
            //新房房源
            $result['is_ok'] = 0;
            $result['msg'] = '已设置新房直售';
            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;
        }
        
        /*** 多图房源属性判断 ***/
       /* $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;
        }
        
        /***  是否超过标签套餐上限 ***/
       /* $this->load->model('broker_package_model'); //套餐标签个数
        $package_info = array();
        $package_info = $this->broker_package_model->find_by_id($broker_info['package_id']);
        if( empty($package_info) || $package_info['package_tag_num'] == 0)
        {
            $result['is_ok'] = 0;
            $result['msg'] = '超过标签套餐上限，无法设置标签房源';
            return  $result;
        }
        
        /** 查看是否已经是急推房源 **/
       /* if( self::ISTAG_VALUE == $houseinfo['istag'])
        {
            $result['is_ok'] = 0;
            $result['msg'] = '此房源已是标签房源，无法重复设置标签房源';
            return  $result;
        }
        
        /***  经纪人当月已设置标签个数 ***/
       /* $match_num_month = $month_tag_num;
        if( empty($package_info) || $match_num_month >= $package_info['package_tag_num'] )
		{
            $result['is_ok'] = 0;
            $result['msg'] = '当月设置标签房源已满，无法设置标签房源';
            return  $result;
        }
        
        $result['is_ok'] = 1;
        $result['msg'] = '设置急推';
        
        return $result;
    }*/

    
    /**
     * 通过经纪人编号获取本月已设置的标签房源个数
     *
     * @access  public
     * @param   int $uid 经纪人账号编号
	 * @param   int $update_cache 是否清除之前的缓存重新缓存 1代表清除之前缓存
	 * @param   int $force_db 是否强制走主库，1代表走主库
     * @return  boolean  是否符合多图房源标准
     */
    public function get_month_tagnum_by_uid( $uid , $update_cache = 0 , $force_db = 0)
    {   
        $tag_num = 0;
        $this->load->model('statistics_model');
        $tag_num = $this->statistics_model->jtLabelCount($uid , $update_cache , $force_db);
        
        return $tag_num ;
    }
    
    
    /**
     * 判断某条房源是否已经设置了房源标签
     *
     * @access  public
     * @param   int $rowid 房源编号
     * @param   string $tbl 房源类型
     * @param   int $esta 记录状态
     * @return  boolean  是否存在记录，存在则返回TRUE，不存在返回FALSE 
     */
    public function is_tag_exists_by_rowid( $rowid , $tbl = 'sell' , $esta = 1 )
    {   
        $tag_num = 0;
         
        $cond_where = "rowid = '".$rowid."' AND tbl = '".$tbl."' AND esta = $esta ";
        
        $this->dbback->where($cond_where);
        
        $tag_num = $this->dbback->count_all_results(self::TAG_TBL);
        
        return $tag_num > 0 ? TRUE : FALSE ;
    }
    
    
    /**
     * 获取房源标签剩余天数
     *
     * @access  public
     * @param   int $expiretime 到期时间
     * @return  strting  剩余天数
     */
    public function tag_exipire_days( $expiretime )
    {   
	$strRemainder = '';

        if($expiretime > 0) 
        {
            $nowtime = strtotime( date('Y-m-d 00:00:00') );
            $dif    = $expiretime - $nowtime;
            
            $strRemainder = ceil( $dif / 86400 );
        }
        
        return isset($strRemainder) ? $strRemainder.'天' : '';
    }
}
/* End of file sell_tag_base_model.php */
/* Location: ./applications/models/sell_tag_base_model.php */