<?php
/**
 * Created by PhpStorm.
 * User: sunmoon<i@liming.me>
 * Date: 2019-08-01
 * Time: 09:51
 */

namespace common\libs\logging\system;

use common\helpers\Date;
use common\libs\logging\operate\AbstractOperate;
use common\models\forms\LogSystemForm;
use common\models\LogSystem;
use common\models\Period;
use Yii;
use yii\helpers\Json;

class AsyncSystem extends AbstractOperate {

    /**
     * 请求内容
     * @var
     */
    public $requestContent;
    /**
     * 响应内容
     * @var
     */
    public $responseContent;
    /**
     * 操作类型
     * 调用[call],被调用[called]
     * @var
     */
    public $optType;
    /**
     * 接口名称
     * @var
     */
    public $apiName;
    /**
     * 对象编号
     * @var
     */
    public $optTargetName;
    /**
     * @var
     */
    public $requestRoute;
    /**
     * @var array
     */
    public $columns = [];
    /**
     * Factory constructor.
     * @param array $config
     */
    public function __construct($config = []) {
        parent::__construct($config);
        if(!$this->logModel){
            $this->logModel = LogSystem::class;
        }
        if(!$this->logForm){
            $this->logForm = LogSystemForm::class;
        }
    }

    /**
     * @param $name
     * @return $this
     */
    public function setApiName($name){
        $this->apiName = $name;
        return $this;
    }

    /**
     * @param $name
     * @return $this
     */
    public function setOptTargetName($name){
        $this->optTargetName = $name;
        return $this;
    }

    /**
     * @param $route
     * @return $this
     */
    public function setRequestRoute($route){
        $this->requestRoute = $route;
        return $this;
    }

    /**
     * @param $content
     * @return $this
     */
    public function setRequestContent($content){
        $this->requestContent = Json::encode($content);
        return $this;
    }

    /**
     * @param $content
     * @return $this
     */
    public function setResponseContent($content){
        $this->responseContent = Json::encode($content);
        return $this;
    }
    /**
     * @param array $params
     * @return $this
     */
    public function setColumn(array $params = []){
        if(isset($params['apiName']) && !empty($params['apiName'])){
            $this->apiName = $params['apiName'];
        }
        $period = Period::instance();
        $periodNum = $period->getNowPeriodNum();
        $this->columns[] = [
            'user_agent' => null,
            'created_at' => intval(Date::nowTime()),
            'period_num' => intval($periodNum),
            'request_content' => $this->requestContent,
            'response_content' => $this->responseContent,
            'ip' => null,
            'request_route' => $this->requestRoute ? $this->requestRoute : Yii::$app->requestedRoute,
            'is_batch' => $this->_isBatch ? 1 : 0,
            'device' => null,
            'opt_obj_id' => isset($params['userId']) ? $params['userId'] : null,
            'opt_obj_name' => isset($params['userName']) ? $params['userName'] : null,
            'opt_type' => 'called',
            'opt_target_name' => $this->apiName,
            'opt_user' => isset($params['optUser']) ? $params['optUser'] : null,
        ];
        return $this;
    }
}