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

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

/**
 * 小区业务逻辑层
 *
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          fisher
 */

load_m("block_base_model");
class Block_zhou_model extends Block_base_model 
{
    /**
     * 小区图库引用记录
     * @var type 
     */
    private $_tbl_block_img_used = 'block_img_used_count';
	
	/**
     * 缓存KEY
     * @var private 
     */
	private $_mem_key = 'block_model';
    
    /**
     * 类初始化
     */
    public function __construct() 
    {
        parent::__construct();
    }
    
    /**
     * 根据小区编号获取基本记录
     * @param int $id 小区编号
     * @return array 小区基本记录组成的一维数组
     */
    public function base_info_by_id($id)
    {
        $this->load->model('street_model');
        $base_info_fields = array(
            'id', 'blockname', 'streetid', 'esta',
            'district', 'address', 'averprice'
        );
        $this->set_select_fields($base_info_fields);
        $base_info = $this->find_by_id($id);
        if ($base_info)
        {
            $base_info['averprice'] = floor($base_info['averprice']);
            $base_info['blockid'] = $id;
            $street = $this->street_model->find_by_id($base_info['streetid']);
            $base_info['streetname'] = $street['streetname'];
            $base_info['esta_block'] = $this->find_blocktype_by_esta($base_info['esta']);
            $base_info['url_block'] = $this->find_blockurl_by_id($base_info['id']);
        }
        return $base_info;
    }
    
    /**
     * 查找模匹配某个小区名称的列表
     * @param string $blockname 小区名称
     * @param int $num 查找显示最多个数
     * @return array 返回匹配到小区记录所组成的二维数组
     */
    public function auto_blockname($blockname, $num = 10)
    {
        $this->load->model('street_model');
        $base_info_fields = array(
            'id','blockname','district', 'streetid',
            'averprice','address', 'esta'
        );
        $this->set_select_fields($base_info_fields);
        $block_list = $this->find_like_by_blockname($blockname);
        if (is_array($block_list) && !empty($block_list))
        {
            $street_ids = array();
            foreach($block_list as $v)
            {
                if (isset($v['streetid']) && $v['streetid'] > 0)
                {
                    $street_ids[] = $v['streetid'];
                }
            }
            $where = "id in ('".  implode("','", $street_ids)."')";
            $this->street_model->set_select_fields(array('id', 'streetname'));
            $streetnames = $this->street_model->find_by($where);
            $streetnames = changeToKeyArray($streetnames, 'id');
            foreach($block_list as &$v)
            {
                if (isset($v['streetid']) && $v['streetid'] > 0)
                {
                    $v['streetname'] = $streetnames[$v['streetid']]['streetname'];
                }
                else 
                {
                    $v['streetname'] = '';
                }
                $v['averprice'] = floor($v['averprice']);
                $v['label'] = $v['blockname'];
                $v['esta_block'] = $this->find_blocktype_by_esta($v['esta']);
                $v['url_block'] = $this->find_blockurl_by_id($v['id']);
            }
        }
        else
        {
            $block_list[] = array(
                'label' => '无相似小区，点击添加',
                'id'    => 0,
            );
        }
        return $block_list;
    }
    
