package com.qiaoba.module.monitor.controller;

import com.qiaoba.api.auth.service.OnlineUserService;
import com.qiaoba.common.base.entity.BasePage;
import com.qiaoba.common.base.result.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 在线用户管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023-05-27 21:59:12
 */
@RestController
@RequestMapping("/monitor/online")
@RequiredArgsConstructor
@Tag(name = "在线用户管理")
public class OnlineUserController {

    private final OnlineUserService onlineUserService;

    @PreAuthorize("hasAuthority('monitor:online:list')")
    @Operation(summary = "获取列表")
    @GetMapping("/list")
    public BasePage getList(Integer pageNum, Integer pageSize, @RequestParam(required = false) String username) {
        return onlineUserService.selectPageList(pageNum, pageSize, username);
    }

    @PreAuthorize("hasAuthority('monitor:online:forceLogout')")
    @DeleteMapping("/{username}/{deviceSn}")
    @Operation(summary = "强退用户")
    public AjaxResult forceLogout(@PathVariable String username, @PathVariable String deviceSn) {
        onlineUserService.deleteOne(username, deviceSn, false);
        return AjaxResult.success();
    }
}
