<?php

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

class Vas_packing_house_zhou extends MY_Controller {

    /**
     * 解析函数
     *
     * @access public
     * @return void
     */
    public function __construct($type = NULL) {
        parent::__construct($type);
        $this->config_cityflag = 'nj';
        $this->load->model('broker_model');
        $this->load->model('payment_model');
        $this->house = $this->config->item('house');
        $this->load->helper('common_url_helper');
        //用户信息初始化
        $info = $this->broker_model->get_user_session();
		
		
        $groupId = $info['package_id']; //套餐
        $gbc = new GrowthBrokerCredit(array('id' => $info['uid'], 'groupid' => $groupId));
        $gbe = new GrowthBrokerExp(array('id' => $info['uid'], 'groupid' => $groupId));
        //房豆
        $info['credit'] = $gbc->getValidCredit();
        //等级
        $info['level'] = $gbe->getLevel(2);
        //经验
        $info['exp'] = $gbe->getExp();
        $this->info = $info;
    }

    /**
     * 房源包装页面
     */
    public function index() {
		
        $check = $this->checkVasLevel();
        if ($check !== true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }

        $packType = $this->input->get('type', TRUE);
        $packTypeArr = array("Mansionshow", "Search", "Video");
        if ($packType == "") {
            //$packType = "Search";
			$packType = "Mansionshow";
        } elseif (in_array($packType, $packTypeArr)) {
            $packType = $packType;
        } else {
            //$packType = "Search";
			$packType = "Mansionshow";
        }
        $viewData['page_title'] = '房源包装';
        $viewData['info'] = $this->info;
        $viewData['documentation'] = $this->config->item('documentation');
        //获取房源包装价格
		
		// 2016-1-14 添加星级数据 -----------start -------->
		
		$this->load->model('broker_zhou_model');
		
		$broker_new_info =$this->broker_zhou_model->get_broker_info_by_uid($this->info['uid']);
		// 2016-1-14 添加星级数据 -----------end -------->
		
        $VasPrice = new VasPrice();
        $viewData['price'] = $VasPrice->findByType($packType);
        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
		
        $viewData['buy_num'] = $VasQueueLimit->findtypenum($packType);
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum($packType);
        $viewData['pack_type'] = $packType;
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');        
        
        $viewData['house_num'] = $this->getHouseNum($packType);//符合条件的房源数量
        $viewData['google_analytics'] = get_ga_js_code('/营销中心/房源包装/我要购买/');

		$viewData['broker_new_info'] = $broker_new_info;
        $viewData['css'] = load_css('zsb/css/v1.0/style1.css');
		//echo $this->getIndexTemplet($packType);die;
		//print_r($viewData);die;
        $this->view('market', 'vas/vas_show_zhou', $viewData);
    }

   

