<?php if (!defined('BASEPATH'))  exit('No direct script access allowed');
/**
 * 二手房400电话
 * @author cw
*/
class Esf_400_model extends MY_Model
{
	
	private $_esf_400_tbl = 'esf_400_info';
	function __construct()
	{
		parent::__construct();
		$this->memkey = 'esf_400'.GB_CITY;
	}

	/**
     * 通过手机查找信息
     * @author cw
    */
	public function get_info_by_phone($phone)
	{
		$phone = intval($phone);
		$sql = "select id,num_group,city,uid,phone,status from esf_400_info where phone = '$phone' ";
		$query = $this->db_esfbak->query($sql);
		$data = $query->result_array();
		return $data;
	}

	/**
     * 通过uid查找信息
     * @author cw
    */
	public function get_info_by_uid($uid , $city = 0)
	{
		$uid = intval($uid);
		$sql = "SELECT id,num_group,city,uid,phone,status FROM esf_400_info";
		$cond_where = " WHERE uid = '$uid' ";
		if( $city > 0 )
		{
			$cond_where .= " AND city = '".intval($city)."' ";
		}
		$query = $this->db_esfbak->query($sql.$cond_where);
		$data = $query->result_array();

		return $data;
	}

	/**
     * 通过记录id查找记录信息
     * @author bianshiwu
    */
	public function get_info_by_callRecord_id($callRecord_id){
        if(!$callRecord_id) return array();
		 
		$sql = "SELECT callRecord_id FROM esf_400_recoder";
		$sql .= " WHERE callRecord_id = '{$callRecord_id}' ";
 
		$query = $this->db_esfbak->query($sql);
		$data = $query->result_array();
		return $data;
	}

	/**
     * 通过 短号 查找信息
     * @author cw
    */
	public function get_info_by_group($num_group)
	{
		$num_group = intval($num_group);
		$sql = "select id,num_group,city,uid,phone,status from esf_400_info where num_group = '$num_group' ";
		$query = $this->db_esfbak->query($sql);
		$data = $query->result_array();
		return $data;
	}
	
	/**
     * 分配400 插入用户信息更新状态
     * @author cw
    */
	public function issue($uid,$name,$phone,$num_group,$city)
	{
		$uid	= intval($uid);
		$name	= trim($name);
		$phone	= intval($phone);
		$city	= intval($city);
		$num_group = intval($num_group);

		$arr = array('num_group'=>$num_group);
		$up  = array(	
			'uid'	=>$uid,
			'name'	=>$name,
			'phone'	=>$phone,
			'status'=>1,
			'city'	=>$city,
			'overtime'=>0
		);
		$this->set_table('esf_400_info');
		$result = $this->save($arr,$up,'esf');		
		return $result;
	}

	/**
     * 保存返回400通话记录
     * @author cw
    */
	public function save_recoder($data)
	{
		$this->set_table('esf_400_recoder');
		$result = $this->add($data,'esf');		
		return $result;
	}


	/**
     * 更改状态位 status 分配情况:0未1已分2暂停3真空
     * @author cw
    */
	public function update_staus($arr,$status)
	{
		$status = intval($status);
		$up  = array('status'=>$status);
		$this->set_table('esf_400_info');
		$result = $this->save($arr,$up,'esf');		
		return $result;
	}
	

    /**
    * 更新400绑定手机号码
    *
    * @access public
    * @param  int  $uid 账号编号
	* @param  int  $telno_new 新的手机号码
	* @param  int  $telno_old 旧的手机号码
    * @return boolean 是否成功
    */
	public function update_telno($uid , $telno_new ,$telno_old)
	{	
		$uid = intval($uid);
		$telno_new = strip_tags($telno_new);
		$telno_old = strip_tags($telno_old);

		$result = 0;

		if($uid > 0 && $telno_old > 0 && $telno_new > 0 )
		{
			$up_fileds  = array('phone'=>$telno_new);
			$cond_where = array('uid'=>$uid,'phone'=>$telno_old);
			$this->set_table('esf_400_info');
			$result = $this->save($cond_where , $up_fileds ,'esf');
		}

		return $result;
	}


