package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
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;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

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

    @Autowired
    private SysLogininforMapper logininforMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

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

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
        String loginDay = logininfor.getLoginDay();
        List<SysLogininfor> sysLogininfors = new ArrayList<>();
        LocalDate now = LocalDate.now();
        if (StringUtils.isNotBlank(loginDay)) {
            LocalDate localDate = now.minusDays(Long.parseLong(loginDay));
            Date date = DateUtils.parseDate(localDate.toString());
            logininfor.setLoginTime(date);
            //查询近期X天未登录的用户
            sysLogininfors = logininforMapper.selectLogininforDayList(logininfor);
            List<SysDictData> dictDataList = sysDictDataMapper.selectDictDataByType("sys_login_day");
            String msg = "";
            if (dictDataList != null && dictDataList.size() > 0) {
                for (SysDictData sysDictData : dictDataList) {
                    String dictValue = sysDictData.getDictValue();
                    if (dictValue.equals(loginDay)) {
                        msg = sysDictData.getDictLabel();
                        break;
                    }
                }
            }
            if (sysLogininfors != null && sysLogininfors.size() > 0) {
                for (SysLogininfor sysLogininfor : sysLogininfors) {
                    sysLogininfor.setStatus(msg + "未登录");
                }
            }

        } else {
            sysLogininfors = logininforMapper.selectLogininforList(logininfor);
            List<String> userNames = new ArrayList();
            if (sysLogininfors != null && sysLogininfors.size() > 0) {
                for (SysLogininfor sysLogininfor : sysLogininfors) {
                    userNames.add(sysLogininfor.getUserName());
                }
                logininfor.setUserNames(userNames);

                List<SysDictData> dictDataList = sysDictDataMapper.selectDictDataByType("sys_login_day");
                //分组查询出对应用户最后的登录日期
                List<SysLogininfor> sysLogininforsDay = logininforMapper.selectLogininforDayList(logininfor);
                if (sysLogininforsDay != null && sysLogininforsDay.size() > 0) {
                    //判断这个用户是多少天未登录
                    for (SysLogininfor sysLogininfor : sysLogininforsDay) {
                        sysLogininfor.setStatus("正常");
                        Date loginTime = sysLogininfor.getLoginTime();
                        LocalDate localDate = DateUtils.date2LocalDate(loginTime);
                        long days = localDate.until(now, ChronoUnit.DAYS);
                        for (SysDictData sysDictData : dictDataList) {
                            long dictValue = Long.parseLong(sysDictData.getDictValue());
                            if (days > dictValue) {
                                sysLogininfor.setStatus(sysDictData.getDictLabel() + "未登录");
                            }
                        }
                    }
                    for (SysLogininfor sysLogininfor : sysLogininfors) {
                        sysLogininfor.setStatus("正常");
                        for (SysLogininfor sysLogininfor1 : sysLogininforsDay) {
                            if (sysLogininfor.getUserName().equals(sysLogininfor1.getUserName())) {
                                sysLogininfor.setStatus(sysLogininfor1.getStatus());
                                break;
                            }
                        }
                    }
                }
            }
        }
        return sysLogininfors;
    }

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

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