<?php


namespace App\Console\Commands;


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


class StockChange extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'stock_change';

    /**
     * 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()
    {

        // 过滤试玩场
        $blacklist = [32, 33, 39];

        $list = DB::table('QPPlatformDB.dbo.GameRoomInfo')
            ->whereNotIn('ServerID', $blacklist)
            ->get();

        $stock = [];
        foreach ($list as $value) {
            $RoomStock = $value->RoomStock;
            $RoomStock = $RoomStock ? explode(':', explode(';', $RoomStock)[0])[1] : 0;
            $stock[] = ['stock' => (int)$RoomStock, 'serverID' => $value->ServerID, 'type' => 1, 'mydate' => date('Y-m-d H:i:s')];
        }
        // 首先把标识符取掉
        DB::table('agent.dbo.stock_change')->whereDate('mydate', date('Y-m-d'))->update(['identifier' => 0]);

        // 插入数据
        DB::table('agent.dbo.stock_change')->insert($stock);

    }
}
