package com.railway.web.controller.monitor;

import com.railway.common.annotation.Log;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.page.TableDataInfo;
import com.railway.common.enums.BusinessType;
import com.railway.common.utils.poi.ExcelUtil;
import com.railway.system.domain.SysLogininfor;
import com.railway.system.service.ISysFileService;
import com.railway.system.service.ISysLogininforService;
import io.swagger.annotations.Api;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统访问记录
 *
 * @author railway
 */
@Api(tags = "系统访问记录")
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {

  private final ISysLogininforService logininforService;
  private final ISysFileService sysFileService;

  public SysLogininforController(ISysLogininforService logininforService,
      ISysFileService sysFileService) {
    this.logininforService = logininforService;
    this.sysFileService = sysFileService;
  }

  @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysLogininfor logininfor) {
    startPage();
    List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
    return getDataTable(list);
  }

  @Log(title = "登录日志", businessType = BusinessType.EXPORT)
  @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
  @GetMapping("/export")
  public AjaxResult export(SysLogininfor logininfor) {
    List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
    ExcelUtil<SysLogininfor> util = new ExcelUtil<>(SysLogininfor.class);
    String localFilePath = util.exportExcel(list, "登录日志");
    return sysFileService.uploadFile(localFilePath);
  }

  @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
  @Log(title = "登录日志", businessType = BusinessType.DELETE)
  @DeleteMapping("/{infoIds}")
  public AjaxResult remove(@PathVariable Long[] infoIds) {
    return toAjax(logininforService.deleteLogininforByIds(infoIds));
  }

  @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
  @Log(title = "登录日志", businessType = BusinessType.CLEAN)
  @DeleteMapping("/clean")
  public AjaxResult clean() {
    logininforService.cleanLogininfor();
    return AjaxResult.success();
  }
}
