package ${basePackage}.task;

import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.schedule.RunDelayRepeatTask;
import ${basePackage}.frame.utils.IDgenerator;
import ${basePackage}.module.system.ent.Tokens;
import ${basePackage}.module.system.mgr.TokensManager;
import ${basePackage}.module.system.req.TokensFindRequest;
import ${basePackage}.module.system.req.TokensLogoutRequest;
import ${basePackage}.module.system.rsp.TokensFindResponse;
import ${basePackage}.module.system.rsp.TokensLogoutResponse;

import java.time.Duration;
import java.util.Date;

public class TokenTask extends RunDelayRepeatTask {

    @Override
    public String taskId() {
        return "1";
    }

    @Override
    public String taskName() {
        return "登录凭证处理任务";
    }

    @Override
    public String taskNote () {
        return "处理过期登录凭证状态，及时注销。";
    }

    @Override
    public Duration interval() {
        return Duration.ofMinutes(1);
    }

    @Override
    public void run() {
        TokensManager tokensManager = LocalData.getBean(TokensManager.class);
        if (tokensManager == null) {
            return;
        }

        TokensFindRequest tokensFindRequest = new TokensFindRequest();
        tokensFindRequest.setPageSize(0);
        tokensFindRequest.setValid(true);
        TokensFindResponse tokensFindResponse = tokensManager.find(tokensFindRequest, LocalData.getSysToken());

        Date current = new Date();
        for (Tokens tokens : tokensFindResponse.getResult()) {
            if (tokens.getValidTime().before(current)) {
                TokensLogoutRequest tokensLogoutRequest = new TokensLogoutRequest();
                tokensLogoutRequest.setToken(tokens.getToken());
                TokensLogoutResponse tokensLogoutResponse = tokensManager.logout(tokensLogoutRequest, LocalData.getSysToken());
            }
        }
    }
}
