package ${domain}.task;

import ${domain}.frame.auth.LocalData;
import ${domain}.frame.schedule.RunDelayRepeatTask;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.module.wsys.ent.Tokens;
import ${domain}.module.wsys.mgr.TokensManager;
import ${domain}.module.wsys.req.TokensFindRequest;
import ${domain}.module.wsys.req.TokensLogoutRequest;
import ${domain}.module.wsys.rsp.TokensFindResponse;
import ${domain}.module.wsys.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());
            }
        }
    }
}
