<?php

//消息类
class Message_model extends MY_Model{
    /**
     *表名
     * @var string
     */
    protected $tbl = '';
    
    /**查询的字段
     * @var string
     */
    protected $select_fields = '';
    
    /**
     * 连表查询
     * @var array
     */
    protected $join_arr = array();
    
    /**
     * 连表类型
     * @var string
     */
    protected $join_type = '';
     
    /**
     * 缓存key
     * @var string
     */
    private $_mem_key = "message_model";


    /**
     * 类初始化
     */
    public function __construct() 
    {
        parent::__construct(1,1,0);
    }
    
    
     /**
     * 设置查询数据的表名
     * @param string $tbl
     */
    public function set_tbl($tbl) 
    {
        $this->tbl = $tbl;
    }
    
    /**
     * 获取查询数据的表名
    * @return string $tbl 表名
    */
   public function get_tbl() {
           return $this->tbl;
   }
    
     /**
     * 设置需要查询的字段
     * @param array $select_fields
     */
    public function set_select_fields($select_fields)
    {
        $select_fields_str = '';
        if(isset( $select_fields ) && !empty($select_fields))
        {   
            $select_fields_str = implode( ',', $select_fields) ;
        }
        $this->select_fields = $select_fields;
    }
    
    
    /**
     * 获取需要查询的字段
     * @return type
     */
    public function get_select_fields()
    {
        return $this->select_fields;
    }
    
    /**
     * 设置联合查询
     */
    public function set_join_table($join_arr,$join_type){
        $this->join_arr = $join_arr;
        $this->join_type = $join_type;
    }
    
    /**
     * 清空联合查询
     */
    public function clear_join_table(){
        $this->join_arr = '';
        $this->join_type = '';
    }
    
    /**
     * 获取联合查询
     */
    public function get_join_table(){
        return $this->join_arr;
    }
    
     /**
     * 获取消息列表页
     * @param string $where 查询条件
     * @param int $start 查询开始行
     * @param int $limit 数据偏移量
     * @param int $order_key 排序字段
     * @param string $order_by 升序、降序，默认降序排序
     * @return array 返回消息数组
     */
    public function find_all_by($where, $start = 0, $limit = 20, 
            $order_key = 'id' , $order_by = 'DESC') 
    {
        
        //查询字段
        if ($this->select_fields)
        {
            $this->dbback->select($this->select_fields);
        }
		
		//查询条件
		$this->dbback->where($where);
		//排序条件
		$this->dbback->order_by($order_key, $order_by);

		if ( $start >= 0 && $limit > 0 )
		{
			$this->dbback->limit($limit, $start);
		}
		
		if(is_array($this->join_arr)&&!empty($this->join_arr))
		{
			foreach ($this->join_arr as $key=>$value)
			{
				$this->dbback->join($key,$value,$this->join_type);
			}            
		}

		$result_array = $this->dbback->get($this->tbl)->result_array();
        
        return $result_array;
    }
    
    
    /**
     * 获取满足查询条件的信息条数
     * @param string $where 查询条件
     * @return int 信息数量
     */
    public function find_count_by($where)
    {   
		$this->dbback->where($where);
		$this->dbback->from($this->tbl);
		
		if(is_array($this->join_arr)&&!empty($this->join_arr))
		{
			foreach ($this->join_arr as $key=>$value)
			{
				$this->dbback->join($key,$value,$this->join_type);
			}            
		}
		
		$count = $this->dbback->count_all_results();
      

        return $count;
    }
    
    
    /**
     * 根据查询条件返回一条信息的记录
     * @param string $where 查询条件
     * @return array 返回一条一维数组的信息记录
     */
    public function find_one_by($where = '')
    {
        //查询字段
        if ($this->select_fields)
        {
            $this->dbback->select($this->select_fields);
        }
        //查询条件
        $this->dbback->where($where);       
        if(is_array($this->join_arr)&&!empty($this->join_arr)){
            foreach ($this->join_arr as $key=>$value){
                $this->dbback->join($key,$value,$this->join_type);
            }            
        }
        return $this->dbback->get($this->get_tbl())->row_array();
    }
    
