package com.qiaoba.module.monitor.controller;

import com.qiaoba.api.monitor.entity.SysLoginLog;
import com.qiaoba.api.monitor.entity.param.LoginLogParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.common.poi.util.ExcelUtil;
import com.qiaoba.module.monitor.service.SysLoginLogService;
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.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登录日志管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023-05-27 21:59:12
 */
@RestController
@RequestMapping("/monitor/login-log")
@RequiredArgsConstructor
@Tag(name = "登录日志管理")
public class SysLoginLogController {

    private final SysLoginLogService sysLoginLogService;

    @GetMapping("/list")
    @Operation(summary = "分页查询")
    @PreAuthorize("hasAuthority('monitor:login-log:list')")
    public TableDataInfo<SysLoginLog> list(LoginLogParam param, PageQuery pageQuery) {
        return sysLoginLogService.selectPageList(param, pageQuery);
    }

    @PreAuthorize("hasAuthority('monitor:login-log:export')")
    @PostMapping("/export")
    @Operation(summary = "导出日志")
    public void export(HttpServletResponse response, LoginLogParam param) {
        List<SysLoginLog> list = sysLoginLogService.selectList(param);
        ExcelUtil.exportExcel(list, SysLoginLog.class, "登录日志", response);
    }

    @PreAuthorize("hasAuthority('monitor:login-log:remove')")
    @DeleteMapping("/clean")
    @Operation(summary = "清空日志")
    public AjaxResult clean() {
        sysLoginLogService.cleanLoginLog();
        return AjaxResult.success();
    }
}