    /**
     * 房源包装订单列表
     */
    public function orderlist() {
        $check = $this->checkVasLevel();
        if ($check !== true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }


        $viewData['page_title'] = '房源包装订单列表';
        $viewData['info'] = $this->info;
        $viewData['documentation'] = $this->config->item('documentation');
        $viewData['packing_house_staus'] = $this->config->item('packing_house_staus');
        $viewData['packing_house'] = $this->config->item('packing_house');
        //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price'] = $VasPrice->findByType('Search');
        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
        $viewData['buy_num'] = $VasQueueLimit->findtypenum('Search');
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum('Search');

        $viewData['css'] = load_css('zsb/css/v1.0/style1.css');
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
        //获取订单
        $VasRecord = new VasRecord('vas_packing_house_record');
        $where = "uid = " . $this->user_arr['uid'];
        if ($this->input->post('packing_house_staus')) {
            $viewData['packing_staus'] = $this->input->post('packing_house_staus');
            $where .= " and status=" . $viewData['packing_staus'];
        }
        $viewData['record_num'] = $VasRecord->findNumberBy($where);
        $viewData['pagesize'] = 15; //设定每一页显示的记录数
        $viewData['pages'] = $viewData['record_num'] ? ceil($viewData['record_num'] / $viewData['pagesize']) : 0;  //计算总页数
        $viewData['page'] = $this->input->post('page') ? intval($this->input->post('page')) : 1; // 获取当前页数
        $viewData['page'] = ($viewData['page'] > $viewData['pages'] && $viewData['pages'] != 0) ? $viewData['pages'] : $viewData['page'];  //判断跳转页数
        $viewData['offset'] = $viewData['pagesize'] * ($viewData['page'] - 1);   //计算记录偏移量
        //是否显示分页
        $pagination = NULL;
        if ($viewData['record_num'] > $viewData['pagesize']) {
            $viewData['pagination'] = helper_pagination($viewData['page'], $viewData['pages'], $viewData['record_num']);
        }
        $recordlist = $VasRecord->findBy($where, 'buy_time desc', $viewData['pagesize'], $viewData['offset']);
        $Vas_packing_house = new VasRecord('vas_packing_house');
        if (is_array($recordlist) && !empty($recordlist)) {
            foreach ($recordlist as $key => $value) {
				$housestr = "";
                $conf = "record_id =" . $value['id'];
                $housedetail = $Vas_packing_house->findBy($conf);
                if (is_array($housedetail) && !empty($housedetail)) {
                    $housestr = '';
                    foreach ($housedetail as $value2) {
                        $housestr .= '<a class="c1" href="' . get_sellinfo_url($value2['houseid']) . '" target="_blank">' . $value2['houseid'];
                        $housestr.= $value2['house_typ'] == 2 ? '备用</a> ' : ' </a>';
                    }
                }
                $viewData['recordlist'][$key] = $value;
                $viewData['recordlist'][$key]['housestr'] = $housestr;
				
            }
        }
        $viewData['google_analytics'] = get_ga_js_code('/营销中心/房源包装/我的购买记录/');
        $this->view('market', 'vas/vas_orderlist', $viewData);
    }

    /**
     * Index Page for this controller.
     */
    public function buy() {
        $check = $this->checkVasLevel();
        if ($check !== true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }

        $viewData['page_title'] = '房源包装购买列表';
        $viewData['info'] = $this->info;
        //加载房源类
        $this->load->model('sell_model');
        $uid = $this->user_arr['uid']; //账号
        //查询房源条件
        $cond_where = "uid = '" . $uid . "' AND esta = 1  AND infofrom = '5' ";
        $viewData['house_all'] = $this->sell_model->find_all_by($cond_where, 0, 200);
        //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price'] = $VasPrice->findByType('Search');
        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
        $viewData['buy_num'] = $VasQueueLimit->findtypenum('Search');
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum('Search');
        $viewData['css'] = load_css('zsb/css/v1.0/style1.css,zsb/css/v1.0/jquery.mCustomScrollbar.css');
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
        $this->load->model('street_model');
        $this->view('market', 'vas/vas_packing_buy', $viewData);
    }

    /**
     * 视频看房购买
     * vas_video
     * 
     */
    public function vas_video() {
        $check = $this->checkVasLevel();
        if ($check !== true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }

        $viewData['page_title'] = '房源包装购买列表';
        $viewData['info'] = $this->info;
        //加载房源类
        $this->load->model('sell_model');
        $uid = $this->user_arr['uid']; //账号
        //查询房源条件
        // $cond_where = "uid = '".$uid."' AND esta = 1  AND infofrom = '5'";
        $cond_where = "uid = '" . $uid . "' AND esta = 1  AND infofrom = '5' AND fitment IN('豪华装','精装')";
        $viewData['house_all'] = $this->sell_model->find_all_by($cond_where, 0, 200);

        //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price'] = $VasPrice->findByType('Video');
        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
        $viewData['buy_num'] = $VasQueueLimit->findtypenum('Video');
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum('Video');
        $viewData['css'] = load_css('zsb/css/v1.0/style1.css,zsb/css/v1.0/jquery.mCustomScrollbar.css');
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
        $this->load->model('street_model');
        $this->view('market', 'vas/vas_video_buy', $viewData);
    }

