<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * job_center_cron Class
 *
 * 用户中心定时任务类
 *
 * @package         esf_system
 * @subpackage      Libraries
 * @category        Libraries
 * @author          liuhu
 */
class job_center_cron extends MY_Controller {

    /**
    * 用户选择的城市
    *
    * @access private
    * @var string
    */
    protected  $_city = 'nj';


    /**
    * 解析函数
    *
    * @access public
    * @return void
    */
    public function __construct()
    {
        parent::__construct();

        $this->load->helper('url');
        $this->load->library("apply_class");
    }


    //默认页面
    public function index()
    {
       redirect(site_url());
    }
	

    /**
    * 个人房源匹配求购
    *
    * @access public
    * @param  无
    * @return void
    */
    public function personal_house2match_buy( $city = 'nj' )
    {

		//2016-1-13 添加JOB运行日志 ----- start ------->
		$time_start =time();
		$referer ="http://www.house365.com/center/job_center_cron/personal_house2match_buy/nj/";
		$this->job_ext_stat($referer,$time_start,0);
		
		//die("987");
		//2016-1-13 添加JOB运行日志 ----- end ------->

        $this->_city = $city;

        //城市参数、或者用户编号参数为空，则跳转到首页
        if( empty($this->_city))
        {
             redirect(site_url());
        }

        $tbl_want = 'buy';
        $tbl_match = 'buy_match';
        $tbl_house = 'sell';

        //加载房源操作统计类
        $this->load->model('house_model');

        $perpage_num = 300;
        $total_num_yesterday = $this->house_model->get_personal_house_num_yesterday($tbl_house);
        $while_num = ceil($total_num_yesterday/$perpage_num);

        $result = array();

        for( $i = 0 ; $i < $while_num ; $i++ )
        {
            $offset = $i * $perpage_num;

            //今日发布出售个人房源个数
            $houseinfo = $this->house_model->get_personal_house_yesterday($tbl_house,$offset,$perpage_num);
            $houseinfo = array_key_to_case($houseinfo);//KEY转化为小写

            //循环取出房源信息
            if( is_array($houseinfo) && !empty( $houseinfo ) )
            {
                foreach ( $houseinfo as $key => $value)
                {
                    //查询房源是否符合的求购信息
                    $buy_want  = $this->house_model->get_buy_want_by_house( $value['infotype'] , 
						$value['streetid'] , $value['buildarea'] , $value['price'] , $value['buildyear'] );
					file_put_contents('buy_want.log', date('Y-m-d h:m:s').var_export($buy_want,true),FILE_APPEND);
                    for($j = 0 ; $j < count($buy_want) ; $j++)
                    {
                        //插入求购匹配信息
                        $msg = $this->house_model->add_match_info( $tbl_want , $buy_want[$j]['id'] , 
							$buy_want[$j]['uid'] , $value['id'] , $value['uid'] );

                        if( isset($msg) && $msg['is_ok'] == 1 )
                        {
                            $matched_uid[$buy_want[$j]['id']]['uid'] = $buy_want[$j]['uid'];
                            $matched_uid[$buy_want[$j]['id']]['address'] = $buy_want[$j]['address'];
                        }
                    }
                }

                $result['msg'] = '个人房源匹配工作完成';
            }

            sleep(1);
        }

        //发送站内信
        $subject = '您的求购信息有新的匹配房源';
        foreach($matched_uid as $key => $value)
        {
            $msg = '您发起的求购信息“'.$value['address'].'”有新的房源匹配成功，'
					.'<a href="http://user.house365.com/want/buylist" target="_blank">点击查看</a>';
            $this->apply_class->send_systemmsg($value['uid'] , $subject , $msg , 1 , $this->_city);
        }

        if( $total_num_yesterday <= 0)
        {
            $result['msg'] = '昨日无新增个人房源';
        }

		$time_end =time();
		$this->job_ext_stat($referer,$time_end,1);

        echo $result['msg'];
        exit;
    }
	