	/**
     * 获取一个新的400短号
     * @author cw
    */
	public function get_group($start=0,$end=0,$flag=1)
	{
		if($flag == 1)
		{
			$rand = rand($start,$end);
			$sql = "select id,num_group from esf_400_info where num_group >= $rand and status = 0 limit 1";
		}
		else
		{
			$sql = "select id,num_group from esf_400_info where num_group >=600000 and num_group <601000 and status = 0 limit 1";
		}
		$query = $this->db_esf->query($sql);
		$data = $query->result_array();
		return $data[0]['num_group'];
	}
		
	/**
     * 释放400 转真空期 30天
     * @author cw
    */
	public function release($uid)
	{
		$uid = intval($uid);
		$arr = array('uid'=>$uid);
		$up  = array(	
			'uid'	=>0,
			'name'	=>'',
			'phone'	=>'',
			'status'=>3,
			'city'	=>0,
			'overtime'	=> time() + 30 * 86400
		);
		$this->set_table('esf_400_info');
		$result = $this->save($arr,$up,'esf');		
		return $result;
	}


	/**
     * 释放超过真空期的号码
     * @author cw
    */
	public function release_telno()
	{
		
		$arr = array('overtime<='=>time());
		$up  = array(	
			'uid'	=> 0,
			'name'	=> '',
			'phone'	=> '',
			'status'=> 0,
			'city'	=> 0,
			'overtime' => 0
		);
		$this->set_table('esf_400_info');
		$result = $this->save($arr,$up,'esf');
		return $result;
	}
	

	/**
     * 预设置值,用时开启
     * @author cw
    */
	public function set_table_data()
	{	/*	
		$this->set_table('esf_400_info');
		for($i=800000;$i<=899999;$i++)
		{
			$data   = array(
				'num_group' => $i
			);		

			//插入数据
			$this->add( $data );
	   }
	   */
	   return true;
	}

	
	/**
    * 获取各种状态的400号码数量
    *
    * @access public
	* @param  int  $state 新的手机号码
	* @param  int  $city 城市编号
    * @return int 400号码数量
    */
	public function count_group_num( $state = 0 , $city = 1 )
	{	
		$state = intval($state);
		$city = intval($city);

		$where_cond = '';

		if( $state > 0 )
		{
			$where_cond .=  "status = '".$state."'";
		}

		if( $city > 0 )
		{	
			$where_cond .= $where_cond == '' ? '': ' AND ';
			$where_cond .= "city = '".$city."'";
		}
		
		$where_cond .= $where_cond == '' ? '': ' AND ';
		$where_cond .= "flag = '1' AND uid > 0 ";

		//查询条件
		$this->db_esfbak->where( $where_cond );

		//查询表
		$this->db_esfbak->from('esf_400_info');

		//获取数据
		$total_num = $this->db_esfbak->count_all_results();

		return $total_num;
	}

	

	/**
    * 获取各种状态的400号码记录
    *
    * @access public
	* @param  int  $state 新的手机号码
	* @param  int  $city 城市编号
    * @return array 400号码记录
    */
	public function get_group_num( $state = 0 , $city = 1 , $offset = 0 , $limit = 10 )
	{	
		$state = intval($state);
		$city = intval($city);

		$where_cond = '';

		if( $state > 0 )
		{
			$where_cond .=  "status = '".$state."'";
		}

		if( $city > 0 )
		{	
			$where_cond .= $where_cond == '' ? '': ' AND ';
			$where_cond .= "city = '".$city."'";
		}
		
		$where_cond .= $where_cond == '' ? '': ' AND ';
		$where_cond .= "flag = '1' AND uid > 0 ";

		$this->db_esfbak->where( $where_cond );
		$arr_data = $this->db_esfbak->get( $this->_esf_400_tbl , $limit , $offset )->result_array();

		return $arr_data;
	}

