<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/2/24
 * Time: 下午12:48
 */

namespace backendApi\modules\v1\controllers;

use backendApi\modules\v1\models\exportForms\BonusExportForm;
use backendApi\modules\v1\models\exportForms\LogExportForm;
use backendApi\modules\v1\models\lists\log\AdminHandleList;
use backendApi\modules\v1\models\lists\log\AdminLoginList;
use backendApi\modules\v1\models\lists\bonus\FlowLxList;
use backendApi\modules\v1\models\lists\log\SystemList;
use backendApi\modules\v1\models\lists\log\UserHandleList;
use backendApi\modules\v1\models\lists\log\UserLoginList;
use backendApi\modules\v1\models\LogAdminHandle;
use backendApi\modules\v1\models\LogAdminLogin;
use backendApi\modules\v1\models\LogSystem;
use backendApi\modules\v1\models\LogUserHandle;
use backendApi\modules\v1\models\LogUserLogin;
use common\helpers\Form;
use common\helpers\MongodbSearchFilter;
use common\models\User;
use Yii;

class LogController extends BaseController {
    public $modelClass = User::class;

    /**
     * 管理员操作日志
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionAdminHandle() {
        $listObj = new AdminHandleList();
        $data = $listObj->getList();
        return static::notice($data);
    }

    /**
     * 会员操作日志
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionUserHandle() {
        $listObj = new UserHandleList();
        $data = $listObj->getList();
        return static::notice($data);
    }

    /**
     * 管理员登录日志
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionAdminLogin() {
        $listObj = new AdminLoginList();
        $data = $listObj->getList();
        return static::notice($data);
    }

    /**
     * 会员登录日志
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionUserLogin() {
        $listObj = new UserLoginList();
        $data = $listObj->getList();
        return static::notice($data);
    }

    /**
     * 会员登录日志导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionUserLoginExport() {
        $filter = MongodbSearchFilter::filterCondition([
            'opt_type' => 'opt_type',
            'user_name' => 'user_name',
            'success_times' => 'success_times',
            'fail_times' => 'fail_times',
            'created_at' => 'created_at',
            'period_num' => 'period_num',
            'device' => 'device',
            'user_agent' => 'user_agent',
            'request_route' => 'user_agent',
        ]);
        $form = new LogExportForm();
        $result = $form->run($filter, 'Member_Login_Log'); // 会员登录日志
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 系统日志
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionSystem() {
        $listObj = new SystemList();
        $data = $listObj->getList();
        return static::notice($data);
    }
}