<?php

/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace qcbase\models;

use Yii;
use yii\db\Query;
use yii\base\ErrorException;

/**
 * This is the model class for table "qc_action_log".
 * 
 * 系统日志
 *
 * @property integer $log_id  (ID)
 * @property string $action_name  (活动行为名称)
 * @property string $app_name  (所属应用)
 * @property string $module  (模块名称)
 * @property string $controller  (控制器名称)
 * @property string $action  (动作名称)
 * @property string $url  (操作URL)
 * @property string $ip  (IP)
 * @property integer $user_id  (操作人ID)
 * @property integer $create_time  (操作时间)
 * @property text $content  (内容)
 * @property string $remark  (备注)
 *
 * @author HuLimin <http://blog.u-bo.com>
 * @since 0.1
 */
class ActionLog extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%action_log}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['module', 'controller', 'action', 'url', 'ip', 'user_id', 'create_time'], 'required'],
            [['user_id', 'create_time'], 'integer'],
            [['content'], 'string'],
            [['action_name', 'app_name'], 'string', 'max' => 50],
            [['module', 'controller', 'action', 'ip'], 'string', 'max' => 20],
            [['url'], 'string', 'max' => 100],
            [['remark'], 'string', 'max' => 255]
        ];
    }

    /**
     * 日志记录
     * 
     * @param type $action_name 操作名称 
     * @param type $app_name    应用名
     * @param type $remark      备注
     * @param type $type        类型 默认action ，其他为表明
     * @param type $content     内容（任务，项目，合同等，原有信息）
     * @return boolean
     */
    public static function insertLog($app_name = 'admin', $action_name = '', $remark = '', $content = '')
    {
        $app = \Yii::$app;
        //过滤
        if(!in_array($app->controller->action->id, ['ajax'])){
            $menu_list_for_log = $app->cache->get('menu_list_for_log');
            isset($menu_list_for_log[$app->requestedRoute]['name'])?$name = $menu_list_for_log[$app->requestedRoute]['name']:$name='';
            $action_name = empty($action_name) ? $name : $action_name;
            $actionLog = new ActionLog();
            $actionLog->module = $app->controller->module->id;
            $actionLog->controller = $app->controller->id;
            $actionLog->action = $app->controller->action->id;
            $actionLog->type = $actionLog->module;
            $actionLog->url = $app->getRequest()->getUrl();
            $actionLog->ip = $app->getRequest()->getUserIP();
            $actionLog->create_time = time();
            $actionLog->user_id = $app->user->identity->user_id;
            $actionLog->action_name = trim($action_name);
            $actionLog->app_name = trim($app_name);
            $actionLog->content = $content;
            $actionLog->remark = $remark;
            $actionLog->user_id = $app->user->identity->user_id;
            if($actionLog->validate() && $actionLog->save()){
                return TRUE;
            } else {
//                \common\components\Tool::dump($actionLog->getErrors());
                return FALSE;
            }
        }
    }

    function saveActionLog()
    {
        
    }

}
