package com.pqs9900.task;

import com.njcn.mapper.configuration.SysMapper;
import com.njcn.mapper.user.UserMapper;
import com.njcn.mapper.user.UserSetMapper;
import com.njcn.pojo.configuration.SysSet;
import com.njcn.sso.pojo.user.User;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.List;

public class CasualUserTask implements Job {
    @Autowired
    UserSetMapper userSetMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SysMapper sm;

    void checkUserStatus() { //查看长期用户状态
        List<User> list = userMapper.getAllUser();
        List<SysSet> sysset = sm.selectAll();

        if (sysset == null || sysset.size() == 0) {
            return;
        }

        int limitidle = sysset.get(0).getLimitIdle().intValue();
        int limitlogout = sysset.get(0).getLimitLogout().intValue();
        int limitdate = sysset.get(0).getLimitDate().intValue();


        for (User user : list) {
            if (user.getCasualUser().intValue() == 0) {
                boolean b = false;
                List<String> list1 = userMapper.getAdminName(user.getUserIndex());//剔除管理员不参与过滤

                for (String s : list1) {
                    if (s.indexOf("管理员") > -1) {
                        b = true;
                        break;
                    }
                }

                if (b)
                    continue;

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(user.getLoginTime());
                calendar.add(Calendar.MONTH, limitidle);
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(user.getPsdvalidity());
                calendar1.add(Calendar.MONTH, limitdate);
                Calendar calendar2 = Calendar.getInstance();
                calendar2.setTime(user.getLoginTime());
                calendar2.add(Calendar.MONTH, limitidle + limitlogout);
                Calendar now = Calendar.getInstance();

                if (now.after(calendar) && user.getState().intValue() != 0) { //置为休眠
                    userMapper.updateState(user.getUserIndex(), 4);
                }

                if (now.after(calendar1) && user.getState().intValue() == 1) {  //提示修改密码
                    userMapper.updateState(user.getUserIndex(), 5);
                }

                if (now.after(calendar2) && user.getState().intValue() == 4) { //置为注销(删除)
                    userMapper.updateState(user.getUserIndex(), 0);
                }
            }
        }
    }

    void checkCasualUserState() { //检查临时用户状态
        List<SysSet> sysset = sm.selectAll();
        List<User> list = userMapper.getAllUser();

        if (sysset == null || sysset.size() == 0) {
            return;
        }

        int casualusertime = sysset.get(0).getCasualUserTime().intValue();
        int logouttime = sysset.get(0).getLogOutTime().intValue();

        for (User user : list) {
            if (user.getCasualUser().intValue() == 1) {
                user = userMapper.selectOne(user);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(user.getRegisterTime());
                calendar.add(Calendar.MONTH, casualusertime);
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(user.getRegisterTime());
                calendar1.add(Calendar.MONTH, casualusertime + logouttime); //休眠后检测是否需要激活，如果超过期限未激活则注销该账号*/
                Calendar now = Calendar.getInstance();

                if (user.getState().intValue() != 0 && now.after(calendar)) { //置为休眠
                    userMapper.updateState(user.getUserIndex(), 4);
                }

                if (user.getState().intValue() == 4 && now.after(calendar1)) { //置为注销(删除)
                    userMapper.updateState(user.getUserIndex(), 0);
                }
            }
        }
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        checkCasualUserState();
        checkUserStatus();
    }
}
