package com.hxhq.system.controller;

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

import com.hxhq.business.domain.SystemLog;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.utils.ServletUtils;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.annotation.Logical;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hxhq.common.core.constant.CacheConstants;
import com.hxhq.common.core.utils.poi.ExcelUtil;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;
import com.hxhq.common.log.annotation.Log;
import com.hxhq.common.log.enums.BusinessType;
import com.hxhq.common.redis.service.RedisService;
import com.hxhq.common.security.annotation.InnerAuth;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.system.api.domain.SysLogininfor;
import com.hxhq.system.service.ISysLogininforService;

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

    @Autowired
    private RedisService redisService;

    @RequiresPermissions(value = {"system:logininfor:list","business:systemLog:login"},logical = Logical.OR)
    @GetMapping("/list")
    public TableDataInfo list(SysLogininfor logininfor)
    {
        startPage();
        if(StringUtils.isNoneBlank(logininfor.getEndDate())){
            logininfor.setEndDate(DateUtils.dateTime(DateUtils.addDays(DateUtils.parseDate(logininfor.getEndDate()),1)));
        }
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions(value = {"system:logininfor:export","business:systemLog:export"},logical = Logical.OR)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor)
    {
        if(StringUtils.isNoneBlank(logininfor.getEndDate())){
            logininfor.setEndDate(DateUtils.dateTime(DateUtils.addDays(DateUtils.parseDate(logininfor.getEndDate()),1)));
        }
        List<SystemLog> logList = new ArrayList<>();
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        for(SysLogininfor l:list){
            SystemLog systemLog = new SystemLog();
            systemLog.setQmrMc(l.getNickName());
            systemLog.setCzlx("zh_CN".equals(ServletUtils.getRequest().getHeader("lang"))?"登录":"Login");
            systemLog.setCzsj(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,l.getAccessTime()));
            systemLog.setCzxq("zh_CN".equals(ServletUtils.getRequest().getHeader("lang"))?l.getMsg():l.getMsgEn());
            logList.add(systemLog);
        }
        ExcelUtil<SystemLog> util = new ExcelUtil<SystemLog>(SystemLog.class);
        util.exportExcel(response, logList, "登录日志");
    }

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

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

    @RequiresPermissions("system: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 SysLogininfor logininfor)
    {
        return toAjax(logininforService.insertLogininfor(logininfor));
    }
}