    /**
     * 添加临时小区
     * @param string $blockname 小区名称
     * @param string $blockaddress 小区地址
     * @param string $district 区属
     * @param int $streetid 板块编号
     * @return array 返回一维数组 array(code => 200, id => 222) 
     * code结果编码 id为成功后小区编号
     */
    public function add_temp_block($blockname, $blockaddress, $district, $streetid)
    {
        $result = array('code' => '', 'id' => '');
        //验证提交的各个字段(长度)
        $len_blockname = strlen(trim($blockname));
        if( $len_blockname < 4 || $len_blockname > 16 ){
            $result['code'] = 100;
            return $result; //小区名称在2-8个汉字之间
        }
        //是否有相同小区
        $blockname_num = $this->find_count_by_blockname($blockname);
        if ($blockname_num > 0)
        {
            $result['code'] = 300;
            return $result; //已存在同名小区！
        }
        $len_district = strlen(trim($district));
        if($len_district <= 0 )
        {
            $result['code'] = 400;
            return $result; //区属必须选择！
        }
        if(trim($streetid) <= 0 )
        {
            $result['code'] = 500;
            return $result; //版块必须选择！
        }
        $blockaddress = trim($blockaddress) ;
        $len_blockaddress = strlen($blockaddress);
        if($len_blockaddress <= 4 || $len_blockaddress >=100 )
        {
            $result['code'] = 600;
            return $result; //小区地址在2-50汉字之间！
        }
        else
        {
            $blockaddress_num = $this->find_count_by_address($blockaddress);
            if($blockaddress_num > 0)
            {
                $result['code'] = 700;
                return $result; //小区地址在2-50汉字之间！
            }
        }
        $this->load->helper('common_string_helper');
        $this->load->library('Spell', '', 'spell');
        $insert_data = array();
        $insert_data['blockname'] =  trim($blockname) ;
        $insert_data['address']	 =  trim($blockaddress)  ;
        $insert_data['district']  =  $district;
        $insert_data['streetid']	 =  $streetid;
        $insert_data['bi_s']		 =	strtoupper(csubstr($this->spell->s_spell(trim($blockname)),1));
        /*app-控制申请小区专家时是否可以编辑提交小区记录,
            0-表示不可编辑小区记录不可申请小区专家,
            1-表示可申请小区专家，但是不可编辑小区记录
            2-表示可申请小区专家，可以编辑小区记录
        */
        $insert_data['app']			=  1;
        $insert_data['esta']			=  0;
        $insert_id = $this->insert($insert_data);
        if ($insert_id > 0)
        {
            $result['code'] = 200;
            $result['id'] = $insert_id;
            return $result;
        }
        else 
        {
            $result['code'] = 800;
            return $result;
        }
    }
    
    /**
     * 插入图库图片使用记录
     * @param array $insert_data 插入图库数组
     * @return int 返回插入成功后的id
     */
    public function insert_block_img_used($insert_data)
    {
        if ($this->db->insert($this->_tbl_block_img_used, $insert_data))
        {
            return $this->db->insert_id();
        }
    }
    
    /**
     * 查询所有参与新房直售的小区信息
     * @param string $city 城市参数
     * @return array 小区信息数组
     */
    public function get_nofee_block( $city = 'nj')
    {
        //查询全部参与新房免佣的小区
        $nofee_block_arr = array();
        $cond_where = 'nofee = 1';
        $mem_key = $this->_mem_key.'_get_nofee_block_'.$city.'_'.$cond_where;
        //$this->mc->delete($mem_key);
        $cache = $this->mc->get($mem_key);

        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $nofee_block_arr = $cache['data'];
        }
        else
        {   
            $nofee_block_arr = $this->find_by_cond($cond_where);
            $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$nofee_block_arr),  3600);
        }
        
        return $nofee_block_arr;
    }

	/*
     * 根据blockid查找是否在排除之内
     * @access public 
     * @return int 次数
     */
	public function get_block_remove_info_by_blockid($blockid)
	{

		$mem_key = $this->_mem_key.'_get_block_remove_info_by_blockid'.$blockid;
        $cache = $this->mc->get($mem_key);
		$this->mc->delete($mem_key);
		if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $status = $cache['data'];
        }
        else
        {
			$this->db->select('id,status,num');
			$cond_where = "block_id = '$blockid' and status =1 ";
			$this->db->where( $cond_where );
			
			$arr_data = $this->db->get('block_remove_list')->result_array(); 
			
			
			if(is_array($arr_data) && !empty($arr_data))
			{
				$status =$arr_data[0]['num'];
				$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$status) ,  86400);
			}
			else
			{
				$status =0;
			}
			
			
		}
		
		return $status;
	}
}