package com.microservices.system.task;

import com.microservices.common.core.constant.CacheConstants;
import com.microservices.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @author OTTO
 */
@Component
public class SystemApplicationRunner implements ApplicationRunner {

    @Autowired
    private RedisService redisService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Collection<String> userIdentifyKeyCollection = redisService.keys(CacheConstants.USER_IDENTIFY_LIST_KEY + "*");
        if (userIdentifyKeyCollection != null) {
            userIdentifyKeyCollection.forEach(x -> redisService.deleteObject(x));
        }
        Collection<String> roleMenuKeyCollection = redisService.keys(CacheConstants.ROLE_MENU_PERMISSIONS_KEY + "*");
        if (roleMenuKeyCollection != null) {
            roleMenuKeyCollection.forEach(x -> redisService.deleteObject(x));
        }
        Collection<String> tokenKeyCollection = redisService.keys(CacheConstants.GITLINK_ACCESS_TOKEN + "*");
        if (tokenKeyCollection != null) {
            tokenKeyCollection.forEach(x -> redisService.deleteObject(x));
        }
        Collection<String> loginTokenKeyCollection = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        if (loginTokenKeyCollection != null) {
            loginTokenKeyCollection.forEach(x -> redisService.deleteObject(x));
        }
    }
}
