<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace App\Services\Auth;

use App\Models\Auth\Users;
use App\Models\Auth\UsersLog;
// use xin\helper\Server;
// use xin\helper\Time;

class UsersLogService{

	/**
	 * @title 获取用户操作日志
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function getUserLogList($request){
		$param = $request->all();
		$map = [];
		if(isset($param['date_type']) && $param['date_type']){
			$time = Time::today();
			if($param['date_type'] == 'seven'){
				$time = Time::dayToNow(7);
			}elseif($param['date_type'] == 'yesterday'){
				$time = Time::yesterday(7);
			}elseif($param['date_type'] == 'week'){
				$time = Time::week(7);
			}
			$map[] = ['create_time', 'BETWEEN TIME', $time];
		}
		if(isset($param['method']) && $param['method']){
			$map[] = ['method', '=', strtoupper($param['method'])];
		}
		if(isset($param['date']) && $param['date'] && count($param['date']) == 2){
			$map[] = ['create_time', 'BETWEEN TIME', $param['date']];
		}

		$list = UsersLog::with(['user'])->where($map)->orderBy('create_time', 'desc')->paginate($param['pageSize']);
		return $list;
	}

	/**
	 * @title 获取用户操作日志
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function getMyLogList($request){
		$param = $request->param();
		$map = [];
		$map[] = ['uid', '=', $request->user['uid']];
		if(isset($param['method']) && $param['method']){
			$map[] = ['method', '=', strtoupper($param['method'])];
		}
		if(isset($param['date_type']) && $param['date_type']){
			$time = Time::today();
			if($param['date_type'] == 'seven'){
				$time = Time::dayToNow(7);
			}elseif($param['date_type'] == 'yesterday'){
				$time = Time::yesterday(7);
			}elseif($param['date_type'] == 'week'){
				$time = Time::week(7);
			}
			$map[] = ['create_time', 'BETWEEN TIME', $time];
		}
		if(isset($param['date']) && $param['date'] && count($param['date']) == 2){
			$map[] = ['create_time', 'BETWEEN TIME', $param['date']];
		}

		$list = UsersLog::with(['user'])->where($map)->order('create_time desc')->paginate($request->pageConfig);
		return $list;
	}

	/**
	 * @title 用户操作记录
	 *
	 * @param [type] $request
	 * @param integer $code
	 * @return void
	 */
	public function record($request, $code = 200){
	    $param = array_merge($request->get(), $request->post());

		if(!isset($request->user['uid'])){
			return false;
		}
		$param = strlen(json_encode($param)) > 1000 ? 'param to loog' : json_encode($param);
		$data = [
			'uid' => isset($request->user['uid']) ? $request->user['uid'] : '',
			'title' => self::getCurrentTitle($request),
			'route' => $request->baseUrl(),
			'params' => $param,
			'method' => $request->method(),
			'client_ip' => Server::getRemoteIp(),
			'browser'   => $request->header('user-agent'),
            'code'      => $code
		];
		if($data['route'] == '/admin/system/log/index'){
			return false;
		}
		UsersLog::create($data);
	}

	protected static function getCurrentTitle($request) {
		$mate = '';
		$controller = strtr(strtolower($request->controller()), '.', '\\');
		$action = $request->action();
		$class = "\\app\\controller\\" . $controller;
		if (class_exists($class)) {
			$reflection = new \ReflectionClass($class);
			$group_doc = self::Parser($reflection->getDocComment());
			if (isset($group_doc['title'])) {
				$mate = $group_doc['title'];
			}
			$method = $reflection->getMethods(\ReflectionMethod::IS_FINAL | \ReflectionMethod::IS_PUBLIC);
			foreach ($method as $key => $v) {
				if ($action == $v->name) {
					$title_doc = self::Parser($v->getDocComment());
					if (isset($title_doc['title'])) {
						$mate = isset($title_doc['title']) ? $title_doc['title'] : '';
					}
				}
			}
		}
		return $mate;
	}

	protected static function Parser($text) {
		$doc = new \doc\Doc();
		return $doc->parse($text);
	}
}
