<?php

namespace App\libs\helpers;

use App\Auth;
use Illuminate\Support\Facades\DB;

/**
 * 日志统计类.
 */
class LogHelper
{
    /**
     * 记录 exception 日志.
     */
    public static function exceptionLog($exception, $errCode): void
    {
        $names = explode('@', request()->route()->getAction()['controller']);
        $data  = [
            'token'      => request()->header('token', null),
            'uid'        => Auth::$userId ? Auth::$userId : Auth::$adminId,
            'code'       => $errCode,
            'controller' => $names[0],
            'func'       => $names[1],
            'method'     => request()->method(),
            'ip'         => request()->ip(),
            'params'     => json_encode([
                'message' => $exception->getMessage(),
                'code'    => $exception->getCode(),
                'line'    => $exception->getLine(),
                'file'    => $exception->getFile(),
            ], JSON_UNESCAPED_UNICODE),
            'day'        => strtotime('today'),
            'created_at' => time(),
        ];
        Db::table('exception_logs')->insert($data);
    }

    /**
     * 记录第三方日志.
     *
     * @param string $type       日志类型
     * @param string $funcName   调用方法
     * @param array  $bodyParams 请求 body 参数
     * @param array  $response   返回数据
     * @param array  $headers    请求头
     * @param array  $params     请求 query 参数
     */
    public static function thirdLog(string $type, string $funcName, array $bodyParams, array $response, array $headers = [], array $params = []): void
    {
        $data = [
            'uid'        => Auth::$userId ? Auth::$userId : Auth::$adminId,
            'type'       => $type,
            'func_name'  => $funcName,
            'headers'    => json_encode($headers, JSON_UNESCAPED_UNICODE),
            'params'     => json_encode($params, JSON_UNESCAPED_UNICODE),
            'body'       => json_encode($bodyParams, JSON_UNESCAPED_UNICODE),
            'response'   => Helper::filterEmoji(json_encode($response, JSON_UNESCAPED_UNICODE)),
            'created_at' => time(),
        ];

        Db::table('third_logs')->insert($data);
    }

    /**
     * 记录用户日志.
     *
     * @param string $event    用户事件
     * @param string $value    值
     * @param array  $moreInfo 数据信息
     */
    public static function userLog(string $event, string $value = '', array $moreInfo = []): void
    {
        $data = [
            'user_id'    => Auth::$userId ? Auth::$userId : Auth::$adminId,
            'event'      => $event,
            'value'      => $value,
            'more'       => json_encode($moreInfo, JSON_UNESCAPED_UNICODE),
            'created_at' => time(),
            'day'        => strtotime('today'),
            'num'        => '1',
        ];
        Db::table('logs')->insert($data);
    }

    /**
     * 数据记录.
     *
     * @param int $cnt
     */
    public static function record($field, $cnt = 1): void
    {
        $hour = strtotime(date('Y-m-d H:00:00'));
        $res  = Db::name('record')->where(['hour' => $hour])->inc($field, $cnt)->update();
        if (!$res) {
            Db::table('record')
                ->insert([
                    'hour' => $hour,
                    $field => $cnt,
                ])
            ;
        }
    }
}
