<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: sai <1430792918@qq.com>
// +----------------------------------------------------------------------
namespace plugin\saiadmin\app\controller\system;

use plugin\saiadmin\basic\BaseController;
use plugin\saiadmin\app\logic\system\SystemLoginLogLogic;
use plugin\saiadmin\app\logic\system\SystemOperLogLogic;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 日志控制器
 */
class SystemLogController extends BaseController
{

    /**
     * 登录日志列表
     * @param Request $request
     * @return Response
     */
    #[Permission('登录日志列表', 'core:logs:login')]
    public function getLoginLogPageList(Request $request) : Response
    {
        $where = $request->more([
            ['login_time', ''],
            ['username', ''],
            ['status', ''],
            ['ip', ''],
            ['dept_id', ''],
        ]);
        $logic = new SystemLoginLogLogic();
        $query = $logic->search($where);
        $data = $logic->getList($query);
        return $this->success($data);
    }

    /**
     * 删除登录日志
     * @param Request $request
     * @return Response
     */
    #[Permission('登录日志删除', 'core:logs:deleteLogin')]
    public function deleteLoginLog(Request $request) : Response
    {
        $ids = $request->input('ids', '');
        $logic = new SystemLoginLogLogic();
        if (!empty($ids)) {
            $logic->destroy($ids);
            return $this->success('delete success');
        } else {
            return $this->fail('invalid parameters, please check');
        }
    }

    /**
     * 操作日志列表
     * @param Request $request
     * @return Response
     */
    #[Permission('操作日志列表', 'core:logs:Oper')]
    public function getOperLogPageList(Request $request) : Response
    {
        $where = $request->more([
            ['create_time', ''],
            ['username', ''],
            ['service_name', ''],
            ['router', ''],
            ['ip', ''],
            ['dept_id', ''],
        ]);
        $logic = new SystemOperLogLogic();
        $logic->init($this->adminInfo);
        $query = $logic->search($where);
        $data = $logic->getList($query);
        return $this->success($data);
    }

    /**
     * 删除操作日志
     * @param Request $request
     * @return Response
     */
    #[Permission('操作日志删除', 'core:logs:deleteOper')]
    public function deleteOperLog(Request $request) : Response
    {
        $ids = $request->input('ids', '');
        $logic = new SystemOperLogLogic();
        if (!empty($ids)) {
            $logic->destroy($ids);
            return $this->success('delete success');
        } else {
            return $this->fail('invalid parameters, please check');
        }
    }

}
