<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class ApiLogJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    private $data;

    /**
     * Create a new job instance.
     *
     * @param  array  $data
     */
    public function __construct(array $data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if (app()->environment(['production', 'release'])) {
            try {
                $tablename = "kdgx_fpdx_apilog" . date("Ymd");
                $exists = Cache::get("fpdx:apilog:table:exists", false);
                if (!$exists) {
                    if (!Schema::connection('mysql_datalog')->hasTable($tablename)) {
                        Schema::connection('mysql_datalog')->create($tablename, function ($table) {
                            $table->increments('id');
                            $table->string('uri', 128)->comment('uri');
                            $table->string('method', 8)->comment('请求方法');
                            $table->string('controller', 128)->comment('控制器');
                            $table->string('host', 128)->comment('请求域名');
                            $table->integer('uid')->comment('用户');
                            $table->unsignedInteger('ip')->comment('ip');
                            $table->double('begin_at', 16, 6)->comment('请求开始时间');
                            $table->double('end_at', 16, 6)->comment('请求结束时间');
                            $table->longText('request')->comment('请求包');
                            $table->longText('response')->comment('响应包');
                            $table->smallInteger('http_status')->comment('响应码');
                            $table->longText('func_line')->comment('函数调用栈');
                            $table->longText('sql_line')->comment('SQL调用栈');
                            $table->index('uri', 'idx_uri');
                            $table->index('uid', 'idx_uid');
                            $table->index('ip', 'idx_ip');
                            $table->index('begin_at', 'idx_begin');
                        });
                        DB::connection('mysql_datalog')->statement("alter table {$tablename} comment 'API调用日志表'");
                    } else {
                        Cache::put("fpdx:apilog:table:exists", true, (mktime(0, 0, 0) + 86400 - time()) / 60);
                    }
                }
                DB::connection('mysql_datalog')->table("kdgx_fpdx_apilog" . date("Ymd"))->insert([
                    'uri' => $this->data['uri'],
                    'method' => $this->data['method'],
                    'controller' => $this->data['controller'],
                    'uid' => $this->data['uid'],
                    'request' => $this->data["request"],
                    'response' => $this->data["response"],
                    'http_status' => $this->data['http_status'],
                    'func_line' => $this->data["func_line"],
                    'sql_line' => $this->data["sql_line"],
                    'ip' => $this->data['ip'],
                    'begin_at' => $this->data['begin_at'],
                    'end_at' => $this->data['end_at'],
                    'host' => $this->data['host']
                ]);
            } catch (\Exception $exception) {
                dump($exception);
            }
        }
    }
}