    /**
     * vas_show
     * 大宅秀
     */
    public function vas_show() {
        $check = $this->checkVasLevel();
        if ($check !== true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }
        $packType = "Mansionshow";
        $viewData['page_title'] = '房源包装购买列表';
        $viewData['info'] = $this->info;
        //加载房源类
        $this->load->model('sell_model');
        $uid = $this->user_arr['uid']; //账号
        //查询房源条件
        // $cond_where = "uid = '".$uid."' AND esta = 1  AND infofrom = '5'";
        $cond_where = "uid = '" . $uid . "' AND esta = 1  AND infofrom = '5' AND fitment IN('豪华装','精装') AND buildarea>=90 AND room>=3";
        $viewData['house_all'] = $this->sell_model->find_all_by($cond_where, 0, 200);

        //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price'] = $VasPrice->findByType($packType);
        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
        $viewData['buy_num'] = $VasQueueLimit->findtypenum($packType);
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum($packType);
        $viewData['css'] = load_css('zsb/css/v1.0/style1.css,zsb/css/v1.0/jquery.mCustomScrollbar.css');
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
        $this->load->model('street_model');
        $this->view('market', 'vas/vas_show_buy', $viewData);
    }

    /**
     * 生成订单
     */
    public function addorder() {
        $broker['id'] = $this->info['uid'];
        $broker['groupid'] = $this->info['package_id'];
        $str = $this->input->post('str');
        $str = rtrim($str, ',');
        $id_arr = explode(',', $str);
        //不满五条房源
        if (count($id_arr) < 5) {
            echo json_encode(array('status' => '2', 'msg' => '不满五条房源'));
            exit;
        }

        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
        $viewData['buy_num'] = $VasQueueLimit->findtypenum('Search');
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum('Search');
        if ($viewData['buy_num'] >= $viewData['limit_num']) {
            echo json_encode(array('status' => '2', 'msg' => '超过最大购买次数'));
            exit;
        }
        //获取房源包装价格   
        $truthPrice = $this->getTruthPrice('Search');
        //房豆
        $myDou = $this->info['credit'];
        if($myDou<$truthPrice){
            echo json_encode(array('status'=>'4','msg'=>'对不起，您的房豆不足，请充值！'));exit;            
        }
        //获取房源包装价格
        $price = $truthPrice;
        $GrowthBrokerCredit = new GrowthBrokerCredit($broker);
        $pay_order = $GrowthBrokerCredit->reduce('packingHouse', '房源包装', $price);
        if ($pay_order['status'] == 1) {
            //记录详细信息
            $fieldData = array('type' => 'Search', 'uid' => $this->info['uid'], 'buy_time' => date("Y-m-d H:i:s", time()), 'order_id' => $pay_order['transaction_id'], 'cost' => $price, 'status' => 1);
            $VasSetPromotRecord = new VasSetPromotRecord('vas_packing_house_record');
            $record_id = $VasSetPromotRecord->insert($fieldData);
            //记录房源信息
            if (!empty($id_arr)) {
                $Vas_packing_house = new VasSetPromotRecord('vas_packing_house');
                foreach ($id_arr as $key => $value) {
                    $house_typ = $key >= 5 ? 2 : 1;
                    $fieldData = array('record_id' => $record_id, 'houseid' => $value, 'house_typ' => $house_typ);
                    $Vas_packing_house->insert($fieldData);
                }
            }
            //更新购买次数
            $fieldData = array('number' => $viewData['buy_num'] + 1);
            $conf = "type = 'Search'";
            $VasQueueLimit->update($fieldData, $conf);
            //添加购买人数
            $VasBroker = new VasBroker();
            //2表示房源包装
            $VasBroker->addJoinVasRecord(2, $broker['id']);
        }
        print json_encode($pay_order);
    }

