package com.fs.company.controller.company;

import com.fs.common.annotation.DataScope;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyLogininfor;
import com.fs.company.service.ICompanyLogininforService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统访问记录
 *

 */
@RestController
@RequestMapping("/monitor/logininfor")
public class CompanyLogininforController extends BaseController
{
    @Autowired
    private ICompanyLogininforService logininforService;
    @Autowired
    private TokenService tokenService;
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    @DataScope
    public TableDataInfo list(CompanyLogininfor logininfor)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        logininfor.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyLogininfor> list = logininforService.selectCompanyLogininforList(logininfor);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @GetMapping("/export")
    public AjaxResult export(CompanyLogininfor logininfor)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        logininfor.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyLogininfor> list = logininforService.selectCompanyLogininforList(logininfor);
        ExcelUtil<CompanyLogininfor> util = new ExcelUtil<CompanyLogininfor>(CompanyLogininfor.class);
        return util.exportExcel(list, "登录日志");
    }

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

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