<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Sku_mongo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sku:mongo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(0);
        $starttime = explode(' ',microtime());

        $mongo=new \MongoClient(env('MONGO_HOST', ''));
        $mongodb=$mongo->ichunt->sku;


        $count=100;

        for ($dbcode=0;$dbcode<10;$dbcode++){

            $db=DB::connection('sku_'.$dbcode);
            $i=0;
            for($table=0;$table<10;$table++){
                for ($p=1;$p<$count+1;$p++){
                    $data=[];
                    $list=$db->table('lie_sku_'.$table)->select('goods_id','spu_id','moq','supplier_id')->paginate(10000,['*'],'',$p);
                    foreach ($list as $k=>$v){
                        $data[]=[
                            'goods_id'=>intval($v->goods_id),
                            'spu_id'  =>intval($v->spu_id),
                            'supplier_id'=>intval($v->supplier_id),
                            'moq'=>intval($v->moq)
                        ];
                        $i++;
                    }
                    $mongodb->batchInsert($data);
                    $count=$list->lastPage();
                    echo $i.'->';
                }
            }
        }


        //程序运行时间
        $endtime = explode(' ',microtime());
        $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
        $thistime = round($thistime,3);
        echo "本程序执行耗时:".$thistime." 秒。";
    }
}