    /**
    * 个人房源匹配求购
    *
    * @access public
    * @param  无
    * @return void
    */
    public function personal_house2match_rent( $city = 'nj' )
    {

		//2016-1-13 添加JOB运行日志 ----- start ------->
		$time_start =time();
		$referer ="http://www.house365.com/center/job_center_cron/personal_house2match_rent/nj/";
		$this->job_ext_stat($referer,$time_start,0);
		
		//die("987");
		//2016-1-13 添加JOB运行日志 ----- end ------->

        $this->_city = strip_tags($city);

        //城市参数、或者用户编号参数为空，则跳转到首页
        if( empty($this->_city))
        {
             redirect(site_url());
        }

        $tbl_want = 'rentwanted';
        $tbl_match = 'rentwanted_match';
        $tbl_house = 'rent';

        //加载房源操作统计类
        $this->load->model('house_model');

        $perpage_num = 300;
        $total_num_yesterday = $this->house_model->get_personal_house_num_yesterday($tbl_house);
        $while_num = ceil($total_num_yesterday/$perpage_num);

        $result = array();
        $matched_uid = array();
        for( $i = 0 ; $i < $while_num ; $i++ )
        {
            $offset = $i * $perpage_num;

            //今日发布出售个人房源个数
            $houseinfo = $this->house_model->get_personal_house_yesterday($tbl_house,$offset,$perpage_num);
            $houseinfo = array_key_to_case($houseinfo);//KEY转化为小写

            //循环取出房源信息
            if( is_array($houseinfo) && !empty( $houseinfo ) )
            {
                foreach ( $houseinfo as $key => $value)
                {
                    //查询该房源符合哪些求租信息
                    $rent_want  = $this->house_model->get_rent_want_by_house( $value['infotype'] , 
						$value['streetid'] , $value['buildarea'] , $value['price'] , $value['renttype'] );
					file_put_contents('rent_want.log', date('Y-m-d h:m:s').var_export($rent_want,true),FILE_APPEND);
                    for($j = 0 ; $j < count( $rent_want ) ; $j++)
                    {
                        //插入求租匹配信息
                        if( $rent_want[$j]['id'] > 0 && $rent_want[$j]['uid'] > 0)
                        {
                            $msg = $this->house_model->add_match_info( $tbl_want , $rent_want[$j]['id'] , 
								$rent_want[$j]['uid'] , $value['id'] , $value['uid'] );

                            if( $msg['is_ok'] == 1 )
                            {
                                $matched_uid[$rent_want[$j]['id']]['uid'] = $rent_want[$j]['uid'];
                                $matched_uid[$rent_want[$j]['id']]['address'] = $rent_want[$j]['address'];
                            }
                        }
                    }
                }

                $result['msg'] = '个人房源匹配工作完成';
            }

            sleep(1);
        }

        //发送站内信
        $subject = '您的求租信息有新的匹配房源';
        foreach($matched_uid as $key => $value)
        {
            $msg = '您发起的租房信息“'.$value['address'].'”有新的房源匹配成功，'
					.'<a href="http://user.house365.com/want/rentlist" target="_blank">点击查看</a>';
            $this->apply_class->send_systemmsg($value['uid'],$subject,$msg,1,$this->_city);
        }

        if( $total_num_yesterday <= 0)
        {
            $result['msg'] = '昨日无新增个人房源';
        }
        echo $result['msg'];

		$time_end =time();
		$this->job_ext_stat($referer,$time_end,1);
        exit;
    }


    /**
    * 失效过期房源
    *
    * @access public
    * @param  city 城市参数
    * @return void
    */
    public function invalid_expired_info( $city = 'nj' )
    {

		//2016-1-13 添加JOB运行日志 ----- start ------->
		$time_start =time();
		$referer ="http://www.house365.com/center/job_center_cron/invalid_expired_info/nj/";
		
		$this->job_ext_stat($referer,$time_start,0);
		
		//2016-1-13 添加JOB运行日志 ----- end ------->

        $this->_city = strip_tags($city);

        //城市参数、或者用户编号参数为空，则跳转到首页
        if( empty($this->_city) )
        {
             redirect(site_url());
        }

        //加载房源操作统计类
        $this->load->model('house_model');
        $result_buy = $this->house_model->invalid_buy_rentwanted_info('buy');

        sleep(2);//暂停

        $result_rentwanted = $this->house_model->invalid_buy_rentwanted_info('rentwanted');

        echo $result_buy['msg'].'-'.$result_rentwanted['msg'];

		$time_end =time();
		$this->job_ext_stat($referer,$time_end,1);

        exit;
    }


