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

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

/**
 * House_freeze_base_model CLASS
 *
 * 房源冻结类
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          sun
 * @link            
 */
class House_freeze_base_model extends MY_Model {
    
    /**
     * 冻结表名称
     * @var string 
     */
    const FREEZE_TBL = 'house_freeze';
    
    /**
     * 冻结房源类型
     * @var string 
     */
    private $_tbl = '';
    
    /**
     * 冻结记录的状态
     * @var array 
     */
    private $_freeze_esta = array('1' =>'冻结', '2' => '申诉', '3' => '解冻', '4' => '拒绝');
    
    /**
     * 需要查询的数据表字段
     * @var string 
     */
    protected $select_fields = '';

    /**
     * 冻结房源类型
     * @param string $tbl
     */
    public function set_tbl($tbl) 
    {
        $this->_tbl = $tbl;
    }
    
    /**
     * 获取冻结房源类型
     * @return string $tbl 表名
     */
    public function get_tbl() 
    {
         return $this->_tbl;
    }
    
    /**
     * 获取房源状态数组
     * @return string $house_esta 房源状态数组
     */
    public function get_freeze_esta() 
    {
        return $this->_freeze_esta;
    }
    
    
    /**
     * 类初始化
     */
    public function __construct() 
    {
        parent::__construct(1,1,1);
    }

	public function database(){
		return $this->dbback;
	}
    
    
    /**
     * 设置需要查询的字段
     * @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;
    }
    
    
    /**
     * 通过房源编号获取房源冻结日志记录
     * @param int $rowid 房源编号
     * @param int $esta 冻结日志状态
     * @return array 冻结日志记录
     */
    public function get_freeze_by_rowid( $rowid , $esta = '' ) 
    {   
        $freeze_info = array();
       
        //查询字段
        if ($this->select_fields)
        {
            $this->dbback->select($this->select_fields);
        }
        
        $tbl = $this->get_tbl();
        
        if( $rowid > 0 && $tbl != '' )
        {
            //查询条件
            $this->dbback->where('rowid', $rowid);
            $this->dbback->where('tbl', $this->get_tbl());
            if( $esta != '')
            {
                $this->dbback->where('esta', $esta);
            }
            
            
            $freeze_info = $this->dbback->get(self::FREEZE_TBL)->row_array();
        }
        
        return $freeze_info;
    }
    
    
    /**
    * 冻结房源申诉
    *
    * @access	public
    * @param	int $rowid 房源编号
    * @param	array $data_info 房源数组
    * @return	boolean 是否申诉成功
    */
    public function house_appeal ( $rowid , $data_info  )
    {
        $reslut = FALSE;
        
        if( $rowid > 0 && $this->_tbl != '')
        {
            $cond_where =  "rowid = '".$rowid."' AND tbl = '".$this->_tbl."' ";
            $this->db->where($cond_where);
            $this->db->update( self::FREEZE_TBL , $data_info);
            
            $reslut = $this->db->affected_rows() > 0 ? TRUE : FALSE;
        }
        
        return $reslut;
    }

	/**
    * 冻结房源图片信息插入
    *
    * @access	public
    * @param	int $rowid 房源编号
    * @param	array $data_info 房源图片信息
    * @return	boolean 是否申诉成功
    */
    public function house_pic_insert ($tbl, $rowid , $data_pic_info  )
    {
		
        $reslut = FALSE;
        $num =count($data_pic_info['pics_arr']);

		$this->db->delete('house_freeze_pic' , array('tbl' => $tbl, 'house_free_id' => $rowid ) ); 
		
		for($i =0;$i<$num;$i++)
		{
			$data_info['house_free_id'] = $rowid;
			$data_info['position'] = $i;
			$data_info['filename'] = $data_pic_info['pics_arr'][$i];

			//$data_info['title'] = $data_pic_info['titles_arr'][$i];

			$data_info['sort'] = 2;

			$data_info['tbl'] = $tbl;

			$data_info['createtime'] = time();
		
			$this->db->insert( 'house_freeze_pic' , $data_info );
       
			$result = ( $this->db->affected_rows() == 1 ) ? $this->db->insert_id() : 0;
		
		}
		 
        return $result;
        
       
    }

	/**
    * 冻结房源图片删除
    *
    * @access	public
    * @param	int $rowid 房源编号
    * @param	array $data_info 房源图片信息
    * @return	boolean 是否申诉成功
    */
    public function house_pic_delete ($tbl, $rowid)
    {
		

		$this->db->delete('house_freeze_pic' , array('tbl' => $tbl, 'house_free_id' => $rowid ) ); 
		
		 
        return $result;
        
       
    }

}
/* End of file house_freeze_base_model.php */
/* Location: ./application/models/house_freeze_base_model.php */
