<?php
class ControllerToolMongotest extends Controller{

    public function index()
    {

        die();

        $mongo = new MongoDB();

        $results = $mongo->query(array(), array(), 'deercase.orders');

        foreach ($results as $result){
            var_dump($result);
        }

        //$mongo->delete(array(),array(),'deercase.customer_online');




    }

    public function orderadd()
    {

        die();

        //https://www.deercase.com/index.php?route=tool/mongotest/orderadd

        $mongo = new MongoDB();

        $count = 0;

        $orders = $this->db->query("SELECT * FROM ps_order WHERE mongo_status = 0 LIMIT 100 ")->rows;
        foreach ($orders as $order){

            $count++;

            $order_data = array();

            foreach ($order as $key => $order_cell){
                $order_data[$key] = $order_cell;
            }

            $order_data['mongo_status'] = 1;

            $order_data['order_history'] = $this->db->query("SELECT * FROM ps_order_history WHERE order_id = '". $order['order_id'] ."' ")->rows;
            $order_data['order_option'] = $this->db->query("SELECT * FROM ps_order_option WHERE order_id = '". $order['order_id'] ."' ")->rows;
            $order_data['order_product'] = $this->db->query("SELECT * FROM ps_order_product WHERE order_id = '". $order['order_id'] ."' ")->rows;
            $order_data['order_total'] = $this->db->query("SELECT * FROM ps_order_total WHERE order_id = '". $order['order_id'] ."' ")->rows;

            $mongo->bulk($order_data, 'deercase.orders');

            $this->db->query("UPDATE ps_order SET mongo_status = 1 WHERE order_id = '". $order['order_id'] ."' ");


            //var_dump($order_data); die();




        }

        echo $count . ' Adet satır yazlıldı.';

        /*
        $mongo = new MongoDB();

        $orders = $this->db->query("SELECT * FROM ps_order WHERE mongo_status = 0 LIMIT 200 ")->rows;
        $added = 0;
        foreach ($orders as $order){

            $request = $mongo->query(array('order_id' => $order['order_id']), array(), 'deercase.orders');
            if(!isset($request[0]->order_id)){
                $mongo->bulk($order, 'deercase.orders');
                $this->db->query("UPDATE ps_order SET mongo_status = 1 WHERE order_id = '". $order['order_id'] ."' ");
                $added++;
            }else{
                $this->db->query("UPDATE ps_order SET mongo_status = 1 WHERE order_id = '". $order['order_id'] ."' ");
            }

        }

        //echo $added . ' Eklendi!';

        $data['added'] = $added;

        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');


        if (file_exists(DIR_LOCAL_TEMPLATE . $this->config->get('theme_default_directory') . '/template/tool/mongotest.tpl')) {
            $this->response->setOutput($this->load->view(DIR_LOCAL_TEMPLATE . $this->config->get('theme_default_directory') . '/template/tool/mongotest', $data));
        } else {
            $this->response->setOutput($this->load->view(DIR_TEMPLATE . 'default/template/tool/mongotest', $data));
        } */


    }

    public function getorders()
    {

        die();

        $mongo = new MongoDB();

        $requests = $mongo->query(array(), array('limit' => 10), 'deercase.orders');

        foreach ($requests as $request){
            $document = json_decode(json_encode($request),true);
            var_dump($document['order_id']); die();
        }

    }

    public function addproduct()
    {


        die();
        //https://www.deercase.com/index.php?route=tool/mongotest/addproduct


        die();

        $mongo = new MongoDB();

        //$products = $this->db->query("SELECT * FROM ps_product_to_mongo ")->rows;

        $products = $this->db->query("SELECT * FROM ps_product WHERE mongo_status = 0 LIMIT 10 ")->rows;

        foreach ($products as $product){

            $this->db->query("UPDATE ps_product SET mongo_status = 1 WHERE product_id = '". $product['product_id'] ."' ");

            $product_data = array();

            foreach ($product as $key => $product_cell){
                $product_data[$key] = $product_cell;
            }

            $product_data['mongo_status'] = 1;

            $product_data['product_attribute'] = $this->db->query("SELECT * FROM ps_product_attribute WHERE product_id = '". $product['product_id'] ."' ")->rows;
            $product_data['product_description'] = $this->db->query("SELECT * FROM ps_product_description WHERE product_id = '". $product['product_id'] ."' ")->rows;
            $product_data['product_category'] = $this->db->query("SELECT * FROM ps_product_to_category WHERE product_id = '". $product['product_id'] ."' ")->rows;

            $mongo->bulk($product_data, 'deercase.products');
            //var_dump("UPDATE ps_product SET mongo_status = 1 WHERE product_id = '". $product['product_id'] ."' "); die();




        }


    }

    public function addUser()
    {


        die();
        $mongo = new MongoDB();

        $users = $this->db->query("SELECT * FROM ps_user u 
        LEFT JOIN ps_user_group ug ON (u.user_group_id = ug.user_group_id) ")->rows;

        foreach ($users as $user){

            $request = $mongo->query(array('user_id' => $user['user_id']), array(), 'deercase.users');

            if(!isset($request[0]->user_id)){
                $mongo->bulk($user, 'deercase.users');
                //$this->db->query("UPDATE ps_product SET mongo_status = 1 WHERE product_id = '". $user['user_id'] ."' ");

            }
            //var_dump($product); die();
        }



    }


}