package org.wfc.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wfc.common.core.constant.CacheConstants;
import org.wfc.common.core.utils.poi.ExcelUtil;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType;
import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.annotation.InnerAuth;
import org.wfc.common.security.annotation.RequiresPermissions;
import org.wfc.user.api.domain.ULogininfor;
import org.wfc.user.service.IULogininforService;

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

/**
 * 系统访问记录
 * 
 * @author wfc
 */
@RestController
@RequestMapping("/logininfor")
public class ULogininforController extends BaseController
{
    @Autowired
    private IULogininforService logininforService;

    @Autowired
    private RedisService redisService;

    @RequiresPermissions("Utem:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo list(ULogininfor logininfor)
    {
        startPage();
        List<ULogininfor> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    // @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("Utem:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ULogininfor logininfor)
    {
        List<ULogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<ULogininfor> util = new ExcelUtil<ULogininfor>(ULogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

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

    @RequiresPermissions("Utem:logininfor:remove")
    // @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        logininforService.cleanLogininfor();
        return success();
    }

    @RequiresPermissions("Utem:logininfor:unlock")
    // @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName)
    {
        redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName);
        return success();
    }

    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody ULogininfor logininfor)
    {
        return toAjax(logininforService.insertLogininfor(logininfor));
    }
}
