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

/**
 * 首页功能类 
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * 
 */
class welcome_model extends MY_Model{
    
    public function __construct() {
        parent::__construct(1,1,3);
   }
   
   /**
    * 查询经纪人收藏的应用管理
    * get_app_custom
    * @param int $uid
    * @return array
    */
   public function get_app_custom($uid){
        
        $key = GB_CITY.'_get_app_custom_'.$uid;
        //$this->mc->delete($key);
        $cache = $this->mc->get($key);

        if($cache['isok'] == 1)
        {
            $return_data = $cache['data'];
        }
        else
        {
            //查询字段
            $this->dbback->select('app_custom');
            //查询条件
            $this->dbback->where("uid = '".$uid."'");
            //查询
            $arr_data = $this->dbback->get('broker_sub')->row_array();
            $return_data = isset($arr_data['app_custom']) ? $arr_data['app_custom'] : '';

            $this->mc->set($key, array('isok'=>1, 'data'=>$return_data), 86400);
        }

        return $return_data;
   }
   
   /**
    * 修改经纪人应用管理
    * udpate_app_custom
    * @param array $app_arr 应用数组
    * @param int $uid 用户id
    */
   public function update_app_custom($uid,$active_app_s){

       $key = $this->config_cityflag . '_get_app_custom_' . $uid;

       $data = array('uid' => $uid, 'app_custom' => $active_app_s);
       $this->db->replace('broker_sub',$data);

       $this->mc->delete($key);
   }
   
  
   /**
   * 获取经纪人诚信logo
   * @param int $uid 用户id
   * @return int
   */
   public function get_honesty_status($uid)
   {
       $key = $this->config_cityflag.'_get_honesty_status2_'.$uid;
       $cache = $this->mc->get($key);

       if($cache['isok'] == 1)
       {
           $data = $cache['data'];
       }
       else
       {
           $list = $this->dbback->select('honesty_status')->get_where('broker',array('uid'=>$uid))->row_array();
           $data = $list['honesty_status'];
           $this->mc->set($key, array('isok'=>1, 'data'=>$data), 600);
       }

       return $data;
   }

    /**
   * 获取经纪人删除数据
   * @param int $uid 用户id
   * @return int
   */
    public function get_delete_data($uid)
   {
	   //error_reporting(7);
	   //ini_set("display_errors",true);

       $key = $this->config_cityflag.'_get_delete_data3_'.$uid;
	  
       $cache = $this->mc->get($key);

	   //$this->mc->delete($key);
		
       if($cache['isok'] == 1)
       {	

           $data = $cache['data'];
       }
       else
       {
		   $result =  $this->dbback->query("select count(id) as num_total  from  broker_delete_record where uid ='$uid' and ctime >1456156800");
			
           $arr =$result->result_array();
		   
		   if(is_array($arr) && empty($arr))
		   {
			   $data =10;
		   }
		   else
		   {
			   $data = $arr[0]['num_total'];
			   $data = 10-$data%5;
		   }
		   // echo $data."<br/>";
		   
           $this->mc->set($key, array('isok'=>1, 'data'=>$data), 60);
       }
	   
       return $data;
   }

   /**
   * 获取经纪人冻结数据
   * @param int $uid 用户id
   * @return int
   */
   public function get_freeze_data($uid)
   {
       $key = $this->config_cityflag.'_get_freeze_data2_'.$uid;
	  
       $cache = $this->mc->get($key);
		
	   //$this->mc->delete($key);

       if($cache['isok'] == 1)
       {	

           $data = $cache['data'];
       }
       else
       {
		   $result =  $this->dbback->query("select count(id) as num_total from broker_freeze_record where uid ='$uid' and ctime >1456156800");
		
           $arr =$result->result_array();
	
		   if(is_array($arr) && empty($arr))
		   {
			   $data =2;
		   }
		   else
		   {
			   $data_total = $arr[0]['num_total'];
			   
			   $data_mod   =$data_total%2;
			   $data       =2-$data_mod;
		   }
		  
           $this->mc->set($key, array('isok'=>1, 'data'=>$data), 60);
       }
	   
       return $data;
   }

    /**
     * 获取经纪人真房源数据
     * @param int $uid 用户id
     * @return arr
     */
    public function broker_realhouse_info($uid){
        //查询字段
        $this->dbback->select('id,lucupdatetime');
        //查询条件
        $this->dbback->where("uid = '".$uid."'and esta=1 and is_real_house=1");
        //查询
        $arr_data = $this->dbback->get('sell')->result_array();
        return $arr_data;
    }

    /**
     * 获取经纪人下各真房源最新的设置log
     * @param int $uid 用户id
     * @return arr
     */
    public function get_set_real_log($house_id){
        //查询字段
        $this->dbback->select('ctime');
        //查询条件
        $this->dbback->where("rowid = '".$house_id."'and type=1 and tbl='sell'");
        $this->dbback->order_by('id','DESC');
        //查询
        $arr_data = $this->dbback->get('real_house_set_log')->row_array();
        return $arr_data;
    }

    /**
     * 获取经纪人真房源过期时间的设置log
     * @param int $uid 用户id
     * * @param int $house_id 房源id
     * @return arr
     */
    public function  get_real_house_expire_log($uid,$house_id,$lucupdatetime){
        //查询字段
        $this->dbback->select('create_time');
        //查询条件
        $this->dbback->where("uid = '".$uid."'and house_id = '".$house_id."' and lucupdatetime = '".$lucupdatetime."'");
        $this->dbback->order_by('id','DESC');
        //查询
        $arr_data = $this->dbback->get('real_house_expire_log')->row_array();
        return $arr_data;
    }

    /**
     *@explain 发送站内信 send_systemmsg($subject,$msg)
     *@param	$subject 主题 $msg  信息内容
     *@return 无
     *@ xz 2012-9-24
     **/
    public function send_systemmsg($subject,$msg,$uid){

        $ss             = array();
        $ss[kind]		= "system";
        $ss[subject]	= "【系统信息】".$subject;
        $ss[message]	= $msg;
        $ss[operator]	= "system";
        $ss[creattime]	= time();
        $ss[issystem]	= 1;

        if($this->db->insert("agent_announce",$ss)){
            $newid = $this->db->insert_id();//自增的用法
            $ss = array();
            $ss['announceid'] = $newid;
            $ss['agentcode'] = 'B000';
            $ss['uid'] = $uid;
            $ss["new"] = 1;
            //写入数据库
            $this->db->insert("agent_announceitem" ,$ss);
        }
    }

    /**
     * 添加经纪人真房源过期时间的log
     * @param int $uid 用户id
     * @param int $house_id 房源id
     * @param int $lucupdatetime 真房源设置时间
     * @return arr
     */
    public function send_real_house_expire_log($uid,$house_id,$lucupdatetime){
        $arr = array();
        $arr['uid']              = $uid;
        $arr['house_id']         = $house_id;
        $arr['create_time']      = time();
        $arr['lucupdatetime']    = $lucupdatetime;
        $this->db->insert('real_house_expire_log', $arr);

    }

}




