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

// ------------------------------------------------------------------------
/**
 * 中介公司类
 *
 * 中介公司类文件功能描述
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          fisher
 */
class Agency_base_model extends MY_Model {
    /**
     * 中介表
     * @var string 
     */
    private $_agency_tbl = 'agency';
    
    /**
     * 片区门店对应关系
     * @var string 
     */
    private $_area_agency_tbl = 'area_agency';
    
    /**
     * 查询字段
     * @var string 
     */
    public $select_fields = '';
    
    /**
     * 缓存key
     * @var string 
     */
    private $_mem_key ;
    
    /**
    * 类初始化
    */
    public function __construct()
    {
        parent::__construct(0,1,2);
        
        $this->_mem_key = GB_CITY.'_agency_base_model';
    }
    
    /**
     * 设置需要查询的字段
     * @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_str;
    }
    
    /**
     * 获取需要查询的字段
     * @return string
     */
    public function get_select_fields()
    {
        return $this->select_fields;
    }
    
    
    /**
     * 根据公司编号获取门店所属片区及总公司
     * @param string $companycode 公司编号
     * @return array
     */
    public function get_area_parentcode($companycode)
    {
        $this->dbback->where('agentcode', $companycode);
        return $this->dbback->get($this->_area_agency_tbl)->row_array();
    }


    /**
     * 根据公司编号获取公司信息
     * @param string $agentcode 公司编号
     * @return array
     */
    public function get_company_info_by_code($agentcode)
    {	
        $parent_info = array();

        if($agentcode != '')
        {	
            $select_fields = $this->get_select_fields();

            if($select_fields != '')
            {
                $this->dbback->select($select_fields);
            }
            
            $mem_key = $this->_mem_key.'get_company_info_by_code2_'.$agentcode.'_'.$select_fields;
            $cache = $this->mc->get($mem_key);

            if(isset($cache['is_ok']) && $cache['is_ok'] == 1)
            {
                $parent_info = $cache['data'];
            }
            else
            {
                $this->dbback->where('agentcode', $agentcode);
                $parent_info = $this->dbback->get($this->_agency_tbl)->row_array();
                
                $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$parent_info),3600);
            }
        }

        return $parent_info;
    }
	
	
    /**
    * 获取公司所属片区信息（片区ID及总公司agentcode）
    * @param string $agentcode  门店帐号
    * @retrun void
    **/
    public function get_relation_by_agentcode( $agentcode )
    {   
        $arr_data = array();
        
        if($agentcode != '')
        {
            $cond_where = "agentcode = '".$agentcode."'";
            $this->dbback->where($cond_where);
            $arr_data = $this->dbback->get($this->_area_agency_tbl)->row_array(); 
        }
        return $arr_data;
    }
}
/* End of file agency_base_model.php */
/* Location: ./models/agency_base_model.php */