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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.modules.system.module.entity.SysLoginLogEntity;
import com.starry.admin.modules.system.module.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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 系统访问日志表Controller
 *
 * @author admin
 */
@Api(tags = "登录日志管理", description = "系统访问日志相关接口")
@RestController
@RequestMapping("/system/loginlog")
public class SysLoginLogController {
    @Resource
    private ISysLoginLogService xlLoginLogService;

    /**
     * 查询系统访问日志表列表
     */
    @ApiOperation(value = "查询日志列表", notes = "分页查询系统访问日志列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysLoginLogEntity.class, responseContainer = "Page")})
    @PreAuthorize("@customSs.hasPermission('system:loginlog:list')")
    @GetMapping("/list")
    public R list(@ApiParam(value = "查询条件") SysLoginLogVo vo) {
        IPage<SysLoginLogEntity> list = xlLoginLogService.selectXlLoginLogList(vo);
        return R.ok(list);
    }

    /**
     * 导出系统访问日志表列表
     */
    @ApiOperation(value = "导出日志", notes = "导出系统访问日志表列表")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PreAuthorize("@customSs.hasPermission('system:loginlog:export')")
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ApiParam(value = "查询条件") SysLoginLogVo vo) {
        IPage<SysLoginLogEntity> list = xlLoginLogService.selectXlLoginLogList(vo);
        ExcelUtils.exportEasyExcel(response, SysLoginLogEntity.class, list.getRecords(), "系统访问日志表数据");
    }

    /**
     * 获取系统访问日志表详细信息
     */
    @ApiOperation(value = "获取日志详情", notes = "根据ID获取系统访问日志详细信息")
    @ApiImplicitParam(name = "loginId", value = "日志ID", required = true, dataType = "Long", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = SysLoginLogEntity.class)})
    @PreAuthorize("@customSs.hasPermission('system:log:query')")
    @GetMapping(value = "/{loginId}")
    public R getInfo(@PathVariable("loginId") Long loginId) {
        return R.ok(xlLoginLogService.selectXlLoginLogByLoginId(loginId));
    }

    /**
     * 删除系统访问日志表
     */
    @ApiOperation(value = "删除日志", notes = "根据ID批量删除系统访问日志")
    @ApiImplicitParam(name = "loginIds", value = "日志ID数组", required = true, dataType = "Long[]", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Boolean.class)})
    @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));
    }

    @ApiOperation(value = "清空日志", notes = "清空所有系统访问日志")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PreAuthorize("@customSs.hasPermission('monitor:logininfor:clean')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R clean() {
        xlLoginLogService.cleanLoginlog();
        return R.ok();
    }
}