    /**
     * addorder_video
     * 视频看房生成订单
     */
    public function addorder_video() {
        $packType = "Video";
        $broker['id'] = $this->info['uid'];
        $broker['groupid'] = $this->info['package_id'];
        $rowid = $this->input->post('rowid', TRUE);
		
        //加载房源类
        $this->load->model('sell_model');
        $houseInfo = $this->sell_model->find_by_id($rowid);
        if ($houseInfo['uid'] != $this->info['uid'] || !in_array($houseInfo['fitment'], array('精装', '豪华装'))) {
            echo json_encode(array('status' => '2', 'msg' => '错误房源'));
            exit;
        }

        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
        $viewData['buy_num'] = $VasQueueLimit->findtypenum($packType);
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum($packType);
        if ($viewData['buy_num'] >= $viewData['limit_num']) {
            echo json_encode(array('status' => '4', 'msg' => '超过最大购买次数'));
            exit;
        }	
		
        $truthPrice = $this->getTruthPrice($packType);//var_dump($truthPrice);
        //房豆
        $myDou = $this->info['credit'];
        if($myDou<$truthPrice){
            echo json_encode(array('status'=>'4','msg'=>'对不起，您的房豆不足，请充值！'));exit;            
        }
        //获取房源包装价格     
        $price = $truthPrice;       
        $GrowthBrokerCredit = new GrowthBrokerCredit($broker);
        $pay_order = $GrowthBrokerCredit->reduce('packingHouse', '视频房源-房源包装', $price);
        if ($pay_order['status'] == 1) {
            //记录详细信息
            $fieldData = array('type' => $packType, 'uid' => $this->info['uid'], 'buy_time' => date("Y-m-d H:i:s", time()), 'order_id' => $pay_order['transaction_id'], 'cost' => $price, 'status' => 1);
            $VasSetPromotRecord = new VasSetPromotRecord('vas_packing_house_record');
            $record_id = $VasSetPromotRecord->insert($fieldData);
            //记录房源信息
            if ($record_id>0) {
                $Vas_packing_house = new VasSetPromotRecord('vas_packing_house');
                $fieldData = array('record_id' => $record_id, 'houseid' => $rowid, 'house_typ' => 1);
                $Vas_packing_house->insert($fieldData);
				
            }

            //更新购买次数           
            $VasQueueLimit->addNumberToOneType($packType, 1);
            //添加购买人数
            $VasBroker = new VasBroker();
            //2表示房源包装
            $VasBroker->addJoinVasRecord(2, $broker['id']);
        }
        print json_encode($pay_order);
    }

    /**
     * addorder_show
     * 视频看房生成订单
     */
    public function addorder_show() {
        $packType = "Mansionshow";
        $broker['id'] = $this->info['uid'];
        $broker['groupid'] = $this->info['package_id'];
        $rowid = $this->input->post('rowid', TRUE);
        //判断房源
		$this->load->model('sell_model');
        $houseInfo = $this->sell_model->find_by_id($rowid);
        if ($houseInfo['uid'] != $this->info['uid'] || !in_array($houseInfo['fitment'], array('精装', '豪华装')) || $houseInfo['room'] < 3 || $houseInfo['buildarea'] < 90) {
            echo json_encode(array('status' => '2', 'msg' => '错误房源'));
            exit;
        }
        //获取房源包装已购买情况
        $VasQueueLimit = new VasQueueLimit();
        $viewData['buy_num'] = $VasQueueLimit->findtypenum($packType);
        $viewData['limit_num'] = $VasQueueLimit->findtypelimitnum($packType);
        if ($viewData['buy_num'] >= $viewData['limit_num']) {
            echo json_encode(array('status' => '4', 'msg' => '超过最大购买次数'));
            exit;
        }        
        
        $truthPrice = $this->getTruthPrice($packType);
        //房豆
        $myDou = $this->info['credit'];
        if($myDou<$truthPrice){
            echo json_encode(array('status'=>'4','msg'=>'对不起，您的房豆不足，请充值！'));exit;            
        }
        //获取房源包装价格            
        $price = $truthPrice;
        $GrowthBrokerCredit = new GrowthBrokerCredit($broker);        
        $pay_order = $GrowthBrokerCredit->reduce('packingHouse', '大宅秀-房源包装', $price);
        if ($pay_order['status'] == 1) {
            //记录详细信息
            $fieldData = array('type' => $packType, 'uid' => $this->info['uid'], 'buy_time' => date("Y-m-d H:i:s", time()), 'order_id' => $pay_order['transaction_id'], 'cost' => $price, 'status' => 1);
            $VasSetPromotRecord = new VasSetPromotRecord('vas_packing_house_record');
            $record_id = $VasSetPromotRecord->insert($fieldData);
            //记录房源信息
            if ($record_id>0) {
                $Vas_packing_house = new VasSetPromotRecord('vas_packing_house');
                $fieldData = array('record_id' => $record_id, 'houseid' => $rowid, 'house_typ' => 1);
                $Vas_packing_house->insert($fieldData);
            }

            //更新购买次数           
            $VasQueueLimit->addNumberToOneType($packType, 1);
            //添加购买人数
            $VasBroker = new VasBroker();
            //2表示房源包装
            $VasBroker->addJoinVasRecord(2, $broker['id']);
        }
        print json_encode($pay_order);
    }
    