	/**
    * 获取400电话对应的房源信息
    * @parms int $uid 用户uid
    * @return array
	* bianshiwu 2014-12-11
    */
	public function get400HousesByUid($uid){
		if(!$uid) return array();
        
		$sql = "select id,infotype,district,streetid,price,buildarea,room from sell where uid = {$uid} and infofrom = 1 and tel400 = 1 and esta = 1 ";
 
        $query = $this->db_esfhousebak->query($sql);
		return $query->result_array(); 
	}

	/**
    * 查询是否是中介公司的电话号码
    * @parms int $telno 电话号码
    * @return array
	* bianshiwu 2014-12-11
    */
	public function checkAgencyTel($telno){
		if(!$telno) return array();
        
		$sql = "select id from agencytel  where telno = '{$telno}' ";
        $query = $this->db_esfhousebak->query($sql);
		return $query->result_array(); 
	}

   /**
    * 将400通话信息保存到统计表里面（esf_400_statistics）
    * @parms array $data
    * @return 
	* bianshiwu 2014-12-11
    */
	public function saveStatistics($data){
		$param = "num_group=".$data['num_group']."&callphone=".$data['callphone'];
  $url = "api.house365.com/esf/web/api_4002.php?stc=".urlencode($param);
		//ECHO $url;
		curl_get_contents($url);
        if($data['num_group'] && $data['callphone']){

			//号码暂停后，过滤
			if(!$data['calledphone']) return array();

            //过滤固定电话
			$telno = trim($data['callphone']);
			$flag = substr($telno,0,1);
			if($flag !== '1') return array();
$param = "num_group=".$data['num_group']."&callphone=".$data['callphone'];
  $url = "api.house365.com/esf/web/api_4003.php?stc=".urlencode($param);
		//ECHO $url;
		curl_get_contents($url);
            //检查是否是中介电话号码,过滤中介号码
			$checkResult = $this->checkAgencyTel($telno);
			if($checkResult) return array();
$param = "num_group=".$data['num_group']."&callphone=".$data['callphone'];
  $url = "api.house365.com/esf/web/api_4004.php?stc=".urlencode($param);
		//ECHO $url;
		curl_get_contents($url);
			$call_info = $this->get_info_by_group($data['num_group']);
			if(!$call_info) return false;//400分号不存在
$param = "num_group=".$data['num_group']."&callphone=".$data['callphone'];
  $url = "api.house365.com/esf/web/api_4005.php?stc=".urlencode($param);
		//ECHO $url;
		curl_get_contents($url);
            $info = array('callid'=>$data['callid'],
				          'num_group'=>$data['num_group'],
				          'call_phone'=>$data['callphone'],
				          'called_phone'=>$data['calledphone'],
				          'call_result'=>$data['callResult'],
				          'call_result'=>$data['callResult'],
				          'indate'=>$data['indate']
				    );
            
			//获取400电话对应的房源
			$houseInfo = $this->get400HousesByUid($call_info[0]['uid']);
			$num = count($houseInfo);
        $param = "num_group=".$data['num_group']."&callphone=".$data['callphone'];
  $url = "api.house365.com/esf/web/api_4006.php?stc=".urlencode($param);
			//如果只有一条有记录，则入库
			if($num == 1) {
				$info['houseid'] = $houseInfo[0]['id'];
				$info['infotype'] = $houseInfo[0]['infotype'];
				$info['district'] = $houseInfo[0]['district'];
				$info['streetid'] = $houseInfo[0]['streetid'];
				$info['buildarea'] = $houseInfo[0]['buildarea'];
                $info['price'] = $houseInfo[0]['price'];
				$info['room'] = $houseInfo[0]['room'];
            }

			$this->set_table('esf_400_statistics ');
			return $this->add($info,'db_esfhouse');
		}else{
			return array();
		}

	}

}

?>