package org.eco.system.controller.monitor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.eco.system.domain.bo.SysLogininforBo;
import org.eco.common.core.constant.GlobalConstants;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.redis.utils.RedisUtils;
import org.eco.common.web.core.BaseController;
import org.eco.system.domain.vo.SysLogininforVo;
import org.eco.system.service.ISysLogininforService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统访问记录
 *
 * @author wgk
 */
@Validated
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {
    @Resource
    private ISysLogininforService logininforService;

    @SaCheckPermission("monitor:logininfor:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SysLogininforVo>> list(SysLogininforBo logininforBo) {
        return CommonResult.success(logininforService.selectPage(logininforBo));
    }

    /**
     * 导出系统访问记录列表
     */
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @SaCheckPermission("monitor:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininforBo logininfor) {
        List<SysLogininforVo> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil.exportExcel(list, "登录日志", SysLogininforVo.class, response);
    }

    /**
     * 批量删除登录日志
     *
     * @param infoIds 日志ids
     */
    @SaCheckPermission("monitor:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public CommonResult<Void> remove(@PathVariable Long[] infoIds) {
        boolean deleted = logininforService.deleteLogininforByIds(infoIds);
        if (!deleted) {
            CommonResult.fail("删除登录日志记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 清理系统访问记录
     */
    @SaCheckPermission("monitor:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public CommonResult<Void> clean() {
        boolean cleaned = logininforService.cleanLogininfor();
        if (!cleaned) {
            CommonResult.fail("清除登录日志记录失败!");
        }
        return CommonResult.success();
    }

    @SaCheckPermission("monitor:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public CommonResult<Void> unlock(@PathVariable("userName") String userName) {
        String loginName = GlobalConstants.PWD_ERR_CNT_KEY + userName;
        if (RedisUtils.hasKey(loginName)) {
            RedisUtils.deleteObject(loginName);
        }
        return CommonResult.success();
    }
}