	public function  job_ext_stat($referer,$time,$value)
	{
		
		$referer =urlencode($referer);
		if($value==0)
		{
			$url = 'http://jobsadmin.house365.com/index.php?a=track&m=Api&referer='.$referer.'&start='.$time;
		}
		else
		{
			$url = 'http://jobsadmin.house365.com/index.php?a=track&m=Api&referer='.$referer.'&end='.$time;
		}
		//echo $url."<br/>";
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 3);
		$output = curl_exec($curl);
		//print_r($output);die;
		curl_close($curl);
		//return $output;
	}
	

    /**
    * 个人房源400分配号码状态管理
    *
    * @access public
    * @param  city 城市参数
    * @return void
    */
	public function telno400_state_manage($city = 'nj' , $state = 1)
	{	

		//2016-1-13 添加JOB运行日志 ----- start ------->
		$time_start =time();
		$referer ="http://user.house365.com/job_center_cron/telno400_state_manage/?city=nj";
		
		$this->job_ext_stat($referer,$time_start,0);
		
		//2016-1-13 添加JOB运行日志 ----- end ------->


		$this->load->model('esf_400_model');
		
		$group_num = $this->esf_400_model->count_group_num(1,1);
		$limit = 20;
		$do_count = (isset($group_num) && $group_num > 0) ? ceil($group_num/$limit) : 0;
		
		if( $do_count > 0 )
		{	
			for($i = 0 ; $i < $do_count ; $i ++)
			{	
				$offset = $i * $limit;
				$arr_group = $this->esf_400_model->get_group_num(1, 1 , $offset , $limit);

				if(is_array($arr_group) && !empty($arr_group))
				{	
					//加载房源MODEL
					$this->load->model('house_model');

					foreach ($arr_group as $key => $value )
					{	
						$uid = intval($value['uid']);

						if( $uid > 0)
						{	
							//echo $uid.':';
							//判断每个用户是否还存在激活房源，不存在则真空
							$house_num = $this->house_model->gethousenumbyuid( $uid , 'sell' , 1);
							if( intval($house_num) == 0)
							{
								$house_num = $this->house_model->gethousenumbyuid( $uid , 'sell' , 0);
							}

							//echo ',激活房源个数:'.$house_num;
							if( 0 == intval($house_num))
							{	
								//把分配的400失效为真空状态
								$this->esf_400_model->release($uid);
								continue;
							}
							
							//用户存在激活房源但是不存在400房源则处于暂停状态
							$arr_house_400 = array();
							$where_cond = "uid = '".$uid."' AND infofrom = '1' AND tel400 = '1'";
							$house_400_num = $this->house_model->get_house_num_by_where( $where_cond , 'sell');
							//echo ',400房源个数:'.$house_num.'<br>';
							if(intval($house_400_num) == 0 )
							{	
								//把分配的400改成暂停状态
								$arr_where = array("uid" => $uid , 'status' => 1);
								$this->esf_400_model->update_staus($arr_where , 2);
							}
						}
					}
				}
			}
		}

		$time_end =time();
		$this->job_ext_stat($referer,$time_end,1);
		echo 'over';

		exit;
	}
	
	
	/**
    * 释放已经超过真空期的手机号码
    *
    * @access public
    * @return void
    */
	public function telno400_release()
	{	
		$this->load->model('esf_400_model');
		$this->esf_400_model->release_telno();
	}
}


/* End of file job_center_cron.php */
/* Location: ./center/controllers/job_center_cron.php */