package com.starry.admin.modules.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.modules.system.entity.SysLoginLogEntity;
import com.starry.admin.modules.system.entity.SysLoginLogVo;
import com.starry.admin.modules.system.service.ISysLoginLogService;
import com.starry.admin.utils.ExcelUtils;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 系统访问日志表Controller
 *
 * @author admin
 */
@RestController
@RequestMapping("/system/loginlog")
public class SysLoginLogController {
    @Resource
    private ISysLoginLogService xlLoginLogService;

    /**
     * 查询系统访问日志表列表
     */
    @PreAuthorize("@customSs.hasPermission('system:loginlog:list')")
    @GetMapping("/list")
    public R list(SysLoginLogVo vo) {
        IPage<SysLoginLogEntity> list = xlLoginLogService.selectXlLoginLogList(vo);
        return R.ok(list);
    }

    /**
     * 导出系统访问日志表列表
     */
    @PreAuthorize("@customSs.hasPermission('system:loginlog:export')")
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLogVo vo) {
        IPage<SysLoginLogEntity> list = xlLoginLogService.selectXlLoginLogList(vo);
        ExcelUtils.exportEasyExcel(response, SysLoginLogEntity.class, list.getRecords(), "系统访问日志表数据");
    }

    /**
     * 获取系统访问日志表详细信息
     */
    @PreAuthorize("@customSs.hasPermission('system:log:query')")
    @GetMapping(value = "/{loginId}")
    public R getInfo(@PathVariable("loginId") Long loginId) {
        return R.ok(xlLoginLogService.selectXlLoginLogByLoginId(loginId));
    }

    /**
     * 删除系统访问日志表
     */
    @PreAuthorize("@customSs.hasPermission('system:loginlog:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{loginIds}")
    public R remove(@PathVariable Long[] loginIds) {
        return R.ok(xlLoginLogService.deleteXlLoginLogByLoginIds(loginIds));
    }

    @PreAuthorize("@customSs.hasPermission('monitor:logininfor:clean')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R clean() {
        xlLoginLogService.cleanLoginlog();
        return R.ok();
    }
}
