package ${basePackage}.task;

import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.schedule.RunDelayRepeatTask;
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 "task1";
    }

    @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());
            }
        }
    }
}
