package com.xjrsoft.module.job;

import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.module.base.entity.WhitelistManagement;
import com.xjrsoft.module.base.service.IWhitelistManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dzx
 * @date 2024/9/2
 */
@Component
@Slf4j
public class WhitelistTask {

    @Autowired
    private IWhitelistManagementService whitelistService;

    @Autowired
    private RedisUtil redisUtil;

    @Scheduled(cron = "0 */15 * * * ?")
    public void execute() {
        doExecute();
    }

    public void doExecute(){
        List<WhitelistManagement> list = whitelistService.getCachesList();

        Map map = redisUtil.get(GlobalConstant.WHITE_MANAGEMENT_CACHE_KEY, Map.class);
        if(map.size() != list.size()){
            Map<Long, Long> newMap = list.stream().collect(Collectors.toMap(WhitelistManagement::getUserId, WhitelistManagement::getUserId));
            redisUtil.set(GlobalConstant.WHITE_MANAGEMENT_CACHE_KEY, newMap);
        }

    }

}

