<?php

namespace App\Console\Commands;

use App\Facade\TableName;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '按天统计游戏人数-游戏房间去重过的用户';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        // 今日执行昨日数据
        $DateID = Carbon::yesterday()->format('Ymd');
        // 查找按天去重玩过游戏的人
        $list = DB::connection('write')->table(TableName::QPRecordDB() . 'RecordUserGameDayCount')
            ->where('DateID', $DateID)
            ->selectRaw('count(UserID) UserCount,GameID,sum(Cnt) Cnt')
            ->groupBy('GameID')
            ->get();

        foreach ($list as $value) {
            DB::connection('write')->table(TableName::QPRecordDB() . 'RecordServerGameCount')
                ->updateOrInsert(['DateID' => $DateID, 'GameID' => $value->GameID], ['DateID' => $DateID, 'GameID' => $value->GameID, 'Cnt' => $value->Cnt, 'UserCount' => $value->UserCount]);
        }

    }
}
