package com.ozs.web.controller.monitor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.ozs.common.core.domain.vo.SysLoginInfoResq;
import com.ozs.common.core.domain.vo.SysLoginInfoVo;
import com.ozs.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.ozs.common.annotation.Log;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.page.TableDataInfo;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.framework.web.service.SysPasswordService;
import com.ozs.system.domain.SysLoginInfo;
import com.ozs.system.service.ISysLoginInfoService;

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLoginInfoController extends BaseController {
    @Autowired
    private ISysLoginInfoService logininforService;

    @Autowired
    private SysPasswordService passwordService;

//    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(SysLoginInfo logininfor) {
//        startPage();
//        List<SysLoginInfo> list = logininforService.selectLogininforList(logininfor);
//        return getDataTable(list);
//    }

    /**
     * 获取登录日志分页列表
     *
     * @param sysLoginInfoVo
     * @return
     */
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    public AjaxResult list(@RequestBody SysLoginInfoVo sysLoginInfoVo) throws ParseException {
        LambdaQueryWrapper<SysLoginInfo> wrapper = new LambdaQueryWrapper<SysLoginInfo>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!ObjectUtils.isEmpty(sysLoginInfoVo.getIpaddr())) {
            wrapper.like(SysLoginInfo::getIpaddr, sysLoginInfoVo.getIpaddr());
        }
        if (!ObjectUtils.isEmpty(sysLoginInfoVo.getUserName())) {
            wrapper.like(SysLoginInfo::getUserName, sysLoginInfoVo.getUserName());
        }
        if (!ObjectUtils.isEmpty(sysLoginInfoVo.getStatus())) {
            wrapper.eq(SysLoginInfo::getStatus, sysLoginInfoVo.getStatus());
        }
        if (!ObjectUtils.isEmpty(sysLoginInfoVo.getStartTime())) {
//            wrapper.apply(StringUtils.isNotBlank(sysLoginInfoVo.getStartTime()), "date_format(login_time,'%y%m%d') >= date_format('" + sysLoginInfoVo.getStartTime() + "','%y%m%d')");
            wrapper.apply(StringUtils.isNotBlank(sysLoginInfoVo.getStartTime()), "to_char(login_time,'YYYYMMDD') >= to_char('" + sysLoginInfoVo.getStartTime() + "','YYYYMMDD')");
        }
        if (!ObjectUtils.isEmpty(sysLoginInfoVo.getEndTime())) {
//            wrapper.apply(StringUtils.isNotBlank(sysLoginInfoVo.getEndTime()), "date_format(login_time,'%y%m%d') <= date_format('" + sysLoginInfoVo.getEndTime() + "','%y%m%d')");
            wrapper.apply(StringUtils.isNotBlank(sysLoginInfoVo.getEndTime()), "to_char(login_time,'YYYYMMDD') <= to_char('" + sysLoginInfoVo.getEndTime() + "','YYYYMMDD')");
        }
        wrapper.orderByDesc(SysLoginInfo::getLoginTime);
        IPage<SysLoginInfo> page = logininforService.page(new Page<>(sysLoginInfoVo.getPageNum(), sysLoginInfoVo.getPageSize()), wrapper);
        return AjaxResult.success(page);
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:detail')")
    @PostMapping("/detail")
    public AjaxResult remove(Long infoId) {
        SysLoginInfo info = logininforService.selectLoginLogById(infoId);
        return AjaxResult.success(info);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SysLoginInfoResq sysLoginInfoResq) {
        List<SysLoginInfo> list = logininforService.selectLogininforList(sysLoginInfoResq);
        ExcelUtil<SysLoginInfo> util = new ExcelUtil<SysLoginInfo>(SysLoginInfo.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @RequestMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds)
    {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @RequestMapping("/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();
    }
}