    /**
     * 查找某中类型下的房源数量
     * getHouseNum
     * @param string $type 类型
     * @return int $house_num 数量
     */
    private function getHouseNum($type){  
        $key = $this->config_cityflag."_vas_packing_house_get_house_num_".$type;
        $house_num = $this->mc->get($key);
        if($house_num === false) {
            //加载房源类
            $this->load->model('sell_model');
            $uid = $this->user_arr['uid']; //账号
            //查询房源条件
            switch ($type){
                case 'Search':  $cond_where = "uid = '" . $uid . "' AND esta = 1  AND infofrom = '5' ";
                    break;
                case 'Mansionshow': $cond_where = "uid = '" . $uid . "' AND esta = 1  AND infofrom = '5' AND fitment "
                        . "IN('豪华装','精装') AND buildarea>=90 AND room>=3";
                    break;
                case 'Video':         $cond_where = "uid = '" . $uid . "' AND esta = 1  AND infofrom = '5' "
                        . "AND fitment IN('豪华装','精装')";
                    break;
                default : $cond_where = "uid = '" . $uid . "' AND esta = 1  AND infofrom = '5' ";
            }          
            $house_num = $this->sell_model->find_count_by($cond_where);
            $this->mc->set($key,$house_num,20);
        }
        return $house_num; 
    }
    
    
     /**
     * 获取首页模版名称
     * getIndexTemplet
     * @param string $searchType 房源包装类型
     * @return string $templet 
     */
    private function getIndexTemplet($searchType) {
        switch ($searchType) {
            case 'Search': $templet = "vas/vas_packing";
                break;
            case 'Video' : $templet = "vas/vas_video";
                break;
            case 'Mansionshow' : $templet = "vas/vas_show";
                break;
            //default : $templet = "vas/vas_packing";
			default : $templet = "vas/vas_show";
        }
        return $templet;
    }
    
    
    
    
    /**
     * 获取最终价格
     */
    private function getTruthPrice($type){
        $key = $this->config_cityflag."_vas_set_promot_truth_price_".$type;
        $SchoolNowPrice = $this->mc->get($key);
        if($SchoolNowPrice === false) {
            $this->VasPrice = new VasPrice(); 
            $SchoolNowPrice = $this->VasPrice->ServicePrice($type);//黄金实际价格
            $this->mc->set($key,$SchoolNowPrice,600);	
        }
        return $SchoolNowPrice;      
    }
    
    

    /**
     *
     * @return boolean 成功时返回true，失败时返回等级
     */
    private function checkVasLevel() {
        //判断有无权访问
        $brokerLevel = $this->info['level'];
        //经纪人操作等级最低权限
        $permi = new GrowthLevelWayPermission();
        $minLevel = $permi->findMinLevelByWayAliasAndType('packingHouse', 2);

        if ($brokerLevel < $minLevel || $minLevel == '') {
            return $minLevel;
        } else {
            return true;
        }
    }

}

/* End of file validcode.php */
/* Location: ./application/controllers/validcode.php */

