package com.yunfeiyun.agmp.common.web.system.controller;

import com.yunfeiyun.agmp.common.framework.web.service.ISysLoginLogService;
import com.yunfeiyun.agmp.common.framework.web.service.SysPasswordService;
import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.domain.entity.SysLoginLog;
import com.yunfeiyun.agmp.common.core.domain.model.IBaseLoginLog;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 系统访问记录
 * 
 * @author 杨晓辉
 */
@RestController
@RequestMapping("/monitor/loginlog")
public class SysLoginLogController extends BaseController
{
    @Autowired
    private ISysLoginLogService logininLogService;

    @Autowired
    private SysPasswordService passwordService;

    @PreAuthorize("@ss.hasPermi('monitor:logininlog:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLoginLog logininlog)
    {
        startPage();
        List<IBaseLoginLog> list = logininLogService.selectLoginLogList(logininlog);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLog logininlog)
    {
        List<IBaseLoginLog> list = logininLogService.selectLoginLogList(logininlog);
        ExcelUtil<SysLoginLog> util = new ExcelUtil<SysLoginLog>(SysLoginLog.class);
        util.exportExcel(response, (List)list, "登录日志");
    }

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

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

    @PreAuthorize("@ss.hasPermi('monitor:logininlog:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock")
    public AjaxResult unlock(String userName)
    {
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
}
