<?php
/**
 * 出售房源业务逻辑
 *
 * @author user
 */
load_m("rent_base_model");
class Rent_model extends Rent_base_model {
    
    /**
     * 类初始化
     */
    public function __construct() 
    {   
        parent::__construct(1,0,0);
    }
    
    /**
     * 房源模板对外接口的url地址
     * @param string $func_name 功能名称
     * @return string 相应功能url地址
     */
    public function find_func_url($func_name = '')
    {
        $url = '/' . $this->get_tbl() . '/';
        switch ($func_name)
        {
            case 'add': //发布房源页
                $url .= 'add/';
                break;
            case 'insert': //插入房源
                $url .= 'insert/';
                break;
            case 'modify': //修改房源页
                $url .= 'modify/';
                break;
            case 'update': //修改房源页
                $url .= 'update/';
                break;
            default :  //房源模板列表页
                $url .= 'manage/';
        }
        return $url;
    } 

    /**
     * 获取修改房源地址
     * @param int $id 房源编号
     * @param string $tab tab页
     */
    public function get_modify_house_url($id, $tab = '')
    {
        return $this->find_func_url('modify') . $id . '/' . $tab;
    }
    
    /**
     * 插入房源图片
     * @param type $pics
     * @param type $tbl
     * @param type $house_id
     * @param type $pic1
     * @param type $blockid
     */
    public function insert_house_pic($pics, $tbl, $house_id, $pic1, 
            $blockid, $deleteupload = '')
    {
        //调用pic_model
        $this->load->model('pic_model');
        $return = array();
        $temp = 0;
        //先删除原上传的图片，再重新添加上传图片数据，这个不知道是谁设计的
        if ($deleteupload == 1) 
        {
            $this->pic_model->del_house_pic_by($tbl, $house_id);
        }
        if (!$pics)
        {
            return $return;
        }
		$i=1;
		$indexpic = $firstpic = '';
		$temppic1 = false;	//判断设置的封面图片是否被上传
		foreach($pics['p_filename'] as $k => $v)
		{
			if (!in_array($pics['p_sort'][$k], array(1,2,0,9,10)) ) {
				echo '<script type="text/javascript">alert("上传图片的类型有误！");history.go(-1);</script>';
				exit();
			}
			$token = explode("=",$v);
			$pid = intval($token[1]);
			$filename = substr($v, 0, strpos($v, '?'));
			$idkind   = substr( $token[0],-3);
			$title = $pics['p_title'][$k]=='图片描述..限30字' ? '': strip_tags($pics['p_title'][$k]);
            $sort = $pics['p_sort'][$k];
			$flg = $sort == 9 || $sort == 10 ? 1 : 0;//图库选择的图片不需要再次审核。
			
			//是否图库图片
			$fromblockimg = false;
			//图库图片使用记录
			if($sort == 9 || $sort == 10)
			{
                //图库选择的户型图为9，外景图为10
				$sort = $sort == 9 ? 0 : ($sort == 10 ? 1 : $sort);
                $insert_data = array(
                    'blockid' => $blockid, 'tbl' => $tbl,
                    'rowid' => $house_id, 'pic' => $filename,
                    'type' => $sort, 'dateline' => date("Ymd")
                );
				$this->load->model('block_model');
                $this->block_model->insert_block_img_used($insert_data);
				$fromblockimg = true;
			}
			$sort = $sort == 9 ? 0 : ($sort == 10 ? 1 : $sort);//图库选择的户型图为9，外景图为10
			if (isset($pics['p_block'][$k])) 
            {
                $isblock = intval($pics['p_block'][$k]);
            }
            else
            {
                $isblock = 0;
            }
			$createtime = time();
            //房源图片的参数
            $insert_data_house = array(
                'tbl' => $tbl, 'rowid' => $house_id, 'position' => "pic$i",
                'filename' => $filename, 'title' => $title, 'blockid' => $blockid,
                'sort' => $sort, 'isblock' => $isblock, 'createtime' => $createtime,
                'flg' => $flg, 'isfrom' => 2, 'isinfotype' => $pics['infotype']
            );
			$picid = $this->pic_model->insert_house_pic($insert_data_house, $tbl, $house_id);
			if($idkind=='pid' && $pid > 0 && !$fromblockimg)
			{
				//审图用，获取缓存图片库中的图片信息
				$parr = $this->pic_model->find_temp_by_id($pid);
				//审图用，组合图片信息
				$return[$temp]['pictbl'] = $this->pic_model->get_upload_table_name($tbl, $house_id);
				$return[$temp]['picid'] = $picid;
				$return[$temp]['picurl'] = $filename;
				$return[$temp]['sort'] = $sort;
				$return[$temp]['surface'] = 0;
				$return[$temp]['from_device'] = $parr['from_device'];
				$return[$temp]['file_md5'] = $parr['file_md5'];
				$temp++;
			}

			$temppic1 = $temppic1 ? true : $pic1 == $filename;	//判断设置的封面图片是否被上传
			if($firstpic == '') $firstpic = $filename;
			$i++;
		}
		$indexpic = $temppic1 ? $pic1 : $firstpic;	//当封面被删除，默认设定第一张图片为封面
		//审图用，设置封面图并过滤掉来自设备的非封面图
		if(is_array($return) && !empty($return))
		{
			foreach($return as $key=>$val)
			{
				if($val['picurl'] == $indexpic)
				{	
					$return[$key]['surface'] = 1;
				}
			}
		}
        //更新房源封面图
        //$this->update(array('pic1' => $indexpic), $house_id);
		return array('pic_check' => $return, 'indexpic' => $indexpic);
    }
    