    /**
     * 删除信息
     * delete_message
     * @param str $del_where 删除条件
     * @param int $id_field 表主键
     * @param array $id_arr需要删除的记录id
     */
    public function delete_message($del_where,$id_field,$id_arr)
	{
        if(is_array($id_arr)&&!empty($id_arr))
		{	
			if($id_field != '' && !empty($id_arr))
			{
				$this->db->where_in($id_field,$id_arr);
			}

			if($del_where != '')
			{
				$this->db->where($del_where);
			}
             
            $this->db->delete($this->get_tbl());

			if( $_COOKIE['admin_debug'] && $_COOKIE['admin_debug'] == 1)
			{
				echo $this->db->last_query();
			}

			return $this->db->affected_rows();
        }
    }
    
    /**
     * 预约看房删除
     * see_house_del
     * @param int $uid 用户id
     * @param array $id_arr 删除id数组
     */
    public function sell_house_del($uid,$id_arr)
	{
        $this->db->where('uid',$uid);

        if(is_array($id_arr)&&!empty($id_arr))
		{
            $this->db->where_in('id',$id__str);
            $db->update($this->tbl,array('esta'=>2));           
        }        
        
    }
    

    
    /**
     * 修改消息查看状况
     * update_message_status
     * @param array $where_cond where条件
     * @param string  $flag_field 标志位字段 
     * @param string $flag_value 标志位的值
     */
    public function update_message_status($where_cond,$flag_field,$flag_value)
	{ 
        $this->db->where($where_cond);
        $this->db->update($this->get_tbl(),array($flag_field=>$flag_value));
		//echo $this->db->last_query();
    }
    

    //获取未读信息
    public function get_message_no_read($uid)
	{
        //公告
        $no_announce_num = $this->get_announce_no_read($uid);

        //系统
        $no_system_num = $this->get_announce_no_read($uid,'system');

        //店铺留言
        $no_shop_num = $this->get_shop_no_read($uid);

        //预约看房
        $no_see_num = 0;
        $no_read_count = $no_announce_num + $no_system_num + $no_shop_num + $no_see_num;
        $no_read_num = array($no_announce_num,$no_system_num,$no_shop_num,$no_see_num,$no_read_count);
       
        return $no_read_num;
    }
    

    //系统未读信息数量
    private function get_announce_no_read($uid,$type='')
	{       
        $tbl = 'agent_announceitem';        
        $this->set_tbl($tbl);       
        $join_arr = array('agent_announce'=>'agent_announceitem.announceid=agent_announce.id');
        $this->set_join_table($join_arr,'left');
        $strInfofrom = $type == 'system' ? 'agent_announce.issystem = 1 ' : 'agent_announce.issystem = 0 ';       
        $cond_where_no = "agent_announceitem.uid = $uid and $strInfofrom and agent_announceitem.new = 1";
        $no_read_num = $this->find_count_by($cond_where_no);
     
        return $no_read_num;
    }
    

    
    //店铺留言未读信息数量
    private function get_shop_no_read($uid)
	{
        $tbl = "broker_guestbook";
        $this->set_tbl($tbl);  
        $this->clear_join_table();
        $cond_where_no = "uid = $uid and new = 1";
        $no_read_num = $this->find_count_by($cond_where_no);
        return $no_read_num;
    }
    

    //预约看房未读信息数量
    private function get_see_no_read($uid){
        $tbl = "see_house";
        $this->set_tbl($tbl);
        $this->clear_join_table();
        $cond_where_no = "uid = $uid and esta = 1 and new = 1";        
        $no_read_num = $this->find_count_by($cond_where_no); 
        return $no_read_num;
    }


	public function get_annouce_list_by_cond($cond_where,$start,$limit)
	{
		//查询字段
        if ($this->select_fields)
        {
            $this->dbback->select($this->select_fields);
        }

        //查询条件
		
		$this->dbback->limit($limit,$start);
        $this->dbback->where($cond_where);
		$this->dbback->order_by('id','asc');
        return $this->dbback->get($this->get_tbl())->result_array();
	}


	public function annouce_count_by($where)
    {   
		$this->dbback->where($where);
		$this->dbback->from($this->tbl);

		$count = $this->dbback->count_all_results();
      

        return $count;
    }


    public function delete_message_annouce($del_where)
	{	

		
		if($del_where != '')
		{
			$this->db->where($del_where);
			$this->db->delete('agent_announce');
		}

		if( $_COOKIE['admin_debug'] && $_COOKIE['admin_debug'] == 1)
		{
			echo $this->db->last_query();
		}

		return $this->db->affected_rows();
    }
}