<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/3/2
 * Time: 下午1:29
 */

namespace common\helpers;

use common\models\LogAdminHandle;
use common\models\LogAdminLogin;
use common\models\LogApiNotice;
use common\models\LogAsync;
use common\models\Period;
use Yii;
use yii\helpers\Json;

class Log
{
    /**
     * 管理员操作日志
     * @param $message
     * @param int $isKey
     * @param null $userId
     * @param null $userName
     * @param null $remark
     */
    public static function adminHandle($message, $isKey = 0, $userId = null, $userName = null, $remark = null){
        if(Yii::$app->params['enableLog']){
            $period = Period::instance();
            $deviceInfo = Yii::$app->request->getDeviceInfo();
            $userInfo = Yii::$app->user->getUserInfo();
            $model = new LogAdminHandle();
            $model->ADMIN_ID = $userInfo['id'];
            $model->ADMIN_NAME = $userInfo['adminName'];
            $model->IP = $userInfo['ip'];
            $model->REQUEST_ROUTE = Yii::$app->controller->id.'/'.Yii::$app->controller->action->id;
            $model->OPT_CONTENT = $message;
            $model->KEY_LOG = $isKey;
            $model->OPT_OBJ_ID = $userId;
            $model->OPT_OBJ_NAME = $userName;
            $model->REMARK = $remark;
            $model->DEVICE_TYPE = Yii::$app->request->getDevice();
            $model->DEVICE_SYSTEM = $deviceInfo ? $deviceInfo['system'] : null;
            $model->DEVICE_VERSION = $deviceInfo ? $deviceInfo['version'] : null;
            $model->DEVICE_NET = $deviceInfo ? $deviceInfo['networkType'] : null;
            $model->DEVICE_UUID = $deviceInfo ? $deviceInfo['uuid'] : null;
            $model->USER_AGENT = Yii::$app->request->getUserAgent();
            $model->P_MONTH = Date::ociToDate();
            $model->CREATED_AT = Date::nowTime();
            $model->PERIOD_NUM = $period->getNowPeriodNum();
            $model->save();
        }
    }

    /**
     * 管理员登录日志
     */
    public static function adminLogin(){
        if(Yii::$app->params['enableLog']) {
            $period = Period::instance();
            $userInfo = Yii::$app->user->getUserInfo();
            $deviceInfo = Yii::$app->request->getDeviceInfo();
            $model = new LogAdminLogin();
            $model->ADMIN_ID = $userInfo['id'];
            $model->ADMIN_NAME = $userInfo['adminName'];
            $model->IP = $userInfo['ip'];
            $model->P_MONTH = Date::ociToDate();
            $model->DEVICE_TYPE = Yii::$app->request->getDevice();
            $model->DEVICE_SYSTEM = $deviceInfo ? $deviceInfo['system'] : null;
            $model->DEVICE_VERSION = $deviceInfo ? $deviceInfo['version'] : null;
            $model->DEVICE_NET = $deviceInfo ? $deviceInfo['networkType'] : null;
            $model->DEVICE_UUID = $deviceInfo ? $deviceInfo['uuid'] : null;
            $model->USER_AGENT = Yii::$app->request->getUserAgent();
            $model->CREATED_AT = Date::nowTime();
            $model->PERIOD_NUM = $period->getNowPeriodNum();
            $model->save();
        }
    }

    /**
     * 接口异步通知日志
     * @param $param
     */
    public static function apiNotice($param){
        if(Yii::$app->params['enableLog']) {
            [
                'url' => $url,
                'method' => $method,
                'data' => $data,
                'route' => $route,
                'status' => $status,
                'response' => $response,
                'remark' => $remark,
            ] = $param;
            $model = new LogApiNotice();
            $model->URL = $url;
            $model->METHOD = $method;
            $model->DATA = Json::encode($data);
            $model->ROUTE = $route;
            $model->STATUS = $status;
            $model->RESPONSE = Json::encode($response);
            $model->REMARK = $remark;
            $model->P_MONTH = Date::ociToDate();
            $model->CREATED_AT = Date::nowTime();
            $model->save();
        }
    }

    /**
     * console中异步任务的执行日志
     * @param $param
     */
    public static function async($param){
//        file_put_contents(Yii::getAlias('@common/runtime/logs/asyncLog.log'), var_export($param, true));
        if(Yii::$app->params['enableLog']) {
            [
                'type' => $type,
                'route' => $route,
                'title' => $title,
                'detail' => $detail,
                'status' => $status,
            ] = $param;
            $model = new LogAsync();
            $model->TYPE = $type;
            $model->ROUTE = $route;
            $model->TITLE = $title;
            $model->DETAIL = $detail;
            $model->STATUS = $status;
            $model->P_MONTH = Date::ociToDate();
            $model->CREATED_AT = Date::nowTime();
            $model->save();
        }
    }

    /**
     * 文件日志
     * @param $message
     * @param $category
     */
    public static function cliInfo($message, $category = 'application'){
        Yii::getLogger()->flush(true);
        Yii::info($message, $category);
    }
}