<?php
/**
 * Created by PhpStorm.
 * User: sunmoon<i@liming.me>
 * Date: 2019/07/29
 * Time: 上午9:39
 */


namespace common\libs\logging\operate;

use common\models\forms\LogAdminHandleForm;
use common\models\LogAdminHandle;
use common\models\Period;
use Yii;
use common\helpers\Date;

class AdminOperate extends AbstractOperate {
    /**
     * Factory constructor.
     * @param array $config
     */
    public function __construct($config = []) {
        parent::__construct($config);
        if(!$this->logModel){
            $this->logModel = LogAdminHandle::class;
        }
        if(!$this->logForm){
            $this->logForm = LogAdminHandleForm::class;
        }
    }

    /**
     * set column
     * @param array $params
     * @return $this|bool
     * @throws \Exception
     */
    public function setColumn(array $params = []){
        if(!isset($params['optType'])){
            throw new \Exception('请设置操作类型');
        }

        $period = Period::instance();
        $periodNum = $period->getNowPeriodNum();
        $this->columns[] = [
            'user_agent' => Yii::$app->request->getUserAgent(),
            'created_at' => intval(Date::nowTime()),
            'period_num' => intval($periodNum),
            'save_before_content' => $this->saveBeforeContent,
            'save_after_content' => $this->saveAfterContent,
            'admin_id' => Yii::$app->user->id,
            'admin_name' => isset(\Yii::$app->user->getUserInfo()['adminName']) ?\Yii::$app->user->getUserInfo()['adminName'] :$params['adminName'],
            'ip' => Yii::$app->request->getUserIP(),
            'request_route' => Yii::$app->requestedRoute,
            'opt_type' => $params['optType'],
            'opt_obj_id' => isset($params['userId']) ? $params['userId'] : null,
            'opt_obj_name' => isset($params['userName']) ? $params['userName'] : 'System',
            'remark' => isset($params['remark']) ? $params['remark'] : null,
            'is_batch' => $this->_isBatch ? 1 : 0,
            'device' => Yii::$app->request->getDevice(),
        ];
        return $this;
    }
}