package com.lij.himeeter.web.controller.monitor;

import com.lij.himeeter.common.annotation.Log;
import com.lij.himeeter.common.core.controller.BaseController;
import com.lij.himeeter.common.core.domain.AjaxResult;
import com.lij.himeeter.common.core.page.TableDataInfo;
import com.lij.himeeter.common.enums.BusinessType;
import com.lij.himeeter.common.utils.poi.ExcelUtil;
import com.lij.himeeter.framework.web.service.SysPasswordService;
import com.lij.himeeter.system.domain.SysLogininfor;
import com.lij.himeeter.system.service.ISysLogininforService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <pre>
 *
 * 文件名 :  SysLogininforCtrl
 * 创建时间 : 2025年01月06日 04:32
 *
 * 文件描述 : 系统访问记录
 *
 * </pre>
 *
 * @author Nobita
 * @version 1.0.0
 */

@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforCtrl extends BaseController {

    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private SysPasswordService passwordService;

    @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')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor) {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    @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 success();
    }

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