package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.mapper.SysLogininforMapper;
import com.ruoyi.system.service.ISysLogininforService;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService
{

    @Autowired
    private SysLogininforMapper logininforMapper;

    /**
     * 新增系统登录日志
     * 
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor)
    {
        logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor)
    {
        return logininforMapper.selectLogininforList(logininfor);
    }

    /**
     * 批量删除系统登录日志
     * 
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
        return logininforMapper.deleteLogininforByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        logininforMapper.cleanLogininfor();
    }

    /**
     * 根据用户获取最新一条访问记录
     */
    @Override
    public SysLogininfor getNewSysLogininfor(String str) {
        SysLogininfor ns = logininforMapper.getNewSysLogininfor(str);
        if(ns!=null){
            LocalDateTime ld = ns.getLoginTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            Period period = Period.between(ld.toLocalDate(), LocalDateTime.now().toLocalDate());
            int days = period.getDays() + (period.getMonths() * 30) + (period.getYears() * 365);
            if(days>=90){
                return ns;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }
}