     /**
     * 修改经纪人的房源电话信息  
     * update_sell_mobile
     * @param int $mobile
     * @param string $uid 条件
     * @return void
     */
    public function update_rent_mobile($uid,$mobile){
        $cond_where['uid'] = $uid;
        $cond_where['infofrom'] = 5;
        $this->db->where($cond_where); 
        //$esta_arr = array(1,2,5);
        //$this->db->where_in('esta',$esta_arr);        
        $this->db->update($this->get_tbl(),array('telno'=>$mobile));
        
    }
    
    /**
     * 获取发布出售房源的权限
     * @param int $groupid 权限编号
     * @return boolean
     */
    public function get_public_permission($groupid)
    {
        $this->load->model('package_model');
        $package = $this->package_model->find_by_id($groupid);
        if (isset($package['package_rent_purview']) 
                && $package['package_rent_purview'] == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * 获取发布出售房源类型的权限
     * @param int $groupid 权限编号
     * @return array 可发布类型数组
     */
    public function get_public_infotype_permission($groupid)
    {
        $this->load->model('package_model');
        $package = $this->package_model->find_by_id($groupid);
        $my_allow_infotype = explode(';', $package['allow_infotype']);
        return $my_allow_infotype;
    }


    public function save_owner_rent_check($house_id,$check_code,$infotype,$city){

	    $owner = $this->dbback->from('owner_rent_check')->where(array('house_id'=>$house_id,'infofrom'=>1))->get()->row_array();
	    //更改类型后，直接删除
	    if($infotype != 1 && $owner){
		    $datau['owner_real'] = 0;
		    $this->db->where(array('house_id'=>$house_id))->update('owner_rent_check',$datau);
	    }
	    if(!$check_code || $city != 'hf' || $infotype !=1){
    		return false;
	    }

	    if($owner){
		    $data['check_code'] = $check_code;
		    $data['update_time'] = time();
		    $data['owner_real'] = 1;
		    $this->db->where(array('house_id'=>$house_id))->update('owner_rent_check',$data);
	    }else{
		    $data['infofrom'] = 1;
		    $data['house_id'] = $house_id;
		    $data['owner_real'] = 1;
		    $data['check_code'] = $check_code;
		    $data['create_time'] = time();
		    $this->db->insert('owner_rent_check', $data);
	    }

	    return true;
    }


    public function get_owner_rent_code($house_id){
	    $owner = $this->dbback->from('owner_rent_check')->where(array('house_id'=>$house_id,'owner_real'=>1,'infofrom'=>1))->get()->row_array();

	    if($owner){
	    	return $owner['check_code'];
	    }
	    return '';
    }

}
