<?php

/**
 * Survey Model
 * @author Candison <www.kandisheng.com>
 */

class Vr_model extends MY_Model
{
    public $status = array(
        1 => '审核中',
        2 => '已预约',
        3 => '已拍摄',
        4 => '已上传',
        5 => '已取消',
        6 => '未通过',
        7 => '待排期',
        8 => '待定',
        9 => '套餐取消'
    );


    function __construct()
    {
       parent::__construct();
    }


    /**
     * 获取正在处理的vr数量
     */
	public function getVrApplyNowNum($house_type,$applay_type){
        $sql = "select count(*) as count from vr_applay where house_type = {$house_type} and shot_status in (1,2,7,8) and applay_type = {$applay_type}";
        $data = $this->db_esfhousebak->query($sql)->result_array();
        $count = $data[0]['count'];
        return $count;
    }

    /**
     * wh的实勘套餐 支付成功新增一条实勘待审信息
     * @param $orderInfo
     */
    public function whServey($orderInfo){
        $add = array();
        $add['broker_telno'] = '';
        $add['house_id'] = $orderInfo['house_id'];
        $add['house_id'] = $orderInfo['house_id'];
        $add['house_type'] = 1;
        $add['order_time'] = 0;
        $add['order_date'] = '0000-00-00';
        $add['applay_type'] = 3;
        $add['save_time'] = date('Y-m-d H:i:s');
        $this->load->model("house_model");
        $add['remark'] = '个人实勘套餐';

        $searchArr['id'] = $orderInfo['house_id'];
        //获取房源信息
        $houseData = $this->house_model->gethousebywhere($searchArr,'sell','*',1);
        $add['broker_id'] = $houseData[0]['uid'];


        if(empty($houseData)) return false;
        $add['village_name'] = $houseData[0]['blockshowname'];
        $add['area'] = $houseData[0]['district'];
        $add['plate'] = '';
        $add['broker_telno'] = $houseData[0]['telno'];
        $add['truename'] =  $houseData[0]['contactor'];
        $add['username'] =  $houseData[0]['username'];


        $add['building'] = $houseData[0]['buildingnum'];
        $add['unit'] = $houseData[0]['unitnum'];
        $add['room_num'] = $houseData[0]['roomnum'];

        $add['room'] = $houseData[0]['room'];
        $add['hall'] = $houseData[0]['hall'];
        $add['toilet'] = $houseData[0]['toilet'];

        if($houseData[0]['streetid']>0){
            $this->load->model("block_model");
            $streetname = $this->block_model->getstreetname($houseData[0]['streetid']);
            $add['plate'] = $streetname ? $streetname['streetname'] : '';
        }

        $result =  $this->createVrApply($add);
        if($result > 0){

            //添加实堪进程记录
            $processlog['house_id'] = $houseData[0]['id'];
            $processlog['esta'] = $houseData['esta'];
            $processlog['city'] = 'wh';
            $processlog['tbl'] = 'sell';
            $processlog['b_id'] = $result;
            $processlog['type'] = 8;

            $title = $houseData['uid'].'购买实堪服务';
            $processlog['title'] = iconv('gbk','utf-8',$title);
            $state = iconv('gbk','utf-8','审核中');
            $processlog['state'] = $state;

            //给房源打上实勘标记
            $sql = "UPDATE sell SET survey = 1 WHERE id = {$orderInfo['house_id']}";
            $this->db_esfhouse->query($sql);

            $url = 'http://newrent.house365.com/api/common/house-process';
            $this->curl_posts($url,$processlog);

            return true;
        }
        return false;
    }



    public  function curl_posts($url , $post)
    {
        $ch = curl_init ();
        // print_r($ch);
        curl_setopt ( $ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_POST, 1);
        curl_setopt ( $ch, CURLOPT_HEADER, 0);
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($post));
        $return = curl_exec($ch);
        curl_close ( $ch );
        return $return;
    }

    /**
     * 创建一条vr申请信息
     * add
     * @param array $data
     * @return int
     */
    public function createVrApply($data = array())
    {
        $this->set_table('vr_applay');
        $insert_id = $this->add( $data , 'esfhouse' );
//        echo $this->db->last_query();die;
        return intval($insert_id);
    }


    /**
     * 查询房源的vr申请信息
     * @param string $type
     * @param int $houseID
     * @return array
     */
    public function getVrInfoByHouseId($type='sell', $houseID=0)
    {
        $types = array(
            'sell' => 1,
            'rent' => 2,
        );


        $where = array(
            'house_type' => $types[$type],
            'house_id' => $houseID
        );
        $select = array(
            '*'
        );
        $data = array(
            'form_name' => 'vr_applay',
            'where' => $where,
            'select' => $select,
            'order_by' => 'id desc',
            'limit' => 1,
            'offset' => 0
        );
        $arr = $this->get_data($data, 'esfhousebak');

        if($arr && !empty($arr[0])){
            //状态汉字
            $arr[0]['vr_status_word'] = isset($this->status[$arr[0]['shot_status']]) ? $this->status[$arr[0]['shot_status']] : '未知状态';

            //是否是展示vr的状态
            $arr[0]['show_icon'] = in_array($arr[0]['shot_status'],array(5,6,9)) ? false : true; //5 已取消 6 未通过 9套餐取消
        }
        return $arr[0];
    }

}